2020-09-21 17:25:26 +00:00
|
|
|
''' using django model forms '''
|
2020-06-03 16:38:30 +00:00
|
|
|
import datetime
|
2020-09-29 17:21:10 +00:00
|
|
|
from collections import defaultdict
|
2020-06-03 16:38:30 +00:00
|
|
|
|
2020-03-23 16:40:09 +00:00
|
|
|
from django import forms
|
2020-09-29 17:21:10 +00:00
|
|
|
from django.forms import ModelForm, PasswordInput, widgets
|
|
|
|
from django.forms.widgets import Textarea
|
2020-11-28 00:24:53 +00:00
|
|
|
from django.utils import timezone
|
2021-03-02 17:55:28 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2020-01-29 09:05:27 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
2020-01-29 09:05:27 +00:00
|
|
|
|
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class CustomForm(ModelForm):
|
|
|
|
''' add css classes to the forms '''
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
css_classes = defaultdict(lambda: '')
|
|
|
|
css_classes['text'] = 'input'
|
|
|
|
css_classes['password'] = 'input'
|
|
|
|
css_classes['email'] = 'input'
|
2020-09-30 03:36:43 +00:00
|
|
|
css_classes['number'] = 'input'
|
2020-09-29 17:21:10 +00:00
|
|
|
css_classes['checkbox'] = 'checkbox'
|
|
|
|
css_classes['textarea'] = 'textarea'
|
|
|
|
super(CustomForm, self).__init__(*args, **kwargs)
|
|
|
|
for visible in self.visible_fields():
|
|
|
|
if hasattr(visible.field.widget, 'input_type'):
|
|
|
|
input_type = visible.field.widget.input_type
|
|
|
|
if isinstance(visible.field.widget, Textarea):
|
|
|
|
input_type = 'textarea'
|
2020-10-02 21:42:42 +00:00
|
|
|
visible.field.widget.attrs['cols'] = None
|
|
|
|
visible.field.widget.attrs['rows'] = None
|
2020-09-29 17:21:10 +00:00
|
|
|
visible.field.widget.attrs['class'] = css_classes[input_type]
|
|
|
|
|
2020-12-22 17:26:40 +00:00
|
|
|
|
2020-12-13 02:13:00 +00:00
|
|
|
# pylint: disable=missing-class-docstring
|
2020-09-29 17:21:10 +00:00
|
|
|
class LoginForm(CustomForm):
|
2020-01-29 09:05:27 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.User
|
2021-01-04 18:48:48 +00:00
|
|
|
fields = ['localname', 'password']
|
2020-01-29 09:05:27 +00:00
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
widgets = {
|
|
|
|
'password': PasswordInput(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class RegisterForm(CustomForm):
|
2020-01-29 09:05:27 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.User
|
2020-12-24 19:39:27 +00:00
|
|
|
fields = ['localname', 'email', 'password']
|
2020-01-29 09:05:27 +00:00
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
widgets = {
|
2020-06-01 21:34:45 +00:00
|
|
|
'password': PasswordInput()
|
2020-01-29 09:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class RatingForm(CustomForm):
|
2020-04-03 19:43:49 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Review
|
2020-10-27 18:32:15 +00:00
|
|
|
fields = ['user', 'book', 'content', 'rating', 'privacy']
|
2020-04-03 19:43:49 +00:00
|
|
|
|
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class ReviewForm(CustomForm):
|
2020-01-29 09:05:27 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Review
|
2020-12-17 04:10:50 +00:00
|
|
|
fields = [
|
2020-12-17 19:32:09 +00:00
|
|
|
'user', 'book',
|
|
|
|
'name', 'content', 'rating',
|
|
|
|
'content_warning', 'sensitive',
|
2020-12-17 04:10:50 +00:00
|
|
|
'privacy']
|
2020-01-29 09:05:27 +00:00
|
|
|
|
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class CommentForm(CustomForm):
|
2020-03-21 23:50:49 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Comment
|
2020-12-17 19:32:09 +00:00
|
|
|
fields = [
|
|
|
|
'user', 'book', 'content',
|
|
|
|
'content_warning', 'sensitive',
|
|
|
|
'privacy']
|
2020-03-21 23:50:49 +00:00
|
|
|
|
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class QuotationForm(CustomForm):
|
2020-04-08 16:40:47 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Quotation
|
2020-12-17 04:10:50 +00:00
|
|
|
fields = [
|
2020-12-17 19:32:09 +00:00
|
|
|
'user', 'book', 'quote', 'content',
|
|
|
|
'content_warning', 'sensitive', 'privacy']
|
2020-04-08 16:40:47 +00:00
|
|
|
|
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class ReplyForm(CustomForm):
|
2020-02-18 05:39:08 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Status
|
2020-12-17 04:10:50 +00:00
|
|
|
fields = [
|
2020-12-17 19:32:09 +00:00
|
|
|
'user', 'content', 'content_warning', 'sensitive',
|
|
|
|
'reply_parent', 'privacy']
|
2020-02-18 05:39:08 +00:00
|
|
|
|
2021-01-29 19:14:18 +00:00
|
|
|
class StatusForm(CustomForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Status
|
|
|
|
fields = [
|
|
|
|
'user', 'content', 'content_warning', 'sensitive', 'privacy']
|
|
|
|
|
2020-02-18 05:39:08 +00:00
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class EditUserForm(CustomForm):
|
2020-01-29 09:05:27 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.User
|
2020-10-02 21:42:42 +00:00
|
|
|
fields = [
|
2020-10-05 21:48:26 +00:00
|
|
|
'avatar', 'name', 'email', 'summary', 'manually_approves_followers'
|
2020-10-02 21:42:42 +00:00
|
|
|
]
|
2020-01-29 09:05:27 +00:00
|
|
|
help_texts = {f: None for f in fields}
|
2020-02-18 05:39:08 +00:00
|
|
|
|
2020-02-21 06:19:19 +00:00
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class TagForm(CustomForm):
|
2020-02-21 02:01:50 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Tag
|
|
|
|
fields = ['name']
|
|
|
|
help_texts = {f: None for f in fields}
|
2020-02-21 06:19:19 +00:00
|
|
|
labels = {'name': 'Add a tag'}
|
2020-02-21 02:01:50 +00:00
|
|
|
|
2020-03-23 16:40:09 +00:00
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class CoverForm(CustomForm):
|
2020-03-28 22:06:16 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Book
|
|
|
|
fields = ['cover']
|
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
|
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class EditionForm(CustomForm):
|
2020-03-28 22:06:16 +00:00
|
|
|
class Meta:
|
2020-04-02 15:44:53 +00:00
|
|
|
model = models.Edition
|
2020-03-28 22:06:16 +00:00
|
|
|
exclude = [
|
2020-09-30 03:36:43 +00:00
|
|
|
'remote_id',
|
2020-12-22 17:26:40 +00:00
|
|
|
'origin_id',
|
2020-03-28 22:06:16 +00:00
|
|
|
'created_date',
|
|
|
|
'updated_date',
|
2021-01-12 04:21:32 +00:00
|
|
|
'edition_rank',
|
2020-03-28 22:06:16 +00:00
|
|
|
|
2021-03-08 17:28:22 +00:00
|
|
|
'authors',
|
2020-03-28 22:06:16 +00:00
|
|
|
'parent_work',
|
|
|
|
'shelves',
|
|
|
|
|
2020-04-02 15:44:53 +00:00
|
|
|
'subjects',# TODO
|
|
|
|
'subject_places',# TODO
|
2020-03-28 22:06:16 +00:00
|
|
|
|
|
|
|
'connector',
|
|
|
|
]
|
|
|
|
|
2020-12-22 17:26:40 +00:00
|
|
|
class AuthorForm(CustomForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Author
|
|
|
|
exclude = [
|
|
|
|
'remote_id',
|
|
|
|
'origin_id',
|
|
|
|
'created_date',
|
|
|
|
'updated_date',
|
|
|
|
]
|
|
|
|
|
2020-03-28 22:06:16 +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
|
|
|
|
|
|
|
class ExpiryWidget(widgets.Select):
|
|
|
|
def value_from_datadict(self, data, files, name):
|
2020-12-13 02:13:00 +00:00
|
|
|
''' human-readable exiration time buckets '''
|
2020-06-03 16:38:30 +00:00
|
|
|
selected_string = super().value_from_datadict(data, files, name)
|
|
|
|
|
|
|
|
if selected_string == 'day':
|
|
|
|
interval = datetime.timedelta(days=1)
|
|
|
|
elif selected_string == 'week':
|
|
|
|
interval = datetime.timedelta(days=7)
|
|
|
|
elif selected_string == 'month':
|
|
|
|
interval = datetime.timedelta(days=31) # Close enough?
|
|
|
|
elif selected_string == 'forever':
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return selected_string # "This will raise
|
|
|
|
|
2020-11-28 00:24:53 +00:00
|
|
|
return timezone.now() + interval
|
2020-06-03 16:38:30 +00:00
|
|
|
|
2020-09-29 17:21:10 +00:00
|
|
|
class CreateInviteForm(CustomForm):
|
2020-06-03 16:38:30 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.SiteInvite
|
|
|
|
exclude = ['code', 'user', 'times_used']
|
|
|
|
widgets = {
|
|
|
|
'expiry': ExpiryWidget(choices=[
|
2021-03-02 17:55:28 +00:00
|
|
|
('day', _('One Day')),
|
|
|
|
('week', _('One Week')),
|
|
|
|
('month', _('One Month')),
|
|
|
|
('forever', _('Does Not Expire'))]),
|
2020-06-03 16:38:30 +00:00
|
|
|
'use_limit': widgets.Select(
|
2021-03-02 17:55:28 +00:00
|
|
|
choices=[(i, _("%(count)d uses" % {'count': i})) \
|
|
|
|
for i in [1, 5, 10, 25, 50, 100]]
|
|
|
|
+ [(None, _('Unlimited'))])
|
2020-06-03 16:38:30 +00:00
|
|
|
}
|
2020-11-10 22:52:04 +00:00
|
|
|
|
|
|
|
class ShelfForm(CustomForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Shelf
|
|
|
|
fields = ['user', 'name', 'privacy']
|
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-01-16 19:34:19 +00:00
|
|
|
fields = ['user', 'year', 'goal', 'privacy']
|
2021-01-29 23:38:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SiteForm(CustomForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.SiteSettings
|
|
|
|
exclude = []
|
2021-01-31 16:08:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ListForm(CustomForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.List
|
|
|
|
fields = ['user', 'name', 'description', 'curation', 'privacy']
|