From ce924d7af43ccaa1f774e8628fb8a299cfae1ffd Mon Sep 17 00:00:00 2001 From: kdeterme Date: Fri, 25 Sep 2026 14:40:38 +0200 Subject: [PATCH] fix(interventions): import Contract and ContractPost in admin to fix OperationTemplate change view --- loko/interventions/admin.py | 3 +-- .../tests/test_preventive_templates.py | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/loko/interventions/admin.py b/loko/interventions/admin.py index 1d4401b..36ecf74 100644 --- a/loko/interventions/admin.py +++ b/loko/interventions/admin.py @@ -41,6 +41,7 @@ from .services import generate_interventions_excel from common.models import Thematic from assets.models import AssetCategory +from contracts.models import Contract, ContractPost from interventions.views.preventive_import import process_worksheet as _process_worksheet import json from django.contrib.contenttypes.models import ContentType @@ -944,7 +945,6 @@ class ContractOperationTemplatePostInlineForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - from contracts.models import ContractPost contract_id = None if self.instance and self.instance.pk and self.instance.contract_id: contract_id = self.instance.contract_id @@ -1036,7 +1036,6 @@ class ContractOperationTemplatePostAdminForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - from contracts.models import ContractPost contract_id = None if self.instance and self.instance.pk and self.instance.contract_id: contract_id = self.instance.contract_id diff --git a/loko/interventions/tests/test_preventive_templates.py b/loko/interventions/tests/test_preventive_templates.py index 48f99c6..b10a20a 100644 --- a/loko/interventions/tests/test_preventive_templates.py +++ b/loko/interventions/tests/test_preventive_templates.py @@ -1238,6 +1238,33 @@ class ContractOperationTemplatePostTests(TestCase): self.assertIn(self.contract.id, contract_ids) self.assertNotIn(self.contract_other.id, contract_ids) + def test_operation_template_admin_change_view(self): + """Test that OperationTemplate admin change view renders without NameError.""" + self.user.is_staff = True + self.user.is_superuser = True + self.user.save() + self.client.login(username='test_user_posts', password='password123') + url = reverse('admin:interventions_operationtemplate_change', args=[self.template_a.id]) + res = self.client.get(url, {'_changelist_filters': f'thematic__id__exact={self.thematic.id}'}) + self.assertEqual(res.status_code, 200) + + def test_contract_operation_template_post_admin_change_view(self): + """Test that ContractOperationTemplatePost admin change view renders without NameError.""" + self.user.is_staff = True + self.user.is_superuser = True + self.user.save() + self.client.login(username='test_user_posts', password='password123') + mapping = ContractOperationTemplatePost.objects.create( + operation_template=self.template_a, + contract=self.contract, + contract_post=self.post_1, + default_quantity=Decimal('2.00'), + ) + url = reverse('admin:interventions_contractoperationtemplatepost_change', args=[mapping.id]) + res = self.client.get(url) + self.assertEqual(res.status_code, 200) + +