bookwyrm/bookwyrm/forms/forms.py

84 lines
2.5 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" using django model forms """
2022-10-12 09:25:03 +00:00
import datetime
2020-03-23 16:40:09 +00:00
from django import forms
2022-03-14 18:46:08 +00:00
from django.forms import widgets
2021-04-04 20:22:36 +00:00
from django.utils.translation import gettext_lazy as _
2020-01-29 09:05:27 +00:00
from bookwyrm import models
2021-11-24 10:59:45 +00:00
from bookwyrm.models.user import FeedFilterChoices
2022-03-14 18:46:08 +00:00
from .custom_form import CustomForm
2020-12-13 02:13:00 +00:00
# pylint: disable=missing-class-docstring
2021-11-22 17:52:57 +00:00
class FeedStatusTypesForm(CustomForm):
2021-11-21 23:25:47 +00:00
class Meta:
model = models.User
fields = ["feed_status_types"]
help_texts = {f: None for f in fields}
widgets = {
2021-11-22 17:52:57 +00:00
"feed_status_types": widgets.CheckboxSelectMultiple(
2021-11-24 10:59:45 +00:00
choices=FeedFilterChoices,
2021-11-21 23:25:47 +00:00
),
}
2020-03-23 16:40:09 +00:00
class ImportForm(forms.Form):
csv_file = forms.FileField()
2020-06-03 16:38:30 +00:00
2021-03-08 16:49:10 +00:00
class ImportUserForm(forms.Form):
archive_file = forms.FileField()
2020-11-10 22:52:04 +00:00
class ShelfForm(CustomForm):
class Meta:
model = models.Shelf
2021-09-28 23:36:47 +00:00
fields = ["user", "name", "privacy", "description"]
2021-01-16 16:18:54 +00:00
2021-01-29 23:38:42 +00:00
2021-01-16 16:18:54 +00:00
class GoalForm(CustomForm):
class Meta:
model = models.AnnualGoal
2021-03-08 16:49:10 +00:00
fields = ["user", "year", "goal", "privacy"]
2021-01-29 23:38:42 +00:00
2021-03-09 02:36:34 +00:00
class ReportForm(CustomForm):
class Meta:
model = models.Report
2022-02-24 20:48:52 +00:00
fields = ["user", "reporter", "status", "links", "note"]
2021-04-07 18:52:13 +00:00
2022-01-11 17:50:04 +00:00
class ReadThroughForm(CustomForm):
def clean(self):
2022-03-19 02:11:58 +00:00
"""don't let readthroughs end before they start"""
cleaned_data = super().clean()
start_date = cleaned_data.get("start_date")
finish_date = cleaned_data.get("finish_date")
if start_date and finish_date and start_date > finish_date:
self.add_error(
"finish_date", _("Reading finish date cannot be before start date.")
)
stopped_date = cleaned_data.get("stopped_date")
if start_date and stopped_date and start_date > stopped_date:
self.add_error(
"stopped_date", _("Reading stopped date cannot be before start date.")
)
current_time = datetime.datetime.now()
2022-10-11 12:07:17 +00:00
if (
stopped_date is not None
and current_time.timestamp() < stopped_date.timestamp()
):
self.add_error(
"stopped_date", _("Reading stopped date cannot be in the future.")
)
2022-10-11 12:07:17 +00:00
if (
finish_date is not None
and current_time.timestamp() < finish_date.timestamp()
):
self.add_error(
"finish_date", _("Reading finished date cannot be in the future.")
)
2022-01-11 17:50:04 +00:00
class Meta:
model = models.ReadThrough
fields = ["user", "book", "start_date", "finish_date", "stopped_date"]