Skip to content

Commit

Permalink
Merge pull request #180 from mlebreuil/improve-filterset
Browse files Browse the repository at this point in the history
Improve filtering
  • Loading branch information
mlebreuil authored Sep 8, 2024
2 parents 59a19f5 + 8864dfd commit 443359e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Version 2.2.5

* [178](https://github.com/mlebreuil/netbox-contract/issues/178) Add the possibility to filter on invoice number, and contract name through the API.
* Generally improve filtering options

### Version 2.2.4

Expand Down
2 changes: 2 additions & 0 deletions src/netbox_contract/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ContractViewSet(NetBoxModelViewSet):
)
)
serializer_class = ContractSerializer
filterset_class = filtersets.ContractFilterSet


class InvoiceViewSet(NetBoxModelViewSet):
Expand All @@ -44,6 +45,7 @@ class InvoiceLineViewSet(NetBoxModelViewSet):
'invoice', 'accounting_dimensions', 'tags'
)
serializer_class = InvoiceLineSerializer
filterset_class = filtersets.InvoiceLineFilterSet


class AccountingDimensionViewSet(NetBoxModelViewSet):
Expand Down
47 changes: 43 additions & 4 deletions src/netbox_contract/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import django_filters
from django.db.models import Q
from netbox.filtersets import NetBoxModelFilterSet
from tenancy.filtersets import TenancyFilterSet

from .models import (
AccountingDimension,
AccountingDimensionStatusChoices,
Contract,
ContractAssignment,
CurrencyChoices,
InternalEntityChoices,
Invoice,
InvoiceLine,
ServiceProvider,
StatusChoices,
)


class ContractFilterSet(NetBoxModelFilterSet):
class ContractFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
status = django_filters.MultipleChoiceFilter(choices=StatusChoices, null_value=None)
internal_partie = django_filters.MultipleChoiceFilter(
choices=InternalEntityChoices, null_value=None
)
currency = django_filters.MultipleChoiceFilter(
choices=CurrencyChoices, null_value=None
)

class Meta:
model = Contract
fields = ('id', 'name', 'internal_partie', 'external_reference', 'parent')
fields = (
'id',
'name',
'external_reference',
'start_date',
'end_date',
'initial_term',
'parent',
)

def search(self, queryset, name, value):
return queryset.filter(
Expand All @@ -30,9 +48,22 @@ def search(self, queryset, name, value):


class InvoiceFilterSet(NetBoxModelFilterSet):
currency = django_filters.MultipleChoiceFilter(
choices=CurrencyChoices, null_value=None
)

class Meta:
model = Invoice
fields = ('id', 'number', 'template', 'contracts')
fields = (
'id',
'number',
'template',
'date',
'contracts',
'period_start',
'period_end',
'amount',
)

def search(self, queryset, name, value):
return queryset.filter(
Expand All @@ -59,9 +90,13 @@ def search(self, queryset, name, value):


class InvoiceLineFilterSet(NetBoxModelFilterSet):
currency = django_filters.MultipleChoiceFilter(
choices=CurrencyChoices, null_value=None
)

class Meta:
model = InvoiceLine
fields = ('id', 'invoice')
fields = ('id', 'invoice', 'accounting_dimensions')

def search(self, queryset, name, value):
return queryset.filter(
Expand All @@ -70,6 +105,10 @@ def search(self, queryset, name, value):


class AccountingDimensionFilterSet(NetBoxModelFilterSet):
status = django_filters.MultipleChoiceFilter(
choices=AccountingDimensionStatusChoices, null_value=None
)

class Meta:
model = AccountingDimension
fields = ('name', 'value')
Expand Down
20 changes: 16 additions & 4 deletions src/netbox_contract/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from extras.filters import TagFilter
from netbox.forms import (
NetBoxModelBulkEditForm,
NetBoxModelFilterSetForm,
NetBoxModelForm,
NetBoxModelImportForm,
)
from tenancy.models import Tenant
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES
from utilities.forms.fields import (
CommentField,
ContentTypeChoiceField,
Expand All @@ -21,6 +21,7 @@
DynamicModelMultipleChoiceField,
JSONField,
SlugField,
TagFilterField,
)
from utilities.forms.widgets import DatePicker, HTMXSelect

Expand Down Expand Up @@ -161,9 +162,11 @@ class ContractFilterSetForm(NetBoxModelFilterSetForm):

tenant = DynamicModelChoiceField(queryset=Tenant.objects.all(), required=False)
external_reference = forms.CharField(required=False)
internal_partie = forms.CharField(required=False)
internal_partie = forms.ChoiceField(choices=InternalEntityChoices, required=False)
status = forms.ChoiceField(choices=StatusChoices, required=False)
currency = forms.ChoiceField(choices=CurrencyChoices, required=False)
parent = DynamicModelChoiceField(queryset=Contract.objects.all(), required=False)
tag = TagFilterField(model)


class ContractCSVForm(NetBoxModelImportForm):
Expand Down Expand Up @@ -359,9 +362,15 @@ class Meta:

class InvoiceFilterSetForm(NetBoxModelFilterSetForm):
model = Invoice
number = forms.CharField(required=False)
template = forms.NullBooleanField(
required=False, widget=forms.Select(choices=BOOLEAN_WITH_BLANK_CHOICES)
)
currency = forms.ChoiceField(choices=CurrencyChoices, required=False)
contracts = DynamicModelMultipleChoiceField(
queryset=Contract.objects.all(), required=False
)
tag = TagFilterField(model)


class InvoiceCSVForm(NetBoxModelImportForm):
Expand Down Expand Up @@ -425,7 +434,8 @@ class Meta:

class ServiceProviderFilterSetForm(NetBoxModelFilterSetForm):
model = ServiceProvider
tag = TagFilter()
name = forms.CharField(required=False)
tag = TagFilterField(model)


class ServiceProviderCSVForm(NetBoxModelImportForm):
Expand Down Expand Up @@ -514,9 +524,11 @@ class Meta:
class InvoiceLineFilterSetForm(NetBoxModelFilterSetForm):
model = InvoiceLine
invoice = DynamicModelChoiceField(queryset=Invoice.objects.all(), required=False)
accounting_dimensions = DynamicModelChoiceField(
accounting_dimensions = DynamicModelMultipleChoiceField(
queryset=AccountingDimension.objects.all(), required=False
)
currency = forms.ChoiceField(choices=CurrencyChoices, required=False)
tag = TagFilterField(model)


class InvoiceLineImportForm(NetBoxModelImportForm):
Expand Down

0 comments on commit 443359e

Please sign in to comment.