Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Nautobot v2 #47

Merged
merged 17 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% raw %}
{% extends 'generic/object_retrieve.html' %}
{% load helpers %}

{% block content_left_page %}
<div class="panel panel-default">
<div class="panel-heading">
<strong>{% endraw %}{{ cookiecutter.model_class_name }}{% raw %}</strong>
</div>
<table class="table table-hover panel-body attr-table">
<tr>
<td>Name</td>
<td>
<a href="{{ object.get_absolute_url }}">{{ object.name }}</a>
whitej6 marked this conversation as resolved.
Show resolved Hide resolved
</td>
</tr>
<tr>
<td>Description</td>
<td>
{{ object.description|placeholder }}
</td>
</tr>
</table>
</div>
{% endblock %}
{% endraw %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Views for FloorPlan."""
whitej6 marked this conversation as resolved.
Show resolved Hide resolved
from nautobot.apps.viewsets import NautobotUIViewSet

from {{ cookiecutter.plugin_name }} import filters, forms, models, tables
from {{ cookiecutter.plugin_name }}.api import serializers


class {{ cookiecutter.model_class_name}}UIViewSet(NautobotUIViewSet):
"""ViewSet for {{ cookiecutter.model_class_name}} views."""

bulk_update_form_class = forms.{{ cookiecutter.model_class_name}}BulkEditForm
filterset_class = filters.{{ cookiecutter.model_class_name}}FilterSet
filterset_form_class = forms.{{ cookiecutter.model_class_name}}FilterForm
form_class = forms.{{ cookiecutter.model_class_name}}Form
lookup_field = "pk"
queryset = models.{{ cookiecutter.model_class_name}}.objects.all()
serializer_class = serializers.{{ cookiecutter.model_class_name}}Serializer
table_class = tables.{{ cookiecutter.model_class_name}}Table

This file was deleted.

4 changes: 2 additions & 2 deletions nautobot-app/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"project_slug": "nautobot-plugin-{{cookiecutter.plugin_slug}}",
"repo_url": "https://github.com/{{cookiecutter.github_org}}/{{cookiecutter.project_slug}}",
"base_url": "{{cookiecutter.plugin_slug}}",
"min_nautobot_version": "1.6.0",
"max_nautobot_version": "1.9999",
"min_nautobot_version": "2.0.0",
"max_nautobot_version": "2.9999",
"camel_name": "{{cookiecutter.plugin_slug.title().replace(' ', '').replace('-', '')}}",
"project_short_description": "{{cookiecutter.verbose_name}}",
"model_class_name": "None",
Expand Down
8 changes: 3 additions & 5 deletions nautobot-app/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
if "{{ cookiecutter.model_class_name }}" == "None":
files_to_remove = [
"api/__init__.py",
"api/nested_serializers.py",
"api/serializers.py",
"api/urls.py",
"api/views.py",
Expand All @@ -35,15 +34,15 @@
"models.py",
"navigation.py",
"tables.py",
"templates/{{ cookiecutter.plugin_name }}/{{ cookiecutter.model_class_name.lower() }}.html",
"templates/{{ cookiecutter.plugin_name }}/{{ cookiecutter.model_class_name.lower() }}_retrieve.html",
"tests/fixtures.py",
"tests/test_api_views.py",
"tests/test_filter_{{ cookiecutter.model_class_name.lower() }}.py",
"tests/test_form_{{ cookiecutter.model_class_name.lower() }}.py",
"tests/test_model_{{ cookiecutter.model_class_name.lower() }}.py",
"tests/test_views.py",
"urls.py",
"views/{{ cookiecutter.model_class_name.lower() }}.py",
"views.py",
]
for file in files_to_remove:
(_ADDONS_PATH / file).unlink()
Expand All @@ -52,13 +51,12 @@
"migrations",
"templates/{{ cookiecutter.plugin_name }}",
"templates",
"views",
]
for folder in folders_to_remove:
(_ADDONS_PATH / folder).rmdir()

# Persist the baked cookie parameters in-repo for future usage as a replay file or for the drift management.
cookie = {{cookiecutter}}
cookie = {{ cookiecutter }}
(_PROJECT_PATH / ".cookiecutter.json").write_text(
json.dumps({"cookiecutter": cookie}, indent=4) + "\n", encoding="utf-8"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ services:
- "-c" # this is to evaluate the $NAUTOBOT_LOG_LEVEL from the env
- "nautobot-server celery beat -l $$NAUTOBOT_LOG_LEVEL" ## $$ because of docker-compose
depends_on:
- "nautobot"
nautobot:
condition: "service_healthy"
healthcheck:
disable: true
<<: *nautobot-base
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

__version__ = metadata.version(__name__)

from nautobot.extras.plugins import NautobotAppConfig
from nautobot.apps import NautobotAppConfig


class {{cookiecutter.camel_name}}Config(NautobotAppConfig):
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
"""API serializers for {{ cookiecutter.plugin_name }}."""
from rest_framework import serializers

from nautobot.core.api.serializers import ValidatedModelSerializer
from nautobot.apps.api import NautobotModelSerializer, TaggedModelSerializerMixin

from {{ cookiecutter.plugin_name }} import models

from . import nested_serializers # noqa: F401, pylint: disable=unused-import


class {{ cookiecutter.model_class_name }}Serializer(ValidatedModelSerializer):
class {{ cookiecutter.model_class_name }}Serializer(NautobotModelSerializer, TaggedModelSerializerMixin):
"""{{ cookiecutter.model_class_name }} Serializer."""

url = serializers.HyperlinkedIdentityField(view_name="plugins-api:{{ cookiecutter.plugin_name }}-api:{{ cookiecutter.model_class_name | lower }}-detail")

class Meta:
"""Meta attributes."""

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Django API urlpatterns declaration for {{ cookiecutter.plugin_name }} plugin."""

from nautobot.core.api import OrderedDefaultRouter
from nautobot.apps.api import OrderedDefaultRouter

from {{ cookiecutter.plugin_name }}.api import views

router = OrderedDefaultRouter()
# add the name of your api endpoint, usually hyphenated model name in plural, e.g. "my-model-classes"
router.register("{{ cookiecutter.model_class_name.lower() }}", views.{{ cookiecutter.model_class_name }}ViewSet)


app_name = "{{ cookiecutter.plugin_name }}-api"
urlpatterns = router.urls
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""API views for {{ cookiecutter.plugin_name }}."""

from nautobot.core.api.views import ModelViewSet
from nautobot.apps.api import NautobotModelViewSet

from {{cookiecutter.plugin_name }} import filters, models

from {{ cookiecutter.plugin_name }}.api import serializers


class {{ cookiecutter.model_class_name }}ViewSet(ModelViewSet): # pylint: disable=too-many-ancestors
class {{ cookiecutter.model_class_name }}ViewSet(NautobotModelViewSet): # pylint: disable=too-many-ancestors
"""{{ cookiecutter.model_class_name }} viewset."""

queryset = models.{{ cookiecutter.model_class_name }}.objects.all()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Filtering for {{ cookiecutter.plugin_name }}."""

from nautobot.utilities.filters import BaseFilterSet, NameSlugSearchFilterSet
from nautobot.apps.filters import NautobotFilterSet

from {{ cookiecutter.plugin_name }} import models


class {{ cookiecutter.model_class_name }}FilterSet(BaseFilterSet, NameSlugSearchFilterSet):
class {{ cookiecutter.model_class_name }}FilterSet(NautobotFilterSet):
"""Filter for {{ cookiecutter.model_class_name }}."""

class Meta:
Expand All @@ -14,4 +14,4 @@ class Meta:
model = models.{{ cookiecutter.model_class_name }}

# add any fields from the model that you would like to filter your searches by using those
fields = ["id", "name", "slug", "description"]
fields = ["id", "name", "description"]
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
"""Forms for {{ cookiecutter.plugin_name }}."""
from django import forms
from nautobot.utilities.forms import (
BootstrapMixin,
BulkEditForm,
SlugField,
)
from nautobot.apps.forms import NautobotBulkEditForm, NautobotFilterForm, NautobotModelForm, TagsBulkEditFormMixin

from {{ cookiecutter.plugin_name }} import models


class {{ cookiecutter.model_class_name }}Form(BootstrapMixin, forms.ModelForm):
class {{ cookiecutter.model_class_name }}Form(NautobotModelForm):
"""{{ cookiecutter.model_class_name }} creation/edit form."""

slug = SlugField()

class Meta:
"""Meta attributes."""

model = models.{{ cookiecutter.model_class_name }}
fields = [
"name",
"slug",
"description",
]


class {{ cookiecutter.model_class_name }}BulkEditForm(BootstrapMixin, BulkEditForm):
class {{ cookiecutter.model_class_name }}BulkEditForm(TagsBulkEditFormMixin, NautobotBulkEditForm):
"""{{ cookiecutter.model_class_name }} bulk edit form."""

pk = forms.ModelMultipleChoiceField(queryset=models.{{ cookiecutter.model_class_name }}.objects.all(), widget=forms.MultipleHiddenInput)
Expand All @@ -39,25 +32,15 @@ class Meta:
]


class {{ cookiecutter.model_class_name }}FilterForm(BootstrapMixin, forms.ModelForm):
class {{ cookiecutter.model_class_name }}FilterForm(NautobotFilterForm):
"""Filter form to filter searches."""

model = models.{{ cookiecutter.model_class_name }}
field_order = ["q", "name"]

q = forms.CharField(
required=False,
label="Search",
help_text="Search within Name or Slug.",
)
name = forms.CharField(required=False, label="Name")
slug = forms.CharField(required=False, label="Slug")

class Meta:
"""Meta attributes."""

model = models.{{ cookiecutter.model_class_name }}
# Define the fields above for ordering and widget purposes
fields = [
"q",
"name",
"slug",
"description",
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

# Django imports
from django.db import models
from django.urls import reverse

# Nautobot imports
from nautobot.core.models import BaseModel
from nautobot.extras.models.change_logging import ChangeLoggedModel
from nautobot.apps.models import PrimaryModel


# from nautobot.extras.utils import extras_features
Expand All @@ -17,11 +15,10 @@

# If you want to choose a specific model to overload in your class declaration, please reference the following documentation:
# how to chose a database model: https://docs.nautobot.com/projects/core/en/stable/plugins/development/#database-models
class {{ cookiecutter.model_class_name }}(BaseModel, ChangeLoggedModel):
class {{ cookiecutter.model_class_name }}(PrimaryModel):
"""Base model for {{ cookiecutter.verbose_name }} plugin."""

name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
description = models.CharField(max_length=200, blank=True)
# additional model fields

Expand All @@ -36,10 +33,6 @@ class Meta:
# Option for fixing plural name (i.e. "Chicken Tenders" vs "Chicken Tendies")
# verbose_name_plural = "{{ cookiecutter.verbose_name }}s"

def get_absolute_url(self):
"""Return detail view for {{ cookiecutter.model_class_name }}."""
return reverse("plugins:{{ cookiecutter.plugin_name }}:{{ cookiecutter.model_class_name.lower() }}", args=[self.slug])

def __str__(self):
"""Stringify instance."""
return self.name
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
"""Menu items."""

from nautobot.extras.plugins import PluginMenuButton, PluginMenuItem
from nautobot.apps.ui import NavMenuAddButton, NavMenuGroup, NavMenuItem, NavMenuTab
from nautobot.utilities.choices import ButtonColorChoices
whitej6 marked this conversation as resolved.
Show resolved Hide resolved

menu_items = (
PluginMenuItem(
items = (
NavMenuItem(
link="plugins:{{ cookiecutter.plugin_name }}:{{ cookiecutter.model_class_name.lower() }}_list",
link_text="{{ cookiecutter.verbose_name }}",
permissions=["{{ cookiecutter.plugin_name }}.view_{{ cookiecutter.model_class_name.lower() }}"],
buttons=(
PluginMenuButton(
NavMenuAddButton(
link="plugins:{{ cookiecutter.plugin_name }}:{{ cookiecutter.model_class_name.lower() }}_add",
title="Add",
icon_class="mdi mdi-plus-thick",
color=ButtonColorChoices.GREEN,
permissions=["{{ cookiecutter.plugin_name }}.add_{{ cookiecutter.model_class_name.lower() }}"],
),
),
),
)

menu_items = (
NavMenuTab(
name="Plugins",
groups=(NavMenuGroup(name="{{cookiecutter.verbose_name}}", items=tuple(items)),),
),
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tables for {{ cookiecutter.plugin_name }}."""

import django_tables2 as tables
from nautobot.utilities.tables import BaseTable, ButtonsColumn, ToggleColumn
from nautobot.apps.tables import BaseTable, ButtonsColumn, ToggleColumn

from {{ cookiecutter.plugin_name }} import models

Expand Down
Loading