diff --git a/contracts/forms.py b/contracts/forms.py index 4c1fc92586bae01c09e00860d9e7530feb7969fb..5eb5c034ef06c44c675f9915198789cc49ebbd45 100644 --- a/contracts/forms.py +++ b/contracts/forms.py @@ -2,6 +2,7 @@ import djhacker import dal.autocomplete from django import forms +from django.core.exceptions import ValidationError from webpack_loader.loader import WebpackLoader from .models import Contract, ContracteeSignature, SigneeSignature @@ -45,33 +46,72 @@ class SigneeAdminForm(forms.ModelForm): "shared/admin_signee_form.js", ) + def clean(self): + cleaned_data = super().clean() + + if cleaned_data.get("entity_type") == "natural_person": + if cleaned_data.get("ico_number") is not None: + raise ValidationError({"ico_number": "IČO nesmí být pro fyzické osoby definováno."}) + + if cleaned_data.get("department") is not None: + raise ValidationError({"department": "Organizační složka nesmí být pro fyzické osoby definována."}) + + if cleaned_data.get("date_of_birth") is None: + raise ValidationError({"date_of_birth": "Datum narození musí pro fyzické osoby být definováno."}) + + if cleaned_data.get("entity_type") == "business_natural_person": + if cleaned_data.get("ico_number") is None: + raise ValidationError({"ico_number": "IČO musí být pro podnikající fyzické osoby definováno."}) + + if cleaned_data.get("department") is not None: + raise ValidationError({"department": "Organizační složka nesmí být pro podnikající fyzické osoby definována."}) + + if cleaned_data.get("date_of_birth") is not None: + raise ValidationError({"date_of_birth": "Datum narození nesmí pro podnikající fyzické osoby být definováno."}) + + if cleaned_data.get("entity_type") == "legal_entity": + if cleaned_data.get("ico_number") is None: + raise ValidationError({"ico_number": "IČO musí být pro právnické osoby definováno."}) + + if cleaned_data.get("date_of_birth") is not None: + raise ValidationError({"date_of_birth": "Datum narození nesmí pro právnické osoby být definováno."}) + + return cleaned_data + # BEGIN Autocompleted Contract fields -for foreign_key_field in ( - Contract.filing_area, +djhacker.formfield( Contract.primary_contract, -): - djhacker.formfield( - foreign_key_field, - forms.ModelChoiceField, - widget=dal.autocomplete.ModelSelect2( - url="contracts:select2_djhacker_contract_autocomplete" - ) + forms.ModelChoiceField, + widget=dal.autocomplete.ModelSelect2( + url="contracts:select2_djhacker_contract_autocomplete" + ) +) + +djhacker.formfield( + Contract.filing_area, + forms.ModelChoiceField, + widget=dal.autocomplete.ModelSelect2( + url="contracts:select2_djhacker_contract_filing_area_autocomplete" ) +) +djhacker.formfield( + Contract.issues, + forms.ModelMultipleChoiceField, + widget=dal.autocomplete.ModelSelect2Multiple( + url="contracts:select2_djhacker_contract_issue_autocomplete" + ) +) -for many_to_many_field in ( +djhacker.formfield( Contract.types, - Contract.issues, -): - djhacker.formfield( - many_to_many_field, - forms.ModelMultipleChoiceField, - widget=dal.autocomplete.ModelSelect2Multiple( - url="contracts:select2_djhacker_contract_autocomplete" - ) + forms.ModelMultipleChoiceField, + widget=dal.autocomplete.ModelSelect2Multiple( + url="contracts:select2_djhacker_contract_type_autocomplete" ) +) # END Autocompleted Contract fields diff --git a/contracts/models.py b/contracts/models.py index 4e4b33d28cbdbf98690a4c09f0bb3ce59a7aa9da..8d6236eec7cd5e1ae65c4896fe61b2537f852f40 100644 --- a/contracts/models.py +++ b/contracts/models.py @@ -289,9 +289,9 @@ class Contract(models.Model): created_by = models.ForeignKey( settings.AUTH_USER_MODEL, + on_delete=models.SET_NULL, blank=True, null=True, - on_delete=models.SET_NULL, related_name="uploaded_contracts", verbose_name="Vytvořena uživatelem", help_text="Informace není veřejně přístupná. Pokud vytváříš novou smlouvu, budeš to ty.", diff --git a/contracts/templates/contracts/view_contract.html b/contracts/templates/contracts/view_contract.html index 52996e9adbebec8afdd0426d3d43a1ef550edcd7..26974eb57355ca31a2f79a0ab612af60ef95df7b 100644 --- a/contracts/templates/contracts/view_contract.html +++ b/contracts/templates/contracts/view_contract.html @@ -45,8 +45,13 @@ <td class="w-4/5 !p-2.5">{{ contract.summary }}</td> </tr> <tr> - <td class="w-1/5 !p-2.5">Právní stav</td> - <td class="w-4/5 !p-2.5">{{ contract.get_legal_state_display }}</td> + <td class="w-1/5 !p-2.5">Je platná</td> + <td class="w-4/5 !p-2.5"> + <i + class="{% if contract.legal_state == contract.LegalStates.VALID %}ico--checkmark{% else %}ico--cross{% endif %} __tooltipped" + aria-label="{{ contract.get_legal_state_display }}" + ></i> + </td> </tr> <tr> <td class="w-1/5 !p-2.5">Soubory</td> @@ -310,12 +315,12 @@ <div class="mb-1"> {{ signature.signee.get_entity_type_display }} - {% if signature.signee.entity_type == signature.signee.EntityTypes.NATURAL_PERSON %} + {% if signature.signee.entity_type == signature.signee.EntityTypes.NATURAL_PERSON or signature.signee.entity_type == signature.signee.EntityTypes.BUSINESS_NATURAL_PERSON %} <span class="text-gray-300">(zobrazujeme pouze obec)</span> {% endif %} </div> - {% if signature.signee.entity_type == signature.signee.EntityTypes.NATURAL_PERSON %} + {% if signature.signee.entity_type == signature.signee.EntityTypes.LEGAL_ENTITY or signature.signee.entity_type == signature.signee.EntityTypes.OTHER %} {{ signature.signee.address_street_with_number }}<br> {{ signature.signee.address_zip }} {{ signature.signee.address_district }}<br> {{ signature.signee.get_address_country_display }}<br> diff --git a/contracts/urls.py b/contracts/urls.py index 130a892ac7caceb4719ea6770682eb938bb61457..ff346a750c59c098e3761d0837d1ca47a3bf1997 100644 --- a/contracts/urls.py +++ b/contracts/urls.py @@ -13,6 +13,21 @@ urlpatterns = [ dal.autocomplete.Select2QuerySetView.as_view(model=models.Contract), name="select2_djhacker_contract_autocomplete", ), + path( + "contracts/issues/autocomplete", + dal.autocomplete.Select2QuerySetView.as_view(model=models.ContractIssue), + name="select2_djhacker_contract_issue_autocomplete", + ), + path( + "contracts/filing-areas/autocomplete", + dal.autocomplete.Select2QuerySetView.as_view(model=models.ContractFilingArea), + name="select2_djhacker_contract_filing_area_autocomplete", + ), + path( + "contracts/types/autocomplete", + dal.autocomplete.Select2QuerySetView.as_view(model=models.ContractType), + name="select2_djhacker_contract_type_autocomplete", + ), path( "contracts/signees/signatures/autocomplete", dal.autocomplete.Select2QuerySetView.as_view(model=models.SigneeSignature), diff --git a/static_src/admin/signee_form.js b/static_src/admin/signee_form.js index bb191380dd698752a20bdf7f5a274f403f152aa0..3776c5db2544638ae5b165dc19fe5520a8c17e22 100644 --- a/static_src/admin/signee_form.js +++ b/static_src/admin/signee_form.js @@ -7,22 +7,23 @@ const fieldDepartmentValues = new Set([ $(window).ready( () => { - let isPhysicalPerson = ($("#id_entity_type").find(":selected").val() === "natural_person"); + let isNaturalPerson = ($("#id_entity_type").find(":selected").val() === "natural_person"); let isEmpty = ($("#id_entity_type").find(":selected").val() === ""); $(".field-date_of_birth"). css( "display", ( - (!isEmpty && isPhysicalPerson) ? + (isNaturalPerson) ? "block" : "none" ) ); + $(".field-ico_number"). css( "display", ( - (!isEmpty && !isPhysicalPerson) ? + (!isEmpty && !isNaturalPerson) ? "block" : "none" ) ); @@ -36,17 +37,21 @@ $(window).ready( ) ); + if (!isNaturalPerson && !isEmpty) $("#id_date_of_birth").val(""); + if (isNaturalPerson) $("#id_ico_number").val(""); + if (!fieldDepartmentValues.has($("#id_entity_type").find(":selected").val()) && !isEmpty) $("#id_department").val(""); + $("#id_entity_type").on( "change", event => { isEmpty = ($(event.target).val() === ""); - isPhysicalPerson = ($(event.target).val() === "natural_person"); + isNaturalPerson = ($(event.target).val() === "natural_person"); $(".field-date_of_birth"). css( "display", ( - (!isEmpty && isPhysicalPerson) ? + (!isEmpty && isNaturalPerson) ? "block" : "none" ) ); @@ -54,7 +59,7 @@ $(window).ready( css( "display", ( - (!isEmpty && !isPhysicalPerson) ? + (!isEmpty && !isNaturalPerson) ? "block" : "none" ) ); @@ -67,6 +72,10 @@ $(window).ready( "block" : "none" ) ); + + if (!isNaturalPerson && !isEmpty) $("#id_date_of_birth").val(""); + if (isNaturalPerson) $("#id_ico_number").val(""); + if (!fieldDepartmentValues.has($(event.target).val()) && !isEmpty) $("#id_department").val(""); } ); }