From 8e3e8654973cb2fe77092f465f72d27dbd08068a Mon Sep 17 00:00:00 2001 From: Marc Lebreuil Date: Sun, 18 Aug 2024 19:34:09 +0000 Subject: [PATCH 1/2] accounting lin and dimension search fixed --- src/netbox_contract/api/serializers.py | 25 ++++++++++++++++++++++--- src/netbox_contract/api/views.py | 4 +++- src/netbox_contract/filtersets.py | 6 ++---- src/netbox_contract/forms.py | 10 ++++++---- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/netbox_contract/api/serializers.py b/src/netbox_contract/api/serializers.py index ea2d905..f5d1636 100644 --- a/src/netbox_contract/api/serializers.py +++ b/src/netbox_contract/api/serializers.py @@ -66,8 +66,15 @@ class NestedInvoicelineSerializer(WritableNestedSerializer): class Meta: model = InvoiceLine - fields = ('id', 'url', 'display', 'invoice', 'amount') - brief_fields = ('id', 'url', 'display', 'invoice', 'amount') + fields = ('id', 'url', 'display', 'invoice', 'accounting_dimensions', 'amount') + brief_fields = ( + 'id', + 'url', + 'display', + 'invoice', + 'accounting_dimensions', + 'amount', + ) class NestedAccountingDimensionSerializer(WritableNestedSerializer): @@ -227,6 +234,10 @@ class InvoiceLineSerializer(NetBoxModelSerializer): url = serializers.HyperlinkedIdentityField( view_name='plugins-api:netbox_contract-api:invoiceline-detail' ) + invoice = NestedInvoiceSerializer(many=False, required=False) + accounting_dimensions = NestedAccountingDimensionSerializer( + many=True, required=False + ) class Meta: model = InvoiceLine @@ -237,13 +248,21 @@ class Meta: 'invoice', 'amount', 'currency', + 'accounting_dimensions', 'comments', 'tags', 'custom_fields', 'created', 'last_updated', ) - brief_fields = ('invoice', 'amount', 'url', 'display', 'name') + brief_fields = ( + 'invoice', + 'accounting_dimensions', + 'amount', + 'url', + 'display', + 'name', + ) class AccountingDimensionSerializer(NetBoxModelSerializer): diff --git a/src/netbox_contract/api/views.py b/src/netbox_contract/api/views.py index 2f4c770..904270c 100644 --- a/src/netbox_contract/api/views.py +++ b/src/netbox_contract/api/views.py @@ -40,7 +40,9 @@ class ContractAssignmentViewSet(NetBoxModelViewSet): class InvoiceLineViewSet(NetBoxModelViewSet): - queryset = models.InvoiceLine.objects.prefetch_related('invoice', 'tags') + queryset = models.InvoiceLine.objects.prefetch_related( + 'invoice', 'accounting_dimensions', 'tags' + ) serializer_class = InvoiceLineSerializer diff --git a/src/netbox_contract/filtersets.py b/src/netbox_contract/filtersets.py index d156483..df0481d 100644 --- a/src/netbox_contract/filtersets.py +++ b/src/netbox_contract/filtersets.py @@ -61,7 +61,7 @@ class Meta: def search(self, queryset, name, value): return queryset.filter( - Q(comments__icontains=value) | Q(invoice__name__icontains=value) + Q(comments__icontains=value) | Q(invoice__number__icontains=value) ) @@ -71,6 +71,4 @@ class Meta: fields = ('name', 'value') def search(self, queryset, name, value): - return queryset.filter( - Q(comments__icontains=value) | Q(invoice__name__icontains=value) - ) + return queryset.filter(Q(comments__icontains=value) | Q(name__icontains=value)) diff --git a/src/netbox_contract/forms.py b/src/netbox_contract/forms.py index 84f2520..e7300fc 100644 --- a/src/netbox_contract/forms.py +++ b/src/netbox_contract/forms.py @@ -26,6 +26,7 @@ from .constants import SERVICE_PROVIDER_MODELS from .models import ( AccountingDimension, + AccountingDimensionStatusChoices, Contract, ContractAssignment, InternalEntityChoices, @@ -488,10 +489,7 @@ class Meta: class InvoiceLineFilterSetForm(NetBoxModelFilterSetForm): model = InvoiceLine - invoice = DynamicModelChoiceField(queryset=Invoice.objects.all()) - accounting_dimensions = DynamicModelMultipleChoiceField( - queryset=AccountingDimension.objects.all() - ) + invoice = DynamicModelChoiceField(queryset=Invoice.objects.all(), required=False) class InvoiceLineImportForm(NetBoxModelImportForm): @@ -544,6 +542,10 @@ class Meta: class AccountingDimensionFilterSetForm(NetBoxModelFilterSetForm): model = AccountingDimension + name = forms.CharField(required=False) + value = forms.CharField(required=False) + status = forms.ChoiceField(choices=AccountingDimensionStatusChoices, required=False) + class AccountingDimensionImportForm(NetBoxModelImportForm): status = CSVChoiceField(choices=StatusChoices, help_text='Contract status') From 2bed2d683a0ac1b7881231e97d9d1bfce7fe0db9 Mon Sep 17 00:00:00 2001 From: Marc Lebreuil Date: Sun, 18 Aug 2024 19:54:36 +0000 Subject: [PATCH 2/2] Attempt to fix filtering invoiceline by dimensions Not working --- CHANGELOG.md | 1 + src/netbox_contract/api/serializers.py | 6 +++--- ...alter_invoiceline_accounting_dimensions.py | 19 +++++++++++++++++++ src/netbox_contract/models.py | 4 +--- 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 src/netbox_contract/migrations/0036_alter_invoiceline_accounting_dimensions.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7317f45..5731dcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [154](https://github.com/mlebreuil/netbox-contract/issues/154) Fix edit and delete bulk operations on dimensions and invoice lines. * [153](https://github.com/mlebreuil/netbox-contract/issues/153) Enforce uniquness of accounting dimensions. * Adds a status ( Active or Inactive ) to accounitng dimensions. +* [151](https://github.com/mlebreuil/netbox-contract/issues/151) Fix accounting line and dimensions search. ### Version 2.2.1 diff --git a/src/netbox_contract/api/serializers.py b/src/netbox_contract/api/serializers.py index f5d1636..9511ac0 100644 --- a/src/netbox_contract/api/serializers.py +++ b/src/netbox_contract/api/serializers.py @@ -50,7 +50,7 @@ class Meta: class NestedContractAssignmentSerializer(WritableNestedSerializer): url = serializers.HyperlinkedIdentityField( - view_name='plugins-api:netbox_contract-api:ContractAssignment-detail' + view_name='plugins-api:netbox_contract-api:contractassignment-detail' ) class Meta: @@ -61,7 +61,7 @@ class Meta: class NestedInvoicelineSerializer(WritableNestedSerializer): url = serializers.HyperlinkedIdentityField( - view_name='plugins-api:netbox_contract-api:InvoiceLine-detail' + view_name='plugins-api:netbox_contract-api:invoiceline-detail' ) class Meta: @@ -79,7 +79,7 @@ class Meta: class NestedAccountingDimensionSerializer(WritableNestedSerializer): url = serializers.HyperlinkedIdentityField( - view_name='plugins-api:netbox_contract-api:AccountingDimension-detail' + view_name='plugins-api:netbox_contract-api:accountingdimension-detail' ) class Meta: diff --git a/src/netbox_contract/migrations/0036_alter_invoiceline_accounting_dimensions.py b/src/netbox_contract/migrations/0036_alter_invoiceline_accounting_dimensions.py new file mode 100644 index 0000000..93f2218 --- /dev/null +++ b/src/netbox_contract/migrations/0036_alter_invoiceline_accounting_dimensions.py @@ -0,0 +1,19 @@ +# Generated by Django 5.0.6 on 2024-08-18 19:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('netbox_contract', '0035_accountingdimension_status_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='invoiceline', + name='accounting_dimensions', + field=models.ManyToManyField( + blank=True, to='netbox_contract.accountingdimension' + ), + ), + ] diff --git a/src/netbox_contract/models.py b/src/netbox_contract/models.py index ff0cf09..a15e6f9 100644 --- a/src/netbox_contract/models.py +++ b/src/netbox_contract/models.py @@ -239,9 +239,7 @@ class InvoiceLine(NetBoxModel): max_length=3, choices=CurrencyChoices, default=CurrencyChoices.CURRENCY_USD ) amount = models.DecimalField(max_digits=10, decimal_places=2) - accounting_dimensions = models.ManyToManyField( - AccountingDimension, related_name='invoicelines', blank=True - ) + accounting_dimensions = models.ManyToManyField(AccountingDimension, blank=True) comments = models.TextField(blank=True) def get_absolute_url(self):