diff --git a/contracts/admin.py b/contracts/admin.py index ed869a6a50d25d43000623fe2f3be3ebd5beb723..80659a7b02226569e5536cb34c716a791f5c1304 100644 --- a/contracts/admin.py +++ b/contracts/admin.py @@ -342,7 +342,50 @@ class ContractAdmin( "Content-Type": "application/json", } - if obj.status == obj.StatusTypes.TO_BE_APPROVED: + if obj.status == obj.StatusTypes.WORK_IN_PROGRESS: + try: + user = User.objects.filter(id=obj.created_by.id).first() + + notice = requests.post( + settings.NASTENKA_API_URL, + data=json.dumps( + { + "name": f"Smlouva vrácena k ĂşpravÄ› - {obj.name}", + "description": ( + obj.summary + if obj.summary not in (None, "") + else "Bez popisu." + ), + "contract_id": obj.id, + "sso_ids": [user.sso_id], + } + ), + headers=headers, + ).raise_for_status() + + if obj.after_approval_nastenka_notice_id is not None: + requests.delete( + f"{settings.NASTENKA_API_URL}/{obj.after_approval_nastenka_notice_id}", + headers=headers, + ).raise_for_status() + + obj.after_approval_nastenka_notice_id = None + obj.save() + + if obj.to_be_approved_nastenka_notice_id is not None: + requests.delete( + f"{settings.NASTENKA_API_URL}/{obj.to_be_approved_nastenka_notice_id}", + headers=headers, + ).raise_for_status() + + obj.to_be_approved_nastenka_notice_id = None + obj.save() + except Exception as exception: + logger.error( + 'Failed to send out "contract returned for editing" NástÄ›nka notification: %s', + str(exception), + ) + elif obj.status == obj.StatusTypes.TO_BE_APPROVED: try: sso_ids = [] @@ -381,7 +424,7 @@ class ContractAdmin( try: user = User.objects.filter(id=obj.created_by.id).first() - requests.post( + notice = requests.post( settings.NASTENKA_API_URL, data=json.dumps( { @@ -396,7 +439,13 @@ class ContractAdmin( } ), headers=headers, - ).raise_for_status() + ) + + notice.raise_for_status() + notice = notice.json() + + obj.after_approval_nastenka_notice_id = uuid.UUID(notice["id"]) + obj.save() if obj.to_be_approved_nastenka_notice_id is not None: requests.delete( @@ -415,7 +464,7 @@ class ContractAdmin( try: user = User.objects.filter(id=obj.created_by.id).first() - requests.post( + notice = requests.post( settings.NASTENKA_API_URL, data=json.dumps( { @@ -430,7 +479,13 @@ class ContractAdmin( } ), headers=headers, - ).raise_for_status() + ) + + notice.raise_for_status() + notice = notice.json() + + obj.after_approval_nastenka_notice_id = uuid.UUID(notice["id"]) + obj.save() if obj.to_be_approved_nastenka_notice_id is not None: requests.delete( diff --git a/contracts/forms.py b/contracts/forms.py index 935d34dba8853a8894edef1f106b2c71e5c2c01c..a6a99e7e5a8ed99c9bdd744ec35c3ccec14a0d5c 100644 --- a/contracts/forms.py +++ b/contracts/forms.py @@ -80,7 +80,7 @@ class ContractAdminForm(forms.ModelForm): if choice_key in ( Contract.StatusTypes.APPROVED, Contract.StatusTypes.REJECTED, - Contract.StatusTypes.WORK_IN_PROGRESS + Contract.StatusTypes.WORK_IN_PROGRESS, ): allowed_choices.append( ( diff --git a/contracts/migrations/0065_alter_signee_options_alter_contract_status.py b/contracts/migrations/0065_alter_signee_options_alter_contract_status.py index 712b59da6f9332d78042117ff9d8cb69c567b03a..2934140c0ee6578632e89903615b275fc0eb7a9b 100644 --- a/contracts/migrations/0065_alter_signee_options_alter_contract_status.py +++ b/contracts/migrations/0065_alter_signee_options_alter_contract_status.py @@ -4,19 +4,42 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ - ('contracts', '0064_contract_to_be_approved_nastenka_notice_id'), + ("contracts", "0064_contract_to_be_approved_nastenka_notice_id"), ] operations = [ migrations.AlterModelOptions( - name='signee', - options={'ordering': ['name', models.OrderBy(models.F('department'), descending=True, nulls_last=False)], 'permissions': [('edit_others', 'Upravit cizĂ'), ('delete_others', 'Odstranit cizĂ')], 'verbose_name': 'Jiná smluvnĂ strana', 'verbose_name_plural': 'OstatnĂ smluvnĂ strany'}, + name="signee", + options={ + "ordering": [ + "name", + models.OrderBy( + models.F("department"), descending=True, nulls_last=False + ), + ], + "permissions": [ + ("edit_others", "Upravit cizĂ"), + ("delete_others", "Odstranit cizĂ"), + ], + "verbose_name": "Jiná smluvnĂ strana", + "verbose_name_plural": "OstatnĂ smluvnĂ strany", + }, ), migrations.AlterField( - model_name='contract', - name='status', - field=models.CharField(choices=[('work_in_progress', 'Rozpracovaná'), ('to_be_approved', 'Ke schválenĂ'), ('approved', 'Schválená'), ('rejected', 'ZamĂtnutá')], default='work_in_progress', help_text='OznaÄŤenĂm jako "Ke schválenĂ" se smlouva pĹ™edá schvalovateli.', max_length=16, verbose_name='Stav'), + model_name="contract", + name="status", + field=models.CharField( + choices=[ + ("work_in_progress", "Rozpracovaná"), + ("to_be_approved", "Ke schválenĂ"), + ("approved", "Schválená"), + ("rejected", "ZamĂtnutá"), + ], + default="work_in_progress", + help_text='OznaÄŤenĂm jako "Ke schválenĂ" se smlouva pĹ™edá schvalovateli.', + max_length=16, + verbose_name="Stav", + ), ), ] diff --git a/contracts/migrations/0066_alter_contractee_options.py b/contracts/migrations/0066_alter_contractee_options.py index 12bb14d89d154613e4a607334505efad6f8e3122..243e1a7732b1ceea6783b0a6979ce2fc7aeeb0ba 100644 --- a/contracts/migrations/0066_alter_contractee_options.py +++ b/contracts/migrations/0066_alter_contractee_options.py @@ -4,14 +4,26 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ - ('contracts', '0065_alter_signee_options_alter_contract_status'), + ("contracts", "0065_alter_signee_options_alter_contract_status"), ] operations = [ migrations.AlterModelOptions( - name='contractee', - options={'ordering': ['name', models.OrderBy(models.F('department'), descending=True, nulls_last=False)], 'permissions': [('edit_others', 'Upravit cizĂ'), ('delete_others', 'Odstranit cizĂ')], 'verbose_name': 'Naše smluvnĂ strana', 'verbose_name_plural': 'Naše smluvnĂ strany'}, + name="contractee", + options={ + "ordering": [ + "name", + models.OrderBy( + models.F("department"), descending=True, nulls_last=False + ), + ], + "permissions": [ + ("edit_others", "Upravit cizĂ"), + ("delete_others", "Odstranit cizĂ"), + ], + "verbose_name": "Naše smluvnĂ strana", + "verbose_name_plural": "Naše smluvnĂ strany", + }, ), ] diff --git a/contracts/migrations/0067_alter_contractee_options_alter_signee_options.py b/contracts/migrations/0067_alter_contractee_options_alter_signee_options.py index bfda986a38104a5618060f7bf60bef93c13b4404..b211adf894a3e446b068f13e1cabc79e3410e411 100644 --- a/contracts/migrations/0067_alter_contractee_options_alter_signee_options.py +++ b/contracts/migrations/0067_alter_contractee_options_alter_signee_options.py @@ -4,18 +4,39 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ - ('contracts', '0066_alter_contractee_options'), + ("contracts", "0066_alter_contractee_options"), ] operations = [ migrations.AlterModelOptions( - name='contractee', - options={'ordering': ['name', models.OrderBy(models.F('department'), nulls_first=True)], 'permissions': [('edit_others', 'Upravit cizĂ'), ('delete_others', 'Odstranit cizĂ')], 'verbose_name': 'Naše smluvnĂ strana', 'verbose_name_plural': 'Naše smluvnĂ strany'}, + name="contractee", + options={ + "ordering": [ + "name", + models.OrderBy(models.F("department"), nulls_first=True), + ], + "permissions": [ + ("edit_others", "Upravit cizĂ"), + ("delete_others", "Odstranit cizĂ"), + ], + "verbose_name": "Naše smluvnĂ strana", + "verbose_name_plural": "Naše smluvnĂ strany", + }, ), migrations.AlterModelOptions( - name='signee', - options={'ordering': ['name', models.OrderBy(models.F('department'), nulls_first=True)], 'permissions': [('edit_others', 'Upravit cizĂ'), ('delete_others', 'Odstranit cizĂ')], 'verbose_name': 'Jiná smluvnĂ strana', 'verbose_name_plural': 'OstatnĂ smluvnĂ strany'}, + name="signee", + options={ + "ordering": [ + "name", + models.OrderBy(models.F("department"), nulls_first=True), + ], + "permissions": [ + ("edit_others", "Upravit cizĂ"), + ("delete_others", "Odstranit cizĂ"), + ], + "verbose_name": "Jiná smluvnĂ strana", + "verbose_name_plural": "OstatnĂ smluvnĂ strany", + }, ), ] diff --git a/contracts/models.py b/contracts/models.py index bcd61d75af4427a5b2a6cd145fccb4e5a558626f..17e70ac546e7657d59909a2e826786390f4e8384 100644 --- a/contracts/models.py +++ b/contracts/models.py @@ -698,6 +698,12 @@ class Contract(NameStrMixin, models.Model): blank=True, null=True, verbose_name="ID oznámenĂ o novĂ© smlouvÄ› v NástÄ›nce" ) + after_approval_nastenka_notice_id = models.UUIDField( + blank=True, + null=True, + verbose_name="ID oznámenĂ o schválenĂ / vrácenĂ v NástÄ›nce", + ) + @property def primary_contract_url(self) -> typing.Union[None, str]: if self.primary_contract is None: