Skip to content

Commit

Permalink
Merge pull request #69 from mlebreuil/64-master-agreement
Browse files Browse the repository at this point in the history
Add hierarchy to contract
  • Loading branch information
mlebreuil authored Jun 18, 2023
2 parents 31176fc + b7e5605 commit 1e9271a
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ Add support for Netbox 3.5 which become the minimum version supported to accomod

* Add bulk update capability for contract assignement
* [#63](https://github.com/mlebreuil/netbox-contract/issues/63) Correct an API issue on the invoice object.
* [#64](https://github.com/mlebreuil/netbox-contract/issues/64) Add hierarchy to contract; New parent field created.

3 changes: 2 additions & 1 deletion src/netbox_contract/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ class ContractSerializer(NetBoxModelSerializer):
)
circuit= NestedCircuitSerializer(many=True, required=False)
external_partie = NestedServiceProviderSerializer(many=False)
parent = NestedContracSerializer(many=False, required=False)

class Meta:
model = Contract
fields = (
'id', 'url','display', 'name', 'status', 'external_partie','internal_partie','circuit','comments',
'id', 'url','display', 'name', 'status', 'external_partie','internal_partie','parent','circuit','comments',
'tags', 'custom_fields', 'created', 'last_updated',
)

Expand Down
4 changes: 3 additions & 1 deletion src/netbox_contract/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from .serializers import ContractSerializer, InvoiceSerializer, ServiceProviderSerializer, ContractAssignementSerializer

class ContractViewSet(NetBoxModelViewSet):
queryset = models.Contract.objects.prefetch_related('circuit','tags')
queryset = models.Contract.objects.prefetch_related(
'parent','circuit','tags'
)
serializer_class = ContractSerializer

class InvoiceViewSet(NetBoxModelViewSet):
Expand Down
2 changes: 1 addition & 1 deletion src/netbox_contract/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ContractFilterSet(NetBoxModelFilterSet):

class Meta:
model = Contract
fields = ('id', 'external_partie', 'internal_partie', 'status','circuit')
fields = ('id', 'external_partie', 'internal_partie', 'status','parent','circuit')

def search(self, queryset, name, value):
return queryset.filter( Q(name__icontains=value)
Expand Down
30 changes: 22 additions & 8 deletions src/netbox_contract/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ class ContractForm(NetBoxModelForm):
external_partie=DynamicModelChoiceField(
queryset=ServiceProvider.objects.all()
)
parent=DynamicModelChoiceField(
queryset=Contract.objects.all(),
required=False
)

class Meta:
model = Contract
fields = ('name', 'external_partie', 'external_reference', 'internal_partie','tenant', 'status',
'start_date', 'end_date','initial_term', 'renewal_term', 'currency','accounting_dimensions',
'mrc', 'nrc','invoice_frequency','circuit', 'documents', 'comments', 'tags')
'mrc', 'nrc','invoice_frequency','circuit','parent','documents', 'comments', 'tags')

widgets = {
'start_date': DatePicker(),
Expand All @@ -47,7 +51,7 @@ class Meta:

class ContractFilterSetForm(NetBoxModelFilterSetForm):
model = Contract
external_partie=DynamicModelMultipleChoiceField(
external_partie=DynamicModelChoiceField(
queryset=ServiceProvider.objects.all(),
required=False
)
Expand All @@ -65,6 +69,10 @@ class ContractFilterSetForm(NetBoxModelFilterSetForm):
queryset=Circuit.objects.all(),
required=False
)
parent=DynamicModelChoiceField(
queryset=Contract.objects.all(),
required=False
)

class InvoiceFilterSetForm(NetBoxModelFilterSetForm):
model = Invoice
Expand All @@ -77,25 +85,26 @@ class ContractCSVForm(NetBoxModelImportForm):
circuit = CSVModelChoiceField(
queryset=Circuit.objects.all(),
to_field_name='name',
help_text='Related Circuit'
help_text='Related Circuit',
required=False
)

class Meta:
model = Contract
fields = [
'name', 'external_partie', 'internal_partie','tenant', 'status',
'start_date', 'initial_term', 'renewal_term', 'mrc', 'nrc',
'invoice_frequency', 'circuit'
'invoice_frequency', 'parent','circuit'
]

class ContractBulkEditForm(NetBoxModelBulkEditForm):
name = forms.CharField(
max_length=100,
required=True
)
external_partie = forms.CharField(
max_length=30,
required=True
external_partie = DynamicModelChoiceField(
queryset=ServiceProvider.objects.all(),
required=False
)
external_reference=forms.CharField(
max_length=100,
Expand All @@ -107,7 +116,12 @@ class ContractBulkEditForm(NetBoxModelBulkEditForm):
)
comments = CommentField()
circuit=DynamicModelChoiceField(
queryset=Circuit.objects.all()
queryset=Circuit.objects.all(),
required=False
)
parent = DynamicModelChoiceField(
queryset=Contract.objects.all(),
required=False
)

nullable_fields = (
Expand Down
25 changes: 25 additions & 0 deletions src/netbox_contract/migrations/0016_contract_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.1.9 on 2023-06-18 14:45

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("netbox_contract", "0015_contractassignement"),
]

operations = [
migrations.AddField(
model_name="contract",
name="parent",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="child",
to="netbox_contract.contract",
),
),
]
7 changes: 7 additions & 0 deletions src/netbox_contract/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ class Contract(NetBoxModel):
comments = models.TextField(
blank=True
)
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
related_name='child',
null=True,
blank=True
)

def get_absolute_url(self):
return reverse('plugins:netbox_contract:contract', args=[self.pk])
Expand Down
7 changes: 5 additions & 2 deletions src/netbox_contract/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ class ContractListTable(NetBoxTable):
linkify=True
)
circuit = tables.ManyToManyColumn()
parent = tables.Column(
linkify=True
)

class Meta(NetBoxTable.Meta):
model = Contract
fields = ('pk', 'id', 'name', 'circuit', 'external_partie',
'external_reference','internal_partie', 'status', 'mrc',
'comments', 'actions')
default_columns = ('name', 'status', 'circuit')
'parent','comments', 'actions')
default_columns = ('name', 'status', 'parent','circuit')

class ContractListBottomTable(NetBoxTable):
name = tables.Column(
Expand Down
6 changes: 6 additions & 0 deletions src/netbox_contract/templates/netbox_contract/contract.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ <h5 class="card-header">Contract</h5>
<th scope="row">Invoice frequency</th>
<td>{{ object.invoice_frequency }}</td>
</tr>
<tr>
<th scope="row">Parent</th>
<td>
<a href="{{ object.parent.get_absolute_url }}">{{ object.parent.name }}</a>
</td>
</tr>
{% if object.documents %}
<tr>
<th scope="row">Documents</th>
Expand Down

0 comments on commit 1e9271a

Please sign in to comment.