from django import forms from ..models import GeschichteBild, GeschichteSeite class GeschichteSeiteForm(forms.ModelForm): """Form for creating and editing history pages""" class Meta: from ..models import GeschichteSeite model = GeschichteSeite fields = ['titel', 'slug', 'inhalt', 'ist_veroeffentlicht', 'sortierung'] widgets = { 'titel': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'z.B. Gründung der Stiftung' }), 'slug': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'z.B. gruendung-der-stiftung' }), 'inhalt': forms.Textarea(attrs={ 'class': 'form-control rich-text-editor', 'rows': 20, 'placeholder': 'Schreiben Sie hier den Inhalt der Geschichtsseite...' }), 'ist_veroeffentlicht': forms.CheckboxInput(attrs={ 'class': 'form-check-input' }), 'sortierung': forms.NumberInput(attrs={ 'class': 'form-control', 'min': 0 }) } help_texts = { 'slug': 'URL-freundliche Version des Titels (nur Buchstaben, Zahlen und Bindestriche)', 'inhalt': 'Unterstützt Rich-Text-Formatierung, Bilder und Videos', 'sortierung': 'Niedrigere Zahlen erscheinen zuerst in der Navigation' } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Auto-generate slug from title if not provided if not self.instance.pk: self.fields['slug'].required = False def clean_slug(self): slug = self.cleaned_data.get('slug') titel = self.cleaned_data.get('titel', '') if not slug and titel: # Auto-generate slug from title from django.utils.text import slugify slug = slugify(titel) if not slug: raise forms.ValidationError('Slug ist erforderlich. Bitte geben Sie einen Titel ein.') return slug def clean(self): cleaned_data = super().clean() titel = cleaned_data.get('titel', '') slug = cleaned_data.get('slug', '') # Auto-generate slug if empty if titel and not slug: from django.utils.text import slugify cleaned_data['slug'] = slugify(titel) return cleaned_data class GeschichteBildForm(forms.ModelForm): """Form for uploading images to history pages""" class Meta: from ..models import GeschichteBild model = GeschichteBild fields = ['titel', 'bild', 'beschreibung', 'alt_text', 'sortierung'] widgets = { 'titel': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'z.B. Gründungsurkunde 1895' }), 'bild': forms.ClearableFileInput(attrs={ 'class': 'form-control' }), 'beschreibung': forms.Textarea(attrs={ 'class': 'form-control', 'rows': 3, 'placeholder': 'Beschreibung des Bildes...' }), 'alt_text': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Alternativtext für Bildschirmleser' }), 'sortierung': forms.NumberInput(attrs={ 'class': 'form-control', 'min': 0 }) } help_texts = { 'bild': 'Unterstützte Formate: JPG, PNG, GIF (max. 10MB)', 'alt_text': 'Wichtig für Barrierefreiheit', 'sortierung': 'Reihenfolge in der Bildergalerie' }