Initial
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 30.11.20
|
||||
|
||||
"""
|
||||
from dal import autocomplete
|
||||
from django import forms
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import transaction
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from intervention.models import Intervention
|
||||
from konova.forms import BaseForm
|
||||
from organisation.models import Organisation
|
||||
from process.enums import ProcessStateEnum
|
||||
from process.models import Process
|
||||
|
||||
|
||||
class NewProcessForm(BaseForm):
|
||||
"""
|
||||
A form for new process
|
||||
"""
|
||||
identifier = forms.CharField(
|
||||
max_length=255,
|
||||
label=_("Identifier"),
|
||||
label_suffix=_(""),
|
||||
help_text=_("Generated automatically if none was given"),
|
||||
required=False,
|
||||
)
|
||||
title = forms.CharField(
|
||||
max_length=255,
|
||||
label=_("Title"),
|
||||
label_suffix=_(""),
|
||||
help_text=_("Proper title of the process"),
|
||||
required=True,
|
||||
)
|
||||
type = forms.CharField(
|
||||
max_length=255,
|
||||
label=_("Type"),
|
||||
label_suffix=_(""),
|
||||
help_text=_("Which process type is this"),
|
||||
required=True,
|
||||
)
|
||||
|
||||
licensing_authority = forms.ModelChoiceField(
|
||||
label=_("Licencing Authority"),
|
||||
label_suffix=_(""),
|
||||
required=True,
|
||||
queryset=Organisation.objects.all(),
|
||||
widget=autocomplete.ModelSelect2(
|
||||
url="orgs-autocomplete",
|
||||
attrs={
|
||||
"data-placeholder": _("Organization"),
|
||||
"data-minimum-input-length": 3,
|
||||
}
|
||||
),
|
||||
)
|
||||
licensing_authority_document_identifier = forms.CharField(
|
||||
max_length=255,
|
||||
label=_("Licencing document identifier"),
|
||||
label_suffix=_(""),
|
||||
required=True,
|
||||
)
|
||||
comment_licensing_authority = forms.CharField(
|
||||
widget=forms.Textarea,
|
||||
label=_("Comment licensing authority"),
|
||||
label_suffix=_(""),
|
||||
required=False,
|
||||
)
|
||||
|
||||
registration_office = forms.ModelChoiceField(
|
||||
label=_("Registration office"),
|
||||
label_suffix=_(""),
|
||||
required=True,
|
||||
queryset=Organisation.objects.all(),
|
||||
widget=autocomplete.ModelSelect2(
|
||||
url="orgs-autocomplete",
|
||||
attrs={
|
||||
"data-placeholder": _("Organization"),
|
||||
"data-minimum-input-length": 3,
|
||||
}
|
||||
),
|
||||
)
|
||||
registration_office_document_identifier = forms.CharField(
|
||||
max_length=255,
|
||||
label=_("Registration document identifier"),
|
||||
label_suffix=_(""),
|
||||
required=True,
|
||||
)
|
||||
comment_registration_office = forms.CharField(
|
||||
widget=forms.Textarea,
|
||||
label=_("Comment registration office"),
|
||||
label_suffix=_(""),
|
||||
required=False,
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.action_url = reverse("process:new")
|
||||
self.cancel_redirect = reverse("process:index")
|
||||
self.form_title = _("Add new process")
|
||||
self.form_caption = _("Enter these basic information for the new process.")
|
||||
|
||||
def save(self, user: User):
|
||||
"""
|
||||
Persists process objects into database
|
||||
"""
|
||||
with transaction.atomic():
|
||||
identifier = self.cleaned_data.get("identifier", None)
|
||||
title = self.cleaned_data.get("title", None)
|
||||
_type = self.cleaned_data.get("type", None)
|
||||
licencing_auth = self.cleaned_data.get("licensing_authority", None)
|
||||
licencing_auth_doc_id = self.cleaned_data.get("licensing_authority_document_identifier", None)
|
||||
reg_off = self.cleaned_data.get("registration_office", None)
|
||||
reg_off_doc_id = self.cleaned_data.get("registration_office_document_identifier", None)
|
||||
comment_license = self.cleaned_data.get("comment_licensing_authority", None)
|
||||
comment_registration = self.cleaned_data.get("comment_registration_office", None)
|
||||
|
||||
process = Process()
|
||||
process.licensing_authority = licencing_auth
|
||||
process.licensing_authority_document_identifier = licencing_auth_doc_id
|
||||
process.registration_office = reg_off
|
||||
process.registration_office_document_identifier = reg_off_doc_id
|
||||
process.state = ProcessStateEnum.PRIVATE.value
|
||||
process.created_by = user
|
||||
process.licensing_authority_comment = comment_license
|
||||
process.registration_office_comment = comment_registration
|
||||
process.save()
|
||||
|
||||
intervention = Intervention()
|
||||
intervention.title = title
|
||||
intervention.type = _type
|
||||
intervention.created_by = user
|
||||
intervention.process = process
|
||||
intervention.identifier = identifier
|
||||
intervention.save()
|
||||
|
||||
return process
|
||||
|
||||
|
||||
class EditProcessForm(NewProcessForm):
|
||||
status = forms.ChoiceField(
|
||||
label=_("Status"),
|
||||
label_suffix="",
|
||||
choices=[],
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.user = kwargs.pop("user", None)
|
||||
super().__init__(*args, **kwargs)
|
||||
if self.instance is not None:
|
||||
self.action_url = reverse("process:edit", args=(self.instance.id,))
|
||||
self.cancel_redirect = reverse("process:index")
|
||||
self.form_title = _("Edit process")
|
||||
self.form_caption = ""
|
||||
self.fields["status"].choices = ProcessStateEnum.as_next_role_choices(self.user, self.instance.state)
|
||||
|
||||
# Initialize form data
|
||||
form_data = {
|
||||
"identifier": self.instance.intervention.identifier,
|
||||
"title": self.instance.intervention.title,
|
||||
"type": self.instance.intervention.type,
|
||||
"licensing_authority": self.instance.licensing_authority,
|
||||
"licensing_authority_document_identifier": self.instance.licensing_authority_document_identifier,
|
||||
"registration_office": self.instance.registration_office,
|
||||
"registration_office_document_identifier": self.instance.registration_office_document_identifier,
|
||||
"comment_licensing_authority": self.instance.licensing_authority_comment,
|
||||
"comment_registration_office": self.instance.registration_office_comment,
|
||||
"status": self.instance.state,
|
||||
}
|
||||
disabled_fields = [
|
||||
"identifier",
|
||||
]
|
||||
self.load_initial_data(
|
||||
form_data,
|
||||
disabled_fields,
|
||||
)
|
||||
|
||||
def save(self, user: User):
|
||||
""" Persists changes from form to instance
|
||||
|
||||
Args:
|
||||
user (User): The performing user
|
||||
|
||||
Returns:
|
||||
process (Process): The edited process instance
|
||||
"""
|
||||
with transaction.atomic():
|
||||
title = self.cleaned_data.get("title", None)
|
||||
_type = self.cleaned_data.get("type", None)
|
||||
licencing_auth = self.cleaned_data.get("licensing_authority", None)
|
||||
licencing_auth_doc_id = self.cleaned_data.get("licensing_authority_document_identifier", None)
|
||||
reg_off = self.cleaned_data.get("registration_office", None)
|
||||
reg_off_doc_id = self.cleaned_data.get("registration_office_document_identifier", None)
|
||||
comment_license = self.cleaned_data.get("comment_licensing_authority", None)
|
||||
comment_registration = self.cleaned_data.get("comment_registration_office", None)
|
||||
new_state = self.cleaned_data.get("status", None)
|
||||
|
||||
self.instance.licensing_authority = licencing_auth
|
||||
self.instance.licensing_authority_document_identifier = licencing_auth_doc_id
|
||||
self.instance.registration_office = reg_off
|
||||
self.instance.registration_office_document_identifier = reg_off_doc_id
|
||||
self.instance.state = ProcessStateEnum.PRIVATE.value
|
||||
self.instance.created_by = user
|
||||
self.instance.licensing_authority_comment = comment_license
|
||||
self.instance.registration_office_comment = comment_registration
|
||||
self.instance.state = new_state
|
||||
self.instance.save()
|
||||
|
||||
self.instance.intervention.title = title
|
||||
self.instance.intervention.type = _type
|
||||
self.instance.intervention.created_by = user
|
||||
self.instance.intervention.save()
|
||||
|
||||
return self.instance
|
||||
Reference in New Issue
Block a user