Skip to content

Commit

Permalink
INIT
Browse files Browse the repository at this point in the history
  • Loading branch information
minitriga committed Mar 13, 2024
0 parents commit 6958f2c
Show file tree
Hide file tree
Showing 32 changed files with 6,480 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: pre-commit

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]
25 changes: 25 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Upload Python Package

on:
release:
types: [published]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

netbox_reorder_rack/static_dev/node_modules/
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.12.0
hooks:
- id: reorder-python-imports
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.1
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
recursive-include netbox_reorder_rack/static *
recursive-include netbox_reorder_rack/templates *
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Netbox Reorder Rack Plugin

![Version](https://img.shields.io/pypi/v/netbox-reorder-rack) ![Downloads](https://img.shields.io/pypi/dm/netbox-reorder-rack)

Allow the ability to reorder rack units in NetBox using a drag and drop interface.

![Reorder Rack](docs/img/netbox-reorder-rack.gif)

## Installation

**_NOTE:_** For docker please see: [Docker install](https://github.com/netbox-community/netbox-docker/wiki/Using-Netbox-Plugins)

The plugin is available as a Python package and can be installed with pip.

Run the following commands to install the required package after activating the virtual environment:

```shell
source /opt/netbox/venv/bin/activate
pip install netbox-reorder-rack
```

To ensure the plugin is automatically re-installed during future upgrades, create a file named `local_requirements.txt` (if it does not exist already) in the NetBox root directory (alongside `requirements.txt`) and list the `netbox-reorder-rack` package:

```shell
echo netbox-reorder-rack >> local_requirements.txt
```

Once installed, the plugin needs to be enabled in your `configuration.py`

```python
# In your configuration.py
PLUGINS = ["netbox_reorder_rack"]
```

You may need to collect static files and restart NetBox for the plugin to be properly installed.

```shell
python3 manage.py collectstatic --no-input
systemctl restart netbox
```


## Compatibility Matrix

| netbox version | plugin version |
| -------------- | ----------------------------- |
| >= 3.7.0 | >= v1.0.0 |

### Update

To update the plugin, you need to update the package and restart NetBox.

```shell
source /opt/netbox/venv/bin/activate
pip install netbox-reorder-rack
python3 manage.py collectstatic --no-input
systemctl restart netbox
```
27 changes: 27 additions & 0 deletions docs/img/development.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Development - Netbox Reorder Rack

## Installation
```shell
git clone ...
cd netbox-reorder-rack
source /opt/netbox/venv/bin/activate
pip install -e .
```

Edit `configuration.py` to include the plugin:
```python
PLUGINS = ["netbox_reorder_rack"]
```

Start the development server:
```shell
python3 manage.py runserver
```

## JavaScript Development
To edit javascript you must change files contained within static_dev and then run the following command to compile the javascript:
```shell
node bundle.js
```

## Build
Binary file added docs/img/netbox-reorder-rack.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions netbox_reorder_rack/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from extras.plugins import PluginConfig


class NetboxReorderRackConfig(PluginConfig):
name = "netbox_reorder_rack"
verbose_name = "NetBox Reorder Rack"
description = "NetBox plugin to reorder rack layouts."
version = "1.0.0"
base_url = "reorder"


config = NetboxReorderRackConfig
Empty file.
9 changes: 9 additions & 0 deletions netbox_reorder_rack/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from netbox.api.routers import NetBoxRouter

from netbox_reorder_rack.api import views

router = NetBoxRouter()

router.register("save", views.SaveViewSet, basename="reorder")

urlpatterns = router.urls
59 changes: 59 additions & 0 deletions netbox_reorder_rack/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import decimal

from dcim.models import Device
from dcim.models import Rack
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db import transaction
from django.shortcuts import get_object_or_404
from rest_framework import serializers
from rest_framework import status
from rest_framework import viewsets
from rest_framework.response import Response


class ReorderRackSerializer(serializers.Serializer):
rack_id = serializers.IntegerField()
front = serializers.ListField(child=serializers.JSONField())
rear = serializers.ListField(child=serializers.JSONField())
other = serializers.ListField(child=serializers.JSONField())


class SaveViewSet(PermissionRequiredMixin, viewsets.ViewSet):
permission_required = ["dcim.change_device", "dcim.view_device"]
serializer_class = ReorderRackSerializer
queryset = Device.objects.none()
schema = None

def update(self, request, pk):
rack = get_object_or_404(Rack, pk=pk)
try:
serializer = ReorderRackSerializer(request.data)
with transaction.atomic():
for device in rack.devices.all():
device.position = None
device.clean()
device.save()

for new in request.data["front"]:
device = rack.devices.filter(pk=new["id"]).first()
device.position = decimal.Decimal(new["y"])
device.face = new["face"]
device.clean()
device.save()

for new in request.data["rear"]:
device = rack.devices.filter(pk=new["id"]).first()
device.position = decimal.Decimal(new["y"])
device.face = new["face"]
device.clean()
device.save()

return Response(
{"message": "POST request received", "data": serializer.data},
status=status.HTTP_201_CREATED,
)
except Exception as e:
return Response(
{"message": "Error saving data", "error": str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
Loading

0 comments on commit 6958f2c

Please sign in to comment.