# Index Intervention refactoring
* introduces BaseIndexView class * refactors index view for interventions
This commit is contained in:
parent
e5db7f6b13
commit
1ceffccd40
@ -14,7 +14,8 @@ from intervention.views.deduction import NewInterventionDeductionView, EditInter
|
||||
RemoveInterventionDeductionView
|
||||
from intervention.views.document import NewInterventionDocumentView, GetInterventionDocumentView, \
|
||||
RemoveInterventionDocumentView, EditInterventionDocumentView
|
||||
from intervention.views.intervention import index_view, new_view, new_id_view, detail_view, edit_view, remove_view
|
||||
from intervention.views.intervention import new_view, new_id_view, detail_view, edit_view, remove_view, \
|
||||
InterventionIndexView
|
||||
from intervention.views.log import InterventionLogView
|
||||
from intervention.views.record import InterventionRecordView
|
||||
from intervention.views.report import report_view
|
||||
@ -25,7 +26,7 @@ from intervention.views.share import InterventionShareFormView, InterventionShar
|
||||
|
||||
app_name = "intervention"
|
||||
urlpatterns = [
|
||||
path("", index_view, name="index"),
|
||||
path("", InterventionIndexView.as_view(), name="index"),
|
||||
path('new/', new_view, name='new'),
|
||||
path('new/id', new_id_view, name='new-id'),
|
||||
path('<id>', detail_view, name='detail'),
|
||||
|
||||
@ -7,6 +7,7 @@ Created on: 19.08.22
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import JsonResponse, HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render, redirect
|
||||
from django.urls import reverse
|
||||
@ -25,40 +26,22 @@ from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.message_templates import DATA_CHECKED_PREVIOUSLY_TEMPLATE, RECORDED_BLOCKS_EDIT, \
|
||||
CHECK_STATE_RESET, FORM_INVALID, IDENTIFIER_REPLACED, DO_NOT_FORGET_TO_SHARE, GEOMETRY_SIMPLIFIED, \
|
||||
GEOMETRIES_IGNORED_TEMPLATE
|
||||
from konova.views.base import BaseIndexView
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def index_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the index view for Interventions
|
||||
class InterventionIndexView(LoginRequiredMixin, BaseIndexView):
|
||||
_INDEX_TABLE_CLS = InterventionTable
|
||||
_TAB_TITLE = _("Interventions - Overview")
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
A rendered view
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
|
||||
# Filtering by user access is performed in table filter inside InterventionTableFilter class
|
||||
interventions = Intervention.objects.filter(
|
||||
deleted=None, # not deleted
|
||||
).select_related(
|
||||
"legal"
|
||||
).order_by(
|
||||
"-modified__timestamp"
|
||||
)
|
||||
table = InterventionTable(
|
||||
request=request,
|
||||
queryset=interventions
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("Interventions - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
def _get_queryset(self):
|
||||
qs = Intervention.objects.filter(
|
||||
deleted=None,
|
||||
).select_related(
|
||||
"legal"
|
||||
).order_by(
|
||||
"-modified__timestamp"
|
||||
)
|
||||
return qs
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@ -5,6 +5,9 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 17.09.21
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.http import HttpRequest
|
||||
|
||||
|
||||
def format_german_float(num) -> str:
|
||||
@ -19,3 +22,19 @@ def format_german_float(num) -> str:
|
||||
num (str): The number as german Gleitkommazahl
|
||||
"""
|
||||
return format(num, "0,.2f").replace(",", "X").replace(".", ",").replace("X", ".")
|
||||
|
||||
|
||||
def check_user_is_in_any_group(request: HttpRequest):
|
||||
"""
|
||||
Checks for any group membership. Adds a message in case of having none.
|
||||
|
||||
"""
|
||||
user = request.user
|
||||
# Inform user about missing group privileges!
|
||||
groups = user.groups.all()
|
||||
if not groups:
|
||||
messages.info(
|
||||
request,
|
||||
_("+++ Attention: You are not part of any group. You won't be able to create, edit or do anything. Please contact an administrator. +++")
|
||||
)
|
||||
return request
|
||||
|
||||
47
konova/views/base.py
Normal file
47
konova/views/base.py
Normal file
@ -0,0 +1,47 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 15.10.25
|
||||
|
||||
"""
|
||||
from abc import abstractmethod
|
||||
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import render
|
||||
from django.views import View
|
||||
|
||||
from konova.contexts import BaseContext
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.general import check_user_is_in_any_group
|
||||
|
||||
|
||||
class BaseIndexView(View):
|
||||
""" Base class for index views
|
||||
|
||||
"""
|
||||
_TEMPLATE: str = 'generic_index.html'
|
||||
_TAB_TITLE: str = "CHANGE_ME"
|
||||
_INDEX_TABLE_CLS = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
request = check_user_is_in_any_group(request)
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get(self, request: HttpRequest):
|
||||
qs = self._get_queryset()
|
||||
table = self._INDEX_TABLE_CLS(
|
||||
request=request,
|
||||
queryset=qs
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: self._TAB_TITLE,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, self._TEMPLATE, context)
|
||||
|
||||
@abstractmethod
|
||||
def _get_queryset(self):
|
||||
raise NotImplementedError
|
||||
Loading…
x
Reference in New Issue
Block a user