-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
168 lines (122 loc) · 4.35 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from django import forms
from django.forms import SelectMultiple
from django.template.loader import render_to_string
from django.utils.text import slugify
from django_summernote.widgets import SummernoteWidget
from plugins.books import models, files
class DateInput(forms.DateInput):
input_type = 'date'
class MonthInput(forms.DateInput):
input_type = 'month'
class TableMultiSelect(SelectMultiple):
def __init__(self, *args, **kwargs):
items = kwargs.pop('items')
super(TableMultiSelect, self).__init__(*args, **kwargs)
self.items = items
self.queryset = self.get_queryset()
def get_queryset(self):
pks = [row.get('object', None).pk for row in self.items if row.get('object', None)]
return models.Contributor.objects.filter(pk__in=pks)
def render(self, name, value, attrs=None, choices=(), renderer=None):
value_for_template = [int(val) for val in value] if value else None
context = {
'items': self.items,
'name': name,
'value': value_for_template,
}
return render_to_string(
'books/forms/table_mult_select.html',
context,
)
class BookForm(forms.ModelForm):
class Meta:
model = models.Book
exclude = ('keywords', 'publisher_notes')
widgets = {
'description': SummernoteWidget(),
'date_published': DateInput(),
'date_embargo': DateInput(),
}
class ContributorForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
book = kwargs.pop('book', None)
super(ContributorForm, self).__init__(*args, **kwargs)
self.fields['sequence'].initial = book.get_next_contributor_sequence()
class Meta:
model = models.Contributor
exclude = ('book',)
class FormatForm(forms.ModelForm):
file = forms.FileField()
class Meta:
model = models.Format
exclude = ('book', 'filename')
def save(self, commit=True, *args, **kwargs):
save_format = super(FormatForm, self).save(commit=False)
file = self.cleaned_data["file"]
filename = files.save_file_to_disk(file, save_format)
save_format.filename = filename
if commit:
save_format.save()
return save_format
def clean(self):
cleaned_data = self.cleaned_data
return cleaned_data
class ChapterForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
items = kwargs.pop('items', None)
super(ChapterForm, self).__init__(*args, **kwargs)
self.fields['contributors'].widget = TableMultiSelect(items=items)
self.fields['contributors'].required = False
file = forms.FileField(required=False)
class Meta:
model = models.Chapter
fields = [
'title',
'description',
'pages',
'doi',
'number',
'date_embargo',
'date_published',
'sequence',
'contributors',
'license_information',
'custom_how_to_cite',
]
def save(self, commit=True, book=None, *args, **kwargs):
save_chapter = super(ChapterForm, self).save(commit=False)
if book:
save_chapter.book = book
file = self.cleaned_data["file"]
if file:
filename = files.save_file_to_disk(file, save_chapter)
save_chapter.filename = filename
if commit:
save_chapter.save()
return save_chapter
class DateForm(forms.Form):
start_date = forms.DateField(widget=DateInput())
end_date = forms.DateField(widget=DateInput())
class MonthForm(forms.Form):
start_month = forms.DateField(widget=MonthInput())
end_month = forms.DateField(widget=MonthInput())
class CategoryForm(forms.ModelForm):
class Meta:
model = models.Category
fields = (
'name',
'description',
'display_title',
'chapter_name',
'chapter_name_plural',
'buy_button_text',
)
widgets = {
'description': SummernoteWidget,
}
def save(self, commit=True):
save_category = super(CategoryForm, self).save(commit=False)
save_category.slug = slugify(save_category.name)
if commit:
save_category.save()
return save_category