Compare commits

..

No commits in common. "3938db189339e71300381e27fc050fcab6ddd36a" and "b9d532aa8149cf74ce81c2ed664661cd2bdd69f8" have entirely different histories.

21 changed files with 0 additions and 362 deletions

View File

View File

@ -1,16 +0,0 @@
from django.contrib import admin
from api.models.token import APIUserToken
class APITokenAdmin(admin.ModelAdmin):
list_display = [
"token",
"valid_until",
"is_active",
]
readonly_fields = [
"token"
]
admin.site.register(APIUserToken, APITokenAdmin)

View File

@ -1,5 +0,0 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'

View File

@ -1,8 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
from .token import *

View File

@ -1,48 +0,0 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import timezone
from konova.utils.generators import generate_token
class APIUserToken(models.Model):
token = models.CharField(
primary_key=True,
max_length=1000,
default=generate_token,
)
valid_until = models.DateField(
blank=True,
null=True,
help_text="Token is only valid until this date",
)
is_active = models.BooleanField(
default=False,
help_text="Must be activated by an admin"
)
def __str__(self):
return self.token
@staticmethod
def get_user_from_token(token: str):
""" Getter for the related user object
Args:
token (str): The used token
Returns:
user (User): Otherwise None
"""
_today = timezone.now().date()
try:
token_obj = APIUserToken.objects.get(
token=token,
)
if not token_obj.is_active:
raise PermissionError("Token unverified")
if token_obj.valid_until is not None and token_obj.valid_until < _today:
raise PermissionError("Token validity expired")
except ObjectDoesNotExist:
raise PermissionError("Token invalid")
return token_obj.user

View File

@ -1,8 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
KSP_TOKEN_HEADER_IDENTIFIER = "ksptoken"

View File

@ -1,7 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""

View File

@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.

View File

@ -1,8 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
from .urls import *

View File

@ -1,14 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
from django.urls import path, include
app_name = "api"
urlpatterns = [
path("v1/", include("api.urls.v1.urls", namespace="v1")),
]

View File

@ -1,7 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""

View File

@ -1,15 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
from django.urls import path
from api.views.v1.intervention import APIInterventionViewV1
app_name = "v1"
urlpatterns = [
path("intervention/<identifier>", APIInterventionViewV1.as_view(), name="intervention"),
]

View File

@ -1,8 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
from .v1 import *

View File

@ -1,7 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""

View File

@ -1,77 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
from django.db.models import QuerySet
from api.views.views import AbstractModelAPIView
from codelist.models import KonovaCode
from intervention.models import Responsibility, Legal
class AbstractModelAPIViewV1(AbstractModelAPIView):
""" Holds general serialization functions for API v1
"""
def konova_code_to_json(self, konova_code: KonovaCode):
return {
"atom_id": konova_code.atom_id,
"long_name": konova_code.long_name,
"short_name": konova_code.short_name,
}
def responsible_to_json(self, responsible: Responsibility):
return {
"registration_office": self.konova_code_to_json(responsible.registration_office),
"registration_file_number": responsible.registration_file_number,
"conservation_office": self.konova_code_to_json(responsible.conservation_office),
"conservation_file_number": responsible.conservation_file_number,
"handler": responsible.handler,
}
def legal_to_json(self, legal: Legal):
return {
"registration_date": legal.registration_date,
"binding_date": legal.binding_date,
"process_type": self.konova_code_to_json(legal.process_type),
"laws": [self.konova_code_to_json(law) for law in legal.laws.all()],
}
def payments_to_json(self, qs: QuerySet):
""" Serializes payments into json
Args:
qs (QuerySet): A queryset of Payment entries
Returns:
"""
return [
{
"amount": entry.amount,
"due_on": entry.due_on,
"comment": entry.comment,
}
for entry in qs
]
def deductions_to_json(self, qs: QuerySet):
""" Serializes eco account deductions into json
Args:
qs (QuerySet): A queryset of EcoAccountDeduction entries
Returns:
"""
return [
{
"eco_account": entry.account.pk,
"surface": entry.surface,
}
for entry in qs
]

View File

@ -1,41 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
import json
from django.http import HttpRequest, JsonResponse
from api.views.v1.general import AbstractModelAPIViewV1
from intervention.models import Intervention
class APIInterventionViewV1(AbstractModelAPIViewV1):
model = Intervention
def get(self, request: HttpRequest, identifier):
_filter = {
"identifier": identifier,
"users__in": [self.user],
"deleted__isnull": True,
}
data = self.fetch_and_serialize(_filter)
return JsonResponse(data)
def model_to_json(self, entry: Intervention):
entry_json = {
"identifier": entry.identifier,
"title": entry.title,
"responsible": self.responsible_to_json(entry.responsible),
"legal": self.legal_to_json(entry.legal),
"compensations": list(entry.compensations.all().values_list("pk", flat=True)),
"payments": self.payments_to_json(entry.payments.all()),
"deductions": self.deductions_to_json(entry.deductions.all()),
}
geom = entry.geometry.geom.geojson
geo_json = json.loads(geom)
geo_json["properties"] = entry_json
return geo_json

View File

@ -1,68 +0,0 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 21.01.22
"""
from abc import abstractmethod
from django.http import JsonResponse
from django.views import View
from api.models import APIUserToken
from api.settings import KSP_TOKEN_HEADER_IDENTIFIER
class AbstractModelAPIView(View):
""" Base class for API views
The API must follow the GeoJSON Specification RFC 7946
https://geojson.org/
https://datatracker.ietf.org/doc/html/rfc7946
"""
model = None
user = None
class Meta:
abstract = True
@abstractmethod
def model_to_json(self, entry):
""" Defines the returned json values of the model
Args:
entry (): The found entry from the database
Returns:
"""
raise NotImplementedError("Must be implemented in subclasses")
def fetch_and_serialize(self, _filter):
""" Serializes the model entry according to the given lookup data
Args:
_filter (dict): Lookup declarations
Returns:
serialized_data (dict)
"""
qs = self.model.objects.filter(**_filter)
serialized_data = {}
for entry in qs:
serialized_data[str(entry.pk)] = self.model_to_json(entry)
return serialized_data
def dispatch(self, request, *args, **kwargs):
try:
self.user = APIUserToken.get_user_from_token(request.headers.get(KSP_TOKEN_HEADER_IDENTIFIER, None))
except PermissionError as e:
return JsonResponse(
{
"error": e.__str__()
},
status=403
)
return super().dispatch(request, *args, **kwargs)

View File

@ -70,7 +70,6 @@ INSTALLED_APPS = [
'ema',
'codelist',
'analysis',
'api',
]
if DEBUG:
INSTALLED_APPS += [

View File

@ -38,7 +38,6 @@ urlpatterns = [
path('news/', include("news.urls")),
path('cl/', include("codelist.urls")),
path('analysis/', include("analysis.urls")),
path('api/', include("api.urls")),
# Generic deadline routes
path('deadline/<id>/remove', remove_deadline_view, name="deadline-remove"),

View File

@ -13,19 +13,6 @@ import qrcode.image.svg
from io import BytesIO
def generate_token() -> str:
""" Shortcut for default generating of e.g. API token
Returns:
token (str)
"""
return generate_random_string(
length=64,
use_numbers=True,
use_letters_lc=True
)
def generate_random_string(length: int, use_numbers: bool = False, use_letters_lc: bool = False, use_letters_uc: bool = False) -> str:
"""
Generates a random string of variable length

View File

@ -15,13 +15,6 @@ from user.enums import UserNotificationEnum
class User(AbstractUser):
notifications = models.ManyToManyField("user.UserNotification", related_name="+", blank=True)
api_token = models.OneToOneField(
"api.APIUserToken",
blank=True,
null=True,
help_text="The user's API token",
on_delete=models.SET_NULL
)
def is_notification_setting_set(self, notification_enum: UserNotificationEnum):
return self.notifications.filter(