2020-01-29 09:05:27 +00:00
|
|
|
''' usin django model forms '''
|
2020-06-03 16:38:30 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.forms import ModelForm, PasswordInput, widgets
|
2020-03-23 16:40:09 +00:00
|
|
|
from django import forms
|
2020-01-29 09:05:27 +00:00
|
|
|
|
|
|
|
from fedireads import models
|
|
|
|
|
|
|
|
|
|
|
|
class LoginForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.User
|
|
|
|
fields = ['username', 'password']
|
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
widgets = {
|
|
|
|
'password': PasswordInput(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class RegisterForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.User
|
|
|
|
fields = ['username', 'email', 'password']
|
|
|
|
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-04-03 19:43:49 +00:00
|
|
|
class RatingForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Review
|
|
|
|
fields = ['rating']
|
|
|
|
|
|
|
|
|
2020-01-29 09:05:27 +00:00
|
|
|
class ReviewForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Review
|
2020-04-03 23:19:11 +00:00
|
|
|
fields = ['name', 'content']
|
2020-01-29 09:05:27 +00:00
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
labels = {
|
|
|
|
'name': 'Title',
|
2020-03-15 21:15:36 +00:00
|
|
|
'content': 'Review',
|
2020-01-29 09:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-18 05:39:08 +00:00
|
|
|
class CommentForm(ModelForm):
|
2020-03-21 23:50:49 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Comment
|
2020-04-01 18:22:22 +00:00
|
|
|
fields = ['content']
|
2020-03-21 23:50:49 +00:00
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
labels = {
|
|
|
|
'content': 'Comment',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-08 16:40:47 +00:00
|
|
|
class QuotationForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Quotation
|
|
|
|
fields = ['quote', 'content']
|
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
labels = {
|
|
|
|
'quote': 'Quote',
|
|
|
|
'content': 'Comment',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-21 23:50:49 +00:00
|
|
|
class ReplyForm(ModelForm):
|
2020-02-18 05:39:08 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Status
|
|
|
|
fields = ['content']
|
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
labels = {'content': 'Comment'}
|
|
|
|
|
|
|
|
|
2020-01-29 09:05:27 +00:00
|
|
|
class EditUserForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.User
|
2020-03-09 20:11:44 +00:00
|
|
|
fields = ['avatar', 'name', 'summary', 'manually_approves_followers']
|
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-02-21 02:01:50 +00:00
|
|
|
class TagForm(ModelForm):
|
|
|
|
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-03-28 22:06:16 +00:00
|
|
|
class CoverForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Book
|
|
|
|
fields = ['cover']
|
|
|
|
help_texts = {f: None for f in fields}
|
|
|
|
|
|
|
|
|
2020-04-02 15:44:53 +00:00
|
|
|
class EditionForm(ModelForm):
|
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 = [
|
|
|
|
'created_date',
|
|
|
|
'updated_date',
|
|
|
|
'last_sync_date',
|
|
|
|
|
2020-05-09 21:26:27 +00:00
|
|
|
'authors',# TODO
|
2020-03-28 22:06:16 +00:00
|
|
|
'parent_work',
|
|
|
|
'shelves',
|
|
|
|
'misc_identifiers',
|
|
|
|
|
2020-04-02 15:44:53 +00:00
|
|
|
'subjects',# TODO
|
|
|
|
'subject_places',# TODO
|
2020-03-28 22:06:16 +00:00
|
|
|
|
|
|
|
'connector',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
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
|
|
|
|
|
|
|
|
return datetime.datetime.now() + interval
|
|
|
|
|
|
|
|
class CreateInviteForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.SiteInvite
|
|
|
|
exclude = ['code', 'user', 'times_used']
|
|
|
|
widgets = {
|
|
|
|
'expiry': ExpiryWidget(choices=[
|
|
|
|
('day', 'One Day'),
|
|
|
|
('week', 'One Week'),
|
|
|
|
('month', 'One Month'),
|
|
|
|
('forever', 'Does Not Expire')]),
|
|
|
|
'use_limit': widgets.Select(
|
|
|
|
choices=[(i, "%d uses" % (i,)) for i in [1, 5, 10, 25, 50, 100]]
|
|
|
|
+ [(None, 'Unlimited')])
|
|
|
|
}
|