-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements views for: - design - design instance - journal - journal entry
- Loading branch information
1 parent
24a473a
commit 2b4bb07
Showing
22 changed files
with
1,722 additions
and
848 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Nested serializers for design builder.""" | ||
from nautobot.core.api import BaseModelSerializer | ||
from rest_framework.relations import HyperlinkedIdentityField | ||
|
||
from design_builder.models import Design, DesignInstance, Journal | ||
|
||
|
||
class NestedDesignSerializer(BaseModelSerializer): | ||
"""Nested serializer for the design model.""" | ||
|
||
url = HyperlinkedIdentityField(view_name="plugins-api:design_builder-api:design-detail") | ||
|
||
class Meta: | ||
"""Nested serializer options for the design model.""" | ||
|
||
model = Design | ||
fields = ["id", "url", "name"] | ||
|
||
|
||
class NestedDesignInstanceSerializer(BaseModelSerializer): | ||
"""Nested serializer for the design instance model.""" | ||
|
||
url = HyperlinkedIdentityField(view_name="plugins-api:design_builder-api:designinstance-detail") | ||
|
||
class Meta: | ||
"""Nested serializer options for the design instance model.""" | ||
|
||
model = DesignInstance | ||
fields = ["id", "url", "name"] | ||
|
||
|
||
class NestedJournalSerializer(BaseModelSerializer): | ||
"""Nested serializer for the journal model.""" | ||
|
||
url = HyperlinkedIdentityField(view_name="plugins-api:design_builder-api:journal-detail") | ||
|
||
class Meta: | ||
"""Nested serializer options for the journal model.""" | ||
|
||
model = Journal | ||
fields = ["id", "url"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
"""Serializers for design builder.""" | ||
from django.contrib.contenttypes.models import ContentType | ||
from drf_spectacular.utils import extend_schema_field | ||
from nautobot.apps.api import NautobotModelSerializer, TaggedModelSerializerMixin | ||
from nautobot.core.api import ContentTypeField | ||
from nautobot.extras.api.nested_serializers import NestedJobResultSerializer | ||
from nautobot.utilities.api import get_serializer_for_model | ||
from rest_framework.fields import SerializerMethodField, DictField | ||
from rest_framework.relations import HyperlinkedIdentityField | ||
|
||
from design_builder.models import Design, DesignInstance, Journal, JournalEntry | ||
|
||
from design_builder.api.nested_serializers import ( | ||
NestedDesignSerializer, | ||
NestedDesignInstanceSerializer, | ||
NestedJournalSerializer, | ||
) | ||
|
||
|
||
class DesignSerializer(NautobotModelSerializer, TaggedModelSerializerMixin): | ||
"""Serializer for the design model.""" | ||
|
||
url = HyperlinkedIdentityField(view_name="plugins-api:design_builder-api:design-detail") | ||
|
||
class Meta: | ||
"""Serializer options for the design model.""" | ||
|
||
model = Design | ||
fields = [ | ||
"id", | ||
"url", | ||
"name", | ||
] | ||
|
||
|
||
class DesignInstanceSerializer(NautobotModelSerializer, TaggedModelSerializerMixin): | ||
"""Serializer for the design instance model.""" | ||
|
||
url = HyperlinkedIdentityField(view_name="plugins-api:design_builder-api:design-detail") | ||
design = NestedDesignSerializer() | ||
|
||
class Meta: | ||
"""Serializer options for the design model.""" | ||
|
||
model = DesignInstance | ||
fields = [ | ||
"id", | ||
"url", | ||
"design", | ||
"name", | ||
"owner", | ||
"first_implemented", | ||
"last_implemented", | ||
] | ||
|
||
|
||
class JournalSerializer(NautobotModelSerializer, TaggedModelSerializerMixin): | ||
"""Serializer for the journal model.""" | ||
|
||
url = HyperlinkedIdentityField(view_name="plugins-api:design_builder-api:journal-detail") | ||
design_instance = NestedDesignInstanceSerializer() | ||
job_result = NestedJobResultSerializer() | ||
|
||
class Meta: | ||
"""Serializer options for the journal model.""" | ||
|
||
model = Journal | ||
fields = ["id", "url", "design_instance", "job_result"] | ||
|
||
|
||
class JournalEntrySerializer(NautobotModelSerializer, TaggedModelSerializerMixin): | ||
"""Serializer for the journal entry model.""" | ||
|
||
url = HyperlinkedIdentityField(view_name="plugins-api:design_builder-api:journalentry-detail") | ||
journal = NestedJournalSerializer() | ||
_design_object_type = ContentTypeField(queryset=ContentType.objects.all(), label="design_object_type") | ||
design_object = SerializerMethodField(read_only=True) | ||
|
||
class Meta: | ||
"""Serializer options for the journal entry model.""" | ||
|
||
model = JournalEntry | ||
fields = ["id", "url", "journal", "_design_object_type", "design_object", "changes", "full_control"] | ||
|
||
@extend_schema_field(DictField()) | ||
def get_design_object(self, obj): | ||
serializer = get_serializer_for_model(obj.design_object, prefix="Nested") | ||
context = {"request": self.context["request"]} | ||
return serializer(obj.design_object, context=context).data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""API URLs for design builder.""" | ||
from nautobot.core.api import OrderedDefaultRouter | ||
from design_builder.api.views import ( | ||
DesignAPIViewSet, | ||
DesignInstanceAPIViewSet, | ||
JournalAPIViewSet, | ||
JournalEntryAPIViewSet, | ||
) | ||
|
||
router = OrderedDefaultRouter() | ||
|
||
router.register("designs", DesignAPIViewSet) | ||
router.register("design-instances", DesignInstanceAPIViewSet) | ||
router.register("journals", JournalAPIViewSet) | ||
router.register("journal-entries", JournalEntryAPIViewSet) | ||
|
||
urlpatterns = router.urls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""UI Views for design builder.""" | ||
from nautobot.extras.api.views import NautobotModelViewSet | ||
|
||
from design_builder.api.serializers import ( | ||
DesignSerializer, | ||
DesignInstanceSerializer, | ||
JournalSerializer, | ||
JournalEntrySerializer, | ||
) | ||
from design_builder.filters import DesignFilterSet, DesignInstanceFilterSet, JournalFilterSet, JournalEntryFilterSet | ||
from design_builder.models import Design, DesignInstance, Journal, JournalEntry | ||
|
||
|
||
class DesignAPIViewSet(NautobotModelViewSet): | ||
"""API views for the design model.""" | ||
|
||
queryset = Design.objects.all() | ||
serializer_class = DesignSerializer | ||
filterset_class = DesignFilterSet | ||
|
||
|
||
class DesignInstanceAPIViewSet(NautobotModelViewSet): | ||
"""API views for the design instance model.""" | ||
|
||
queryset = DesignInstance.objects.all() | ||
serializer_class = DesignInstanceSerializer | ||
filterset_class = DesignInstanceFilterSet | ||
|
||
|
||
class JournalAPIViewSet(NautobotModelViewSet): | ||
"""API views for the journal model.""" | ||
|
||
queryset = Journal.objects.all() | ||
serializer_class = JournalSerializer | ||
filterset_class = JournalFilterSet | ||
|
||
|
||
class JournalEntryAPIViewSet(NautobotModelViewSet): | ||
"""API views for the journal entry model.""" | ||
|
||
queryset = JournalEntry.objects.all() | ||
serializer_class = JournalEntrySerializer | ||
filterset_class = JournalEntryFilterSet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Filters for the design builder app.""" | ||
from nautobot.apps.filters import NautobotFilterSet, NaturalKeyOrPKMultipleChoiceFilter | ||
from nautobot.extras.models import Job, JobResult | ||
|
||
from design_builder.models import Design, DesignInstance, Journal, JournalEntry | ||
|
||
|
||
class DesignFilterSet(NautobotFilterSet): | ||
"""Filter set for the design model.""" | ||
|
||
job = NaturalKeyOrPKMultipleChoiceFilter( | ||
queryset=Job.objects.all(), | ||
label="Job (ID or slug)", | ||
) | ||
|
||
class Meta: | ||
"""Meta attributes for filter.""" | ||
|
||
model = Design | ||
fields = ["id", "job"] | ||
|
||
|
||
class DesignInstanceFilterSet(NautobotFilterSet): | ||
"""Filter set for the design instance model.""" | ||
|
||
design = NaturalKeyOrPKMultipleChoiceFilter( | ||
queryset=Design.objects.all(), | ||
label="Design (ID or slug)", | ||
) | ||
|
||
class Meta: | ||
"""Meta attributes for filter.""" | ||
|
||
model = DesignInstance | ||
fields = ["id", "design", "name", "owner", "first_implemented", "last_implemented"] | ||
|
||
|
||
class JournalFilterSet(NautobotFilterSet): | ||
"""Filter set for the journal model.""" | ||
|
||
design_instance = NaturalKeyOrPKMultipleChoiceFilter( | ||
queryset=DesignInstance.objects.all(), | ||
label="Design Instance (ID)", | ||
) | ||
|
||
job_result = NaturalKeyOrPKMultipleChoiceFilter( | ||
queryset=JobResult.objects.all(), | ||
label="Job Result (ID)", | ||
) | ||
|
||
class Meta: | ||
"""Meta attributes for filter.""" | ||
|
||
model = Journal | ||
fields = ["id", "design_instance", "job_result"] | ||
|
||
|
||
class JournalEntryFilterSet(NautobotFilterSet): | ||
"""Filter set for the journal entrymodel.""" | ||
|
||
journal = NaturalKeyOrPKMultipleChoiceFilter( | ||
queryset=Journal.objects.all(), | ||
label="Journal (ID)", | ||
) | ||
|
||
class Meta: | ||
"""Meta attributes for filter.""" | ||
|
||
model = JournalEntry | ||
# TODO: Support design_object somehow? | ||
fields = ["id", "journal", "changes", "full_control"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Forms for the design builder app.""" | ||
from django.forms import NullBooleanField | ||
from nautobot.extras.forms import NautobotFilterForm | ||
from nautobot.extras.models import Job, JobResult | ||
from nautobot.utilities.forms import TagFilterField, DynamicModelChoiceField, StaticSelect2, BOOLEAN_WITH_BLANK_CHOICES | ||
|
||
from design_builder.models import Design, DesignInstance, Journal, JournalEntry | ||
|
||
|
||
class DesignFilterForm(NautobotFilterForm): | ||
"""Filter form for the design model.""" | ||
model = Design | ||
|
||
job = DynamicModelChoiceField(queryset=Job.objects.all()) | ||
tag = TagFilterField(model) | ||
|
||
|
||
class DesignInstanceFilterForm(NautobotFilterForm): | ||
"""Filter form for the design instance model.""" | ||
model = DesignInstance | ||
|
||
design = DynamicModelChoiceField(queryset=Design.objects.all()) | ||
tag = TagFilterField(model) | ||
|
||
|
||
class JournalFilterForm(NautobotFilterForm): | ||
"""Filter form for the journal model.""" | ||
model = Journal | ||
|
||
design_instance = DynamicModelChoiceField(queryset=DesignInstance.objects.all()) | ||
job_result = DynamicModelChoiceField(queryset=JobResult.objects.all()) | ||
tag = TagFilterField(model) | ||
|
||
|
||
class JournalEntryFilterForm(NautobotFilterForm): | ||
"""Filter form for the journal entry model.""" | ||
model = JournalEntry | ||
|
||
journal = DynamicModelChoiceField(queryset=Journal.objects.all()) | ||
full_control = NullBooleanField( | ||
required=False, | ||
label="Does the design have full control over the object?", | ||
widget=StaticSelect2(choices=BOOLEAN_WITH_BLANK_CHOICES), | ||
) | ||
tag = TagFilterField(model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.