Files
fet2020/fet2020/posts/forms.py
2022-10-04 16:16:25 +00:00

198 lines
5.8 KiB
Python

from ckeditor_uploader.widgets import CKEditorUploadingWidget
from taggit.models import Tag
from django import forms
from django.forms.widgets import CheckboxInput
from django.utils import timezone
from django.utils.dates import MONTHS
from .models import Post, Event, News, FetMeeting
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = [
"title",
"subtitle",
"tags",
"image",
"body",
"slug",
"author",
"public_date",
]
widgets = {"body": CKEditorUploadingWidget(config_name="default")}
class Media:
js = (
"js/auto_slug.js", # automatic slag completion via ajax
"js/tag_completion.js", # to get a list for tag autocompletion via ajax
)
class NewsForm(PostForm):
class Meta:
model = News
fields = "__all__"
help_texts = {
"tags": (
"Die Hashtags ohne '#' eintragen, und mit Komma kann man mehrere Tags anfügen."
),
"image": "Verwendbare Formate: ...",
"is_pinned": (
"Dieser Post soll an die Startseite als erster Post angeheftet werden."
),
}
labels = {
"title": "Titel",
"subtitle": "Untertitel",
"image": "Hintergrundbild",
"body": "Text",
"slug": "Permalink",
"author": "Autor",
"public_date": "Veröffentlichung",
"is_pinned": "Post anheften",
}
widgets = {"body": CKEditorUploadingWidget(config_name="default")}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # to get the self.fields set
author_qs = self.fields["author"].queryset.order_by("username")
self.fields["author"].queryset = author_qs
class EventForm(PostForm):
class Meta:
model = Event
fields = "__all__"
help_texts = {
"tags": (
"Die Hashtags ohne '#' eintragen, und mit Komma kann man mehrere Tags anfügen."
),
"image": "Verwendbare Formate: Bildformate",
"is_pinned": (
"Dieses Event soll an die Startseite als erster Post angeheftet werden."
),
}
labels = {
"title": "Titel",
"subtitle": "Untertitel",
"image": "Hintergrundbild",
"body": "Text",
"event_start": "Start des Events",
"event_end": "Ende des Events",
"event_place": "Ort des Events",
"slug": "Permalink",
"author": "Autor",
"public_date": "Veröffentlichung",
"is_pinned": "Event anheften",
}
widgets = {"body": CKEditorUploadingWidget(config_name="default")}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # to get the self.fields set
author_qs = self.fields["author"].queryset.order_by("username")
self.fields["author"].queryset = author_qs
self.fields["event_start"].required = True
self.fields["event_end"].required = False
if "event_place" in self.fields:
self.fields["event_place"].required = True
class FetMeetingForm(PostForm):
class Meta:
model = FetMeeting
fields = ["event_start", "event_end", "event_place", "tags"]
labels = {
"event_start": "Start der Sitzung",
"event_end": "Ende der Sitzung",
"event_place": "Ort der Sitzung",
}
help_texts = {
"event_end": "Bei leeren Eingabe werden 2h zur Startzeit dazugezählt.",
"tags": (
"Die Hashtags ohne '#' eintragen, und mit Komma kann man mehrere Tags anfügen."
),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # to get the self.fields set
self.fields["event_start"].required = True
self.fields["event_end"].required = False
self.fields["event_place"].initial = "FET"
tags = []
tags.append(Tag())
tags[0].name = "fachschaft"
self.fields["tags"].initial = tags
class PostSearchForm(forms.Form):
year_choices = [("", "Alle")]
month_choices = [("", "Alle")] + list(MONTHS.items())
year = forms.ChoiceField(label="Jahr", choices=year_choices, required=False)
month = forms.ChoiceField(label="Monat", choices=month_choices, required=False)
compact_view = forms.BooleanField(
label="Kompakte Ansicht", required=False, widget=CheckboxInput
)
fet_meeting_only = forms.BooleanField(
label="nur FET Sitzungen", required=False, widget=CheckboxInput
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # to get the self.fields set
try:
first_post = Post.objects.get_queryset().last()
last_post = Post.objects.get_queryset().first()
if first_post and last_post:
years = range(last_post.date.year, first_post.date.year - 1, -1)
year_choices = [("", "Alle")] + [(i, i) for i in years]
self.fields["year"].choices = year_choices
except:
pass
class PostUpdateForm(forms.ModelForm):
class Meta:
model = Post
fields = [
"title",
"image",
"body",
"is_pinned",
"status",
]
labels = {
"title": "Titel",
"image": "Hintergrundbild",
"body": "Text",
"is_pinned": "Event anheften",
}
widgets = {"body": CKEditorUploadingWidget(config_name="default")}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # to get the self.fields set