ausglamr/blogs/forms.py

168 lines
4.1 KiB
Python
Raw Permalink Normal View History

2024-01-04 11:54:56 +11:00
"""forms for use in views"""
from django import forms
2024-03-23 16:23:41 +11:00
from django.core.exceptions import ValidationError
2024-01-04 11:54:56 +11:00
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
2024-01-04 11:54:56 +11:00
from .models import Blog, CallForPapers, Event, Group, Newsletter, Subscriber
class DateInput(forms.DateInput):
"""make date input a date picker"""
input_type = "date"
class RegisterBlogForm(forms.ModelForm):
"""form for registering a blog"""
class Meta:
"""set fields and model"""
model = Blog
2024-01-07 16:58:56 +11:00
fields = ["url", "category", "activitypub_account_name", "contact_email"]
2024-01-04 11:54:56 +11:00
class ConfirmBlogForm(forms.ModelForm):
"""confirm all details are correct before final blog submission"""
class Meta:
"""set fields and model"""
model = Blog
fields = [
"url",
"feed",
"title",
"author_name",
"description",
"category",
"activitypub_account_name",
2024-01-07 16:58:56 +11:00
"contact_email",
2024-01-04 11:54:56 +11:00
]
class RegisterConferenceForm(forms.ModelForm):
"""form for registering a event"""
class Meta:
"""set fields and model"""
model = Event
fields = [
"name",
"url",
"category",
"description",
"start_date",
"activitypub_account_name",
2024-01-07 16:58:56 +11:00
"contact_email",
2024-01-04 11:54:56 +11:00
]
widgets = {
"start_date": DateInput(),
}
class RegisterCallForPapersForm(forms.ModelForm):
"""form for registering a CallForPapers"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# we don't want every event ever created to be listed
self.fields["event"].queryset = Event.objects.filter(
approved=True, start_date__gte=timezone.now()
).order_by("start_date")
class Meta:
"""set fields and model"""
model = CallForPapers
fields = ["event", "name", "details", "opening_date", "closing_date"]
widgets = {
"opening_date": DateInput(),
"closing_date": DateInput(),
}
help_texts = {
"name": _("'Call for papers', 'Call for participation' etc"),
}
2024-01-04 11:54:56 +11:00
class RegisterGroupForm(forms.ModelForm):
"""form for registering a group"""
class Meta:
"""set fields and model"""
model = Group
fields = [
"name",
"category",
"type",
"url",
"registration_url",
"description",
2024-01-07 16:58:56 +11:00
"contact_email",
2024-01-04 11:54:56 +11:00
]
class RegisterNewsletterForm(forms.ModelForm):
"""form for registering a newsletter"""
class Meta:
"""set fields and model"""
model = Newsletter
fields = [
"name",
2024-01-26 11:00:26 +11:00
"author_name",
2024-01-04 11:54:56 +11:00
"category",
"url",
"feed",
2024-01-04 11:54:56 +11:00
"description",
"activitypub_account_name",
2024-01-07 16:58:56 +11:00
"contact_email",
2024-01-04 11:54:56 +11:00
]
help_texts = {
"feed": _(
"The Atom/RSS feed of your newsletter. If you include this, issues will be shown in AusGLAMR"
),
}
2024-01-04 11:54:56 +11:00
class ContactForm(forms.Form):
"""form for contacting site admin"""
from_email = forms.EmailField(label="Email", max_length=200)
subject = forms.CharField(label="Subject", max_length=200)
message = forms.CharField(widget=forms.Textarea)
2024-03-23 16:23:41 +11:00
bot_check = forms.CharField(
label="What is usually stored in a library?",
max_length=10,
2024-06-24 12:23:01 +10:00
help_text="Checking that you are human",
)
2024-03-23 16:23:41 +11:00
def clean_bot_check(self):
"""validate the bot check"""
data = self.cleaned_data["bot_check"]
if data != "books":
raise ValidationError("Try again. Think of something with pages.")
2024-01-04 11:54:56 +11:00
class SubscribeViaMastodon(forms.Form):
"""form for subscribing to Mastodon bot"""
username = forms.CharField(label="Username", max_length=200)
class SubscribeEmailForm(forms.ModelForm):
"""subscribe via email form"""
class Meta:
"""set fields and model"""
model = Subscriber
fields = ["email"]