Merge pull request '490_View_refactoring_II' (#520) from 490_View_refactoring_II into master
Reviewed-on: #520
This commit was merged in pull request #520.
This commit is contained in:
+18
-17
@@ -42,23 +42,24 @@ def generate_random_string(length: int, use_numbers: bool = False, use_letters_l
|
||||
ret_val = "".join(random.choice(elements) for i in range(length))
|
||||
return ret_val
|
||||
|
||||
class IdentifierGenerator:
|
||||
_MODEL = None
|
||||
|
||||
def generate_qr_code(content: str, size: int = 20) -> str:
|
||||
""" Generates a qr code from given content
|
||||
def __init__(self, model):
|
||||
from konova.models import BaseObject
|
||||
if not issubclass(model, BaseObject):
|
||||
raise AssertionError("Model must be a subclass of BaseObject!")
|
||||
|
||||
Args:
|
||||
content (str): The content for the qr code
|
||||
size (int): The image size
|
||||
self._MODEL = model
|
||||
|
||||
Returns:
|
||||
qrcode_svg (str): The qr code as svg
|
||||
"""
|
||||
qrcode_factory = qrcode.image.svg.SvgImage
|
||||
qrcode_img = qrcode.make(
|
||||
content,
|
||||
image_factory=qrcode_factory,
|
||||
box_size=size
|
||||
)
|
||||
stream = BytesIO()
|
||||
qrcode_img.save(stream)
|
||||
return stream.getvalue().decode()
|
||||
def generate_id(self) -> str:
|
||||
""" Generates a unique identifier
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
unpersisted_object = self._MODEL()
|
||||
identifier = unpersisted_object.generate_new_identifier()
|
||||
while self._MODEL.objects.filter(identifier=identifier).exists():
|
||||
identifier = unpersisted_object.generate_new_identifier()
|
||||
return identifier
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 14.12.25
|
||||
|
||||
"""
|
||||
from io import BytesIO
|
||||
|
||||
import qrcode
|
||||
import qrcode.image.svg as svg
|
||||
|
||||
|
||||
class QrCode:
|
||||
""" A wrapping class for creating a qr code with content
|
||||
|
||||
"""
|
||||
_content = None
|
||||
_img = None
|
||||
|
||||
def __init__(self, content: str, size: int):
|
||||
self._content = content
|
||||
self._img = self._generate_qr_code(content, size)
|
||||
|
||||
def _generate_qr_code(self, content: str, size: int = 20) -> str:
|
||||
""" Generates a qr code from given content
|
||||
|
||||
Args:
|
||||
content (str): The content for the qr code
|
||||
size (int): The image size
|
||||
|
||||
Returns:
|
||||
qrcode_svg (str): The qr code as svg
|
||||
"""
|
||||
img_factory = svg.SvgImage
|
||||
qrcode_img = qrcode.make(
|
||||
content,
|
||||
image_factory=img_factory,
|
||||
box_size=size
|
||||
)
|
||||
stream = BytesIO()
|
||||
qrcode_img.save(stream)
|
||||
return stream.getvalue().decode()
|
||||
|
||||
def get_img(self):
|
||||
return self._img
|
||||
|
||||
def get_content(self):
|
||||
return self._content
|
||||
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 14.12.25
|
||||
|
||||
"""
|
||||
from abc import ABC
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from konova.decorators import uuid_required, any_group_check
|
||||
|
||||
|
||||
class AbstractDetailView(LoginRequiredMixin, View, ABC):
|
||||
_TEMPLATE = None
|
||||
|
||||
@method_decorator(uuid_required)
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
@method_decorator(any_group_check)
|
||||
def get(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
|
||||
raise NotImplementedError()
|
||||
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 14.12.25
|
||||
|
||||
"""
|
||||
from abc import ABC
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpRequest, JsonResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from konova.decorators import default_group_required
|
||||
from konova.utils.generators import IdentifierGenerator
|
||||
|
||||
|
||||
class AbstractIdentifierGeneratorView(LoginRequiredMixin, View, ABC):
|
||||
_MODEL = None
|
||||
|
||||
@method_decorator(default_group_required)
|
||||
def get(self, request: HttpRequest, *args, **kwargs):
|
||||
generator = IdentifierGenerator(model=self._MODEL)
|
||||
identifier = generator.generate_id()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 14.12.25
|
||||
|
||||
"""
|
||||
from abc import ABC
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from konova.decorators import any_group_check
|
||||
|
||||
|
||||
class AbstractIndexView(LoginRequiredMixin, View, ABC):
|
||||
_TEMPLATE = "generic_index.html"
|
||||
|
||||
@method_decorator(any_group_check)
|
||||
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
||||
raise NotImplementedError()
|
||||
@@ -0,0 +1,64 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 14.12.25
|
||||
|
||||
"""
|
||||
from abc import ABC
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.urls import reverse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from konova.decorators import default_group_required
|
||||
from konova.forms.modals import RemoveModalForm
|
||||
|
||||
|
||||
class AbstractRemoveView(LoginRequiredMixin, View, ABC):
|
||||
_MODEL = None
|
||||
_REDIRECT_URL = None
|
||||
_FORM = RemoveModalForm
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
@method_decorator(default_group_required)
|
||||
def __process_request(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
|
||||
obj = self._MODEL.objects.get(id=id)
|
||||
identifier = obj.identifier
|
||||
form = self._FORM(request.POST or None, instance=obj, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
_("{} removed").format(identifier),
|
||||
redirect_url=reverse(self._REDIRECT_URL)
|
||||
)
|
||||
|
||||
def get(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
|
||||
""" GET endpoint for removing via modal form
|
||||
|
||||
Due to the legacy logic of the form (which processes get and post requests directly), we simply need to pipe
|
||||
the request from GET and POST endpoints directly into the same method.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The uuid id as string
|
||||
|
||||
Returns:
|
||||
"""
|
||||
return self.__process_request(request, id, *args, **kwargs)
|
||||
|
||||
def post(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
|
||||
""" POST endpoint for removing via modal form
|
||||
|
||||
Due to the legacy logic of the form (which processes get and post requests directly), we simply need to pipe
|
||||
the request from GET and POST endpoints directly into the same method.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The uuid id as string
|
||||
|
||||
Returns:
|
||||
"""
|
||||
return self.__process_request(request, id, *args, **kwargs)
|
||||
@@ -0,0 +1,24 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 14.12.25
|
||||
|
||||
"""
|
||||
from abc import abstractmethod, ABC
|
||||
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
|
||||
from konova.decorators import uuid_required
|
||||
|
||||
|
||||
class AbstractPublicReportView(View, ABC):
|
||||
_TEMPLATE = None
|
||||
|
||||
@method_decorator(uuid_required)
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
@abstractmethod
|
||||
def get(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
|
||||
raise NotImplementedError()
|
||||
Reference in New Issue
Block a user