0797a6b99f
* refactors base attributes created and deleted into UserActionLogEntry foreign keys * refactors all related queries and process logic * fixes binding_on into binding_date in intervention/detail/view.html * adds basic __str__ for some models *
142 lines
4.0 KiB
Python
142 lines
4.0 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
Created on: 16.11.20
|
|
|
|
"""
|
|
from django.contrib import messages
|
|
from django.contrib.auth import logout
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.http import HttpRequest, FileResponse
|
|
from django.shortcuts import redirect, render, get_object_or_404
|
|
from django.utils import timezone
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from compensation.models import Compensation, EcoAccount
|
|
from intervention.models import Intervention
|
|
from konova.contexts import BaseContext
|
|
from konova.forms import RemoveModalForm
|
|
from konova.models import Document
|
|
from news.models import ServerMessage
|
|
from konova.settings import SSO_SERVER_BASE
|
|
|
|
|
|
def logout_view(request: HttpRequest):
|
|
"""
|
|
Logout route for ending the session manually.
|
|
|
|
Args:
|
|
request (HttpRequest): The used request object
|
|
|
|
Returns:
|
|
A redirect
|
|
"""
|
|
logout(request)
|
|
return redirect(SSO_SERVER_BASE)
|
|
|
|
|
|
@login_required
|
|
def home_view(request: HttpRequest):
|
|
"""
|
|
Renders the landing page
|
|
|
|
Args:
|
|
request (HttpRequest): The used request object
|
|
|
|
Returns:
|
|
A redirect
|
|
"""
|
|
template = "konova/home.html"
|
|
now = timezone.now()
|
|
user = request.user
|
|
|
|
# Fetch the four newest active and published ServerMessages
|
|
msgs = ServerMessage.objects.filter(
|
|
is_active=True,
|
|
publish_on__lte=now,
|
|
unpublish_on__gte=now,
|
|
).order_by(
|
|
"-publish_on"
|
|
)[:4]
|
|
|
|
# First fetch all valid objects (undeleted, only newest versions)
|
|
interventions = Intervention.objects.filter(
|
|
deleted=None,
|
|
next_version=None,
|
|
)
|
|
# Then fetch only user related ones
|
|
user_interventions = interventions.filter(
|
|
users__in=[user]
|
|
)
|
|
|
|
# Repeat for other objects
|
|
comps = Compensation.objects.filter(
|
|
deleted=None,
|
|
next_version=None,
|
|
)
|
|
user_comps = comps.filter(
|
|
intervention__users__in=[user]
|
|
)
|
|
eco_accs = EcoAccount.objects.filter(
|
|
deleted=None,
|
|
next_version=None,
|
|
)
|
|
user_ecco_accs = eco_accs.filter(
|
|
users__in=[user]
|
|
)
|
|
|
|
additional_context = {
|
|
"msgs": msgs,
|
|
"total_intervention_count": interventions.count(),
|
|
"user_intervention_count": user_interventions.count(),
|
|
"total_compensation_count": comps.count(),
|
|
"user_compensation_count": user_comps.count(),
|
|
"total_eco_count": eco_accs.count(),
|
|
"user_eco_count": user_ecco_accs.count(),
|
|
}
|
|
context = BaseContext(request, additional_context).context
|
|
return render(request, template, context)
|
|
|
|
|
|
@login_required
|
|
def get_document_view(request: HttpRequest, id: str):
|
|
""" Returns a document as downloadable attachment
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The document id
|
|
|
|
Returns:
|
|
|
|
"""
|
|
doc = get_object_or_404(Document, id=id)
|
|
return FileResponse(doc.document, as_attachment=True)
|
|
|
|
|
|
@login_required
|
|
def remove_document_view(request: HttpRequest, id: str):
|
|
""" Renders a form for uploading new documents
|
|
|
|
This function works using a modal. We are not using the regular way, the django bootstrap modal forms are
|
|
intended to be used. Instead of View classes we work using the classic way of dealing with forms (see below).
|
|
It is important to mention, that modal forms, which should reload the page afterwards, must provide a
|
|
'reload_page' bool in the context. This way, the modal may reload the page or not.
|
|
|
|
For further details see the comments in templates/modal or
|
|
https://github.com/trco/django-bootstrap-modal-forms
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
|
|
Returns:
|
|
|
|
"""
|
|
doc = get_object_or_404(Document, id=id)
|
|
title = doc.title
|
|
form = RemoveModalForm(request.POST or None, instance=doc, user=request.user)
|
|
return form.process_request(
|
|
request=request,
|
|
msg_success=_("Document '{}' deleted").format(title)
|
|
)
|