import json
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.forms import modelformset_factory
from django.shortcuts import render, redirect, get_object_or_404
from django.template import loader
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import ListView
from django_tables2 import SingleTableView
from ferrosoft.apps.befundung.forms import (
SettingsForm,
ExaminationForm,
ReductionForm,
RefusalForm,
PhotoForm,
)
from ferrosoft.apps.befundung.models import (
Settings,
Log,
Reduction,
Refusal,
Photo,
create_log,
ExaminationRequest,
)
from ferrosoft.apps.befundung.tables import LogTable
from ferrosoft.apps.ferrobase.services.taskqueue import background_task_tenant
from ferrosoft.apps.ferrobase.views import PlatformActivityMixin
[docs]
class LogListView(SingleTableView):
extra_context = {
"title": _("Protocol of examination report generator"),
}
model = Log
template_name = "befundung/log.html"
table_class = LogTable
ordering = "-date"
[docs]
@login_required
def settings_index(request):
report_template = loader.get_template("befundung/index.html")
instance = Settings.get_instance()
if request.method == "POST":
form = SettingsForm(request.POST, request.FILES, instance=instance)
if form.is_valid():
form.save()
else:
form = SettingsForm(instance=instance)
return render(
request,
"befundung/settings/index.html",
context={
"form": form,
"report_template": report_template.template.source,
},
)
[docs]
def demo(request):
reduction_amount = int(request.GET.get("reduction_amount", 1))
refusal_amount = int(request.GET.get("refusal_amount", 1))
photo_amount = int(request.GET.get("photo_amount", 1))
ReductionFormSet = modelformset_factory(
Reduction, ReductionForm, extra=reduction_amount
)
RefusalFormSet = modelformset_factory(Refusal, RefusalForm, extra=refusal_amount)
PhotoFormSet = modelformset_factory(Photo, PhotoForm, extra=photo_amount)
if request.method == "POST":
examination_form = ExaminationForm(request.POST)
if examination_form.is_valid():
examination = examination_form.save()
form_kwargs = {"examination": examination}
reduction_forms = ReductionFormSet(
request.POST,
prefix="reduction",
form_kwargs=form_kwargs,
queryset=Reduction.objects.none(),
)
refusal_forms = RefusalFormSet(
request.POST,
prefix="refusal",
form_kwargs=form_kwargs,
queryset=Refusal.objects.none(),
)
photo_forms = PhotoFormSet(
request.POST,
request.FILES,
prefix="photo",
form_kwargs=form_kwargs,
queryset=Photo.objects.none(),
)
if (
reduction_forms.is_valid()
and refusal_forms.is_valid()
and photo_forms.is_valid()
):
reduction_forms.save()
refusal_forms.save()
photo_forms.save()
task_id = background_task_tenant(
"ferrosoft.apps.befundung.request.report_examination",
str(examination.pk),
)
create_log(
"enqueued examination for processing, weighing_slip_id=%s; task_id=%s"
% (
examination.weighing_slip_id,
task_id,
)
)
return redirect(reverse("befundung:log"))
else:
reduction_forms = ReductionFormSet(
prefix="reduction", queryset=Reduction.objects.none()
)
refusal_forms = RefusalFormSet(
prefix="refusal", queryset=Refusal.objects.none()
)
photo_forms = PhotoFormSet(prefix="photo", queryset=Photo.objects.none())
else:
examination_form = ExaminationForm()
reduction_forms = ReductionFormSet(
prefix="reduction", queryset=Reduction.objects.none()
)
refusal_forms = RefusalFormSet(
prefix="refusal", queryset=Refusal.objects.none()
)
photo_forms = PhotoFormSet(prefix="photo", queryset=Photo.objects.none())
return render(
request,
"befundung/demo.html",
context={
"form": examination_form,
"reduction_forms": reduction_forms,
"refusal_forms": refusal_forms,
"photo_forms": photo_forms,
},
)
[docs]
class ReplayListView(PlatformActivityMixin, LoginRequiredMixin, ListView):
activity_title = _("Replay Examination")
activity_app = "befundung"
model = ExaminationRequest
paginate_by = 10
template_name = "ferrobase/bootstrap5/list_view.html"
[docs]
def get_queryset(self):
return super().get_queryset().order_by("-creation_date")
[docs]
def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs) | {
"update_allowed": True,
}
[docs]
def replay_request(request, pk):
report = get_object_or_404(ExaminationRequest, pk=pk)
payload = report.payload
# Remove photos from payload, to avoid overloading browsers with megabytes
# of data in a textarea. Photos should already be removed by DRF API.
if "photos" in payload:
del payload["photos"]
payload = json.dumps(payload, indent=4)
return render(
request,
"befundung/replay/request.html",
context={
"report": report,
"payload": payload,
"weighing_slip_id": report.payload.get("weighing_slip_id", ""),
},
)