forked from openlibhums/typesetting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
268 lines (207 loc) · 8.22 KB
/
forms.py
File metadata and controls
268 lines (207 loc) · 8.22 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from django import forms
from django.core.exceptions import ValidationError
from django.db import transaction
from django.utils.translation import ugettext_lazy as _
from django_summernote.widgets import SummernoteWidget
from core import models as core_models, forms as core_forms
from plugins.typesetting import models
from utils.forms import HTMLDateInput
class AssignTypesetter(forms.ModelForm, core_forms.ConfirmableIfErrorsForm):
# Confirmable form modification
QUESTION = _('Are you sure you want to create a typesetting assignment?')
def __init__(self, *args, **kwargs):
typesetters = kwargs.pop('typesetters')
files = kwargs.pop('files')
galleys = kwargs.pop('galleys', ())
self.rounds = kwargs.pop('rounds')
super(AssignTypesetter, self).__init__(*args, **kwargs)
self.fields['typesetter'].queryset = typesetters
self.fields['files_to_typeset'].queryset = files
self.fields['corrections'] = forms.MultipleChoiceField(
required=False,
choices=[(g.pk, g.label) for g in galleys],
help_text='Select which files require corrections '
'(Click and drag to select multiple)',
initial=[galley.pk for galley in galleys],
)
class Meta:
model = models.TypesettingAssignment
fields = (
'typesetter',
'due',
'task',
'files_to_typeset',
'display_proof_comments',
)
widgets = {
'due': HTMLDateInput(),
}
def save(self, commit=True):
assignment = super(AssignTypesetter, self).save(commit=False)
assignment.round = self.rounds[0]
if commit:
with transaction.atomic():
assignment.save()
for file in self.cleaned_data.get('files_to_typeset'):
assignment.files_to_typeset.add(file)
for galley_id in self.cleaned_data.get("corrections", []):
galley = core_models.Galley.objects.get(pk=galley_id)
correction, _ = assignment.corrections.get_or_create(
task=assignment,
galley=galley,
label=galley.label,
)
return assignment
def clean(self):
cleaned_data = super().clean()
if not cleaned_data.get('files_to_typeset'):
raise ValidationError("At least one file must be made picked")
def check_for_potential_errors(self):
# Confirmable form modification
potential_errors = []
typesetter = self.cleaned_data.get('typesetter', None)
message = self.check_for_inactive_account(typesetter)
if message:
potential_errors.append(message)
return potential_errors
class AssignProofreader(forms.ModelForm, core_forms.ConfirmableIfErrorsForm):
# Confirmable form modification
QUESTION = _('Are you sure you want to create a proofreading assignment?')
def __init__(self, *args, **kwargs):
self.proofreaders = kwargs.pop('proofreaders')
self.round = kwargs.pop('round')
self.manager = kwargs.pop('user')
super(AssignProofreader, self).__init__(*args, **kwargs)
self.fields['proofreader'].queryset = self.proofreaders
class Meta:
model = models.GalleyProofing
fields = ('proofreader', 'task', 'due',)
widgets = {
'due': HTMLDateInput(),
}
def save(self, commit=True):
assignment = super(AssignProofreader, self).save(commit=False)
assignment.round = self.round
assignment.manager = self.manager
if commit:
assignment.save()
return assignment
def check_for_potential_errors(self):
# Confirmable form modification
potential_errors = []
proofreader = self.cleaned_data.get('proofreader', None)
message = self.check_for_inactive_account(proofreader)
if message:
potential_errors.append(message)
return potential_errors
def decision_choices():
return (
('accept', 'Accept'),
('decline', 'Decline'),
)
class TypesetterDecision(forms.Form):
decision = forms.ChoiceField(choices=decision_choices(), required=True)
note = forms.CharField(widget=forms.Textarea, required=False)
class ManagerDecision(forms.ModelForm):
class Meta:
model = models.TypesettingAssignment
fields = ('review_decision',)
def save(self, commit=True):
decision = super(ManagerDecision, self).save(commit=False)
decision.reviewed = True
if commit:
decision.save()
return decision
class SupplementaryFileChoiceForm(forms.ModelForm):
label = forms.CharField(
required=False,
help_text=_("Text to show as the download link on the article page")
)
def __init__(self, *args, **kwargs):
self.article = kwargs.pop('article')
super().__init__(*args, **kwargs)
files = core_models.File.objects.filter(
article_id=self.article.pk,
).exclude(
supplementaryfile__file__article_id = self.article.pk,
)
self.fields['file'].queryset = files
class Meta:
model = core_models.SupplementaryFile
fields = ('file',)
def save(self, commit=True):
instance = super().save(commit=commit)
if commit:
if self.cleaned_data.get("label"):
instance.file.label = self.cleaned_data["label"]
instance.file.save()
self.article.supplementary_files.add(instance)
return instance
class EditProofingAssignment(forms.ModelForm):
class Meta:
model = models.GalleyProofing
fields = ('task', 'due',)
widgets = {
'due': HTMLDateInput(),
}
class ProofingForm(forms.ModelForm, core_forms.ConfirmableForm):
# Confirmable form constants
QUESTION = _('Are you sure you want to complete the proofreading task?')
class Meta:
model = models.GalleyProofing
fields = ('notes',)
summernote_attrs = {
'disable_attachment': True,
'height': '500px',
}
widgets = {
'notes': SummernoteWidget(
attrs={'summernote': summernote_attrs},
),
}
def check_for_potential_errors(self):
# This customizes the confirmable form method
potential_errors = []
# Check for unproofed galleys
galleys = core_models.Galley.objects.filter(
article=self.instance.round.article,
)
unproofed_galleys = self.instance.unproofed_galleys(galleys)
if unproofed_galleys:
message = _('You have not proofed some galleys:')+' {}.'.format(
", ".join([g.label for g in unproofed_galleys])
)
potential_errors.append(message)
# Check if a note was added
if not self.cleaned_data.get('notes', None):
message = 'The Notes field is empty.'
potential_errors.append(_(message))
# Check if new files were added
annotated_files = self.instance.annotated_files.all()
if annotated_files:
last_upload = max(set(an_file.date_uploaded for an_file in annotated_files))
last_editor_or_typesetter_action = max(filter(bool, [
self.instance.completed,
self.instance.assigned,
]))
if last_editor_or_typesetter_action > last_upload:
message = 'The annotated files have not been changed.'
potential_errors.append(_(message))
else:
message = 'No annotated files have been uploaded.'
potential_errors.append(_(message))
return potential_errors
class GalleyForm(forms.ModelForm):
file = forms.FileField()
class Meta:
model = core_models.Galley
fields = (
'label',
'public',
)
def __init__(self, *args, **kwargs):
include_file = kwargs.pop('include_file', True)
super().__init__(*args, **kwargs)
self.fields['label'].required = False
if not include_file:
self.fields.pop('file')