From 806706ca1d56e7a6b02a7962bd3f65db8cb5b28b Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Wed, 15 Dec 2021 16:31:06 -0500 Subject: [PATCH] Refresh development documentation --- docs/development/adding-models.md | 14 ++++++-------- docs/development/extending-models.md | 15 +++++++-------- docs/development/getting-started.md | 27 +++++++++++++++++++-------- docs/development/index.md | 13 ++++++++----- docs/development/models.md | 5 +++-- docs/development/style-guide.md | 6 +++--- 6 files changed, 46 insertions(+), 34 deletions(-) diff --git a/docs/development/adding-models.md b/docs/development/adding-models.md index 6b778d886..156a8ba97 100644 --- a/docs/development/adding-models.md +++ b/docs/development/adding-models.md @@ -6,9 +6,9 @@ Models within each app are stored in either `models.py` or within a submodule un Each model should define, at a minimum: +* A `Meta` class specifying a deterministic ordering (if ordered by fields other than the primary ID) * A `__str__()` method returning a user-friendly string representation of the instance * A `get_absolute_url()` method returning an instance's direct URL (using `reverse()`) -* A `Meta` class specifying a deterministic ordering (if ordered by fields other than the primary ID) ## 2. Define field choices @@ -16,9 +16,9 @@ If the model has one or more fields with static choices, define those choices in ## 3. Generate database migrations -Once your model definition is complete, generate database migrations by running `manage.py -n $NAME --no-header`. Always specify a short unique name when generating migrations. +Once your model definition is complete, generate database migrations by running `manage.py makemigrations -n $NAME --no-header`. Always specify a short unique name when generating migrations. -!!! info +!!! info "Configuration Required" Set `DEVELOPER = True` in your NetBox configuration to enable the creation of new migrations. ## 4. Add all standard views @@ -41,9 +41,7 @@ Add the relevant URL path for each view created in the previous step to `urls.py Each model should have a corresponding FilterSet class defined. This is used to filter UI and API queries. Subclass the appropriate class from `netbox.filtersets` that matches the model's parent class. -Every model FilterSet should define a `q` filter to support general search queries. - -## 7. Create the table +## 7. Create the table class Create a table class for the model in `tables.py` by subclassing `utilities.tables.BaseTable`. Under the table's `Meta` class, be sure to list both the fields and default columns. @@ -53,7 +51,7 @@ Create the HTML template for the object view. (The other views each typically em ## 9. Add the model to the navigation menu -For NetBox releases prior to v3.0, add the relevant link(s) to the navigation menu template. For later releases, add the relevant items in `netbox/netbox/navigation_menu.py`. +Add the relevant navigation menu items in `netbox/netbox/navigation_menu.py`. ## 10. REST API components @@ -64,7 +62,7 @@ Create the following for each model: * API view in `api/views.py` * Endpoint route in `api/urls.py` -## 11. GraphQL API components (v3.0+) +## 11. GraphQL API components Create a Graphene object type for the model in `graphql/types.py` by subclassing the appropriate class from `netbox.graphql.types`. diff --git a/docs/development/extending-models.md b/docs/development/extending-models.md index 99c448c06..ad8fe5024 100644 --- a/docs/development/extending-models.md +++ b/docs/development/extending-models.md @@ -4,16 +4,16 @@ Below is a list of tasks to consider when adding a new field to a core model. ## 1. Generate and run database migrations -Django migrations are used to express changes to the database schema. In most cases, Django can generate these automatically, however very complex changes may require manual intervention. Always remember to specify a short but descriptive name when generating a new migration. +[Django migrations](https://docs.djangoproject.com/en/stable/topics/migrations/) are used to express changes to the database schema. In most cases, Django can generate these automatically, however very complex changes may require manual intervention. Always remember to specify a short but descriptive name when generating a new migration. ``` ./manage.py makemigrations -n ./manage.py migrate ``` -Where possible, try to merge related changes into a single migration. For example, if three new fields are being added to different models within an app, these can be expressed in the same migration. You can merge a new migration with an existing one by combining their `operations` lists. +Where possible, try to merge related changes into a single migration. For example, if three new fields are being added to different models within an app, these can be expressed in a single migration. You can merge a newly generated migration with an existing one by combining their `operations` lists. -!!! note +!!! warning "Do not alter existing migrations" Migrations can only be merged within a release. Once a new release has been published, its migrations cannot be altered (other than for the purpose of correcting a bug). ## 2. Add validation logic to `clean()` @@ -24,7 +24,6 @@ If the new field introduces additional validation requirements (beyond what's in class Foo(models.Model): def clean(self): - super().clean() # Custom validation goes here @@ -40,9 +39,9 @@ If you're adding a relational field (e.g. `ForeignKey`) and intend to include th Extend the model's API serializer in `.api.serializers` to include the new field. In most cases, it will not be necessary to also extend the nested serializer, which produces a minimal representation of the model. -## 5. Add field to forms +## 5. Add fields to forms -Extend any forms to include the new field as appropriate. Common forms include: +Extend any forms to include the new field(s) as appropriate. These are found under the `forms/` directory within each app. Common forms include: * **Credit/edit** - Manipulating a single object * **Bulk edit** - Performing a change on many objects at once @@ -51,11 +50,11 @@ Extend any forms to include the new field as appropriate. Common forms include: ## 6. Extend object filter set -If the new field should be filterable, add it to the `FilterSet` for the model. If the field should be searchable, remember to reference it in the FilterSet's `search()` method. +If the new field should be filterable, add it to the `FilterSet` for the model. If the field should be searchable, remember to query it in the FilterSet's `search()` method. ## 7. Add column to object table -If the new field will be included in the object list view, add a column to the model's table. For simple fields, adding the field name to `Meta.fields` will be sufficient. More complex fields may require declaring a custom column. +If the new field will be included in the object list view, add a column to the model's table. For simple fields, adding the field name to `Meta.fields` will be sufficient. More complex fields may require declaring a custom column. Also add the field name to `default_columns` if the column should be present in the table by default. ## 8. Update the UI templates diff --git a/docs/development/getting-started.md b/docs/development/getting-started.md index beeae2ffb..acf13b82f 100644 --- a/docs/development/getting-started.md +++ b/docs/development/getting-started.md @@ -35,6 +35,8 @@ The NetBox project utilizes three persistent git branches to track work: Typically, you'll base pull requests off of the `develop` branch, or off of `feature` if you're working on a new major release. **Never** merge pull requests into the `master` branch, which receives merged only from the `develop` branch. +For example, assume that the current NetBox release is v3.1.1. Work applied to the `develop` branch will appear in v3.1.2, and work done under the `feature` branch will be included in the next minor release (v3.2.0). + ### Enable Pre-Commit Hooks NetBox ships with a [git pre-commit hook](https://githooks.com/) script that automatically checks for style compliance and missing database migrations prior to committing changes. This helps avoid erroneous commits that result in CI test failures. You are encouraged to enable it by creating a link to `scripts/git-hooks/pre-commit`: @@ -46,7 +48,7 @@ $ ln -s ../../scripts/git-hooks/pre-commit ### Create a Python Virtual Environment -A [virtual environment](https://docs.python.org/3/tutorial/venv.html) is like a container for a set of Python packages. They allow you to build environments suited to specific projects without interfering with system packages or other projects. When installed per the documentation, NetBox uses a virtual environment in production. +A [virtual environment](https://docs.python.org/3/tutorial/venv.html) (or "venv" for short) is like a container for a set of Python packages. These allow you to build environments suited to specific projects without interfering with system packages or other projects. When installed per the documentation, NetBox uses a virtual environment in production. Create a virtual environment using the `venv` Python module: @@ -57,8 +59,8 @@ $ python3 -m venv ~/.venv/netbox This will create a directory named `.venv/netbox/` in your home directory, which houses a virtual copy of the Python executable and its related libraries and tooling. When running NetBox for development, it will be run using the Python binary at `~/.venv/netbox/bin/python`. -!!! info - Keeping virtual environments in `~/.venv/` is a common convention but entirely optional: Virtual environments can be created wherever you please. +!!! info "Where to Create Your Virtual Environments" + Keeping virtual environments in `~/.venv/` is a common convention but entirely optional: Virtual environments can be created almost wherever you please. Once created, activate the virtual environment: @@ -94,7 +96,7 @@ Within the `netbox/netbox/` directory, copy `configuration.example.py` to `confi ### Start the Development Server -Django provides a lightweight, auto-updating HTTP/WSGI server for development use. NetBox extends this slightly to automatically import models and other utilities. Run the NetBox development server with the `nbshell` management command: +Django provides a lightweight, auto-updating HTTP/WSGI server for development use. It is started with the `runserver` management command: ```no-highlight $ python netbox/manage.py runserver @@ -109,9 +111,12 @@ Quit the server with CONTROL-C. This ensures that your development environment is now complete and operational. Any changes you make to the code base will be automatically adapted by the development server. +!!! info "IDE Integration" + Some IDEs, such as PyCharm, will integrate with Django's development server and allow you to run it directly within the IDE. This is strongly encouraged as it makes for a much more convenient development environment. + ## Running Tests -Throughout the course of development, it's a good idea to occasionally run NetBox's test suite to catch any potential errors. Tests are run using the `test` management command: +Prior to committing any substantial changes to the code base, be sure to run NetBox's test suite to catch any potential errors. Tests are run using the `test` management command. Remember to ensure the Python virtual environment is active before running this command. ```no-highlight $ python netbox/manage.py test @@ -123,9 +128,15 @@ In cases where you haven't made any changes to the database (which is most of th $ python netbox/manage.py test --keepdb ``` +You can also limit the command to running only a specific subset of tests. For example, to run only IPAM and DCIM view tests: + +```no-highlight +$ python netbox/manage.py test dcim.tests.test_views ipam.tests.test_views +``` + ## Submitting Pull Requests -Once you're happy with your work and have verified that all tests pass, commit your changes and push it upstream to your fork. Always provide descriptive (but not excessively verbose) commit messages. When working on a specific issue, be sure to reference it. +Once you're happy with your work and have verified that all tests pass, commit your changes and push it upstream to your fork. Always provide descriptive (but not excessively verbose) commit messages. When working on a specific issue, be sure to prefix your commit message with the word "Fixes" or "Closes" and the issue number (with a hash mark). This tells GitHub to automatically close the referenced issue once the commit has been merged. ```no-highlight $ git commit -m "Closes #1234: Add IPv5 support" @@ -136,5 +147,5 @@ Once your fork has the new commit, submit a [pull request](https://github.com/ne Once submitted, a maintainer will review your pull request and either merge it or request changes. If changes are needed, you can make them via new commits to your fork: The pull request will update automatically. -!!! note - Remember, pull requests are entertained only for **accepted** issues. If an issue you want to work on hasn't been approved by a maintainer yet, it's best to avoid risking your time and effort on a change that might not be accepted. +!!! note "Remember to Open an Issue First" + Remember, pull requests are permitted only for **accepted** issues. If an issue you want to work on hasn't been approved by a maintainer yet, it's best to avoid risking your time and effort on a change that might not be accepted. (The one exception to this is trivial changes to the documentation or other non-critical resources.) diff --git a/docs/development/index.md b/docs/development/index.md index c10c752d5..03e2cc0c3 100644 --- a/docs/development/index.md +++ b/docs/development/index.md @@ -1,25 +1,25 @@ # NetBox Development -NetBox is maintained as a [GitHub project](https://github.com/netbox-community/netbox) under the Apache 2 license. Users are encouraged to submit GitHub issues for feature requests and bug reports, however we are very selective about pull requests. Please see the `CONTRIBUTING` guide for more direction on contributing to NetBox. +NetBox is maintained as a [GitHub project](https://github.com/netbox-community/netbox) under the Apache 2 license. Users are encouraged to submit GitHub issues for feature requests and bug reports, however we are very selective about pull requests. Each pull request must be preceded by an **approved** issue. Please see the `CONTRIBUTING` guide for more direction on contributing to NetBox. ## Communication There are several official forums for communication among the developers and community members: -* [GitHub issues](https://github.com/netbox-community/netbox/issues) - All feature requests, bug reports, and other substantial changes to the code base **must** be documented in an issue. +* [GitHub issues](https://github.com/netbox-community/netbox/issues) - All feature requests, bug reports, and other substantial changes to the code base **must** be documented in a GitHub issue. * [GitHub Discussions](https://github.com/netbox-community/netbox/discussions) - The preferred forum for general discussion and support issues. Ideal for shaping a feature request prior to submitting an issue. * [#netbox on NetDev Community Slack](https://netdev.chat/) - Good for quick chats. Avoid any discussion that might need to be referenced later on, as the chat history is not retained long. * [Google Group](https://groups.google.com/g/netbox-discuss) - Legacy mailing list; slowly being phased out in favor of GitHub discussions. ## Governance -NetBox follows the [benevolent dictator](http://oss-watch.ac.uk/resources/benevolentdictatorgovernancemodel) model of governance, with [Jeremy Stretch](https://github.com/jeremystretch) ultimately responsible for all changes to the code base. While community contributions are welcomed and encouraged, the lead maintainer's primary role is to ensure the project's long-term maintainability and continued focus on its primary functions (in other words, avoid scope creep). +NetBox follows the [benevolent dictator](http://oss-watch.ac.uk/resources/benevolentdictatorgovernancemodel) model of governance, with [Jeremy Stretch](https://github.com/jeremystretch) ultimately responsible for all changes to the code base. While community contributions are welcomed and encouraged, the lead maintainer's primary role is to ensure the project's long-term maintainability and continued focus on its primary functions. ## Project Structure -All development of the current NetBox release occurs in the `develop` branch; releases are packaged from the `master` branch. The `master` branch should _always_ represent the current stable release in its entirety, such that installing NetBox by either downloading a packaged release or cloning the `master` branch provides the same code base. +All development of the current NetBox release occurs in the `develop` branch; releases are packaged from the `master` branch. The `master` branch should _always_ represent the current stable release in its entirety, such that installing NetBox by either downloading a packaged release or cloning the `master` branch provides the same code base. Only pull requests representing new releases should be merged into `master`. -NetBox components are arranged into functional subsections called _apps_ (a carryover from Django vernacular). Each app holds the models, views, and templates relevant to a particular function: +NetBox components are arranged into Django apps. Each app holds the models, views, and other resources relevant to a particular function: * `circuits`: Communications circuits and providers (not to be confused with power circuits) * `dcim`: Datacenter infrastructure management (sites, racks, and devices) @@ -29,3 +29,6 @@ NetBox components are arranged into functional subsections called _apps_ (a carr * `users`: Authentication and user preferences * `utilities`: Resources which are not user-facing (extendable classes, etc.) * `virtualization`: Virtual machines and clusters +* `wireless`: Wireless links and LANs + +All core functionality is stored within the `netbox/` subdirectory. HTML templates are stored in a common `templates/` directory, with model- and view-specific templates arranged by app. Documentation is kept in the `docs/` root directory. diff --git a/docs/development/models.md b/docs/development/models.md index 62dd016f3..ae1bab7e7 100644 --- a/docs/development/models.md +++ b/docs/development/models.md @@ -17,12 +17,12 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/ * Nesting - These models can be nested recursively to create a hierarchy | Type | Change Logging | Webhooks | Custom Fields | Export Templates | Tags | Journaling | Nesting | -| ------------------ | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | +| ------------------ | ---------------- | ---------------- |------------------| ---------------- | ---------------- | ---------------- | ---------------- | | Primary | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | | | Organizational | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | | | | Nested Group | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | | :material-check: | | Component | :material-check: | :material-check: | :material-check: | :material-check: | :material-check: | | | -| Component Template | :material-check: | :material-check: | :material-check: | | | | | +| Component Template | :material-check: | :material-check: | | | | | | ## Models Index @@ -44,6 +44,7 @@ The Django [content types](https://docs.djangoproject.com/en/stable/ref/contrib/ * [ipam.ASN](../models/ipam/asn.md) * [ipam.FHRPGroup](../models/ipam/fhrpgroup.md) * [ipam.IPAddress](../models/ipam/ipaddress.md) +* [ipam.IPRange](../models/ipam/iprange.md) * [ipam.Prefix](../models/ipam/prefix.md) * [ipam.RouteTarget](../models/ipam/routetarget.md) * [ipam.Service](../models/ipam/service.md) diff --git a/docs/development/style-guide.md b/docs/development/style-guide.md index 6081867f0..2a6d86ab0 100644 --- a/docs/development/style-guide.md +++ b/docs/development/style-guide.md @@ -1,6 +1,6 @@ # Style Guide -NetBox generally follows the [Django style guide](https://docs.djangoproject.com/en/stable/internals/contributing/writing-code/coding-style/), which is itself based on [PEP 8](https://www.python.org/dev/peps/pep-0008/). [Pycodestyle](https://github.com/pycqa/pycodestyle) is used to validate code formatting, ignoring certain violations. See `scripts/cibuild.sh`. +NetBox generally follows the [Django style guide](https://docs.djangoproject.com/en/stable/internals/contributing/writing-code/coding-style/), which is itself based on [PEP 8](https://www.python.org/dev/peps/pep-0008/). [Pycodestyle](https://github.com/pycqa/pycodestyle) is used to validate code formatting, ignoring certain violations. See `scripts/cibuild.sh` for details. ## PEP 8 Exceptions @@ -30,7 +30,7 @@ pycodestyle --ignore=W504,E501 netbox/ ## Introducing New Dependencies -The introduction of a new dependency is best avoided unless it is absolutely necessary. For small features, it's generally preferable to replicate functionality within the NetBox code base rather than to introduce reliance on an external project. This reduces both the burden of tracking new releases and our exposure to outside bugs and attacks. +The introduction of a new dependency is best avoided unless it is absolutely necessary. For small features, it's generally preferable to replicate functionality within the NetBox code base rather than to introduce reliance on an external project. This reduces both the burden of tracking new releases and our exposure to outside bugs and supply chain attacks. If there's a strong case for introducing a new dependency, it must meet the following criteria: @@ -43,7 +43,7 @@ When adding a new dependency, a short description of the package and the URL of ## General Guidance -* When in doubt, remain consistent: It is better to be consistently incorrect than inconsistently correct. If you notice in the course of unrelated work a pattern that should be corrected, continue to follow the pattern for now and open a bug so that the entire code base can be evaluated at a later point. +* When in doubt, remain consistent: It is better to be consistently incorrect than inconsistently correct. If you notice in the course of unrelated work a pattern that should be corrected, continue to follow the pattern for now and submit a separate bug report so that the entire code base can be evaluated at a later point. * Prioritize readability over concision. Python is a very flexible language that typically offers several options for expressing a given piece of logic, but some may be more friendly to the reader than others. (List comprehensions are particularly vulnerable to over-optimization.) Always remain considerate of the future reader who may need to interpret your code without the benefit of the context within which you are writing it.