1
0
Fork 0
bookwyrm/bookwyrm/forms.py

329 lines
8.1 KiB
Python
Raw Normal View History

2021-03-08 08:49:10 -08:00
""" using django model forms """
2020-06-03 17:38:30 +01:00
import datetime
2020-09-29 10:21:10 -07:00
from collections import defaultdict
2020-06-03 17:38:30 +01:00
2020-03-23 16:40:09 +00:00
from django import forms
2021-04-08 12:05:21 -04:00
from django.forms import ModelForm, PasswordInput, widgets, ChoiceField
2020-09-29 10:21:10 -07:00
from django.forms.widgets import Textarea
from django.utils import timezone
2021-04-04 16:22:36 -04:00
from django.utils.translation import gettext_lazy as _
2020-01-29 01:05:27 -08:00
from bookwyrm import models
2020-01-29 01:05:27 -08:00
2020-09-29 10:21:10 -07:00
class CustomForm(ModelForm):
2021-04-26 09:15:42 -07:00
"""add css classes to the forms"""
2021-03-08 08:49:10 -08:00
2020-09-29 10:21:10 -07:00
def __init__(self, *args, **kwargs):
2021-03-08 08:49:10 -08:00
css_classes = defaultdict(lambda: "")
css_classes["text"] = "input"
css_classes["password"] = "input"
css_classes["email"] = "input"
css_classes["number"] = "input"
css_classes["checkbox"] = "checkbox"
css_classes["textarea"] = "textarea"
2021-06-18 14:12:56 -07:00
# pylint: disable=super-with-arguments
2020-09-29 10:21:10 -07:00
super(CustomForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
2021-03-08 08:49:10 -08:00
if hasattr(visible.field.widget, "input_type"):
2020-09-29 10:21:10 -07:00
input_type = visible.field.widget.input_type
if isinstance(visible.field.widget, Textarea):
2021-03-08 08:49:10 -08:00
input_type = "textarea"
visible.field.widget.attrs["cols"] = None
visible.field.widget.attrs["rows"] = None
visible.field.widget.attrs["class"] = css_classes[input_type]
2020-09-29 10:21:10 -07:00
2020-12-12 18:13:00 -08:00
# pylint: disable=missing-class-docstring
2020-09-29 10:21:10 -07:00
class LoginForm(CustomForm):
2020-01-29 01:05:27 -08:00
class Meta:
model = models.User
2021-03-08 08:49:10 -08:00
fields = ["localname", "password"]
2020-01-29 01:05:27 -08:00
help_texts = {f: None for f in fields}
widgets = {
2021-03-08 08:49:10 -08:00
"password": PasswordInput(),
2020-01-29 01:05:27 -08:00
}
2020-09-29 10:21:10 -07:00
class RegisterForm(CustomForm):
2020-01-29 01:05:27 -08:00
class Meta:
model = models.User
2021-03-08 08:49:10 -08:00
fields = ["localname", "email", "password"]
2020-01-29 01:05:27 -08:00
help_texts = {f: None for f in fields}
2021-03-08 08:49:10 -08:00
widgets = {"password": PasswordInput()}
2020-01-29 01:05:27 -08:00
2020-09-29 10:21:10 -07:00
class RatingForm(CustomForm):
2020-04-03 12:43:49 -07:00
class Meta:
model = models.ReviewRating
2021-03-08 09:48:25 -08:00
fields = ["user", "book", "rating", "privacy"]
2020-04-03 12:43:49 -07:00
2020-09-29 10:21:10 -07:00
class ReviewForm(CustomForm):
2020-01-29 01:05:27 -08:00
class Meta:
model = models.Review
fields = [
2021-03-08 08:49:10 -08:00
"user",
"book",
"name",
"content",
"rating",
"content_warning",
"sensitive",
"privacy",
]
2020-01-29 01:05:27 -08:00
2020-09-29 10:21:10 -07:00
class CommentForm(CustomForm):
2020-03-21 16:50:49 -07:00
class Meta:
model = models.Comment
2021-03-20 17:39:05 -07:00
fields = [
"user",
"book",
"content",
"content_warning",
"sensitive",
"privacy",
"progress",
2021-03-20 18:03:20 -07:00
"progress_mode",
2021-08-16 13:32:20 -07:00
"reading_status",
2021-03-20 17:39:05 -07:00
]
2020-03-21 16:50:49 -07:00
2020-09-29 10:21:10 -07:00
class QuotationForm(CustomForm):
2020-04-08 09:40:47 -07:00
class Meta:
model = models.Quotation
fields = [
2021-03-08 08:49:10 -08:00
"user",
"book",
"quote",
"content",
"content_warning",
"sensitive",
"privacy",
2021-09-05 16:00:40 -07:00
"position",
"position_mode",
2021-03-08 08:49:10 -08:00
]
2020-04-08 09:40:47 -07:00
2020-09-29 10:21:10 -07:00
class ReplyForm(CustomForm):
2020-02-17 21:39:08 -08:00
class Meta:
model = models.Status
fields = [
2021-03-08 08:49:10 -08:00
"user",
"content",
"content_warning",
"sensitive",
"reply_parent",
"privacy",
]
2020-02-17 21:39:08 -08:00
2021-01-29 11:14:18 -08:00
class StatusForm(CustomForm):
class Meta:
model = models.Status
2021-03-08 08:49:10 -08:00
fields = ["user", "content", "content_warning", "sensitive", "privacy"]
2021-01-29 11:14:18 -08:00
2020-02-17 21:39:08 -08:00
2020-09-29 10:21:10 -07:00
class EditUserForm(CustomForm):
2020-01-29 01:05:27 -08:00
class Meta:
model = models.User
2021-03-18 09:06:00 -07:00
fields = [
"avatar",
"name",
"email",
"summary",
"show_goal",
"show_suggested_users",
2021-03-21 16:37:52 -07:00
"manually_approves_followers",
"default_post_privacy",
2021-03-21 16:37:52 -07:00
"discoverable",
"preferred_timezone",
2021-03-18 09:06:00 -07:00
]
2020-01-29 01:05:27 -08:00
help_texts = {f: None for f in fields}
2020-02-17 21:39:08 -08:00
2020-02-20 22:19:19 -08:00
2021-04-01 08:32:06 -07:00
class LimitedEditUserForm(CustomForm):
class Meta:
model = models.User
fields = [
"avatar",
"name",
"summary",
"manually_approves_followers",
"discoverable",
]
help_texts = {f: None for f in fields}
2021-04-19 18:12:55 -07:00
2021-06-14 10:44:25 -07:00
class DeleteUserForm(CustomForm):
class Meta:
model = models.User
fields = ["password"]
class UserGroupForm(CustomForm):
class Meta:
model = models.User
fields = ["groups"]
2021-04-01 08:32:06 -07:00
2020-09-29 10:21:10 -07:00
class CoverForm(CustomForm):
2020-03-28 15:06:16 -07:00
class Meta:
model = models.Book
2021-03-08 08:49:10 -08:00
fields = ["cover"]
2020-03-28 15:06:16 -07:00
help_texts = {f: None for f in fields}
2020-09-29 10:21:10 -07:00
class EditionForm(CustomForm):
2020-03-28 15:06:16 -07:00
class Meta:
2020-04-02 08:44:53 -07:00
model = models.Edition
2020-03-28 15:06:16 -07:00
exclude = [
2021-03-08 08:49:10 -08:00
"remote_id",
"origin_id",
"created_date",
"updated_date",
"edition_rank",
2021-03-08 10:07:02 -08:00
"authors",
2021-03-08 08:49:10 -08:00
"parent_work",
"shelves",
"connector",
2021-06-26 08:54:52 -07:00
"search_vector",
2020-03-28 15:06:16 -07:00
]
2021-03-08 08:49:10 -08:00
class AuthorForm(CustomForm):
class Meta:
model = models.Author
exclude = [
2021-03-08 08:49:10 -08:00
"remote_id",
"origin_id",
"created_date",
"updated_date",
2021-06-26 09:05:00 -07:00
"search_vector",
]
2020-03-28 15:06:16 -07:00
2020-03-23 16:40:09 +00:00
class ImportForm(forms.Form):
csv_file = forms.FileField()
2020-06-03 17:38:30 +01:00
2021-03-08 08:49:10 -08:00
2020-06-03 17:38:30 +01:00
class ExpiryWidget(widgets.Select):
def value_from_datadict(self, data, files, name):
2021-04-26 09:15:42 -07:00
"""human-readable exiration time buckets"""
2020-06-03 17:38:30 +01:00
selected_string = super().value_from_datadict(data, files, name)
2021-03-08 08:49:10 -08:00
if selected_string == "day":
2020-06-03 17:38:30 +01:00
interval = datetime.timedelta(days=1)
2021-03-08 08:49:10 -08:00
elif selected_string == "week":
2020-06-03 17:38:30 +01:00
interval = datetime.timedelta(days=7)
2021-03-08 08:49:10 -08:00
elif selected_string == "month":
interval = datetime.timedelta(days=31) # Close enough?
elif selected_string == "forever":
2020-06-03 17:38:30 +01:00
return None
else:
2021-03-08 08:49:10 -08:00
return selected_string # "This will raise
2020-06-03 17:38:30 +01:00
return timezone.now() + interval
2020-06-03 17:38:30 +01:00
2021-03-08 08:49:10 -08:00
2021-03-20 18:23:59 -07:00
class InviteRequestForm(CustomForm):
2021-03-20 19:14:41 -07:00
def clean(self):
2021-04-26 09:15:42 -07:00
"""make sure the email isn't in use by a registered user"""
2021-03-20 19:14:41 -07:00
cleaned_data = super().clean()
email = cleaned_data.get("email")
if email and models.User.objects.filter(email=email).exists():
self.add_error("email", _("A user with this email already exists."))
2021-03-20 18:23:59 -07:00
class Meta:
model = models.InviteRequest
fields = ["email"]
2020-09-29 10:21:10 -07:00
class CreateInviteForm(CustomForm):
2020-06-03 17:38:30 +01:00
class Meta:
model = models.SiteInvite
2021-04-05 10:17:01 -07:00
exclude = ["code", "user", "times_used", "invitees"]
2020-06-03 17:38:30 +01:00
widgets = {
2021-03-08 08:49:10 -08:00
"expiry": ExpiryWidget(
choices=[
("day", _("One Day")),
("week", _("One Week")),
("month", _("One Month")),
("forever", _("Does Not Expire")),
]
),
"use_limit": widgets.Select(
choices=[
(i, _("%(count)d uses" % {"count": i}))
for i in [1, 5, 10, 25, 50, 100]
]
+ [(None, _("Unlimited"))]
),
2020-06-03 17:38:30 +01:00
}
2020-11-10 14:52:04 -08:00
2021-03-08 08:49:10 -08:00
2020-11-10 14:52:04 -08:00
class ShelfForm(CustomForm):
class Meta:
model = models.Shelf
2021-03-08 08:49:10 -08:00
fields = ["user", "name", "privacy"]
2021-01-16 08:18:54 -08:00
2021-01-29 15:38:42 -08:00
2021-01-16 08:18:54 -08:00
class GoalForm(CustomForm):
class Meta:
model = models.AnnualGoal
2021-03-08 08:49:10 -08:00
fields = ["user", "year", "goal", "privacy"]
2021-01-29 15:38:42 -08:00
class SiteForm(CustomForm):
class Meta:
model = models.SiteSettings
exclude = []
2021-01-31 08:08:52 -08:00
2021-05-19 14:55:01 -07:00
class AnnouncementForm(CustomForm):
class Meta:
model = models.Announcement
exclude = ["remote_id"]
2021-01-31 08:08:52 -08:00
class ListForm(CustomForm):
class Meta:
model = models.List
2021-03-08 08:49:10 -08:00
fields = ["user", "name", "description", "curation", "privacy"]
2021-03-08 18:36:34 -08:00
class ReportForm(CustomForm):
class Meta:
model = models.Report
fields = ["user", "reporter", "statuses", "note"]
2021-04-07 11:52:13 -07:00
2021-09-08 15:08:22 -07:00
class EmailBlocklistForm(CustomForm):
class Meta:
model = models.EmailBlocklist
fields = ["domain"]
2021-04-07 11:52:13 -07:00
class ServerForm(CustomForm):
class Meta:
model = models.FederatedServer
2021-04-07 12:11:01 -07:00
exclude = ["remote_id"]
2021-04-08 12:05:21 -04:00
class SortListForm(forms.Form):
sort_by = ChoiceField(
choices=(
("order", _("List Order")),
("title", _("Book Title")),
("rating", _("Rating")),
),
label=_("Sort By"),
)
direction = ChoiceField(
choices=(
("ascending", _("Ascending")),
("descending", _("Descending")),
),
)