-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
523 lines (447 loc) · 16.1 KB
/
models.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
from datetime import date, timedelta
from django.db import models
from django.utils import timezone
from core.model_utils import JanewayBleachField
from plugins.typesetting import plugin_settings
from utils import models as utils_models
from utils import notify_helpers
from events import logic as events_logic
from submission import models as submission_models
def review_choices():
return (
('accept', 'Accept'),
('corrections', 'Corrections Required'),
('proofing', 'Proofing Required'),
)
class TypesettingClaim(models.Model):
editor = models.ForeignKey(
'core.Account',
on_delete=models.CASCADE,
)
article = models.OneToOneField(
'submission.Article',
on_delete=models.CASCADE,
)
claimed = models.DateTimeField(default=timezone.now)
class Meta:
unique_together = ('editor', 'article',)
class TypesettingRound(models.Model):
article = models.ForeignKey(
'submission.Article',
on_delete=models.CASCADE,
)
round_number = models.PositiveIntegerField(default=1)
date_created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-round_number', 'date_created')
unique_together = ('round_number', 'article',)
def __str__(self):
return str(self.round_number)
@property
def has_completed_proofing(self):
return GalleyProofing.objects.filter(
round=self,
accepted__isnull=False,
completed__isnull=False,
)
@property
def has_open_tasks(self):
if hasattr(self, 'typesettingassignment'):
if not self.typesettingassignment.done:
return True
if self.galleyproofing_set.filter(completed__isnull=True).exists():
return True
return False
def close(self, user=None):
""" Method that closes a round by cancelling any open tasks """
if hasattr(self, 'typesettingassignment'):
if self.typesettingassignment.is_active:
self.typesettingassignment.cancel(user=user)
for proofing in self.galleyproofing_set.filter(
completed__isnull=True,
):
proofing.cancel(user=user)
class ActiveTypesettingAssignmentManager(models.Manager):
def get_queryset(self):
return super(ActiveTypesettingAssignmentManager, self).get_queryset().exclude(
round__article__stage=submission_models.STAGE_ARCHIVED,
)
class TypesettingAssignment(models.Model):
round = models.OneToOneField(
TypesettingRound,
on_delete=models.CASCADE,
)
manager = models.ForeignKey(
'core.Account',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='manager',
)
typesetter = models.ForeignKey(
'core.Account',
null=True,
on_delete=models.SET_NULL,
related_name='typesetter',
)
assigned = models.DateTimeField(default=timezone.now)
notified = models.BooleanField(default=False)
accepted = models.DateTimeField(blank=True, null=True)
due = models.DateField(null=True)
completed = models.DateTimeField(blank=True, null=True)
cancelled = models.DateTimeField(blank=True, null=True)
reviewed = models.BooleanField(default=False)
display_proof_comments = models.BooleanField(
default=True,
help_text="Allow the typesetter to see the proofreading comments",
)
review_decision = models.CharField(
choices=review_choices(),
max_length=21,
blank=True,
)
task = JanewayBleachField(
blank=True,
verbose_name='Typesetting Task',
help_text='The task description if not explained in the typesetting guidelines.',
)
files_to_typeset = models.ManyToManyField(
'core.File',
related_name='files_to_typeset',
blank=True,
)
galleys_created = models.ManyToManyField(
'core.Galley',
blank=True,
)
typesetter_note = JanewayBleachField(
blank=True,
verbose_name='Note to Editor',
)
objects = models.Manager()
active_objects = ActiveTypesettingAssignmentManager()
@property
def time_to_due(self):
if not self.due:
return ''
due = self.due - timezone.now().date()
if due == timedelta(0):
return 'Due Today'
elif due < timedelta(0):
return 'Overdue'
return '{} days'.format(due.days)
@property
def is_active(self):
return self.assigned and not self.completed
@property
def status(self):
if self.cancelled:
return "cancelled"
if self.assigned and not self.accepted and not self.completed:
return "assigned"
elif self.assigned and self.accepted and not self.completed:
return "accepted"
elif self.declined:
return "declined"
elif self.done and not self.reviewed:
return "completed"
elif self.done and self.reviewed:
return self.get_review_decision_display()
else:
return "unknown"
@property
def done(self):
return self.completed and self.accepted
@property
def declined(self):
return self.completed and not self.accepted
@property
def is_overdue(self):
return self.due and self.due < date.today()
def reopen(self, user=None):
self.completed = self.accepted = None
self.notified = False
utils_models.LogEntry.add_entry(
types="Typesetting reopened",
description="The typesetting assignment {self.pk} has been "
"re-opened by user {user}".format(self=self, user=user),
level="INFO",
actor=user,
target=self.round.article,
)
self.save()
def delete(self, user=None):
utils_models.LogEntry.add_entry(
types="Typesetting deleted",
description="The typesetting assignment {self.pk} has been "
"deleted by user {user}".format(self=self, user=user),
level="INFO",
actor=user,
target=self.round.article,
)
super().delete()
def cancel(self, user=None):
utils_models.LogEntry.add_entry(
types="Typesetting Task cancelled",
description="The typesetting assignment {self.pk} has been "
"cancelled by user {user}".format(self=self, user=user),
level="INFO",
actor=user,
target=self.round.article,
)
self.cancelled = timezone.now()
self.save()
def complete(self, note='', user=None):
utils_models.LogEntry.add_entry(
types="Typesetting Task Completed",
description="The typesetting assignment {self.pk} has been "
"completed by user {user}".format(self=self, user=user),
level="INFO",
actor=user,
target=self.round.article,
)
self.typesetter_note = note
if not self.accepted:
self.accepted = timezone.now()
self.completed = timezone.now()
self.save()
FRIENDLY_STATUSES = {
"assigned": "Awaiting response from the typesetter.",
"accepted": "Typesetter has accepted task, awaiting completion.",
"declined": "Typesetter has declined this task.",
"completed": "The typesetter has completed their task. "
"You should review the uploaded galley files.",
"cancelled": "The manager has cancelled this typesetting task",
"closed": "Task closed",
"unknown": "Task status unknown",
"Corrections Required": "This article requires corrections.",
"Proofing Required": "This article requires proofing.",
"Accept": "Typesetting for this article has been completed. However,"
" if you want to make further changes, you can still assign"
" proofreaders or start a new typesetting round.",
}
@property
def friendly_status(self):
return self.FRIENDLY_STATUSES.get(self.status)
def proofing_assignments_for_corrections(self):
""" Returns the proofreading assignemnts for corrections
The proofreadings relevant for round n of proofing would have been
stored against round n-1 of this article
"""
rounds = TypesettingRound.objects.filter(article=self.round.article)
if rounds.count() > 1:
previous_round = rounds[1]
proofing_assignments = previous_round.galleyproofing_set.filter(
completed__isnull=False,
cancelled=False,
)
return proofing_assignments
else:
return GalleyProofing.objects.none()
def __str__(self):
return f'Typesetter {self.typesetter} assigned to article {self.round.article}'
class ActiveGalleyProofingManager(models.Manager):
def get_queryset(self):
return super(ActiveGalleyProofingManager, self).get_queryset().exclude(
round__article__stage=submission_models.STAGE_ARCHIVED
)
class GalleyProofing(models.Model):
round = models.ForeignKey(
TypesettingRound,
on_delete=models.CASCADE,
)
manager = models.ForeignKey(
'core.Account',
null=True,
related_name='galley_manager',
on_delete=models.SET_NULL,
)
proofreader = models.ForeignKey(
'core.Account',
null=True,
on_delete=models.SET_NULL,
)
assigned = models.DateTimeField(default=timezone.now)
notified = models.BooleanField(default=False)
due = models.DateTimeField(default=None, verbose_name="Date Due")
accepted = models.DateTimeField(blank=True, null=True)
completed = models.DateTimeField(blank=True, null=True)
cancelled = models.BooleanField(default=False)
task = JanewayBleachField(
verbose_name="Proofing Task",
help_text='Add any additional information or instructions '
'for the proofreader here.',
)
proofed_files = models.ManyToManyField('core.Galley', blank=True)
notes = JanewayBleachField(blank=True)
annotated_files = models.ManyToManyField('core.File', blank=True)
objects = models.Manager()
active_objects = ActiveGalleyProofingManager()
class Meta:
ordering = ('assigned', 'accepted', 'pk')
def __str__(self):
return 'Proofing for Article {0} by {1}'.format(
self.round.article.title,
self.proofreader.full_name(),
)
def assign(self, user=None, skip=False):
if not skip:
self.notified = True
self.save()
utils_models.LogEntry.add_entry(
types='Proofreader Assigned',
description='{} assigned as a proofreader by {}'.format(
self.proofreader.full_name(),
user,
),
level='Info',
actor=user,
target=self.round.article,
)
def cancel(self, user=None):
self.cancelled = True
self.completed = timezone.now()
self.save()
utils_models.LogEntry.add_entry(
types='Proofreading Assignment Cancelled',
description='Proofing by {} cancelled by {}'.format(
self.proofreader.full_name(),
user,
),
level='Info',
actor=user,
target=self.round.article,
)
def reset(self, user=None):
self.cancelled = False
self.completed = None
self.accepted = None
self.save()
utils_models.LogEntry.add_entry(
types='Proofreading Assignment Reset',
description='Proofing by {} reset by {}'.format(
self.proofreader.full_name(),
user,
),
level='Info',
actor=user,
target=self.round.article,
)
def complete(self, user=None):
self.cancelled = False
self.completed = timezone.now()
self.accepted = timezone.now()
self.save()
utils_models.LogEntry.add_entry(
types='Proofreading Assignment Complete',
description='Proofing by {} completed'.format(
user,
),
level='Info',
actor=user,
target=self.round.article,
)
def unproofed_galleys(self, galleys):
check = []
proofed_files = self.proofed_files.all()
for galley in galleys:
if galley not in proofed_files:
check.append(galley)
return check
@property
def time_to_due(self):
due = self.due.date() - timezone.now().date()
if due == timedelta(0):
return 'Due Today'
elif due < timedelta(0):
return 'Overdue'
return '{} days'.format(due.days)
FRIENDLY_STATUSES = {
"assigned": "Awaiting response from the proofreader.",
"accepted": "Proofreader has accepted task, awaiting completion.",
"declined": "Proofreader has declined this task.",
"completed": "The proofreader has completed their task. "
"You should review their response.",
"unknown": "Task status unknown.",
"cancelled": "Task was cancelled by manager/editor."
}
@property
def status(self):
if self.cancelled:
return 'cancelled'
elif self.assigned and self.accepted and not self.completed:
return 'accepted'
elif self.assigned and self.completed and not self.accepted:
return 'declined'
elif self.assigned and self.completed and self.accepted:
return 'completed'
elif self.assigned and not self.accepted and not self.completed:
return 'assigned'
else:
return 'unknown'
@property
def friendly_status(self):
return self.FRIENDLY_STATUSES.get(self.status)
def send_assignment_notification(self, request, message, skip=False):
description = '{0} has been assigned as a proofreader for {1}'.format(
self.proofreader.full_name(),
self.round.article.title,
)
if not skip:
log_dict = {
'level': 'Info',
'action_text': description,
'types': 'Proofing Assignment',
'target': self.round.article,
}
notify_helpers.send_email_with_body_from_user(
request,
'Proofing Request',
self.proofreader.email,
message,
log_dict=log_dict,
)
notify_helpers.send_slack(
request,
description,
['slack_editors'],
)
self.notified = True
self.save()
class TypesettingCorrection(models.Model):
task = models.ForeignKey(
"typesetting.TypesettingAssignment",
related_name="corrections",
blank=True,
on_delete=models.CASCADE,
)
galley = models.ForeignKey(
"core.Galley",
null=True,
blank=True,
on_delete=models.SET_NULL,
)
# A copy of the galley label, in case the galley is deleted
label = models.CharField(max_length=255, blank=True, null=True)
date_requested = models.DateTimeField(auto_now_add=True)
date_completed = models.DateTimeField(blank=True, null=True)
date_declined = models.DateTimeField(blank=True, null=True)
# A copy of the file checksum to detect changes when requesting corrections
file_checksum = models.CharField(max_length=255, blank=True, null=True)
@property
def status(self):
_status = "pending"
if self.date_completed:
_status = "completed"
elif self.date_declined:
_status = "declined"
return _status
def save(self, *args, **kwargs):
if not self.pk and not self.file_checksum:
self.file_checksum = self.galley.file.checksum()
super().save(*args, **kwargs)
@property
def corrected(self):
if self.galley:
return self.file_checksum != self.galley.file.checksum()
return False