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
|
|
|
|
2020-09-21 15:10:37 +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-22 17:26:40 +00:00
|
|
|
|
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
|
|
|
|
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):
|
2022-01-11 18:22:01 +00:00
|
|
|
def clean(self):
|
2022-03-19 02:11:58 +00:00
|
|
|
"""don't let readthroughs end before they start"""
|
2022-01-11 18:22:01 +00:00
|
|
|
cleaned_data = super().clean()
|
|
|
|
start_date = cleaned_data.get("start_date")
|
|
|
|
finish_date = cleaned_data.get("finish_date")
|
2022-01-18 03:46:48 +00:00
|
|
|
if start_date and finish_date and start_date > finish_date:
|
2022-01-11 18:22:01 +00:00
|
|
|
self.add_error(
|
|
|
|
"finish_date", _("Reading finish date cannot be before start date.")
|
|
|
|
)
|
2022-05-26 18:33:31 +00:00
|
|
|
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.")
|
|
|
|
)
|
2022-10-11 11:41:07 +00:00
|
|
|
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()
|
|
|
|
):
|
2022-10-11 11:41:07 +00:00
|
|
|
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()
|
|
|
|
):
|
2022-10-11 11:41:07 +00:00
|
|
|
self.add_error(
|
2022-10-11 12:05:20 +00:00
|
|
|
"finish_date", _("Reading finished date cannot be in the future.")
|
2022-10-11 11:41:07 +00:00
|
|
|
)
|
2022-01-11 18:22:01 +00:00
|
|
|
|
2022-01-11 17:50:04 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.ReadThrough
|
2022-05-26 18:33:31 +00:00
|
|
|
fields = ["user", "book", "start_date", "finish_date", "stopped_date"]
|