Source code for documents.forms
"""The forms defined by the documents package."""
from django import forms
from django.contrib import admin
from django.forms import widgets
from django.utils import timezone
from documents import models
from utils.snippets import datetime_to_lectureyear
[docs]class DocumentFileInput(widgets.ClearableFileInput):
"""Wrapper around Django's :class:`~django.forms.widgets.ClearableFileInput`.
It overrides the URL of the associated file when it is fetched.
"""
template_name = "widgets/clearable_file_input.html"
[docs] def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
if hasattr(value, "url"):
doc = models.Document.objects.get(file=value)
context["document_id"] = doc.pk
context["language"] = "en"
return context
[docs]class MinutesForm(forms.ModelForm):
"""Form that overrides the widgets for the files."""
[docs] class Meta:
model = models.Minutes
fields = (
"file",
"members_only",
)
widgets = {
"file": DocumentFileInput,
}
[docs]class AnnualDocumentForm(forms.ModelForm):
"""Form that provides custom functionality for annual documents."""
[docs] class Meta:
model = models.AnnualDocument
fields = "__all__"
widgets = {
"year": forms.Select,
"file": DocumentFileInput,
}
@staticmethod
def _current_year():
"""Get the current lecture year."""
return datetime_to_lectureyear(timezone.now())
@staticmethod
def _year_choices():
"""Get the lecture years."""
current = datetime_to_lectureyear(timezone.now())
return [
(year, "{}-{}".format(year, year + 1))
for year in range(current + 1, 1989, -1)
]
[docs]class AssociationDocumentForm(forms.ModelForm):
"""Form that overrides the widgets for the files."""
[docs] class Meta:
model = models.AssociationDocument
fields = (
"name",
"file",
"members_only",
)
widgets = {
"file": DocumentFileInput,
}
[docs]class EventDocumentForm(forms.ModelForm):
"""Form that overrides the widgets for the files."""
[docs] class Meta:
model = models.EventDocument
fields = (
"name",
"file",
"members_only",
"owner",
)
widgets = {
"file": DocumentFileInput,
}