Files
Netbox-Documentation/netbox_documentation/forms.py
T
MrBlake ab0ed26ad3 feat: integriertes Dokumentations-Wiki für NetBox hinzufügen
- Markdown-Dokumentationen direkt in NetBox erstellen
- Dokumente Standorten, Racks, Geräten, VMs und Clustern zuordnen
- DOCX-, XLSX-, PDF-, Markdown- und Textimporte unterstützen
- REST-API, Suche, Berechtigungen und Änderungsprotokoll ergänzen
- Installation, Konfiguration und Importgrenzen dokumentieren
2026-07-22 10:23:58 +02:00

77 lines
3.4 KiB
Python

from django import forms
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from utilities.forms.fields import SlugField
from netbox.forms import NetBoxModelForm
from .models import Document, DocumentAssignment
def allowed_content_types():
labels = settings.PLUGINS_CONFIG.get("netbox_documentation", {}).get(
"allowed_object_types", []
)
pairs = [value.split(".", 1) for value in labels if "." in value]
query = Q()
for app_label, model in pairs:
query |= Q(app_label=app_label, model=model)
return ContentType.objects.filter(query).order_by("app_label", "model")
class DocumentForm(NetBoxModelForm):
slug = SlugField(slug_source="title")
body = forms.CharField(required=False, widget=forms.Textarea(attrs={
"rows": 28, "class": "font-monospace", "data-markdown-editor": "true"
}), help_text="Markdown wird unterstützt. HTML wird bei der Ausgabe sicher gefiltert.")
class Meta:
model = Document
fields = ("title", "slug", "summary", "body", "is_published", "tags")
class AssignmentForm(NetBoxModelForm):
assigned_object_type = forms.ModelChoiceField(queryset=ContentType.objects.none(), label="Objekttyp")
assigned_object_id = forms.TypedChoiceField(coerce=int, label="NetBox-Objekt")
class Meta:
model = DocumentAssignment
fields = ("document", "assigned_object_type", "assigned_object_id", "note", "tags")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["assigned_object_type"].queryset = allowed_content_types()
content_type = None
ct_id = (self.data.get("assigned_object_type") or self.initial.get("assigned_object_type")
or getattr(self.instance, "assigned_object_type_id", None))
if ct_id:
content_type = ContentType.objects.filter(pk=ct_id).first()
if content_type:
model = content_type.model_class()
objects = model.objects.all().order_by("pk")
self.fields["assigned_object_id"].choices = [(obj.pk, str(obj)) for obj in objects]
def clean(self):
cleaned = super().clean()
object_id = cleaned.get("assigned_object_id")
ct = cleaned.get("assigned_object_type")
if object_id and ct and not ct.model_class().objects.filter(pk=object_id).exists():
self.add_error("assigned_object_id", "Das Objekt existiert für diesen Objekttyp nicht.")
return cleaned
class ImportForm(forms.Form):
file = forms.FileField(label="Word-, Excel- oder PDF-Datei")
title = forms.CharField(max_length=200, required=False, help_text="Leer lassen, um den Dateinamen zu verwenden")
append = forms.BooleanField(required=False, initial=False, label="An bestehende Dokumentation anhängen")
document = forms.ModelChoiceField(queryset=Document.objects.all(), required=False, label="Bestehende Dokumentation")
def clean(self):
data = super().clean()
if data.get("append") and not data.get("document"):
self.add_error("document", "Zum Anhängen muss eine Dokumentation gewählt werden.")
upload = data.get("file")
limit = settings.PLUGINS_CONFIG.get("netbox_documentation", {}).get("max_import_size_mb", 25)
if upload and upload.size > limit * 1024 * 1024:
self.add_error("file", f"Die Datei ist größer als {limit} MB.")
return data