lib.itmens/books/forms.py

117 lines
3.2 KiB
Python
Raw Normal View History

2020-05-01 22:46:15 +08:00
from django import forms
from django.utils.translation import gettext_lazy as _
from .models import Book, BookMark, BookReview, BookMarkStatusTranslation
2020-05-05 23:50:48 +08:00
from common.models import MarkStatusEnum
2020-10-03 23:27:41 +02:00
from common.forms import *
2020-05-05 23:50:48 +08:00
def BookMarkStatusTranslator(status):
return BookMarkStatusTranslation[status]
2020-05-01 22:46:15 +08:00
class BookForm(forms.ModelForm):
pub_year = forms.IntegerField(required=False, max_value=9999, min_value=0, label=_("出版年份"))
pub_month = forms.IntegerField(required=False, max_value=12, min_value=1, label=_("出版月份"))
2020-05-05 23:50:48 +08:00
id = forms.IntegerField(required=False, widget=forms.HiddenInput())
2020-10-03 23:27:41 +02:00
other_info = JSONField(required=False, label=_("其他信息"))
2020-05-01 22:46:15 +08:00
class Meta:
model = Book
fields = [
2020-05-05 23:50:48 +08:00
'id',
2020-05-01 22:46:15 +08:00
'title',
'source_site',
'source_url',
2020-05-01 22:46:15 +08:00
'isbn',
'author',
'pub_house',
'subtitle',
'translator',
'orig_title',
'language',
'pub_month',
'pub_year',
'binding',
'price',
'pages',
'cover',
'brief',
'contents',
2020-05-01 22:46:15 +08:00
'other_info',
]
labels = {
'title': _("书名"),
'isbn': _("ISBN"),
'author': _("作者"),
'pub_house': _("出版社"),
'subtitle': _("副标题"),
'translator': _("译者"),
'orig_title': _("原作名"),
'language': _("语言"),
'pub_month': _("出版月份"),
'pub_year': _("出版年份"),
'binding': _("装帧"),
'price': _("定价"),
'pages': _("页数"),
'cover': _("封面"),
'brief': _("简介"),
'contents': _("目录"),
2020-05-01 22:46:15 +08:00
'other_info': _("其他信息"),
}
2020-07-03 15:36:23 +08:00
2020-05-01 22:46:15 +08:00
widgets = {
'author': forms.TextInput(attrs={'placeholder': _("多个作者使用英文逗号分隔")}),
'translator': forms.TextInput(attrs={'placeholder': _("多个译者使用英文逗号分隔")}),
2020-05-12 14:05:12 +08:00
# 'cover': forms.FileInput(),
2020-07-03 15:36:23 +08:00
'cover': PreviewImageInput(),
2020-05-01 22:46:15 +08:00
}
2020-05-06 16:42:18 +08:00
def clean_isbn(self):
isbn = self.cleaned_data.get('isbn')
if isbn:
isbn = isbn.strip()
return isbn
2020-05-01 22:46:15 +08:00
2020-07-10 21:28:09 +08:00
2021-02-12 19:23:23 +01:00
class BookMarkForm(MarkForm):
STATUS_CHOICES = [(v, BookMarkStatusTranslator(v))
for v in MarkStatusEnum.values]
2020-08-28 22:22:07 +08:00
2020-05-05 23:50:48 +08:00
status = forms.ChoiceField(
label=_(""),
widget=forms.RadioSelect(),
choices=STATUS_CHOICES
)
2020-08-28 22:22:07 +08:00
2020-05-01 22:46:15 +08:00
class Meta:
model = BookMark
fields = [
2020-05-05 23:50:48 +08:00
'id',
2020-05-01 22:46:15 +08:00
'book',
'status',
'rating',
'text',
2021-12-20 22:59:32 -05:00
'visibility',
]
2020-05-05 23:50:48 +08:00
widgets = {
2020-07-03 15:36:23 +08:00
'book': forms.TextInput(attrs={"hidden": ""}),
2020-05-05 23:50:48 +08:00
}
2020-05-01 22:46:15 +08:00
2021-02-12 19:23:23 +01:00
class BookReviewForm(ReviewForm):
2020-05-01 22:46:15 +08:00
class Meta:
model = BookReview
fields = [
2020-05-05 23:50:48 +08:00
'id',
2020-05-01 22:46:15 +08:00
'book',
'title',
'content',
2021-12-20 22:59:32 -05:00
'visibility'
2020-05-05 23:50:48 +08:00
]
widgets = {
2020-07-03 15:36:23 +08:00
'book': forms.TextInput(attrs={"hidden": ""}),
2020-05-05 23:50:48 +08:00
}