Compare commits
4 Commits
75777d65c6
...
d168ec47ce
Author | SHA1 | Date | |
---|---|---|---|
d168ec47ce | |||
8c3e8b596a | |||
79840550cb | |||
55a69d45c3 |
@ -6,7 +6,17 @@ Created on: 24.08.21
|
||||
|
||||
"""
|
||||
from django.urls import path
|
||||
from compensation.views.compensation import *
|
||||
|
||||
from compensation.views.compensation.document import edit_document_view, new_document_view, remove_document_view, \
|
||||
get_document_view
|
||||
from compensation.views.compensation.resubmission import create_resubmission_view
|
||||
from compensation.views.compensation.report import report_view
|
||||
from compensation.views.compensation.deadline import deadline_new_view, deadline_edit_view, deadline_remove_view
|
||||
from compensation.views.compensation.action import action_edit_view, action_new_view, action_remove_view
|
||||
from compensation.views.compensation.state import state_new_view, state_remove_view, state_edit_view
|
||||
from compensation.views.compensation.compensation import index_view, new_view, new_id_view, detail_view, edit_view, \
|
||||
remove_view
|
||||
from compensation.views.compensation.log import log_view
|
||||
|
||||
urlpatterns = [
|
||||
# Main compensation
|
||||
|
@ -8,7 +8,19 @@ Created on: 24.08.21
|
||||
from django.urls import path
|
||||
|
||||
from compensation.autocomplete.eco_account import EcoAccountAutocomplete
|
||||
from compensation.views.eco_account import *
|
||||
from compensation.views.eco_account.eco_account import index_view, new_view, new_id_view, edit_view, remove_view, \
|
||||
detail_view
|
||||
from compensation.views.eco_account.log import log_view
|
||||
from compensation.views.eco_account.record import record_view
|
||||
from compensation.views.eco_account.report import report_view
|
||||
from compensation.views.eco_account.resubmission import create_resubmission_view
|
||||
from compensation.views.eco_account.state import state_new_view, state_remove_view, state_edit_view
|
||||
from compensation.views.eco_account.action import action_edit_view, action_new_view, action_remove_view
|
||||
from compensation.views.eco_account.deadline import deadline_new_view, deadline_edit_view, deadline_remove_view
|
||||
from compensation.views.eco_account.share import share_view, create_share_view
|
||||
from compensation.views.eco_account.document import get_document_view, new_document_view, remove_document_view, \
|
||||
edit_document_view
|
||||
from compensation.views.eco_account.deduction import deduction_edit_view, deduction_remove_view, new_deduction_view
|
||||
|
||||
app_name = "acc"
|
||||
urlpatterns = [
|
||||
|
@ -5,6 +5,5 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 16.11.21
|
||||
|
||||
"""
|
||||
from .compensation import *
|
||||
from .eco_account import *
|
||||
from .payment import *
|
||||
|
@ -1,685 +0,0 @@
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db.models import Sum
|
||||
from django.http import HttpRequest, JsonResponse
|
||||
from django.shortcuts import render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.forms.compensation import NewCompensationForm, EditCompensationForm
|
||||
from compensation.forms.modals.state import NewCompensationStateModalForm, RemoveCompensationStateModalForm, EditCompensationStateModalForm
|
||||
from compensation.forms.modals.document import NewCompensationDocumentModalForm
|
||||
from compensation.forms.modals.compensation_action import NewCompensationActionModalForm, \
|
||||
EditCompensationActionModalForm, RemoveCompensationActionModalForm
|
||||
from compensation.forms.modals.deadline import NewDeadlineModalForm, EditDeadlineModalForm
|
||||
from compensation.models import Compensation, CompensationState, CompensationAction, CompensationDocument
|
||||
from compensation.tables.compensation import CompensationTable
|
||||
from intervention.models import Intervention
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import *
|
||||
from konova.forms.modals import RemoveModalForm,RemoveDeadlineModalForm, EditDocumentModalForm, \
|
||||
ResubmissionModalForm
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.models import Deadline
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.documents import get_document, remove_document
|
||||
from konova.utils.generators import generate_qr_code
|
||||
from konova.utils.message_templates import FORM_INVALID, IDENTIFIER_REPLACED, \
|
||||
CHECKED_RECORDED_RESET, COMPENSATION_ADDED_TEMPLATE, COMPENSATION_REMOVED_TEMPLATE, DOCUMENT_ADDED, \
|
||||
COMPENSATION_STATE_REMOVED, COMPENSATION_STATE_ADDED, COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_ADDED, \
|
||||
DEADLINE_ADDED, DEADLINE_REMOVED, DOCUMENT_EDITED, COMPENSATION_STATE_EDITED, COMPENSATION_ACTION_EDITED, \
|
||||
DEADLINE_EDITED, RECORDED_BLOCKS_EDIT, PARAMS_INVALID, DATA_CHECKED_PREVIOUSLY_TEMPLATE
|
||||
from konova.utils.user_checks import in_group
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def index_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the index view for compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
A rendered view
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
compensations = Compensation.objects.filter(
|
||||
deleted=None, # only show those which are not deleted individually
|
||||
intervention__deleted=None, # and don't show the ones whose intervention has been deleted
|
||||
)
|
||||
table = CompensationTable(
|
||||
request=request,
|
||||
queryset=compensations
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("Compensations - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "intervention_id")
|
||||
def new_view(request: HttpRequest, intervention_id: str = None):
|
||||
"""
|
||||
Renders a view for a new compensation creation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
if intervention_id is not None:
|
||||
try:
|
||||
intervention = Intervention.objects.get(id=intervention_id)
|
||||
except ObjectDoesNotExist:
|
||||
messages.error(request, PARAMS_INVALID)
|
||||
return redirect("home")
|
||||
if intervention.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("intervention:detail", id=intervention_id)
|
||||
|
||||
data_form = NewCompensationForm(request.POST or None, intervention_id=intervention_id)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
comp = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != comp.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
IDENTIFIER_REPLACED.format(
|
||||
generated_identifier,
|
||||
comp.identifier
|
||||
)
|
||||
)
|
||||
messages.success(request, COMPENSATION_ADDED_TEMPLATE.format(comp.identifier))
|
||||
return redirect("compensation:detail", id=comp.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("New compensation"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_id_view(request: HttpRequest):
|
||||
""" JSON endpoint
|
||||
|
||||
Provides fetching of free identifiers for e.g. AJAX calls
|
||||
|
||||
"""
|
||||
tmp = Compensation()
|
||||
identifier = tmp.generate_new_identifier()
|
||||
while Compensation.objects.filter(identifier=identifier).exists():
|
||||
identifier = tmp.generate_new_identifier()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def edit_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
Renders a view for editing compensations
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
# Get object from db
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
if comp.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("compensation:detail", id=id)
|
||||
|
||||
# Create forms, initialize with values from db/from POST request
|
||||
data_form = EditCompensationForm(request.POST or None, instance=comp)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=comp)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
# Preserve state of intervention recorded/checked to determine whether the user must be informed or not
|
||||
# about a change of the recorded/checked state
|
||||
intervention_recorded = comp.intervention.recorded is not None
|
||||
intervention_checked = comp.intervention.checked is not None
|
||||
|
||||
# The data form takes the geom form for processing, as well as the performing user
|
||||
comp = data_form.save(request.user, geom_form)
|
||||
if intervention_recorded or intervention_checked:
|
||||
messages.info(request, CHECKED_RECORDED_RESET)
|
||||
messages.success(request, _("Compensation {} edited").format(comp.identifier))
|
||||
return redirect("compensation:detail", id=comp.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("Edit {}").format(comp.identifier),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def detail_view(request: HttpRequest, id: str):
|
||||
""" Renders a detail view for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/detail/compensation/view.html"
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
geom_form = SimpleGeomForm(instance=comp)
|
||||
parcels = comp.get_underlying_parcels()
|
||||
_user = request.user
|
||||
is_data_shared = comp.intervention.is_shared_with(_user)
|
||||
|
||||
# Order states according to surface
|
||||
before_states = comp.before_states.all().prefetch_related("biotope_type").order_by("-surface")
|
||||
after_states = comp.after_states.all().prefetch_related("biotope_type").order_by("-surface")
|
||||
actions = comp.actions.all().prefetch_related("action_type")
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
|
||||
request = comp.set_status_messages(request)
|
||||
|
||||
last_checked = comp.intervention.get_last_checked_action()
|
||||
last_checked_tooltip = ""
|
||||
if last_checked:
|
||||
last_checked_tooltip = DATA_CHECKED_PREVIOUSLY_TEMPLATE.format(last_checked.get_timestamp_str_formatted(), last_checked.user)
|
||||
|
||||
context = {
|
||||
"obj": comp,
|
||||
"last_checked": last_checked,
|
||||
"last_checked_tooltip": last_checked_tooltip,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"has_access": is_data_shared,
|
||||
"actions": actions,
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"sum_before_states": sum_before_states,
|
||||
"sum_after_states": sum_after_states,
|
||||
"diff_states": diff_states,
|
||||
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
||||
"is_zb_member": in_group(_user, ZB_GROUP),
|
||||
"is_ets_member": in_group(_user, ETS_GROUP),
|
||||
"LANIS_LINK": comp.get_LANIS_link(),
|
||||
TAB_TITLE_IDENTIFIER: f"{comp.identifier} - {comp.title}",
|
||||
"has_finished_deadlines": comp.get_finished_deadlines().exists(),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": comp.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal view for removing the compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = RemoveModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=COMPENSATION_REMOVED_TEMPLATE.format(comp.identifier),
|
||||
redirect_url=reverse("compensation:index"),
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def new_document_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for uploading new documents
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id to which the new document will be related
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewCompensationDocumentModalForm(request.POST or None, request.FILES or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DOCUMENT_ADDED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def get_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Returns the document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def remove_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
||||
return remove_document(
|
||||
request,
|
||||
doc
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
||||
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=comp, document=doc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
DOCUMENT_EDITED,
|
||||
reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def state_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewCompensationStateModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_ADDED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def action_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new actions for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewCompensationActionModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_ADDED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def action_edit_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for editing actions for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
action_id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = EditCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_EDITED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def deadline_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewDeadlineModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_ADDED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def deadline_edit_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for editing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = EditDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_EDITED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def deadline_remove_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for removing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = RemoveDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_REMOVED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def state_remove_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for removing a compensation state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = RemoveCompensationStateModalForm(request.POST or None, instance=comp, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_REMOVED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def state_edit_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for editing a compensation state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = EditCompensationStateModalForm(request.POST or None, instance=comp, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_EDITED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def action_remove_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for removing a compensation action
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = RemoveCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_REMOVED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
def report_view(request: HttpRequest, id: str):
|
||||
""" Renders the public report view
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The id of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Reuse the compensation report template since compensations are structurally identical
|
||||
template = "compensation/report/compensation/report.html"
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
|
||||
tab_title = _("Report {}").format(comp.identifier)
|
||||
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
||||
if not comp.is_ready_for_publish():
|
||||
template = "report/unavailable.html"
|
||||
context = {
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
# Prepare data for map viewer
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=comp
|
||||
)
|
||||
parcels = comp.get_underlying_parcels()
|
||||
|
||||
qrcode_url = request.build_absolute_uri(reverse("compensation:report", args=(id,)))
|
||||
qrcode_img = generate_qr_code(qrcode_url, 10)
|
||||
qrcode_lanis_url = comp.get_LANIS_link()
|
||||
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
||||
|
||||
# Order states by surface
|
||||
before_states = comp.before_states.all().order_by("-surface").prefetch_related("biotope_type")
|
||||
after_states = comp.after_states.all().order_by("-surface").prefetch_related("biotope_type")
|
||||
actions = comp.actions.all().prefetch_related("action_type")
|
||||
|
||||
context = {
|
||||
"obj": comp,
|
||||
"qrcode": {
|
||||
"img": qrcode_img,
|
||||
"url": qrcode_url,
|
||||
},
|
||||
"qrcode_lanis": {
|
||||
"img": qrcode_img_lanis,
|
||||
"url": qrcode_lanis_url,
|
||||
},
|
||||
"has_access": False, # disables action buttons during rendering
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"actions": actions,
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def create_resubmission_view(request: HttpRequest, id: str):
|
||||
""" Renders resubmission form for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
com = get_object_or_404(Compensation, id=id)
|
||||
form = ResubmissionModalForm(request.POST or None, instance=com, request=request)
|
||||
form.action_url = reverse("compensation:resubmission-create", args=(id,))
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Resubmission set"),
|
||||
redirect_url=reverse("compensation:detail", args=(id,))
|
||||
)
|
7
compensation/views/compensation/__init__.py
Normal file
7
compensation/views/compensation/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
89
compensation/views/compensation/action.py
Normal file
89
compensation/views/compensation/action.py
Normal file
@ -0,0 +1,89 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.compensation_action import RemoveCompensationActionModalForm, \
|
||||
EditCompensationActionModalForm, NewCompensationActionModalForm
|
||||
from compensation.models import Compensation, CompensationAction
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.utils.message_templates import COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_EDITED, \
|
||||
COMPENSATION_ACTION_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def action_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new actions for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewCompensationActionModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_ADDED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def action_edit_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for editing actions for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
action_id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = EditCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_EDITED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def action_remove_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for removing a compensation action
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = RemoveCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_REMOVED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
272
compensation/views/compensation/compensation.py
Normal file
272
compensation/views/compensation/compensation.py
Normal file
@ -0,0 +1,272 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db.models import Sum
|
||||
from django.http import HttpRequest, JsonResponse
|
||||
from django.shortcuts import get_object_or_404, render, redirect
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.forms.compensation import EditCompensationForm, NewCompensationForm
|
||||
from compensation.models import Compensation
|
||||
from compensation.tables.compensation import CompensationTable
|
||||
from intervention.models import Intervention
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, default_group_required, any_group_check
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.forms.modals import RemoveModalForm
|
||||
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.message_templates import COMPENSATION_REMOVED_TEMPLATE, DATA_CHECKED_PREVIOUSLY_TEMPLATE, \
|
||||
RECORDED_BLOCKS_EDIT, CHECKED_RECORDED_RESET, FORM_INVALID, PARAMS_INVALID, IDENTIFIER_REPLACED, \
|
||||
COMPENSATION_ADDED_TEMPLATE
|
||||
from konova.utils.user_checks import in_group
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def index_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the index view for compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
A rendered view
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
compensations = Compensation.objects.filter(
|
||||
deleted=None, # only show those which are not deleted individually
|
||||
intervention__deleted=None, # and don't show the ones whose intervention has been deleted
|
||||
)
|
||||
table = CompensationTable(
|
||||
request=request,
|
||||
queryset=compensations
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("Compensations - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "intervention_id")
|
||||
def new_view(request: HttpRequest, intervention_id: str = None):
|
||||
"""
|
||||
Renders a view for a new compensation creation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
if intervention_id is not None:
|
||||
try:
|
||||
intervention = Intervention.objects.get(id=intervention_id)
|
||||
except ObjectDoesNotExist:
|
||||
messages.error(request, PARAMS_INVALID)
|
||||
return redirect("home")
|
||||
if intervention.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("intervention:detail", id=intervention_id)
|
||||
|
||||
data_form = NewCompensationForm(request.POST or None, intervention_id=intervention_id)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
comp = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != comp.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
IDENTIFIER_REPLACED.format(
|
||||
generated_identifier,
|
||||
comp.identifier
|
||||
)
|
||||
)
|
||||
messages.success(request, COMPENSATION_ADDED_TEMPLATE.format(comp.identifier))
|
||||
return redirect("compensation:detail", id=comp.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("New compensation"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_id_view(request: HttpRequest):
|
||||
""" JSON endpoint
|
||||
|
||||
Provides fetching of free identifiers for e.g. AJAX calls
|
||||
|
||||
"""
|
||||
tmp = Compensation()
|
||||
identifier = tmp.generate_new_identifier()
|
||||
while Compensation.objects.filter(identifier=identifier).exists():
|
||||
identifier = tmp.generate_new_identifier()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def edit_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
Renders a view for editing compensations
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
# Get object from db
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
if comp.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("compensation:detail", id=id)
|
||||
|
||||
# Create forms, initialize with values from db/from POST request
|
||||
data_form = EditCompensationForm(request.POST or None, instance=comp)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=comp)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
# Preserve state of intervention recorded/checked to determine whether the user must be informed or not
|
||||
# about a change of the recorded/checked state
|
||||
intervention_recorded = comp.intervention.recorded is not None
|
||||
intervention_checked = comp.intervention.checked is not None
|
||||
|
||||
# The data form takes the geom form for processing, as well as the performing user
|
||||
comp = data_form.save(request.user, geom_form)
|
||||
if intervention_recorded or intervention_checked:
|
||||
messages.info(request, CHECKED_RECORDED_RESET)
|
||||
messages.success(request, _("Compensation {} edited").format(comp.identifier))
|
||||
return redirect("compensation:detail", id=comp.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("Edit {}").format(comp.identifier),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def detail_view(request: HttpRequest, id: str):
|
||||
""" Renders a detail view for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/detail/compensation/view.html"
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
geom_form = SimpleGeomForm(instance=comp)
|
||||
parcels = comp.get_underlying_parcels()
|
||||
_user = request.user
|
||||
is_data_shared = comp.intervention.is_shared_with(_user)
|
||||
|
||||
# Order states according to surface
|
||||
before_states = comp.before_states.all().prefetch_related("biotope_type").order_by("-surface")
|
||||
after_states = comp.after_states.all().prefetch_related("biotope_type").order_by("-surface")
|
||||
actions = comp.actions.all().prefetch_related("action_type")
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
|
||||
request = comp.set_status_messages(request)
|
||||
|
||||
last_checked = comp.intervention.get_last_checked_action()
|
||||
last_checked_tooltip = ""
|
||||
if last_checked:
|
||||
last_checked_tooltip = DATA_CHECKED_PREVIOUSLY_TEMPLATE.format(last_checked.get_timestamp_str_formatted(), last_checked.user)
|
||||
|
||||
context = {
|
||||
"obj": comp,
|
||||
"last_checked": last_checked,
|
||||
"last_checked_tooltip": last_checked_tooltip,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"has_access": is_data_shared,
|
||||
"actions": actions,
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"sum_before_states": sum_before_states,
|
||||
"sum_after_states": sum_after_states,
|
||||
"diff_states": diff_states,
|
||||
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
||||
"is_zb_member": in_group(_user, ZB_GROUP),
|
||||
"is_ets_member": in_group(_user, ETS_GROUP),
|
||||
"LANIS_LINK": comp.get_LANIS_link(),
|
||||
TAB_TITLE_IDENTIFIER: f"{comp.identifier} - {comp.title}",
|
||||
"has_finished_deadlines": comp.get_finished_deadlines().exists(),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal view for removing the compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = RemoveModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=COMPENSATION_REMOVED_TEMPLATE.format(comp.identifier),
|
||||
redirect_url=reverse("compensation:index"),
|
||||
)
|
||||
|
89
compensation/views/compensation/deadline.py
Normal file
89
compensation/views/compensation/deadline.py
Normal file
@ -0,0 +1,89 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.deadline import EditDeadlineModalForm, NewDeadlineModalForm
|
||||
from compensation.models import Compensation
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.forms.modals import RemoveDeadlineModalForm
|
||||
from konova.models import Deadline
|
||||
from konova.utils.message_templates import DEADLINE_REMOVED, DEADLINE_EDITED, DEADLINE_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def deadline_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewDeadlineModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_ADDED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def deadline_edit_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for editing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = EditDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_EDITED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def deadline_remove_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for removing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = RemoveDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_REMOVED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
111
compensation/views/compensation/document.py
Normal file
111
compensation/views/compensation/document.py
Normal file
@ -0,0 +1,111 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.document import NewCompensationDocumentModalForm
|
||||
from compensation.models import Compensation, CompensationDocument
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.forms.modals import EditDocumentModalForm
|
||||
from konova.utils.documents import remove_document, get_document
|
||||
from konova.utils.message_templates import DOCUMENT_EDITED, DOCUMENT_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def new_document_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for uploading new documents
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id to which the new document will be related
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewCompensationDocumentModalForm(request.POST or None, request.FILES or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DOCUMENT_ADDED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def get_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Returns the document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def remove_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
||||
return remove_document(
|
||||
request,
|
||||
doc
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
doc = get_object_or_404(CompensationDocument, id=doc_id)
|
||||
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=comp, document=doc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
DOCUMENT_EDITED,
|
||||
reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
41
compensation/views/compensation/log.py
Normal file
41
compensation/views/compensation/log.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import Compensation
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": comp.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
79
compensation/views/compensation/report.py
Normal file
79
compensation/views/compensation/report.py
Normal file
@ -0,0 +1,79 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import Compensation
|
||||
from konova.contexts import BaseContext
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.generators import generate_qr_code
|
||||
|
||||
|
||||
def report_view(request: HttpRequest, id: str):
|
||||
""" Renders the public report view
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The id of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Reuse the compensation report template since compensations are structurally identical
|
||||
template = "compensation/report/compensation/report.html"
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
|
||||
tab_title = _("Report {}").format(comp.identifier)
|
||||
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
||||
if not comp.is_ready_for_publish():
|
||||
template = "report/unavailable.html"
|
||||
context = {
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
# Prepare data for map viewer
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=comp
|
||||
)
|
||||
parcels = comp.get_underlying_parcels()
|
||||
|
||||
qrcode_url = request.build_absolute_uri(reverse("compensation:report", args=(id,)))
|
||||
qrcode_img = generate_qr_code(qrcode_url, 10)
|
||||
qrcode_lanis_url = comp.get_LANIS_link()
|
||||
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
||||
|
||||
# Order states by surface
|
||||
before_states = comp.before_states.all().order_by("-surface").prefetch_related("biotope_type")
|
||||
after_states = comp.after_states.all().order_by("-surface").prefetch_related("biotope_type")
|
||||
actions = comp.actions.all().prefetch_related("action_type")
|
||||
|
||||
context = {
|
||||
"obj": comp,
|
||||
"qrcode": {
|
||||
"img": qrcode_img,
|
||||
"url": qrcode_url,
|
||||
},
|
||||
"qrcode_lanis": {
|
||||
"img": qrcode_img_lanis,
|
||||
"url": qrcode_lanis_url,
|
||||
},
|
||||
"has_access": False, # disables action buttons during rendering
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"actions": actions,
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
39
compensation/views/compensation/resubmission.py
Normal file
39
compensation/views/compensation/resubmission.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import Compensation
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.forms.modals import ResubmissionModalForm
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def create_resubmission_view(request: HttpRequest, id: str):
|
||||
""" Renders resubmission form for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
com = get_object_or_404(Compensation, id=id)
|
||||
form = ResubmissionModalForm(request.POST or None, instance=com, request=request)
|
||||
form.action_url = reverse("compensation:resubmission-create", args=(id,))
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Resubmission set"),
|
||||
redirect_url=reverse("compensation:detail", args=(id,))
|
||||
)
|
89
compensation/views/compensation/state.py
Normal file
89
compensation/views/compensation/state.py
Normal file
@ -0,0 +1,89 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.state import EditCompensationStateModalForm, RemoveCompensationStateModalForm, \
|
||||
NewCompensationStateModalForm
|
||||
from compensation.models import Compensation, CompensationState
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.utils.message_templates import COMPENSATION_STATE_EDITED, COMPENSATION_STATE_REMOVED, \
|
||||
COMPENSATION_STATE_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def state_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
form = NewCompensationStateModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_ADDED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def state_remove_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for removing a compensation state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = RemoveCompensationStateModalForm(request.POST or None, instance=comp, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_REMOVED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Compensation, "id")
|
||||
def state_edit_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for editing a compensation state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(Compensation, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = EditCompensationStateModalForm(request.POST or None, instance=comp, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_EDITED,
|
||||
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
@ -1,869 +0,0 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 09.08.21
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.db.models import Sum
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import HttpRequest, Http404, JsonResponse
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
|
||||
from compensation.forms.eco_account import NewEcoAccountForm, EditEcoAccountForm
|
||||
from compensation.forms.modals.compensation_action import NewCompensationActionModalForm, \
|
||||
RemoveCompensationActionModalForm, EditCompensationActionModalForm
|
||||
from compensation.forms.modals.deadline import EditDeadlineModalForm, NewDeadlineModalForm
|
||||
from compensation.forms.modals.document import NewEcoAccountDocumentModalForm
|
||||
from compensation.forms.modals.state import NewCompensationStateModalForm, RemoveCompensationStateModalForm, \
|
||||
EditCompensationStateModalForm
|
||||
from compensation.models import EcoAccount, EcoAccountDocument, CompensationState, CompensationAction
|
||||
from compensation.tables.eco_account import EcoAccountTable
|
||||
from intervention.forms.modals.deduction import RemoveEcoAccountDeductionModalForm, NewEcoAccountDeductionModalForm, \
|
||||
EditEcoAccountDeductionModalForm
|
||||
from intervention.forms.modals.share import ShareModalForm
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import any_group_check, default_group_required, conservation_office_group_required, \
|
||||
shared_access_required
|
||||
from konova.forms.modals import RemoveModalForm, RecordModalForm, \
|
||||
RemoveDeadlineModalForm, EditDocumentModalForm, ResubmissionModalForm
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.models import Deadline
|
||||
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.documents import get_document, remove_document
|
||||
from konova.utils.generators import generate_qr_code
|
||||
from konova.utils.message_templates import IDENTIFIER_REPLACED, FORM_INVALID, \
|
||||
CANCEL_ACC_RECORDED_OR_DEDUCTED, DEDUCTION_REMOVED, DEDUCTION_ADDED, DOCUMENT_ADDED, COMPENSATION_STATE_REMOVED, \
|
||||
COMPENSATION_STATE_ADDED, COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_ADDED, DEADLINE_ADDED, DEADLINE_REMOVED, \
|
||||
DEDUCTION_EDITED, DOCUMENT_EDITED, COMPENSATION_STATE_EDITED, COMPENSATION_ACTION_EDITED, DEADLINE_EDITED, \
|
||||
RECORDED_BLOCKS_EDIT
|
||||
from konova.utils.user_checks import in_group
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def index_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the index view for eco accounts
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
A rendered view
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
eco_accounts = EcoAccount.objects.filter(
|
||||
deleted=None,
|
||||
)
|
||||
table = EcoAccountTable(
|
||||
request=request,
|
||||
queryset=eco_accounts
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("Eco-account - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_view(request: HttpRequest):
|
||||
"""
|
||||
Renders a view for a new eco account creation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
data_form = NewEcoAccountForm(request.POST or None)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
acc = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != acc.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
IDENTIFIER_REPLACED.format(
|
||||
generated_identifier,
|
||||
acc.identifier
|
||||
)
|
||||
)
|
||||
messages.success(request, _("Eco-Account {} added").format(acc.identifier))
|
||||
return redirect("compensation:acc:detail", id=acc.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("New Eco-Account"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_id_view(request: HttpRequest):
|
||||
""" JSON endpoint
|
||||
|
||||
Provides fetching of free identifiers for e.g. AJAX calls
|
||||
|
||||
"""
|
||||
tmp = EcoAccount()
|
||||
identifier = tmp.generate_new_identifier()
|
||||
while EcoAccount.objects.filter(identifier=identifier).exists():
|
||||
identifier = tmp.generate_new_identifier()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def edit_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
Renders a view for editing compensations
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
# Get object from db
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
if acc.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("compensation:acc:detail", id=id)
|
||||
|
||||
# Create forms, initialize with values from db/from POST request
|
||||
data_form = EditEcoAccountForm(request.POST or None, instance=acc)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=acc)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
# The data form takes the geom form for processing, as well as the performing user
|
||||
acc = data_form.save(request.user, geom_form)
|
||||
messages.success(request, _("Eco-Account {} edited").format(acc.identifier))
|
||||
return redirect("compensation:acc:detail", id=acc.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("Edit {}").format(acc.identifier),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def detail_view(request: HttpRequest, id: str):
|
||||
""" Renders a detail view for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/detail/eco_account/view.html"
|
||||
acc = get_object_or_404(
|
||||
EcoAccount.objects.prefetch_related(
|
||||
"deadlines",
|
||||
).select_related(
|
||||
'geometry',
|
||||
'responsible',
|
||||
),
|
||||
id=id
|
||||
)
|
||||
geom_form = SimpleGeomForm(instance=acc)
|
||||
parcels = acc.get_underlying_parcels()
|
||||
_user = request.user
|
||||
is_data_shared = acc.is_shared_with(_user)
|
||||
|
||||
# Order states according to surface
|
||||
before_states = acc.before_states.order_by("-surface")
|
||||
after_states = acc.after_states.order_by("-surface")
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
# Calculate rest of available surface for deductions
|
||||
available_total, available_relative = acc.get_available_rest()
|
||||
|
||||
# Prefetch related data to decrease the amount of db connections
|
||||
deductions = acc.deductions.filter(
|
||||
intervention__deleted=None,
|
||||
)
|
||||
actions = acc.actions.all()
|
||||
|
||||
request = acc.set_status_messages(request)
|
||||
|
||||
context = {
|
||||
"obj": acc,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"has_access": is_data_shared,
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"sum_before_states": sum_before_states,
|
||||
"sum_after_states": sum_after_states,
|
||||
"diff_states": diff_states,
|
||||
"available": available_relative,
|
||||
"available_total": available_total,
|
||||
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
||||
"is_zb_member": in_group(_user, ZB_GROUP),
|
||||
"is_ets_member": in_group(_user, ETS_GROUP),
|
||||
"LANIS_LINK": acc.get_LANIS_link(),
|
||||
"deductions": deductions,
|
||||
"actions": actions,
|
||||
TAB_TITLE_IDENTIFIER: f"{acc.identifier} - {acc.title}",
|
||||
"has_finished_deadlines": acc.get_finished_deadlines().exists(),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal view for removing the eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
|
||||
# If the eco account has already been recorded OR there are already deductions, it can not be deleted by a regular
|
||||
# default group user
|
||||
if acc.recorded is not None or acc.deductions.exists():
|
||||
user = request.user
|
||||
if not in_group(user, ETS_GROUP):
|
||||
messages.info(request, CANCEL_ACC_RECORDED_OR_DEDUCTED)
|
||||
return redirect("compensation:acc:detail", id=id)
|
||||
|
||||
form = RemoveModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("Eco-account removed"),
|
||||
redirect_url=reverse("compensation:acc:index"),
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def deduction_remove_view(request: HttpRequest, id: str, deduction_id: str):
|
||||
""" Renders a modal view for removing deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The eco account's id
|
||||
deduction_id (str): The deduction's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
try:
|
||||
eco_deduction = acc.deductions.get(id=deduction_id)
|
||||
if not eco_deduction.intervention.is_shared_with(request.user):
|
||||
raise ObjectDoesNotExist()
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown deduction")
|
||||
|
||||
form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=acc, deduction=eco_deduction, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DEDUCTION_REMOVED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def deduction_edit_view(request: HttpRequest, id: str, deduction_id: str):
|
||||
""" Renders a modal view for editing deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The eco account's id
|
||||
deduction_id (str): The deduction's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
try:
|
||||
eco_deduction = acc.deductions.get(id=deduction_id)
|
||||
if not eco_deduction.intervention.is_shared_with(request.user):
|
||||
raise ObjectDoesNotExist
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown deduction")
|
||||
|
||||
form = EditEcoAccountDeductionModalForm(request.POST or None, instance=acc, deduction=eco_deduction, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DEDUCTION_EDITED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The eco acount's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(EcoAccount, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": comp.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def record_view(request: HttpRequest, id:str):
|
||||
""" Renders a modal form for recording an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = RecordModalForm(request.POST or None, instance=acc, request=request)
|
||||
msg_succ = _("{} unrecorded") if acc.recorded else _("{} recorded")
|
||||
msg_succ = msg_succ.format(acc.identifier)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_succ
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def state_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewCompensationStateModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def action_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new actions for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewCompensationActionModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def state_remove_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for removing a compensation state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = RemoveCompensationStateModalForm(request.POST or None, instance=acc, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_REMOVED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def state_edit_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for editing a compensation state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = EditCompensationStateModalForm(request.POST or None, instance=acc, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_EDITED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def action_remove_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for removing a compensation action
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = RemoveCompensationActionModalForm(request.POST or None, instance=acc, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_REMOVED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def action_edit_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for editing a compensation action
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = EditCompensationActionModalForm(request.POST or None, instance=acc, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_EDITED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def deadline_edit_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for editing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(EcoAccount, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = EditDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_EDITED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def deadline_remove_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for removing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(EcoAccount, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = RemoveDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_REMOVED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def deadline_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewDeadlineModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def new_document_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for uploading new documents
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id to which the new document will be related
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewEcoAccountDocumentModalForm(request.POST or None, request.FILES or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DOCUMENT_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data",
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def get_document_view(request: HttpRequest, id:str, doc_id: str):
|
||||
""" Returns the document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
doc = get_object_or_404(EcoAccountDocument, id=doc_id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
doc = get_object_or_404(EcoAccountDocument, id=doc_id)
|
||||
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=acc, document=doc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
DOCUMENT_EDITED,
|
||||
reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def remove_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
doc = get_object_or_404(EcoAccountDocument, id=doc_id)
|
||||
return remove_document(
|
||||
request,
|
||||
doc
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_deduction_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal form view for creating deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): THe incoming request
|
||||
id (str): The eco account's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
if not acc.recorded:
|
||||
raise Http404()
|
||||
form = NewEcoAccountDeductionModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEDUCTION_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
def report_view(request:HttpRequest, id: str):
|
||||
""" Renders the public report view
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The id of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Reuse the compensation report template since EcoAccounts are structurally identical
|
||||
template = "compensation/report/eco_account/report.html"
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
|
||||
tab_title = _("Report {}").format(acc.identifier)
|
||||
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
||||
if not acc.is_ready_for_publish():
|
||||
template = "report/unavailable.html"
|
||||
context = {
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
# Prepare data for map viewer
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=acc
|
||||
)
|
||||
parcels = acc.get_underlying_parcels()
|
||||
|
||||
qrcode_url = request.build_absolute_uri(reverse("ema:report", args=(id,)))
|
||||
qrcode_img = generate_qr_code(qrcode_url, 10)
|
||||
qrcode_lanis_url = acc.get_LANIS_link()
|
||||
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
||||
|
||||
# Order states by surface
|
||||
before_states = acc.before_states.all().order_by("-surface").select_related("biotope_type__parent")
|
||||
after_states = acc.after_states.all().order_by("-surface").select_related("biotope_type__parent")
|
||||
actions = acc.actions.all().prefetch_related("action_type__parent")
|
||||
|
||||
# Reduce amount of db fetched data to the bare minimum we need in the template (deduction's intervention id and identifier)
|
||||
deductions = acc.deductions.all()\
|
||||
.distinct("intervention")\
|
||||
.select_related("intervention")\
|
||||
.values_list("intervention__id", "intervention__identifier", "intervention__title", named=True)
|
||||
|
||||
context = {
|
||||
"obj": acc,
|
||||
"qrcode": {
|
||||
"img": qrcode_img,
|
||||
"url": qrcode_url,
|
||||
},
|
||||
"qrcode_lanis": {
|
||||
"img": qrcode_img_lanis,
|
||||
"url": qrcode_lanis_url,
|
||||
},
|
||||
"has_access": False, # disables action buttons during rendering
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"actions": actions,
|
||||
"deductions": deductions,
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
def share_view(request: HttpRequest, id: str, token: str):
|
||||
""" Performs sharing of an eco account
|
||||
|
||||
If token given in url is not valid, the user will be redirected to the dashboard
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EcoAccount's id
|
||||
token (str): Access token for EcoAccount
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
user = request.user
|
||||
obj = get_object_or_404(EcoAccount, id=id)
|
||||
# Check tokens
|
||||
if obj.access_token == token:
|
||||
# Send different messages in case user has already been added to list of sharing users
|
||||
if obj.is_shared_with(user):
|
||||
messages.info(
|
||||
request,
|
||||
_("{} has already been shared with you").format(obj.identifier)
|
||||
)
|
||||
else:
|
||||
messages.success(
|
||||
request,
|
||||
_("{} has been shared with you").format(obj.identifier)
|
||||
)
|
||||
obj.share_with_user(user)
|
||||
return redirect("compensation:acc:detail", id=id)
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_("Share link invalid"),
|
||||
extra_tags="danger",
|
||||
)
|
||||
return redirect("home")
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def create_share_view(request: HttpRequest, id: str):
|
||||
""" Renders sharing form for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EcoAccount's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
obj = get_object_or_404(EcoAccount, id=id)
|
||||
form = ShareModalForm(request.POST or None, instance=obj, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Share settings updated")
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def create_resubmission_view(request: HttpRequest, id: str):
|
||||
""" Renders resubmission form for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EcoAccount's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = ResubmissionModalForm(request.POST or None, instance=acc, request=request)
|
||||
form.action_url = reverse("compensation:acc:resubmission-create", args=(id,))
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Resubmission set"),
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,))
|
||||
)
|
7
compensation/views/eco_account/__init__.py
Normal file
7
compensation/views/eco_account/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
89
compensation/views/eco_account/action.py
Normal file
89
compensation/views/eco_account/action.py
Normal file
@ -0,0 +1,89 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.compensation_action import EditCompensationActionModalForm, \
|
||||
RemoveCompensationActionModalForm, NewCompensationActionModalForm
|
||||
from compensation.models import EcoAccount, CompensationAction
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.utils.message_templates import COMPENSATION_ACTION_EDITED, COMPENSATION_ACTION_REMOVED, \
|
||||
COMPENSATION_ACTION_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def action_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new actions for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewCompensationActionModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def action_remove_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for removing a compensation action
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = RemoveCompensationActionModalForm(request.POST or None, instance=acc, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_REMOVED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def action_edit_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for editing a compensation action
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = EditCompensationActionModalForm(request.POST or None, instance=acc, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_EDITED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
89
compensation/views/eco_account/deadline.py
Normal file
89
compensation/views/eco_account/deadline.py
Normal file
@ -0,0 +1,89 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.deadline import NewDeadlineModalForm, EditDeadlineModalForm
|
||||
from compensation.models import EcoAccount
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.forms.modals import RemoveDeadlineModalForm
|
||||
from konova.models import Deadline
|
||||
from konova.utils.message_templates import DEADLINE_ADDED, DEADLINE_REMOVED, DEADLINE_EDITED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def deadline_edit_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for editing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(EcoAccount, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = EditDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_EDITED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def deadline_remove_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for removing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(EcoAccount, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = RemoveDeadlineModalForm(request.POST or None, instance=comp, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_REMOVED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def deadline_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewDeadlineModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
101
compensation/views/eco_account/deduction.py
Normal file
101
compensation/views/eco_account/deduction.py
Normal file
@ -0,0 +1,101 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import HttpRequest, Http404
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.models import EcoAccount
|
||||
from intervention.forms.modals.deduction import EditEcoAccountDeductionModalForm, RemoveEcoAccountDeductionModalForm, \
|
||||
NewEcoAccountDeductionModalForm
|
||||
from konova.decorators import default_group_required
|
||||
from konova.utils.message_templates import DEDUCTION_EDITED, DEDUCTION_REMOVED, DEDUCTION_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_deduction_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal form view for creating deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): THe incoming request
|
||||
id (str): The eco account's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
if not acc.recorded:
|
||||
raise Http404()
|
||||
form = NewEcoAccountDeductionModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEDUCTION_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def deduction_remove_view(request: HttpRequest, id: str, deduction_id: str):
|
||||
""" Renders a modal view for removing deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The eco account's id
|
||||
deduction_id (str): The deduction's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
try:
|
||||
eco_deduction = acc.deductions.get(id=deduction_id)
|
||||
if not eco_deduction.intervention.is_shared_with(request.user):
|
||||
raise ObjectDoesNotExist()
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown deduction")
|
||||
|
||||
form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=acc, deduction=eco_deduction, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DEDUCTION_REMOVED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def deduction_edit_view(request: HttpRequest, id: str, deduction_id: str):
|
||||
""" Renders a modal view for editing deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The eco account's id
|
||||
deduction_id (str): The deduction's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
try:
|
||||
eco_deduction = acc.deductions.get(id=deduction_id)
|
||||
if not eco_deduction.intervention.is_shared_with(request.user):
|
||||
raise ObjectDoesNotExist
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown deduction")
|
||||
|
||||
form = EditEcoAccountDeductionModalForm(request.POST or None, instance=acc, deduction=eco_deduction, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DEDUCTION_EDITED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
111
compensation/views/eco_account/document.py
Normal file
111
compensation/views/eco_account/document.py
Normal file
@ -0,0 +1,111 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.document import NewEcoAccountDocumentModalForm
|
||||
from compensation.models import EcoAccount, EcoAccountDocument
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.forms.modals import EditDocumentModalForm
|
||||
from konova.utils.documents import remove_document, get_document
|
||||
from konova.utils.message_templates import DOCUMENT_EDITED, DOCUMENT_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def new_document_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for uploading new documents
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id to which the new document will be related
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewEcoAccountDocumentModalForm(request.POST or None, request.FILES or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DOCUMENT_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data",
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def get_document_view(request: HttpRequest, id:str, doc_id: str):
|
||||
""" Returns the document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
doc = get_object_or_404(EcoAccountDocument, id=doc_id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
doc = get_object_or_404(EcoAccountDocument, id=doc_id)
|
||||
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=acc, document=doc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
DOCUMENT_EDITED,
|
||||
reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def remove_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
doc = get_object_or_404(EcoAccountDocument, id=doc_id)
|
||||
return remove_document(
|
||||
request,
|
||||
doc
|
||||
)
|
||||
|
266
compensation/views/eco_account/eco_account.py
Normal file
266
compensation/views/eco_account/eco_account.py
Normal file
@ -0,0 +1,266 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Sum
|
||||
from django.http import HttpRequest, JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.forms.eco_account import EditEcoAccountForm, NewEcoAccountForm
|
||||
from compensation.models import EcoAccount
|
||||
from compensation.tables.eco_account import EcoAccountTable
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, default_group_required, any_group_check
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.forms.modals import RemoveModalForm
|
||||
from konova.settings import ETS_GROUP, DEFAULT_GROUP, ZB_GROUP
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.message_templates import CANCEL_ACC_RECORDED_OR_DEDUCTED, RECORDED_BLOCKS_EDIT, FORM_INVALID
|
||||
from konova.utils.user_checks import in_group
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def index_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the index view for eco accounts
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
A rendered view
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
eco_accounts = EcoAccount.objects.filter(
|
||||
deleted=None,
|
||||
)
|
||||
table = EcoAccountTable(
|
||||
request=request,
|
||||
queryset=eco_accounts
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("Eco-account - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_view(request: HttpRequest):
|
||||
"""
|
||||
Renders a view for a new eco account creation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
data_form = NewEcoAccountForm(request.POST or None)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
acc = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != acc.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
IDENTIFIER_REPLACED.format(
|
||||
generated_identifier,
|
||||
acc.identifier
|
||||
)
|
||||
)
|
||||
messages.success(request, _("Eco-Account {} added").format(acc.identifier))
|
||||
return redirect("compensation:acc:detail", id=acc.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("New Eco-Account"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_id_view(request: HttpRequest):
|
||||
""" JSON endpoint
|
||||
|
||||
Provides fetching of free identifiers for e.g. AJAX calls
|
||||
|
||||
"""
|
||||
tmp = EcoAccount()
|
||||
identifier = tmp.generate_new_identifier()
|
||||
while EcoAccount.objects.filter(identifier=identifier).exists():
|
||||
identifier = tmp.generate_new_identifier()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def edit_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
Renders a view for editing compensations
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
# Get object from db
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
if acc.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("compensation:acc:detail", id=id)
|
||||
|
||||
# Create forms, initialize with values from db/from POST request
|
||||
data_form = EditEcoAccountForm(request.POST or None, instance=acc)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=acc)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
# The data form takes the geom form for processing, as well as the performing user
|
||||
acc = data_form.save(request.user, geom_form)
|
||||
messages.success(request, _("Eco-Account {} edited").format(acc.identifier))
|
||||
return redirect("compensation:acc:detail", id=acc.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("Edit {}").format(acc.identifier),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def detail_view(request: HttpRequest, id: str):
|
||||
""" Renders a detail view for a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/detail/eco_account/view.html"
|
||||
acc = get_object_or_404(
|
||||
EcoAccount.objects.prefetch_related(
|
||||
"deadlines",
|
||||
).select_related(
|
||||
'geometry',
|
||||
'responsible',
|
||||
),
|
||||
id=id
|
||||
)
|
||||
geom_form = SimpleGeomForm(instance=acc)
|
||||
parcels = acc.get_underlying_parcels()
|
||||
_user = request.user
|
||||
is_data_shared = acc.is_shared_with(_user)
|
||||
|
||||
# Order states according to surface
|
||||
before_states = acc.before_states.order_by("-surface")
|
||||
after_states = acc.after_states.order_by("-surface")
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
# Calculate rest of available surface for deductions
|
||||
available_total, available_relative = acc.get_available_rest()
|
||||
|
||||
# Prefetch related data to decrease the amount of db connections
|
||||
deductions = acc.deductions.filter(
|
||||
intervention__deleted=None,
|
||||
)
|
||||
actions = acc.actions.all()
|
||||
|
||||
request = acc.set_status_messages(request)
|
||||
|
||||
context = {
|
||||
"obj": acc,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"has_access": is_data_shared,
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"sum_before_states": sum_before_states,
|
||||
"sum_after_states": sum_after_states,
|
||||
"diff_states": diff_states,
|
||||
"available": available_relative,
|
||||
"available_total": available_total,
|
||||
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
||||
"is_zb_member": in_group(_user, ZB_GROUP),
|
||||
"is_ets_member": in_group(_user, ETS_GROUP),
|
||||
"LANIS_LINK": acc.get_LANIS_link(),
|
||||
"deductions": deductions,
|
||||
"actions": actions,
|
||||
TAB_TITLE_IDENTIFIER: f"{acc.identifier} - {acc.title}",
|
||||
"has_finished_deadlines": acc.get_finished_deadlines().exists(),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal view for removing the eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
|
||||
# If the eco account has already been recorded OR there are already deductions, it can not be deleted by a regular
|
||||
# default group user
|
||||
if acc.recorded is not None or acc.deductions.exists():
|
||||
user = request.user
|
||||
if not in_group(user, ETS_GROUP):
|
||||
messages.info(request, CANCEL_ACC_RECORDED_OR_DEDUCTED)
|
||||
return redirect("compensation:acc:detail", id=id)
|
||||
|
||||
form = RemoveModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("Eco-account removed"),
|
||||
redirect_url=reverse("compensation:acc:index"),
|
||||
)
|
||||
|
42
compensation/views/eco_account/log.py
Normal file
42
compensation/views/eco_account/log.py
Normal file
@ -0,0 +1,42 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import EcoAccount
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The eco acount's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
comp = get_object_or_404(EcoAccount, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": comp.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
38
compensation/views/eco_account/record.py
Normal file
38
compensation/views/eco_account/record.py
Normal file
@ -0,0 +1,38 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import EcoAccount
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
from konova.forms.modals import RecordModalForm
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def record_view(request: HttpRequest, id:str):
|
||||
""" Renders a modal form for recording an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = RecordModalForm(request.POST or None, instance=acc, request=request)
|
||||
msg_succ = _("{} unrecorded") if acc.recorded else _("{} recorded")
|
||||
msg_succ = msg_succ.format(acc.identifier)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_succ
|
||||
)
|
86
compensation/views/eco_account/report.py
Normal file
86
compensation/views/eco_account/report.py
Normal file
@ -0,0 +1,86 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import EcoAccount
|
||||
from konova.contexts import BaseContext
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.generators import generate_qr_code
|
||||
|
||||
|
||||
def report_view(request: HttpRequest, id: str):
|
||||
""" Renders the public report view
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The id of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Reuse the compensation report template since EcoAccounts are structurally identical
|
||||
template = "compensation/report/eco_account/report.html"
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
|
||||
tab_title = _("Report {}").format(acc.identifier)
|
||||
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
||||
if not acc.is_ready_for_publish():
|
||||
template = "report/unavailable.html"
|
||||
context = {
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
# Prepare data for map viewer
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=acc
|
||||
)
|
||||
parcels = acc.get_underlying_parcels()
|
||||
|
||||
qrcode_url = request.build_absolute_uri(reverse("compensation:acc:report", args=(id,)))
|
||||
qrcode_img = generate_qr_code(qrcode_url, 10)
|
||||
qrcode_lanis_url = acc.get_LANIS_link()
|
||||
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
||||
|
||||
# Order states by surface
|
||||
before_states = acc.before_states.all().order_by("-surface").select_related("biotope_type__parent")
|
||||
after_states = acc.after_states.all().order_by("-surface").select_related("biotope_type__parent")
|
||||
actions = acc.actions.all().prefetch_related("action_type__parent")
|
||||
|
||||
# Reduce amount of db fetched data to the bare minimum we need in the template (deduction's intervention id and identifier)
|
||||
deductions = acc.deductions.all()\
|
||||
.distinct("intervention")\
|
||||
.select_related("intervention")\
|
||||
.values_list("intervention__id", "intervention__identifier", "intervention__title", named=True)
|
||||
|
||||
context = {
|
||||
"obj": acc,
|
||||
"qrcode": {
|
||||
"img": qrcode_img,
|
||||
"url": qrcode_url,
|
||||
},
|
||||
"qrcode_lanis": {
|
||||
"img": qrcode_img_lanis,
|
||||
"url": qrcode_lanis_url,
|
||||
},
|
||||
"has_access": False, # disables action buttons during rendering
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"actions": actions,
|
||||
"deductions": deductions,
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
39
compensation/views/eco_account/resubmission.py
Normal file
39
compensation/views/eco_account/resubmission.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import EcoAccount
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.forms.modals import ResubmissionModalForm
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def create_resubmission_view(request: HttpRequest, id: str):
|
||||
""" Renders resubmission form for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EcoAccount's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = ResubmissionModalForm(request.POST or None, instance=acc, request=request)
|
||||
form.action_url = reverse("compensation:acc:resubmission-create", args=(id,))
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Resubmission set"),
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,))
|
||||
)
|
78
compensation/views/eco_account/share.py
Normal file
78
compensation/views/eco_account/share.py
Normal file
@ -0,0 +1,78 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import EcoAccount
|
||||
from intervention.forms.modals.share import ShareModalForm
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
|
||||
|
||||
@login_required
|
||||
def share_view(request: HttpRequest, id: str, token: str):
|
||||
""" Performs sharing of an eco account
|
||||
|
||||
If token given in url is not valid, the user will be redirected to the dashboard
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EcoAccount's id
|
||||
token (str): Access token for EcoAccount
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
user = request.user
|
||||
obj = get_object_or_404(EcoAccount, id=id)
|
||||
# Check tokens
|
||||
if obj.access_token == token:
|
||||
# Send different messages in case user has already been added to list of sharing users
|
||||
if obj.is_shared_with(user):
|
||||
messages.info(
|
||||
request,
|
||||
_("{} has already been shared with you").format(obj.identifier)
|
||||
)
|
||||
else:
|
||||
messages.success(
|
||||
request,
|
||||
_("{} has been shared with you").format(obj.identifier)
|
||||
)
|
||||
obj.share_with_user(user)
|
||||
return redirect("compensation:acc:detail", id=id)
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_("Share link invalid"),
|
||||
extra_tags="danger",
|
||||
)
|
||||
return redirect("home")
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def create_share_view(request: HttpRequest, id: str):
|
||||
""" Renders sharing form for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EcoAccount's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
obj = get_object_or_404(EcoAccount, id=id)
|
||||
form = ShareModalForm(request.POST or None, instance=obj, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Share settings updated")
|
||||
)
|
||||
|
88
compensation/views/eco_account/state.py
Normal file
88
compensation/views/eco_account/state.py
Normal file
@ -0,0 +1,88 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.state import EditCompensationStateModalForm, RemoveCompensationStateModalForm, \
|
||||
NewCompensationStateModalForm
|
||||
from compensation.models import EcoAccount, CompensationState
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
from konova.utils.message_templates import COMPENSATION_STATE_EDITED, COMPENSATION_STATE_REMOVED, \
|
||||
COMPENSATION_STATE_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def state_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for an eco account
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The account's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
form = NewCompensationStateModalForm(request.POST or None, instance=acc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_ADDED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def state_remove_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for removing a compensation state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = RemoveCompensationStateModalForm(request.POST or None, instance=acc, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_REMOVED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(EcoAccount, "id")
|
||||
def state_edit_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for editing a compensation state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
acc = get_object_or_404(EcoAccount, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = EditCompensationStateModalForm(request.POST or None, instance=acc, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_EDITED,
|
||||
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
12
ema/urls.py
12
ema/urls.py
@ -6,7 +6,17 @@ Created on: 19.08.21
|
||||
|
||||
"""
|
||||
from django.urls import path
|
||||
from ema.views import *
|
||||
|
||||
from ema.views.action import action_new_view, action_edit_view, action_remove_view
|
||||
from ema.views.deadline import deadline_new_view, deadline_edit_view, deadline_remove_view
|
||||
from ema.views.document import document_new_view, get_document_view, remove_document_view, edit_document_view
|
||||
from ema.views.ema import index_view, new_view, new_id_view, detail_view, edit_view, remove_view
|
||||
from ema.views.log import log_view
|
||||
from ema.views.record import record_view
|
||||
from ema.views.report import report_view
|
||||
from ema.views.resubmission import create_resubmission_view
|
||||
from ema.views.share import share_view, create_share_view
|
||||
from ema.views.state import state_new_view, state_remove_view, state_edit_view
|
||||
|
||||
app_name = "ema"
|
||||
urlpatterns = [
|
||||
|
739
ema/views.py
739
ema/views.py
@ -1,739 +0,0 @@
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Sum
|
||||
from django.http import HttpRequest, JsonResponse
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.forms.modals.compensation_action import NewCompensationActionModalForm, \
|
||||
EditCompensationActionModalForm, RemoveCompensationActionModalForm
|
||||
from compensation.forms.modals.deadline import NewDeadlineModalForm, EditDeadlineModalForm
|
||||
from compensation.forms.modals.state import NewCompensationStateModalForm, RemoveCompensationStateModalForm, \
|
||||
EditCompensationStateModalForm
|
||||
from compensation.models import CompensationAction, CompensationState
|
||||
from ema.forms import NewEmaForm, EditEmaForm, NewEmaDocumentModalForm
|
||||
from ema.tables import EmaTable
|
||||
from intervention.forms.modals.share import ShareModalForm
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import conservation_office_group_required, shared_access_required
|
||||
from ema.models import Ema, EmaDocument
|
||||
from konova.forms.modals import RemoveModalForm, RecordModalForm, RemoveDeadlineModalForm, \
|
||||
EditDocumentModalForm, ResubmissionModalForm
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.models import Deadline
|
||||
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.documents import get_document, remove_document
|
||||
from konova.utils.generators import generate_qr_code
|
||||
from konova.utils.message_templates import IDENTIFIER_REPLACED, FORM_INVALID, DATA_UNSHARED, DATA_UNSHARED_EXPLANATION, \
|
||||
DOCUMENT_ADDED, COMPENSATION_STATE_REMOVED, COMPENSATION_STATE_ADDED, COMPENSATION_ACTION_REMOVED, \
|
||||
COMPENSATION_ACTION_ADDED, DEADLINE_ADDED, DEADLINE_REMOVED, DOCUMENT_EDITED, COMPENSATION_STATE_EDITED, \
|
||||
COMPENSATION_ACTION_EDITED, DEADLINE_EDITED, RECORDED_BLOCKS_EDIT
|
||||
from konova.utils.user_checks import in_group
|
||||
|
||||
|
||||
@login_required
|
||||
def index_view(request: HttpRequest):
|
||||
""" Renders the index view for EMAs
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
emas = Ema.objects.filter(
|
||||
deleted=None,
|
||||
).order_by(
|
||||
"-modified"
|
||||
)
|
||||
table = EmaTable(
|
||||
request,
|
||||
queryset=emas
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("EMAs - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
def new_view(request: HttpRequest):
|
||||
"""
|
||||
Renders a view for a new eco account creation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "ema/form/view.html"
|
||||
data_form = NewEmaForm(request.POST or None)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
ema = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != ema.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
IDENTIFIER_REPLACED.format(
|
||||
generated_identifier,
|
||||
ema.identifier
|
||||
)
|
||||
)
|
||||
messages.success(request, _("EMA {} added").format(ema.identifier))
|
||||
return redirect("ema:detail", id=ema.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("New EMA"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
def new_id_view(request: HttpRequest):
|
||||
""" JSON endpoint
|
||||
|
||||
Provides fetching of free identifiers for e.g. AJAX calls
|
||||
|
||||
"""
|
||||
tmp = Ema()
|
||||
identifier = tmp.generate_new_identifier()
|
||||
while Ema.objects.filter(identifier=identifier).exists():
|
||||
identifier = tmp.generate_new_identifier()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def detail_view(request: HttpRequest, id: str):
|
||||
""" Renders the detail view of an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "ema/detail/view.html"
|
||||
ema = get_object_or_404(Ema, id=id, deleted=None)
|
||||
|
||||
geom_form = SimpleGeomForm(instance=ema)
|
||||
parcels = ema.get_underlying_parcels()
|
||||
_user = request.user
|
||||
is_data_shared = ema.is_shared_with(_user)
|
||||
|
||||
# Order states according to surface
|
||||
before_states = ema.before_states.all().order_by("-surface")
|
||||
after_states = ema.after_states.all().order_by("-surface")
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
|
||||
ema.set_status_messages(request)
|
||||
|
||||
context = {
|
||||
"obj": ema,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"has_access": is_data_shared,
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"sum_before_states": sum_before_states,
|
||||
"sum_after_states": sum_after_states,
|
||||
"diff_states": diff_states,
|
||||
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
||||
"is_zb_member": in_group(_user, ZB_GROUP),
|
||||
"is_ets_member": in_group(_user, ETS_GROUP),
|
||||
"LANIS_LINK": ema.get_LANIS_link(),
|
||||
TAB_TITLE_IDENTIFIER: f"{ema.identifier} - {ema.title}",
|
||||
"has_finished_deadlines": ema.get_finished_deadlines().exists(),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": ema.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def edit_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
Renders a view for editing compensations
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
# Get object from db
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
if ema.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("ema:detail", id=id)
|
||||
|
||||
# Create forms, initialize with values from db/from POST request
|
||||
data_form = EditEmaForm(request.POST or None, instance=ema)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=ema)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
# The data form takes the geom form for processing, as well as the performing user
|
||||
ema = data_form.save(request.user, geom_form)
|
||||
messages.success(request, _("EMA {} edited").format(ema.identifier))
|
||||
return redirect("ema:detail", id=ema.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("Edit {}").format(ema.identifier),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal view for removing the EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = RemoveModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("EMA removed"),
|
||||
redirect_url=reverse("ema:index"),
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def record_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal view for recording the EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
msg_succ = _("{} unrecorded") if ema.recorded else _("{} recorded")
|
||||
form = RecordModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=msg_succ.format("EMA"),
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def state_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = NewCompensationStateModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_ADDED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def action_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new actions for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = NewCompensationActionModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_ADDED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def action_edit_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for editing an actions for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
action_id (str): The action id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = EditCompensationActionModalForm(request.POST or None, instance=ema, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_EDITED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def deadline_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = NewDeadlineModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_ADDED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def document_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for uploading new documents
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id to which the new document will be related
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = NewEmaDocumentModalForm(request.POST or None, request.FILES or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DOCUMENT_ADDED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def get_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Returns the document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
doc = get_object_or_404(EmaDocument, id=doc_id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
doc = get_object_or_404(EmaDocument, id=doc_id)
|
||||
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=ema, document=doc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
DOCUMENT_EDITED,
|
||||
reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def remove_document_view(request: HttpRequest, id:str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
doc = get_object_or_404(EmaDocument, id=doc_id)
|
||||
return remove_document(
|
||||
request,
|
||||
doc
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def state_remove_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for removing an EMA state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The ema id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = RemoveCompensationStateModalForm(request.POST or None, instance=ema, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_REMOVED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def state_edit_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for editing an EMA state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The ema id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = EditCompensationStateModalForm(request.POST or None, instance=ema, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_EDITED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def action_remove_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for removing an EMA action
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The ema id
|
||||
id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = RemoveCompensationActionModalForm(request.POST or None, instance=ema, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_REMOVED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
def report_view(request:HttpRequest, id: str):
|
||||
""" Renders the public report view
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The id of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Reuse the compensation report template since EMAs are structurally identical
|
||||
template = "ema/report/report.html"
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
|
||||
tab_title = _("Report {}").format(ema.identifier)
|
||||
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
||||
if not ema.is_ready_for_publish():
|
||||
template = "report/unavailable.html"
|
||||
context = {
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
# Prepare data for map viewer
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=ema,
|
||||
)
|
||||
parcels = ema.get_underlying_parcels()
|
||||
|
||||
qrcode_url = request.build_absolute_uri(reverse("ema:report", args=(id,)))
|
||||
qrcode_img = generate_qr_code(qrcode_url, 10)
|
||||
qrcode_lanis_url = ema.get_LANIS_link()
|
||||
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
||||
|
||||
# Order states by surface
|
||||
before_states = ema.before_states.all().order_by("-surface").prefetch_related("biotope_type")
|
||||
after_states = ema.after_states.all().order_by("-surface").prefetch_related("biotope_type")
|
||||
actions = ema.actions.all().prefetch_related("action_type")
|
||||
|
||||
context = {
|
||||
"obj": ema,
|
||||
"qrcode": {
|
||||
"img": qrcode_img,
|
||||
"url": qrcode_url
|
||||
},
|
||||
"qrcode_lanis": {
|
||||
"img": qrcode_img_lanis,
|
||||
"url": qrcode_lanis_url
|
||||
},
|
||||
"has_access": False, # disables action buttons during rendering
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"actions": actions,
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
def share_view(request: HttpRequest, id: str, token: str):
|
||||
""" Performs sharing of an ema
|
||||
|
||||
If token given in url is not valid, the user will be redirected to the dashboard
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EMA's id
|
||||
token (str): Access token for EMA
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
user = request.user
|
||||
obj = get_object_or_404(Ema, id=id)
|
||||
# Check tokens
|
||||
if obj.access_token == token:
|
||||
# Send different messages in case user has already been added to list of sharing users
|
||||
if obj.is_shared_with(user):
|
||||
messages.info(
|
||||
request,
|
||||
_("{} has already been shared with you").format(obj.identifier)
|
||||
)
|
||||
else:
|
||||
messages.success(
|
||||
request,
|
||||
_("{} has been shared with you").format(obj.identifier)
|
||||
)
|
||||
obj.share_with_user(user)
|
||||
return redirect("ema:detail", id=id)
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_("Share link invalid"),
|
||||
extra_tags="danger",
|
||||
)
|
||||
return redirect("home")
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def create_share_view(request: HttpRequest, id: str):
|
||||
""" Renders sharing form for an Ema
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Ema's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
obj = get_object_or_404(Ema, id=id)
|
||||
form = ShareModalForm(request.POST or None, instance=obj, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Share settings updated")
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def deadline_edit_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for editing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = EditDeadlineModalForm(request.POST or None, instance=ema, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_EDITED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def deadline_remove_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for removing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = RemoveDeadlineModalForm(request.POST or None, instance=ema, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_REMOVED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def create_resubmission_view(request: HttpRequest, id: str):
|
||||
""" Renders resubmission form for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = ResubmissionModalForm(request.POST or None, instance=ema, request=request)
|
||||
form.action_url = reverse("ema:resubmission-create", args=(id,))
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Resubmission set"),
|
||||
redirect_url=reverse("ema:detail", args=(id,))
|
||||
)
|
7
ema/views/__init__.py
Normal file
7
ema/views/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
89
ema/views/action.py
Normal file
89
ema/views/action.py
Normal file
@ -0,0 +1,89 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.compensation_action import RemoveCompensationActionModalForm, \
|
||||
EditCompensationActionModalForm, NewCompensationActionModalForm
|
||||
from compensation.models import CompensationAction
|
||||
from ema.models import Ema
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
from konova.utils.message_templates import COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_EDITED, \
|
||||
COMPENSATION_ACTION_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def action_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new actions for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = NewCompensationActionModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_ADDED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def action_edit_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for editing an actions for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
action_id (str): The action id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = EditCompensationActionModalForm(request.POST or None, instance=ema, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_EDITED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def action_remove_view(request: HttpRequest, id: str, action_id: str):
|
||||
""" Renders a form for removing an EMA action
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The ema id
|
||||
id (str): The action's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
action = get_object_or_404(CompensationAction, id=action_id)
|
||||
form = RemoveCompensationActionModalForm(request.POST or None, instance=ema, action=action, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_ACTION_REMOVED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
90
ema/views/deadline.py
Normal file
90
ema/views/deadline.py
Normal file
@ -0,0 +1,90 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.deadline import EditDeadlineModalForm, NewDeadlineModalForm
|
||||
from ema.models import Ema
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
from konova.forms.modals import RemoveDeadlineModalForm
|
||||
from konova.models import Deadline
|
||||
from konova.utils.message_templates import DEADLINE_REMOVED, DEADLINE_EDITED, DEADLINE_ADDED
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def deadline_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = NewDeadlineModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_ADDED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def deadline_edit_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for editing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = EditDeadlineModalForm(request.POST or None, instance=ema, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_EDITED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def deadline_remove_view(request: HttpRequest, id: str, deadline_id: str):
|
||||
""" Renders a form for removing deadlines from a compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
deadline_id (str): The deadline's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
deadline = get_object_or_404(Deadline, id=deadline_id)
|
||||
form = RemoveDeadlineModalForm(request.POST or None, instance=ema, deadline=deadline, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEADLINE_REMOVED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
111
ema/views/document.py
Normal file
111
ema/views/document.py
Normal file
@ -0,0 +1,111 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from ema.forms import NewEmaDocumentModalForm
|
||||
from ema.models import Ema, EmaDocument
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
from konova.forms.modals import EditDocumentModalForm
|
||||
from konova.utils.documents import get_document, remove_document
|
||||
from konova.utils.message_templates import DOCUMENT_ADDED, DOCUMENT_EDITED
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def document_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for uploading new documents
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id to which the new document will be related
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = NewEmaDocumentModalForm(request.POST or None, request.FILES or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DOCUMENT_ADDED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def get_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Returns the document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
doc = get_object_or_404(EmaDocument, id=doc_id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
doc = get_object_or_404(EmaDocument, id=doc_id)
|
||||
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=ema, document=doc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
DOCUMENT_EDITED,
|
||||
reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def remove_document_view(request: HttpRequest, id:str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
doc = get_object_or_404(EmaDocument, id=doc_id)
|
||||
return remove_document(
|
||||
request,
|
||||
doc
|
||||
)
|
||||
|
237
ema/views/ema.py
Normal file
237
ema/views/ema.py
Normal file
@ -0,0 +1,237 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Sum
|
||||
from django.http import HttpRequest, JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from ema.forms import NewEmaForm, EditEmaForm
|
||||
from ema.models import Ema
|
||||
from ema.tables import EmaTable
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.forms.modals import RemoveModalForm
|
||||
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.message_templates import RECORDED_BLOCKS_EDIT, IDENTIFIER_REPLACED, FORM_INVALID
|
||||
from konova.utils.user_checks import in_group
|
||||
|
||||
|
||||
@login_required
|
||||
def index_view(request: HttpRequest):
|
||||
""" Renders the index view for EMAs
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
emas = Ema.objects.filter(
|
||||
deleted=None,
|
||||
).order_by(
|
||||
"-modified"
|
||||
)
|
||||
table = EmaTable(
|
||||
request,
|
||||
queryset=emas
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("EMAs - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
def new_view(request: HttpRequest):
|
||||
"""
|
||||
Renders a view for a new eco account creation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "ema/form/view.html"
|
||||
data_form = NewEmaForm(request.POST or None)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
ema = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != ema.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
IDENTIFIER_REPLACED.format(
|
||||
generated_identifier,
|
||||
ema.identifier
|
||||
)
|
||||
)
|
||||
messages.success(request, _("EMA {} added").format(ema.identifier))
|
||||
return redirect("ema:detail", id=ema.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("New EMA"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
def new_id_view(request: HttpRequest):
|
||||
""" JSON endpoint
|
||||
|
||||
Provides fetching of free identifiers for e.g. AJAX calls
|
||||
|
||||
"""
|
||||
tmp = Ema()
|
||||
identifier = tmp.generate_new_identifier()
|
||||
while Ema.objects.filter(identifier=identifier).exists():
|
||||
identifier = tmp.generate_new_identifier()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def detail_view(request: HttpRequest, id: str):
|
||||
""" Renders the detail view of an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "ema/detail/view.html"
|
||||
ema = get_object_or_404(Ema, id=id, deleted=None)
|
||||
|
||||
geom_form = SimpleGeomForm(instance=ema)
|
||||
parcels = ema.get_underlying_parcels()
|
||||
_user = request.user
|
||||
is_data_shared = ema.is_shared_with(_user)
|
||||
|
||||
# Order states according to surface
|
||||
before_states = ema.before_states.all().order_by("-surface")
|
||||
after_states = ema.after_states.all().order_by("-surface")
|
||||
|
||||
# Precalculate logical errors between before- and after-states
|
||||
# Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
|
||||
sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
diff_states = abs(sum_before_states - sum_after_states)
|
||||
|
||||
ema.set_status_messages(request)
|
||||
|
||||
context = {
|
||||
"obj": ema,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"has_access": is_data_shared,
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"sum_before_states": sum_before_states,
|
||||
"sum_after_states": sum_after_states,
|
||||
"diff_states": diff_states,
|
||||
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
||||
"is_zb_member": in_group(_user, ZB_GROUP),
|
||||
"is_ets_member": in_group(_user, ETS_GROUP),
|
||||
"LANIS_LINK": ema.get_LANIS_link(),
|
||||
TAB_TITLE_IDENTIFIER: f"{ema.identifier} - {ema.title}",
|
||||
"has_finished_deadlines": ema.get_finished_deadlines().exists(),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def edit_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
Renders a view for editing compensations
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "compensation/form/view.html"
|
||||
# Get object from db
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
if ema.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("ema:detail", id=id)
|
||||
|
||||
# Create forms, initialize with values from db/from POST request
|
||||
data_form = EditEmaForm(request.POST or None, instance=ema)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=ema)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
# The data form takes the geom form for processing, as well as the performing user
|
||||
ema = data_form.save(request.user, geom_form)
|
||||
messages.success(request, _("EMA {} edited").format(ema.identifier))
|
||||
return redirect("ema:detail", id=ema.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("Edit {}").format(ema.identifier),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal view for removing the EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = RemoveModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=_("EMA removed"),
|
||||
redirect_url=reverse("ema:index"),
|
||||
)
|
||||
|
41
ema/views/log.py
Normal file
41
ema/views/log.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from ema.models import Ema
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": ema.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
37
ema/views/record.py
Normal file
37
ema/views/record.py
Normal file
@ -0,0 +1,37 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from ema.models import Ema
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
from konova.forms.modals import RecordModalForm
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def record_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal view for recording the EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
msg_succ = _("{} unrecorded") if ema.recorded else _("{} recorded")
|
||||
form = RecordModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=msg_succ.format("EMA"),
|
||||
)
|
79
ema/views/report.py
Normal file
79
ema/views/report.py
Normal file
@ -0,0 +1,79 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from ema.models import Ema
|
||||
from konova.contexts import BaseContext
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.generators import generate_qr_code
|
||||
|
||||
|
||||
def report_view(request:HttpRequest, id: str):
|
||||
""" Renders the public report view
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The id of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Reuse the compensation report template since EMAs are structurally identical
|
||||
template = "ema/report/report.html"
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
|
||||
tab_title = _("Report {}").format(ema.identifier)
|
||||
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
||||
if not ema.is_ready_for_publish():
|
||||
template = "report/unavailable.html"
|
||||
context = {
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
# Prepare data for map viewer
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=ema,
|
||||
)
|
||||
parcels = ema.get_underlying_parcels()
|
||||
|
||||
qrcode_url = request.build_absolute_uri(reverse("ema:report", args=(id,)))
|
||||
qrcode_img = generate_qr_code(qrcode_url, 10)
|
||||
qrcode_lanis_url = ema.get_LANIS_link()
|
||||
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
||||
|
||||
# Order states by surface
|
||||
before_states = ema.before_states.all().order_by("-surface").prefetch_related("biotope_type")
|
||||
after_states = ema.after_states.all().order_by("-surface").prefetch_related("biotope_type")
|
||||
actions = ema.actions.all().prefetch_related("action_type")
|
||||
|
||||
context = {
|
||||
"obj": ema,
|
||||
"qrcode": {
|
||||
"img": qrcode_img,
|
||||
"url": qrcode_url
|
||||
},
|
||||
"qrcode_lanis": {
|
||||
"img": qrcode_img_lanis,
|
||||
"url": qrcode_lanis_url
|
||||
},
|
||||
"has_access": False, # disables action buttons during rendering
|
||||
"before_states": before_states,
|
||||
"after_states": after_states,
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
"actions": actions,
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
39
ema/views/resubmission.py
Normal file
39
ema/views/resubmission.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from ema.models import Ema
|
||||
from konova.decorators import shared_access_required, conservation_office_group_required
|
||||
from konova.forms.modals import ResubmissionModalForm
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def create_resubmission_view(request: HttpRequest, id: str):
|
||||
""" Renders resubmission form for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EMA's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = ResubmissionModalForm(request.POST or None, instance=ema, request=request)
|
||||
form.action_url = reverse("ema:resubmission-create", args=(id,))
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Resubmission set"),
|
||||
redirect_url=reverse("ema:detail", args=(id,))
|
||||
)
|
77
ema/views/share.py
Normal file
77
ema/views/share.py
Normal file
@ -0,0 +1,77 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from ema.models import Ema
|
||||
from intervention.forms.modals.share import ShareModalForm
|
||||
from konova.decorators import conservation_office_group_required, shared_access_required
|
||||
|
||||
|
||||
@login_required
|
||||
def share_view(request: HttpRequest, id: str, token: str):
|
||||
""" Performs sharing of an ema
|
||||
|
||||
If token given in url is not valid, the user will be redirected to the dashboard
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): EMA's id
|
||||
token (str): Access token for EMA
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
user = request.user
|
||||
obj = get_object_or_404(Ema, id=id)
|
||||
# Check tokens
|
||||
if obj.access_token == token:
|
||||
# Send different messages in case user has already been added to list of sharing users
|
||||
if obj.is_shared_with(user):
|
||||
messages.info(
|
||||
request,
|
||||
_("{} has already been shared with you").format(obj.identifier)
|
||||
)
|
||||
else:
|
||||
messages.success(
|
||||
request,
|
||||
_("{} has been shared with you").format(obj.identifier)
|
||||
)
|
||||
obj.share_with_user(user)
|
||||
return redirect("ema:detail", id=id)
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_("Share link invalid"),
|
||||
extra_tags="danger",
|
||||
)
|
||||
return redirect("home")
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def create_share_view(request: HttpRequest, id: str):
|
||||
""" Renders sharing form for an Ema
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Ema's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
obj = get_object_or_404(Ema, id=id)
|
||||
form = ShareModalForm(request.POST or None, instance=obj, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Share settings updated")
|
||||
)
|
90
ema/views/state.py
Normal file
90
ema/views/state.py
Normal file
@ -0,0 +1,90 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.forms.modals.state import NewCompensationStateModalForm, RemoveCompensationStateModalForm, \
|
||||
EditCompensationStateModalForm
|
||||
from compensation.models import CompensationState
|
||||
from ema.models import Ema
|
||||
from konova.decorators import conservation_office_group_required, shared_access_required
|
||||
from konova.utils.message_templates import COMPENSATION_STATE_ADDED, COMPENSATION_STATE_REMOVED, \
|
||||
COMPENSATION_STATE_EDITED
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def state_new_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for adding new states for an EMA
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The EMA's id to which the new state will be related
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
form = NewCompensationStateModalForm(request.POST or None, instance=ema, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_ADDED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def state_remove_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for removing an EMA state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The ema id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = RemoveCompensationStateModalForm(request.POST or None, instance=ema, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_REMOVED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Ema, "id")
|
||||
def state_edit_view(request: HttpRequest, id: str, state_id: str):
|
||||
""" Renders a form for editing an EMA state
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The ema id
|
||||
state_id (str): The state's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
ema = get_object_or_404(Ema, id=id)
|
||||
state = get_object_or_404(CompensationState, id=state_id)
|
||||
form = EditCompensationStateModalForm(request.POST or None, instance=ema, state=state, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=COMPENSATION_STATE_EDITED,
|
||||
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
@ -8,11 +8,18 @@ Created on: 30.11.20
|
||||
from django.urls import path
|
||||
|
||||
from intervention.autocomplete.intervention import InterventionAutocomplete
|
||||
from intervention.views import index_view, new_view, detail_view, edit_view, remove_view, new_document_view, share_view, \
|
||||
create_share_view, remove_revocation_view, new_revocation_view, check_view, log_view, new_deduction_view, \
|
||||
record_view, remove_document_view, get_document_view, get_revocation_view, new_id_view, report_view, \
|
||||
remove_deduction_view, remove_compensation_view, edit_deduction_view, edit_revocation_view, edit_document_view, \
|
||||
create_resubmission_view
|
||||
from intervention.views.check import check_view
|
||||
from intervention.views.compensation import remove_compensation_view
|
||||
from intervention.views.deduction import new_deduction_view, edit_deduction_view, remove_deduction_view
|
||||
from intervention.views.document import new_document_view, get_document_view, remove_document_view, edit_document_view
|
||||
from intervention.views.intervention import index_view, new_view, new_id_view, detail_view, edit_view, remove_view
|
||||
from intervention.views.log import log_view
|
||||
from intervention.views.record import record_view
|
||||
from intervention.views.report import report_view
|
||||
from intervention.views.resubmission import create_resubmission_view
|
||||
from intervention.views.revocation import new_revocation_view, edit_revocation_view, remove_revocation_view, \
|
||||
get_revocation_view
|
||||
from intervention.views.share import share_view, create_share_view
|
||||
|
||||
app_name = "intervention"
|
||||
urlpatterns = [
|
||||
|
@ -1,756 +0,0 @@
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.http import HttpRequest, JsonResponse, Http404
|
||||
from django.shortcuts import render
|
||||
|
||||
from intervention.forms.intervention import NewInterventionForm, EditInterventionForm
|
||||
from intervention.forms.modals.check import CheckModalForm
|
||||
from intervention.forms.modals.deduction import NewEcoAccountDeductionModalForm, RemoveEcoAccountDeductionModalForm, \
|
||||
EditEcoAccountDeductionModalForm
|
||||
from intervention.forms.modals.document import NewInterventionDocumentModalForm
|
||||
from intervention.forms.modals.revocation import EditRevocationModalForm, RemoveRevocationModalForm, \
|
||||
NewRevocationModalForm
|
||||
from intervention.forms.modals.share import ShareModalForm
|
||||
from intervention.models import Intervention, Revocation, InterventionDocument, RevocationDocument
|
||||
from intervention.tables import InterventionTable
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import *
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.forms.modals import RemoveModalForm, RecordModalForm, EditDocumentModalForm, ResubmissionModalForm
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.documents import remove_document, get_document
|
||||
from konova.utils.generators import generate_qr_code
|
||||
from konova.utils.message_templates import INTERVENTION_INVALID, FORM_INVALID, IDENTIFIER_REPLACED, \
|
||||
CHECKED_RECORDED_RESET, DEDUCTION_REMOVED, DEDUCTION_ADDED, REVOCATION_ADDED, REVOCATION_REMOVED, \
|
||||
COMPENSATION_REMOVED_TEMPLATE, DOCUMENT_ADDED, DEDUCTION_EDITED, REVOCATION_EDITED, DOCUMENT_EDITED, \
|
||||
RECORDED_BLOCKS_EDIT, DATA_CHECKED_PREVIOUSLY_TEMPLATE
|
||||
from konova.utils.user_checks import in_group
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def index_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the index view for Interventions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
A rendered view
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
|
||||
# Filtering by user access is performed in table filter inside of InterventionTableFilter class
|
||||
interventions = Intervention.objects.filter(
|
||||
deleted=None, # not deleted
|
||||
).select_related(
|
||||
"legal"
|
||||
)
|
||||
table = InterventionTable(
|
||||
request=request,
|
||||
queryset=interventions
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("Interventions - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_view(request: HttpRequest):
|
||||
"""
|
||||
Renders a view for a new intervention creation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "intervention/form/view.html"
|
||||
data_form = NewInterventionForm(request.POST or None)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
intervention = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != intervention.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
IDENTIFIER_REPLACED.format(
|
||||
generated_identifier,
|
||||
intervention.identifier
|
||||
)
|
||||
)
|
||||
messages.success(request, _("Intervention {} added").format(intervention.identifier))
|
||||
return redirect("intervention:detail", id=intervention.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("New intervention"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_id_view(request: HttpRequest):
|
||||
""" JSON endpoint
|
||||
|
||||
Provides fetching of free identifiers for e.g. AJAX calls
|
||||
|
||||
"""
|
||||
tmp_intervention = Intervention()
|
||||
identifier = tmp_intervention.generate_new_identifier()
|
||||
while Intervention.objects.filter(identifier=identifier).exists():
|
||||
identifier = tmp_intervention.generate_new_identifier()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def new_document_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for uploading new documents
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id to which the new document will be related
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = NewInterventionDocumentModalForm(request.POST or None, request.FILES or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DOCUMENT_ADDED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def get_revocation_view(request: HttpRequest, doc_id: str):
|
||||
""" Returns the revocation document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
doc = get_object_or_404(RevocationDocument, id=doc_id)
|
||||
# File download only possible if related instance is shared with user
|
||||
if not doc.instance.legal.intervention.users.filter(id=request.user.id):
|
||||
messages.info(
|
||||
request,
|
||||
DATA_UNSHARED
|
||||
)
|
||||
return redirect("intervention:detail", id=doc.instance.id)
|
||||
return get_document(doc)
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def get_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Returns the document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
||||
return remove_document(
|
||||
request,
|
||||
doc
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
||||
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=intervention, document=doc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
DOCUMENT_EDITED,
|
||||
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def detail_view(request: HttpRequest, id: str):
|
||||
""" Renders a detail view for viewing an intervention's data
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "intervention/detail/view.html"
|
||||
|
||||
# Fetch data, filter out deleted related data
|
||||
intervention = get_object_or_404(
|
||||
Intervention.objects.select_related(
|
||||
"geometry",
|
||||
"legal",
|
||||
"responsible",
|
||||
),
|
||||
id=id
|
||||
)
|
||||
compensations = intervention.compensations.filter(
|
||||
deleted=None,
|
||||
)
|
||||
_user = request.user
|
||||
is_data_shared = intervention.is_shared_with(user=_user)
|
||||
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=intervention,
|
||||
)
|
||||
last_checked = intervention.get_last_checked_action()
|
||||
last_checked_tooltip = ""
|
||||
if last_checked:
|
||||
last_checked_tooltip = DATA_CHECKED_PREVIOUSLY_TEMPLATE.format(last_checked.get_timestamp_str_formatted(), last_checked.user)
|
||||
|
||||
context = {
|
||||
"obj": intervention,
|
||||
"last_checked": last_checked,
|
||||
"last_checked_tooltip": last_checked_tooltip,
|
||||
"compensations": compensations,
|
||||
"has_access": is_data_shared,
|
||||
"geom_form": geom_form,
|
||||
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
||||
"is_zb_member": in_group(_user, ZB_GROUP),
|
||||
"is_ets_member": in_group(_user, ETS_GROUP),
|
||||
"LANIS_LINK": intervention.get_LANIS_link(),
|
||||
TAB_TITLE_IDENTIFIER: f"{intervention.identifier} - {intervention.title}",
|
||||
}
|
||||
|
||||
request = intervention.set_status_messages(request)
|
||||
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def edit_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
Renders a view for editing interventions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "intervention/form/view.html"
|
||||
# Get object from db
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
if intervention.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("intervention:detail", id=id)
|
||||
|
||||
# Create forms, initialize with values from db/from POST request
|
||||
data_form = EditInterventionForm(request.POST or None, instance=intervention)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=intervention)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
# The data form takes the geom form for processing, as well as the performing user
|
||||
# Save the current state of recorded|checked to inform the user in case of a status reset due to editing
|
||||
i_rec = intervention.recorded is not None
|
||||
i_check = intervention.checked is not None
|
||||
intervention = data_form.save(request.user, geom_form)
|
||||
messages.success(request, _("Intervention {} edited").format(intervention.identifier))
|
||||
if i_check or i_rec:
|
||||
messages.info(request, CHECKED_RECORDED_RESET)
|
||||
return redirect("intervention:detail", id=intervention.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("Edit {}").format(intervention.identifier),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
""" Renders a remove view for this intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The uuid id as string
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
obj = Intervention.objects.get(id=id)
|
||||
identifier = obj.identifier
|
||||
form = RemoveModalForm(request.POST or None, instance=obj, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
_("{} removed").format(identifier),
|
||||
redirect_url=reverse("intervention:index")
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def edit_revocation_view(request: HttpRequest, id: str, revocation_id: str):
|
||||
""" Renders a edit view for a revocation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id as string
|
||||
revocation_id (str): The revocation's id as string
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
revocation = get_object_or_404(Revocation, id=revocation_id)
|
||||
|
||||
form = EditRevocationModalForm(request.POST or None, request.FILES or None, instance=intervention, revocation=revocation, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
REVOCATION_EDITED,
|
||||
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_revocation_view(request: HttpRequest, id: str, revocation_id: str):
|
||||
""" Renders a remove view for a revocation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id as string
|
||||
revocation_id (str): The revocation's id as string
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
revocation = get_object_or_404(Revocation, id=revocation_id)
|
||||
|
||||
form = RemoveRevocationModalForm(request.POST or None, instance=intervention, revocation=revocation, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
REVOCATION_REMOVED,
|
||||
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def share_view(request: HttpRequest, id: str, token: str):
|
||||
""" Performs sharing of an intervention
|
||||
|
||||
If token given in url is not valid, the user will be redirected to the dashboard
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
token (str): Access token for intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
user = request.user
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
# Check tokens
|
||||
if intervention.access_token == token:
|
||||
# Send different messages in case user has already been added to list of sharing users
|
||||
if intervention.is_shared_with(user):
|
||||
messages.info(
|
||||
request,
|
||||
_("{} has already been shared with you").format(intervention.identifier)
|
||||
)
|
||||
else:
|
||||
messages.success(
|
||||
request,
|
||||
_("{} has been shared with you").format(intervention.identifier)
|
||||
)
|
||||
intervention.share_with_user(user)
|
||||
return redirect("intervention:detail", id=id)
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_("Share link invalid"),
|
||||
extra_tags="danger",
|
||||
)
|
||||
return redirect("home")
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def create_share_view(request: HttpRequest, id: str):
|
||||
""" Renders sharing form for an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = ShareModalForm(request.POST or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Share settings updated")
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def create_resubmission_view(request: HttpRequest, id: str):
|
||||
""" Renders resubmission form for an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = ResubmissionModalForm(request.POST or None, instance=intervention, request=request)
|
||||
form.action_url = reverse("intervention:resubmission-create", args=(id,))
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Resubmission set"),
|
||||
redirect_url=reverse("intervention:detail", args=(id,))
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@registration_office_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def check_view(request: HttpRequest, id: str):
|
||||
""" Renders check form for an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = CheckModalForm(request.POST or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Check performed"),
|
||||
msg_error=INTERVENTION_INVALID
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def new_revocation_view(request: HttpRequest, id: str):
|
||||
""" Renders sharing form for an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = NewRevocationModalForm(request.POST or None, request.FILES or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=REVOCATION_ADDED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": intervention.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def new_deduction_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal form view for creating deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id which shall benefit from this deduction
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = NewEcoAccountDeductionModalForm(request.POST or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEDUCTION_ADDED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data",
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_deduction_view(request: HttpRequest, id: str, deduction_id: str):
|
||||
""" Renders a modal view for removing deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
deduction_id (str): The deduction's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
try:
|
||||
eco_deduction = intervention.deductions.get(id=deduction_id)
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown deduction")
|
||||
|
||||
form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DEDUCTION_REMOVED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def edit_deduction_view(request: HttpRequest, id: str, deduction_id: str):
|
||||
""" Renders a modal view for removing deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
deduction_id (str): The deduction's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
try:
|
||||
eco_deduction = intervention.deductions.get(id=deduction_id)
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown deduction")
|
||||
|
||||
form = EditEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DEDUCTION_EDITED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def record_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal form for recording an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = RecordModalForm(request.POST or None, instance=intervention, request=request)
|
||||
msg_succ = _("{} unrecorded") if intervention.recorded else _("{} recorded")
|
||||
msg_succ = msg_succ.format(intervention.identifier)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_succ,
|
||||
msg_error=_("There are errors on this intervention:")
|
||||
)
|
||||
|
||||
|
||||
def remove_compensation_view(request:HttpRequest, id: str, comp_id: str):
|
||||
""" Renders a modal view for removing the compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
try:
|
||||
comp = intervention.compensations.get(
|
||||
id=comp_id
|
||||
)
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown compensation")
|
||||
form = RemoveModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=COMPENSATION_REMOVED_TEMPLATE.format(comp.identifier),
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data",
|
||||
)
|
||||
|
||||
|
||||
def report_view(request:HttpRequest, id: str):
|
||||
""" Renders the public report view
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The id of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "intervention/report/report.html"
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
|
||||
tab_title = _("Report {}").format(intervention.identifier)
|
||||
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
||||
if not intervention.is_ready_for_publish():
|
||||
template = "report/unavailable.html"
|
||||
context = {
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
# Prepare data for map viewer
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=intervention
|
||||
)
|
||||
parcels = intervention.get_underlying_parcels()
|
||||
|
||||
distinct_deductions = intervention.deductions.all().distinct(
|
||||
"account"
|
||||
)
|
||||
qrcode_url = request.build_absolute_uri(reverse("intervention:report", args=(id,)))
|
||||
qrcode_img = generate_qr_code(qrcode_url, 10)
|
||||
qrcode_lanis_url = intervention.get_LANIS_link()
|
||||
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
||||
|
||||
context = {
|
||||
"obj": intervention,
|
||||
"deductions": distinct_deductions,
|
||||
"qrcode": {
|
||||
"img": qrcode_img,
|
||||
"url": qrcode_url,
|
||||
},
|
||||
"qrcode_lanis": {
|
||||
"img": qrcode_img_lanis,
|
||||
"url": qrcode_lanis_url,
|
||||
},
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
7
intervention/views/__init__.py
Normal file
7
intervention/views/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
39
intervention/views/check.py
Normal file
39
intervention/views/check.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.forms.modals.check import CheckModalForm
|
||||
from intervention.models import Intervention
|
||||
from konova.decorators import registration_office_group_required, shared_access_required
|
||||
from konova.utils.message_templates import INTERVENTION_INVALID
|
||||
|
||||
|
||||
@login_required
|
||||
@registration_office_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def check_view(request: HttpRequest, id: str):
|
||||
""" Renders check form for an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = CheckModalForm(request.POST or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Check performed"),
|
||||
msg_error=INTERVENTION_INVALID
|
||||
)
|
||||
|
45
intervention/views/compensation.py
Normal file
45
intervention/views/compensation.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import HttpRequest, Http404
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from intervention.models import Intervention
|
||||
from konova.decorators import shared_access_required
|
||||
from konova.forms.modals import RemoveModalForm
|
||||
from konova.utils.message_templates import COMPENSATION_REMOVED_TEMPLATE
|
||||
|
||||
|
||||
@login_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_compensation_view(request: HttpRequest, id: str, comp_id: str):
|
||||
""" Renders a modal view for removing the compensation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
try:
|
||||
comp = intervention.compensations.get(
|
||||
id=comp_id
|
||||
)
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown compensation")
|
||||
form = RemoveModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=COMPENSATION_REMOVED_TEMPLATE.format(comp.identifier),
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data",
|
||||
)
|
||||
|
97
intervention/views/deduction.py
Normal file
97
intervention/views/deduction.py
Normal file
@ -0,0 +1,97 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import HttpRequest, Http404
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from intervention.forms.modals.deduction import NewEcoAccountDeductionModalForm, RemoveEcoAccountDeductionModalForm, \
|
||||
EditEcoAccountDeductionModalForm
|
||||
from intervention.models import Intervention
|
||||
from konova.decorators import default_group_required, shared_access_required
|
||||
from konova.utils.message_templates import DEDUCTION_ADDED, DEDUCTION_REMOVED, DEDUCTION_EDITED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def new_deduction_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal form view for creating deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id which shall benefit from this deduction
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = NewEcoAccountDeductionModalForm(request.POST or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DEDUCTION_ADDED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data",
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_deduction_view(request: HttpRequest, id: str, deduction_id: str):
|
||||
""" Renders a modal view for removing deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
deduction_id (str): The deduction's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
try:
|
||||
eco_deduction = intervention.deductions.get(id=deduction_id)
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown deduction")
|
||||
|
||||
form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DEDUCTION_REMOVED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def edit_deduction_view(request: HttpRequest, id: str, deduction_id: str):
|
||||
""" Renders a modal view for removing deductions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
deduction_id (str): The deduction's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
try:
|
||||
eco_deduction = intervention.deductions.get(id=deduction_id)
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown deduction")
|
||||
|
||||
form = EditEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=DEDUCTION_EDITED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
112
intervention/views/document.py
Normal file
112
intervention/views/document.py
Normal file
@ -0,0 +1,112 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.forms.modals.document import NewInterventionDocumentModalForm
|
||||
from intervention.models import Intervention, InterventionDocument
|
||||
from konova.decorators import default_group_required, shared_access_required
|
||||
from konova.forms.modals import EditDocumentModalForm
|
||||
from konova.utils.documents import get_document, remove_document
|
||||
from konova.utils.message_templates import DOCUMENT_ADDED, DOCUMENT_EDITED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def new_document_view(request: HttpRequest, id: str):
|
||||
""" Renders a form for uploading new documents
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id to which the new document will be related
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = NewInterventionDocumentModalForm(request.POST or None, request.FILES or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=DOCUMENT_ADDED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def get_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Returns the document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
||||
return remove_document(
|
||||
request,
|
||||
doc
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
|
||||
""" Removes the document from the database and file system
|
||||
|
||||
Wraps the generic functionality from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention id
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
doc = get_object_or_404(InterventionDocument, id=doc_id)
|
||||
form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=intervention, document=doc, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
DOCUMENT_EDITED,
|
||||
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
||||
)
|
||||
|
251
intervention/views/intervention.py
Normal file
251
intervention/views/intervention.py
Normal file
@ -0,0 +1,251 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import JsonResponse, HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render, redirect
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.forms.intervention import EditInterventionForm, NewInterventionForm
|
||||
from intervention.models import Intervention
|
||||
from intervention.tables import InterventionTable
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import default_group_required, shared_access_required, any_group_check
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.forms.modals import RemoveModalForm
|
||||
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.message_templates import DATA_CHECKED_PREVIOUSLY_TEMPLATE, RECORDED_BLOCKS_EDIT, \
|
||||
CHECKED_RECORDED_RESET, FORM_INVALID, IDENTIFIER_REPLACED
|
||||
from konova.utils.user_checks import in_group
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def index_view(request: HttpRequest):
|
||||
"""
|
||||
Renders the index view for Interventions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
A rendered view
|
||||
"""
|
||||
template = "generic_index.html"
|
||||
|
||||
# Filtering by user access is performed in table filter inside of InterventionTableFilter class
|
||||
interventions = Intervention.objects.filter(
|
||||
deleted=None, # not deleted
|
||||
).select_related(
|
||||
"legal"
|
||||
)
|
||||
table = InterventionTable(
|
||||
request=request,
|
||||
queryset=interventions
|
||||
)
|
||||
context = {
|
||||
"table": table,
|
||||
TAB_TITLE_IDENTIFIER: _("Interventions - Overview"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_view(request: HttpRequest):
|
||||
"""
|
||||
Renders a view for a new intervention creation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "intervention/form/view.html"
|
||||
data_form = NewInterventionForm(request.POST or None)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
intervention = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != intervention.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
IDENTIFIER_REPLACED.format(
|
||||
generated_identifier,
|
||||
intervention.identifier
|
||||
)
|
||||
)
|
||||
messages.success(request, _("Intervention {} added").format(intervention.identifier))
|
||||
return redirect("intervention:detail", id=intervention.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("New intervention"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def new_id_view(request: HttpRequest):
|
||||
""" JSON endpoint
|
||||
|
||||
Provides fetching of free identifiers for e.g. AJAX calls
|
||||
|
||||
"""
|
||||
tmp_intervention = Intervention()
|
||||
identifier = tmp_intervention.generate_new_identifier()
|
||||
while Intervention.objects.filter(identifier=identifier).exists():
|
||||
identifier = tmp_intervention.generate_new_identifier()
|
||||
return JsonResponse(
|
||||
data={
|
||||
"gen_data": identifier
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@any_group_check
|
||||
def detail_view(request: HttpRequest, id: str):
|
||||
""" Renders a detail view for viewing an intervention's data
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "intervention/detail/view.html"
|
||||
|
||||
# Fetch data, filter out deleted related data
|
||||
intervention = get_object_or_404(
|
||||
Intervention.objects.select_related(
|
||||
"geometry",
|
||||
"legal",
|
||||
"responsible",
|
||||
),
|
||||
id=id
|
||||
)
|
||||
compensations = intervention.compensations.filter(
|
||||
deleted=None,
|
||||
)
|
||||
_user = request.user
|
||||
is_data_shared = intervention.is_shared_with(user=_user)
|
||||
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=intervention,
|
||||
)
|
||||
last_checked = intervention.get_last_checked_action()
|
||||
last_checked_tooltip = ""
|
||||
if last_checked:
|
||||
last_checked_tooltip = DATA_CHECKED_PREVIOUSLY_TEMPLATE.format(last_checked.get_timestamp_str_formatted(), last_checked.user)
|
||||
|
||||
context = {
|
||||
"obj": intervention,
|
||||
"last_checked": last_checked,
|
||||
"last_checked_tooltip": last_checked_tooltip,
|
||||
"compensations": compensations,
|
||||
"has_access": is_data_shared,
|
||||
"geom_form": geom_form,
|
||||
"is_default_member": in_group(_user, DEFAULT_GROUP),
|
||||
"is_zb_member": in_group(_user, ZB_GROUP),
|
||||
"is_ets_member": in_group(_user, ETS_GROUP),
|
||||
"LANIS_LINK": intervention.get_LANIS_link(),
|
||||
TAB_TITLE_IDENTIFIER: f"{intervention.identifier} - {intervention.title}",
|
||||
}
|
||||
|
||||
request = intervention.set_status_messages(request)
|
||||
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def edit_view(request: HttpRequest, id: str):
|
||||
"""
|
||||
Renders a view for editing interventions
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "intervention/form/view.html"
|
||||
# Get object from db
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
if intervention.is_recorded:
|
||||
messages.info(
|
||||
request,
|
||||
RECORDED_BLOCKS_EDIT
|
||||
)
|
||||
return redirect("intervention:detail", id=id)
|
||||
|
||||
# Create forms, initialize with values from db/from POST request
|
||||
data_form = EditInterventionForm(request.POST or None, instance=intervention)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=intervention)
|
||||
if request.method == "POST":
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
# The data form takes the geom form for processing, as well as the performing user
|
||||
# Save the current state of recorded|checked to inform the user in case of a status reset due to editing
|
||||
i_rec = intervention.recorded is not None
|
||||
i_check = intervention.checked is not None
|
||||
intervention = data_form.save(request.user, geom_form)
|
||||
messages.success(request, _("Intervention {} edited").format(intervention.identifier))
|
||||
if i_check or i_rec:
|
||||
messages.info(request, CHECKED_RECORDED_RESET)
|
||||
return redirect("intervention:detail", id=intervention.id)
|
||||
else:
|
||||
messages.error(request, FORM_INVALID, extra_tags="danger",)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": data_form,
|
||||
"geom_form": geom_form,
|
||||
TAB_TITLE_IDENTIFIER: _("Edit {}").format(intervention.identifier),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_view(request: HttpRequest, id: str):
|
||||
""" Renders a remove view for this intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The uuid id as string
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
obj = Intervention.objects.get(id=id)
|
||||
identifier = obj.identifier
|
||||
form = RemoveModalForm(request.POST or None, instance=obj, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
_("{} removed").format(identifier),
|
||||
redirect_url=reverse("intervention:index")
|
||||
)
|
41
intervention/views/log.py
Normal file
41
intervention/views/log.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.models import Intervention
|
||||
from konova.contexts import BaseContext
|
||||
from konova.decorators import shared_access_required, default_group_required
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def log_view(request: HttpRequest, id: str):
|
||||
""" Renders a log view using modal
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
template = "modal/modal_generic.html"
|
||||
body_template = "log.html"
|
||||
|
||||
context = {
|
||||
"modal_body_template": body_template,
|
||||
"log": intervention.log.all(),
|
||||
"modal_title": _("Log"),
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
39
intervention/views/record.py
Normal file
39
intervention/views/record.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.models import Intervention
|
||||
from konova.decorators import conservation_office_group_required, shared_access_required
|
||||
from konova.forms.modals import RecordModalForm
|
||||
|
||||
|
||||
@login_required
|
||||
@conservation_office_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def record_view(request: HttpRequest, id: str):
|
||||
""" Renders a modal form for recording an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = RecordModalForm(request.POST or None, instance=intervention, request=request)
|
||||
msg_succ = _("{} unrecorded") if intervention.recorded else _("{} recorded")
|
||||
msg_succ = msg_succ.format(intervention.identifier)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_succ,
|
||||
msg_error=_("There are errors on this intervention:")
|
||||
)
|
73
intervention/views/report.py
Normal file
73
intervention/views/report.py
Normal file
@ -0,0 +1,73 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.models import Intervention
|
||||
from konova.contexts import BaseContext
|
||||
from konova.forms import SimpleGeomForm
|
||||
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
|
||||
from konova.utils.generators import generate_qr_code
|
||||
|
||||
|
||||
def report_view(request:HttpRequest, id: str):
|
||||
""" Renders the public report view
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The id of the intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "intervention/report/report.html"
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
|
||||
tab_title = _("Report {}").format(intervention.identifier)
|
||||
# If intervention is not recorded (yet or currently) we need to render another template without any data
|
||||
if not intervention.is_ready_for_publish():
|
||||
template = "report/unavailable.html"
|
||||
context = {
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
||||
# Prepare data for map viewer
|
||||
geom_form = SimpleGeomForm(
|
||||
instance=intervention
|
||||
)
|
||||
parcels = intervention.get_underlying_parcels()
|
||||
|
||||
distinct_deductions = intervention.deductions.all().distinct(
|
||||
"account"
|
||||
)
|
||||
qrcode_url = request.build_absolute_uri(reverse("intervention:report", args=(id,)))
|
||||
qrcode_img = generate_qr_code(qrcode_url, 10)
|
||||
qrcode_lanis_url = intervention.get_LANIS_link()
|
||||
qrcode_img_lanis = generate_qr_code(qrcode_lanis_url, 7)
|
||||
|
||||
context = {
|
||||
"obj": intervention,
|
||||
"deductions": distinct_deductions,
|
||||
"qrcode": {
|
||||
"img": qrcode_img,
|
||||
"url": qrcode_url,
|
||||
},
|
||||
"qrcode_lanis": {
|
||||
"img": qrcode_img_lanis,
|
||||
"url": qrcode_lanis_url,
|
||||
},
|
||||
"geom_form": geom_form,
|
||||
"parcels": parcels,
|
||||
TAB_TITLE_IDENTIFIER: tab_title,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
39
intervention/views/resubmission.py
Normal file
39
intervention/views/resubmission.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.models import Intervention
|
||||
from konova.decorators import default_group_required, shared_access_required
|
||||
from konova.forms.modals import ResubmissionModalForm
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def create_resubmission_view(request: HttpRequest, id: str):
|
||||
""" Renders resubmission form for an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = ResubmissionModalForm(request.POST or None, instance=intervention, request=request)
|
||||
form.action_url = reverse("intervention:resubmission-create", args=(id,))
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Resubmission set"),
|
||||
redirect_url=reverse("intervention:detail", args=(id,))
|
||||
)
|
117
intervention/views/revocation.py
Normal file
117
intervention/views/revocation.py
Normal file
@ -0,0 +1,117 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.urls import reverse
|
||||
|
||||
from intervention.forms.modals.revocation import NewRevocationModalForm, EditRevocationModalForm, \
|
||||
RemoveRevocationModalForm
|
||||
from intervention.models import Intervention, RevocationDocument, Revocation
|
||||
from konova.decorators import default_group_required, shared_access_required
|
||||
from konova.utils.documents import get_document
|
||||
from konova.utils.message_templates import REVOCATION_ADDED, DATA_UNSHARED, REVOCATION_EDITED, REVOCATION_REMOVED
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def new_revocation_view(request: HttpRequest, id: str):
|
||||
""" Renders sharing form for an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = NewRevocationModalForm(request.POST or None, request.FILES or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=REVOCATION_ADDED,
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
def get_revocation_view(request: HttpRequest, doc_id: str):
|
||||
""" Returns the revocation document as downloadable file
|
||||
|
||||
Wraps the generic document fetcher function from konova.utils.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
doc_id (str): The document id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
doc = get_object_or_404(RevocationDocument, id=doc_id)
|
||||
# File download only possible if related instance is shared with user
|
||||
if not doc.instance.legal.intervention.users.filter(id=request.user.id):
|
||||
messages.info(
|
||||
request,
|
||||
DATA_UNSHARED
|
||||
)
|
||||
return redirect("intervention:detail", id=doc.instance.id)
|
||||
return get_document(doc)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def edit_revocation_view(request: HttpRequest, id: str, revocation_id: str):
|
||||
""" Renders a edit view for a revocation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id as string
|
||||
revocation_id (str): The revocation's id as string
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
revocation = get_object_or_404(Revocation, id=revocation_id)
|
||||
|
||||
form = EditRevocationModalForm(request.POST or None, request.FILES or None, instance=intervention, revocation=revocation, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
REVOCATION_EDITED,
|
||||
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_revocation_view(request: HttpRequest, id: str, revocation_id: str):
|
||||
""" Renders a remove view for a revocation
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The intervention's id as string
|
||||
revocation_id (str): The revocation's id as string
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
revocation = get_object_or_404(Revocation, id=revocation_id)
|
||||
|
||||
form = RemoveRevocationModalForm(request.POST or None, instance=intervention, revocation=revocation, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
REVOCATION_REMOVED,
|
||||
redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
|
||||
)
|
||||
|
78
intervention/views/share.py
Normal file
78
intervention/views/share.py
Normal file
@ -0,0 +1,78 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.forms.modals.share import ShareModalForm
|
||||
from intervention.models import Intervention
|
||||
from konova.decorators import default_group_required, shared_access_required
|
||||
|
||||
|
||||
@login_required
|
||||
def share_view(request: HttpRequest, id: str, token: str):
|
||||
""" Performs sharing of an intervention
|
||||
|
||||
If token given in url is not valid, the user will be redirected to the dashboard
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
token (str): Access token for intervention
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
user = request.user
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
# Check tokens
|
||||
if intervention.access_token == token:
|
||||
# Send different messages in case user has already been added to list of sharing users
|
||||
if intervention.is_shared_with(user):
|
||||
messages.info(
|
||||
request,
|
||||
_("{} has already been shared with you").format(intervention.identifier)
|
||||
)
|
||||
else:
|
||||
messages.success(
|
||||
request,
|
||||
_("{} has been shared with you").format(intervention.identifier)
|
||||
)
|
||||
intervention.share_with_user(user)
|
||||
return redirect("intervention:detail", id=id)
|
||||
else:
|
||||
messages.error(
|
||||
request,
|
||||
_("Share link invalid"),
|
||||
extra_tags="danger",
|
||||
)
|
||||
return redirect("home")
|
||||
|
||||
|
||||
@login_required
|
||||
@default_group_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def create_share_view(request: HttpRequest, id: str):
|
||||
""" Renders sharing form for an intervention
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): Intervention's id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
form = ShareModalForm(request.POST or None, instance=intervention, request=request)
|
||||
return form.process_request(
|
||||
request,
|
||||
msg_success=_("Share settings updated")
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user