konova/konova/views.py
mipel a0c0a0f074 Documents
* add get document route
* add missing attributes to intervention detail view
2021-07-23 15:35:05 +02:00

81 lines
2.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.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 konova.contexts import BaseContext
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()
# 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]
additional_context = {
"msgs": msgs,
"total_intervention_count": 123,
"user_intervention_count": 5,
"total_compensation_count": 123,
"user_compensation_count": 5,
"total_eco_count": 123,
"user_eco_count": 5,
}
context = BaseContext(request, additional_context).context
return render(request, template, context)
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.file, as_attachment=True)