views.py
53 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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
import json
import os
import mimetypes
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import render
from django.urls import reverse, reverse_lazy
from django.utils import timezone
from django.views import View
from django.views.generic import DeleteView, CreateView, UpdateView, RedirectView, ListView
from django.contrib import messages
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.auth.decorators import login_required
from django.db.models.query import QuerySet
from django.db.models import Q
from django.template.loader import render_to_string
from django.forms import modelformset_factory, formset_factory
from django.db import transaction, IntegrityError
from keras import backend
from sys import maxsize, modules
from functools import partial, wraps
from .forms import ChunkForm, ParticipantForm, SubchunkForm, MetadataForm, KeywordForm, DocSplitForm, ChunkMoveForm, \
SubDocDetailsForm, AuthorForm, ChunkMergeForm, ChunkSplitForm, DocTypeForm, DocDetailsForm, MagazineDetailsForm, \
BWMADetailsForm
from .models import Chunk, Document, Participant, Metadata, Keyword, Annotation, Magazine, BookWithMultipleAuthors
from .utils import get_remaining_doc_types_tuple, get_remaining_subdoc_types_tuple, get_doc_with_type, SUBDOCUMENT_TYPES
from projects.ppc.models import Utterance
from pipeline.models import ProcessingStatus
def get_closest_from_objects_list(obj_list, obj_pk):
if len(obj_list) == 1:
return None
closest_obj = obj_list[0]
if obj_list.last().pk == obj_pk:
closest_obj = obj_list[len(obj_list) - 2]
else:
for counter, obj in enumerate(obj_list):
if obj.pk == obj_pk:
closest_obj = obj_list[counter + 1]
break
return closest_obj
def handle_chunks_seq_uniqueness(chunks, target_doc_chunks):
if isinstance(chunks, QuerySet):
chunks_seqs = set([chunk.sequence for chunk in chunks])
target_doc_chunks_seqs = set([chunk.sequence for chunk in target_doc_chunks])
intersection = list(chunks_seqs.intersection(target_doc_chunks_seqs))
if len(intersection) > 0:
max_seq = target_doc_chunks.order_by('-sequence')[0].sequence
counter = 1
for chunk in chunks.order_by('-sequence'):
if chunk.sequence in intersection:
chunk.sequence = max_seq + counter
chunk.save()
counter += 1
else: # for single chunk
if chunks[0].sequence in set([chunk.sequence for chunk in target_doc_chunks]):
chunks[0].sequence = target_doc_chunks.order_by('-sequence')[0].sequence + 1
return chunks
def swap_subdocs(direction, subdoc, subdoc_seq, temp_seq, max_seq, parent_doc):
sign = None
if direction == 'up' and subdoc_seq > 1:
sign = '-'
elif direction == 'down' and subdoc_seq < max_seq:
sign = '+'
if sign is not None:
neighbour = Document.objects.get(parent=parent_doc,
sequence=eval(f'{str(subdoc_seq)} {sign} 1'))
neighbour.sequence = temp_seq
neighbour.save()
subdoc.sequence = eval(f'{str(subdoc_seq)} {sign} 1')
subdoc.save()
neighbour.sequence = subdoc_seq
neighbour.save()
def split_chunk(request, pk):
context = {}
chunk = Chunk.objects.get(pk=pk)
ChunksFormset = formset_factory(wraps(ChunkSplitForm)(partial(ChunkSplitForm, initial_text=chunk.text)))
formset = ChunksFormset(request.POST or None, prefix='chunks')
if request.method == "POST":
chunk_document_id = chunk.document.id
formset_len = len(formset)
if formset_len > 1 and formset.is_valid() and not request.is_ajax():
next_chunks = Chunk.objects.filter(document=chunk.document,
sequence__gt=chunk.sequence).order_by('-sequence')
for ch in next_chunks:
ch.sequence += formset_len - 1
ch.save()
old_chunk = Chunk.objects.create(text=formset[0].data['chunks-0-text'],
document=chunk.document,
sequence=chunk.sequence)
old_chunk.save()
for num, f in enumerate(formset[1:]):
ch = Chunk.objects.create(text=f.cleaned_data['text'],
document=chunk.document,
sequence=chunk.sequence + num + 1)
ch.save()
chunk.delete()
return HttpResponseRedirect('%s#chunk-%d' % (
reverse_lazy('annotation', kwargs={'doc_id': chunk_document_id}), old_chunk.pk))
else:
messages.error(request, 'Błąd: Nieprawidłowy podział akapitu.')
return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': chunk_document_id}))
context['formset'] = formset
context['title'] = 'Dzielenie akapitu'
context['submit_btn_text'] = 'Podziel'
formset.data['chunks-TOTAL_FORMS'] = 0
return render(request, 'storage/chunk-split.html', context)
class DocumentView(View):
template_name = 'storage/document.html'
def get(self, request, doc_id=None):
if 'doc_id' in request.GET:
doc_id = request.GET.get('doc_id')
context = {'document': None}
try:
doc = Document.objects.get(id=doc_id)
participants = doc.participants.filter(type='person')
chunks = doc.chunks.order_by('sequence')
context = {'document': doc,
'participants': participants,
'chunks': chunks}
except Document.DoesNotExist:
pass
return render(request, self.template_name, context)
class DocumentAutocompleteView(View):
def get(self, request):
term = request.GET.get('term')
ids = [doc.id for doc in Document.objects.filter(id__startswith=term)[:5]]
return HttpResponse(json.dumps(ids), 'application/json')
class DocumentReprocessView(View):
document = None
template_name = 'storage/document_validation.html'
def post(self, request, doc_id):
backend.clear_session()
document = Document.objects.get(id=doc_id)
document.clear_annotation()
document.indexed = False
document.save()
document.pipeline.annotate(document)
document.pipeline.add_terminology(document)
document.pipeline.write(document)
document.pipeline.index(document)
document.changed = False
document.save()
return HttpResponseRedirect(reverse_lazy('document', kwargs={'doc_id': document.id}))
def get(self, request, doc_id):
context = {'document': None}
try:
self.document = Document.objects.get(id=doc_id)
warnings, errors = self._validate_document()
context = {'document': self.document,
'warnings': warnings,
'errors': errors}
except Document.DoesNotExist:
pass
return render(request, self.template_name, context)
def _validate_document(self):
warnings, errors = self._validate_participants()
errors.extend(self._validate_chunks())
return warnings, errors
def _validate_participants(self):
warnings = []
errors = []
for participant in self.document.participants.filter(type='person'):
if not participant.utterances.exists():
if participant.role != 'author':
warnings.append({'msg': 'Uczestnik "%s" nie wypowiada się.' % participant.name,
'url': self._get_participant_anchor(participant)})
if self.document.participants.filter(type='person', order=participant.order).count() > 1:
errors.append({'msg': 'Liczba porządkowa uczestnika "%s" powtarza się.' % participant.name,
'url': self._get_participant_anchor(participant)})
return warnings, errors
def _get_participant_anchor(self, participant):
return '%s#participant-%d' % (reverse_lazy('document', kwargs={'doc_id': self.document.id}), participant.pk)
def _validate_chunks(self):
errors = []
errors.extend(self._get_general_chunk_errors())
errors.extend(self._get_individual_chunk_errors())
return errors
def _get_general_chunk_errors(self):
errors = []
chunked_structure = False
subchunked_structure = False
for chunk in self.document.chunks.all():
if chunk.text:
chunked_structure = True
if chunk.utterances.exists():
subchunked_structure = True
if chunked_structure and subchunked_structure:
errors.append({'msg': 'Dokument zawiera zarówno podsekcje jak i sekcje tekstowe.',
'url': self._get_doc_anchor()})
return errors
def _get_doc_anchor(self):
return reverse_lazy('document', kwargs={
'dMetadata.objects.create(first_name="Bruce", last_name="Springsteen")oc_id': self.document.id})
def _get_individual_chunk_errors(self):
errors = []
for chunk in self.document.chunks.all():
if chunk.text and chunk.utterances.exists():
errors.append({'msg': 'Sekcja %d nie może zawierać podsekcji i tekstu jednocześnie.' % chunk.sequence,
'url': self._get_chunk_anchor(chunk)})
if not chunk.text and not chunk.utterances.exists():
errors.append({'msg': 'Sekcja %d jest pusta.' % chunk.sequence,
'url': self._get_chunk_anchor(chunk)})
if self.document.chunks.filter(sequence=chunk.sequence).count() > 1:
errors.append({'msg': 'Pozycja sekcji %d powtarza się.' % chunk.sequence,
'url': self._get_chunk_anchor(chunk)})
errors.extend(self._get_utterances_errors(chunk))
return errors
def _get_chunk_anchor(self, chunk):
return '%s#chunk-%d' % (reverse_lazy('document', kwargs={'doc_id': self.document.id}), chunk.pk)
def _get_utterances_errors(self, chunk):
errors = []
for utt in chunk.utterances.all():
if chunk.utterances.filter(sequence=utt.sequence).count() > 1:
errors.append({'msg': 'Pozycja podsekcji %d powtarza się.' % utt.sequence,
'url': self._get_subchunk_anchor(utt)})
return errors
def _get_subchunk_anchor(self, subchunk):
return '%s#subchunk-%d' % (reverse_lazy('document', kwargs={'doc_id': self.document.id}), subchunk.pk)
class ChunkAddView(CreateView):
template_name = 'storage/edit.html'
form_class = ChunkForm
def form_valid(self, form):
if not self.request.is_ajax():
self.object = form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Dodawanie akapitu'
context['submit_btn_text'] = 'Dodaj'
return context
def get_form_kwargs(self):
kwargs = super(ChunkAddView, self).get_form_kwargs()
kwargs['doc_id'] = self.kwargs['doc_id']
return kwargs
def get_success_url(self):
return '%s#chunk-%d' % (reverse_lazy('document', kwargs={'doc_id': self.kwargs['doc_id']}),
self.object.pk)
class ChunkDeleteView(DeleteView):
model = Chunk
template_name = 'storage/delete.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Usuwanie akapitu'
context['msg'] = 'Czy jesteś pewien, że chcesz usunąć akapit?'
context['submit_btn_text'] = 'Usuń'
return context
def get_success_url(self):
chunks = self.object.document.chunks.all()
closest_chunk = get_closest_from_objects_list(chunks, self.object.pk)
if closest_chunk is None:
return reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id})
return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id}),
closest_chunk.pk)
def delete(self, request, *args, **kwargs):
response = super(ChunkDeleteView, self).delete(request, *args, **kwargs)
self.object.document.changed = True
self.object.document.save()
return response
class ChunkEditView(UpdateView):
model = Chunk
template_name = 'storage/edit.html'
form_class = ChunkForm
def form_valid(self, form):
if not self.request.is_ajax():
form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Edycja akapitu'
context['submit_btn_text'] = 'Zapisz'
return context
def get_success_url(self):
return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id}), self.object.pk)
class ChunkAddBetweenView(CreateView):
template_name = 'storage/edit.html'
form_class = ChunkForm
model = Chunk
def form_valid(self, form):
if not self.request.is_ajax():
self.object = form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Dodawanie akapitu'
context['submit_btn_text'] = 'Dodaj'
return context
def get_form_kwargs(self):
kwargs = super(ChunkAddBetweenView, self).get_form_kwargs()
kwargs['doc_id'] = self.kwargs['doc_id']
if 'pk' in self.kwargs.keys():
kwargs['pk'] = self.kwargs['pk']
return kwargs
def get_success_url(self):
return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.kwargs['doc_id']}),
self.object.pk)
class ChunkMoveView(UpdateView):
model = Chunk
template_name = 'storage/edit.html'
form_class = ChunkMoveForm
def form_valid(self, form):
if not self.request.is_ajax():
form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Przenoszenie akapitu'
context['submit_btn_text'] = 'Przenieś'
return context
def get_form_kwargs(self):
kwargs = super(ChunkMoveView, self).get_form_kwargs()
if self.object.document.parent is not None: # subdocument
poss_target_docs = Document.objects.filter(parent=self.object.document.parent)
poss_target_docs = poss_target_docs.exclude(id=self.object.document.id)
poss_target_docs |= Document.objects.filter(pk=self.object.document.parent.pk)
else: # main document
poss_target_docs = Document.objects.filter(parent=self.object.document)
kwargs['poss_target_docs'] = poss_target_docs
return kwargs
def get_success_url(self):
return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id}), self.object.pk)
class ChunkMergeView(UpdateView):
model = Chunk
template_name = 'storage/edit.html'
form_class = ChunkMergeForm
def form_valid(self, form):
if not self.request.is_ajax():
target_chunk = form.cleaned_data['target_chunk']
if target_chunk.sequence > self.object.sequence:
self.object.text += f' {target_chunk.text}'
self.object.save()
target_chunk.delete()
return HttpResponseRedirect(self.get_success_url(self.object))
else:
target_chunk.text += f' {self.object.text}'
target_chunk.save()
self.object.delete()
return HttpResponseRedirect(self.get_success_url(target_chunk))
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Łączenie akapitu'
context['submit_btn_text'] = 'Połącz'
return context
def get_form_kwargs(self):
kwargs = super(ChunkMergeView, self).get_form_kwargs()
all_target_chunks = self.object.document.chunks.all().order_by('sequence')
poss_target_chunks = Chunk.objects.none()
for index, ch in enumerate(all_target_chunks):
if ch.id == self.object.id:
if index > 0:
poss_target_chunks |= Chunk.objects.filter(pk=all_target_chunks[index - 1].pk)
if index < len(all_target_chunks) - 1:
poss_target_chunks |= Chunk.objects.filter(pk=all_target_chunks[index + 1].pk)
break
kwargs['poss_target_chunks'] = poss_target_chunks
return kwargs
def get_success_url(self, target_chunk):
return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': target_chunk.document.id}),
target_chunk.pk)
class SubchunkAddView(CreateView):
template_name = 'storage/edit.html'
form_class = SubchunkForm
def form_valid(self, form):
if not self.request.is_ajax():
self.object = form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Dodawanie podsekcji'
context['submit_btn_text'] = 'Dodaj'
return context
def get_form_kwargs(self):
kwargs = super(SubchunkAddView, self).get_form_kwargs()
kwargs['chunk_pk'] = self.kwargs['chunk_pk']
return kwargs
def get_success_url(self):
return '%s#subchunk-%d' % (
reverse_lazy('annotation', kwargs={'doc_id': Chunk.objects.get(pk=self.kwargs['chunk_pk']).document.id}),
self.object.pk)
class SubchunkDeleteView(DeleteView):
model = Utterance
template_name = 'storage/delete.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Usuwanie podsekcji'
context['msg'] = 'Czy jesteś pewien, że chcesz usunąć podsekcję?'
context['submit_btn_text'] = 'Usuń'
return context
def get_success_url(self):
return '%s#chunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.chunk.document.id}),
self.object.chunk.pk)
def delete(self, request, *args, **kwargs):
response = super(SubchunkDeleteView, self).delete(request, *args, **kwargs)
self.object.chunk.document.changed = True
self.object.chunk.document.save()
return response
class SubchunkEditView(UpdateView):
model = Utterance
template_name = 'storage/edit.html'
form_class = SubchunkForm
def form_valid(self, form):
if not self.request.is_ajax():
form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Edycja podsekcji'
context['submit_btn_text'] = 'Zapisz'
return context
def get_success_url(self):
return '%s#subchunk-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.chunk.document.id}),
self.object.pk)
class ParticipantAddView(CreateView):
template_name = 'storage/edit.html'
form_class = ParticipantForm
def form_valid(self, form):
if not self.request.is_ajax():
self.object = form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Dodawanie uczestnika'
context['submit_btn_text'] = 'Dodaj'
return context
def get_form_kwargs(self):
kwargs = super(ParticipantAddView, self).get_form_kwargs()
kwargs['doc_id'] = self.kwargs['doc_id']
return kwargs
def get_success_url(self):
return '%s#participant-%d' % (reverse_lazy('document', kwargs={'doc_id': self.kwargs['doc_id']}),
self.object.pk)
class ParticipantDeleteView(DeleteView):
model = Participant
template_name = 'storage/delete.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Usuwanie uczestnika'
context['msg'] = 'Czy jesteś pewien, że chcesz usunąć uczestnika?'
context['submit_btn_text'] = 'Usuń'
return context
def get_success_url(self):
return '%s#participants' % reverse_lazy('document', kwargs={'doc_id': self.object.document.id})
def delete(self, request, *args, **kwargs):
response = super(ParticipantDeleteView, self).delete(request, *args, **kwargs)
self.object.document.changed = True
self.object.document.save()
return response
class ParticipantEditView(UpdateView):
model = Participant
template_name = 'storage/edit.html'
form_class = ParticipantForm
def form_valid(self, form):
if not self.request.is_ajax():
form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Edycja uczestnika'
context['submit_btn_text'] = 'Zapisz'
return context
def get_success_url(self):
return '%s#participant-%d' % (reverse_lazy('document', kwargs={'doc_id': self.object.document.id}),
self.object.pk)
class AuthorAddView(ParticipantAddView):
form_class = AuthorForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Dodawanie autora'
context['submit_btn_text'] = 'Dodaj'
context['autocomplete_fields'] = {'id_name': 'author_autocomplete'}
return context
def get_form_kwargs(self):
kwargs = super(AuthorAddView, self).get_form_kwargs()
kwargs['doc_id'] = self.kwargs['doc_id']
return kwargs
def get_success_url(self):
return '%s#author-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.kwargs['doc_id']}),
self.object.pk)
class AuthorDeleteView(ParticipantDeleteView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Usuwanie autora'
context['msg'] = 'Czy jesteś pewien, że chcesz usunąć autora?'
context['submit_btn_text'] = 'Usuń'
return context
def get_success_url(self):
return '%s#authors' % reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id})
def delete(self, request, *args, **kwargs):
response = super(AuthorDeleteView, self).delete(request, *args, **kwargs)
self.object.document.changed = True
self.object.document.save()
return response
class AuthorEditView(ParticipantEditView):
form_class = AuthorForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Edycja autora'
context['submit_btn_text'] = 'Zapisz'
context['autocomplete_fields'] = {'id_name': 'author_autocomplete'}
return context
def get_success_url(self):
return '%s#author-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id}),
self.object.pk)
class AnnotationView(View):
template_name = 'storage/annotation.html'
def get(self, request, doc_id=None):
if 'doc_id' in request.GET:
doc_id = request.GET.get('doc_id')
doc = None
context = {'document': doc}
doc = get_doc_with_type(doc_id)
if doc is not None:
doc_type = doc.get_doc_type_display()
subdocuments = Document.objects.filter(parent=doc).order_by('sequence')
metadata = doc.metadata.order_by('sequence')
chunks = doc.chunks.order_by('sequence')
keywords = doc.keywords.all()
authors = doc.participants.all()
context = {'document': doc,
'subdocuments': subdocuments,
'metadata': metadata,
'chunks': chunks,
'keywords': keywords,
'authors': authors,
'doc_type': doc_type}
return render(request, self.template_name, context)
class ReviewView(AnnotationView):
template_name = 'storage/review.html'
class MetadataAddView(CreateView):
template_name = 'storage/edit.html'
form_class = MetadataForm
def form_valid(self, form):
if not self.request.is_ajax():
self.object = form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Dodawanie metadanej'
context['submit_btn_text'] = 'Dodaj'
context['autocomplete_fields'] = {'id_name': 'md_name_autocomplete',
'id_value': 'md_value_autocomplete'}
names = list(set([md.name for md in Metadata.objects.all()]))
context['all_md_names'] = names
return context
def get_form_kwargs(self):
kwargs = super(MetadataAddView, self).get_form_kwargs()
kwargs['doc_id'] = self.kwargs['doc_id']
return kwargs
def get_success_url(self):
return '%s#metadata-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.kwargs['doc_id']}),
self.object.pk)
class MetadataDeleteView(DeleteView):
model = Metadata
template_name = 'storage/delete.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Usuwanie metadanej'
context['msg'] = 'Czy jesteś pewien, że chcesz usunąć metadaną?'
context['submit_btn_text'] = 'Usuń'
return context
def get_success_url(self):
metadata = self.object.document.metadata.all()
closest_md = get_closest_from_objects_list(metadata, self.object.pk)
if closest_md is None:
return reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id})
return '%s#metadata-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id}),
closest_md.pk)
def delete(self, request, *args, **kwargs):
response = super(MetadataDeleteView, self).delete(request, *args, **kwargs)
self.object.document.changed = True
self.object.document.save()
return response
class MetadataEditView(UpdateView):
model = Metadata
template_name = 'storage/edit.html'
form_class = MetadataForm
def form_valid(self, form):
if not self.request.is_ajax():
form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Edycja metadanej'
context['submit_btn_text'] = 'Zapisz'
context['autocomplete_fields'] = {'id_name': 'md_name_autocomplete',
'id_value': 'md_value_autocomplete'}
names = list(set([md.name for md in Metadata.objects.all()]))
context['all_md_names'] = names
return context
def get_success_url(self):
return '%s#metadata-%d' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.document.id}),
self.object.pk)
class DocDetailsEditView(UpdateView):
model = Document
template_name = 'storage/edit.html'
form_class = DocDetailsForm
def form_valid(self, form):
if not self.request.is_ajax():
form.save(commit=True)
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Edycja szczegółów dokumentu'
context['submit_btn_text'] = 'Zapisz'
context['autocomplete_fields'] = {'id_title': 'details_title_autocomplete',
'id_publication_place': 'details_pub_place_autocomplete',
'id_channel': 'details_channel_autocomplete',
'id_type': 'details_type_autocomplete',
'id_text_origin': 'details_text_origin_autocomplete',
}
return context
def get_success_url(self):
return '%s#doc-details' % (reverse_lazy('annotation', kwargs={'doc_id': self.object.id}))
class MagazineDetailsEditView(DocDetailsEditView):
model = Magazine
form_class = MagazineDetailsForm
class BWMADetailsEditView(DocDetailsEditView):
model = BookWithMultipleAuthors
form_class = BWMADetailsForm
class SubDocDetailsEditView(DocDetailsEditView):
form_class = SubDocDetailsForm
def add_keyword(request, doc_id):
if request.method == 'POST':
form = KeywordForm(request.POST)
if form.is_valid():
kw_label = form.cleaned_data['label']
keyword, _ = Keyword.objects.get_or_create(label=kw_label)
document = Document.objects.get(id=doc_id)
document.keywords.add(keyword)
document.changed = True
document.save()
return HttpResponseRedirect('%s#keyword-%d' % (reverse_lazy('annotation', kwargs={'doc_id': doc_id}),
keyword.pk))
else:
form = KeywordForm()
return render(request, 'storage/edit.html', {'form': form,
'title': 'Dodawanie słowa kluczowego',
'submit_btn_text': 'Dodaj',
'autocomplete_fields': {
'id_label': 'kw_label_autocomplete'
}})
def edit_keyword(request, doc_id, pk):
prev_kw = Keyword.objects.get(pk=pk)
if request.method == 'POST':
form = KeywordForm(request.POST)
if form.is_valid():
kw_label = form.cleaned_data['label']
keyword, _ = Keyword.objects.get_or_create(label=kw_label)
document = Document.objects.get(id=doc_id)
document.keywords.remove(prev_kw)
document.keywords.add(keyword)
document.changed = True
document.save()
return HttpResponseRedirect('%s#keyword-%d' % (reverse_lazy('annotation', kwargs={'doc_id': doc_id}),
keyword.pk))
else:
form = KeywordForm(initial={'label': prev_kw.label})
return render(request, 'storage/edit.html', {'form': form,
'title': 'Edycja słowa kluczowego',
'submit_btn_text': 'Zapisz',
'autocomplete_fields': {
'id_label': 'kw_label_autocomplete'
}})
def delete_keyword(request, doc_id, pk):
prev_kw = Keyword.objects.get(pk=pk)
if request.method == 'POST':
document = Document.objects.get(id=doc_id)
keywords = document.keywords.all()
closest_kw = get_closest_from_objects_list(keywords, pk)
document.keywords.remove(prev_kw)
document.changed = True
document.save()
if closest_kw is None:
return HttpResponseRedirect(reverse_lazy('annotation', kwargs={'doc_id': doc_id}))
return HttpResponseRedirect('%s#keyword-%d' % (reverse_lazy('annotation', kwargs={'doc_id': doc_id}),
closest_kw.pk))
return render(request, 'storage/delete.html', {'title': 'Usuwanie słowa kluczowego',
'msg': 'Czy jesteś pewien, że chcesz usunąć słowo kluczowe?',
'submit_btn_text': 'Usuń'})
class StartAnnotationView(UserPassesTestMixin, RedirectView):
def get_redirect_url(self, *args, **kwargs):
return reverse('annotation', kwargs={'doc_id': kwargs['doc_id']})
def get(self, request, *args, **kwargs):
new_documents = Document.objects.filter(parent__isnull=True,
processing_status=ProcessingStatus.objects.get(key='to_correct'),
broken_source=False)
if len(new_documents) == 0:
messages.error(request, 'Brak dokumentów dostępnych do anotacji')
return HttpResponseRedirect(reverse('annotation'))
else:
doc_id = new_documents[0].id
document = Document.objects.get(id=doc_id)
Annotation.objects.create(document=document, user=request.user)
document.change_processing_status('in_correction')
document.save()
kwargs['doc_id'] = doc_id
return super().get(request, *args, **kwargs)
def test_func(self):
return self.request.user.groups.filter(name='Annotators').exists()
def handle_no_permission(self):
return HttpResponse('Wymagana jest rola anotatora, aby uzyskać dostęp do tej strony.')
class FinishAnnotationView(RedirectView):
def get_redirect_url(self, *args, **kwargs):
return reverse('annotation')
def get(self, request, *args, **kwargs):
document = get_doc_with_type(kwargs['doc_id'])
subdocuments = Document.objects.filter(parent=document)
if document.missing_translator():
messages.warning(request, 'Ostrzeżenie: Dokument posiada język oryginału inny niż polski, ale nie ma '
'dodanego żadnego tłumacza.')
for subdoc in subdocuments:
if subdoc.missing_translator():
messages.warning(request, f'Ostrzeżenie: Poddokument o ID "{subdoc.id}" posiada język oryginału '
f'inny niż polski, ale nie ma dodanego żadnego tłumacza.')
return render(self.request, 'storage/delete.html', {'msg': 'Czy na pewno chcesz zakończyć anotację dokumentu?',
'submit_btn_text': 'Zakończ'})
def post(self, request, *args, **kwargs):
document = get_doc_with_type(kwargs['doc_id'])
subdocuments = Document.objects.filter(parent=document)
if not document.check_details_filling():
messages.error(request, 'Błąd: Nie wypełniono wszystkich szczegółów dokumentu.')
return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': document.id}))
if document.original_lang_error():
messages.error(request, 'Błąd: Dokument posiada tłumacza, ale posiada polski język oryginału.')
return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': document.id}))
for subdoc in subdocuments:
if not subdoc.check_details_filling():
messages.error(request, f'Błąd: Nie wypełniono wszystkich szczegółów poddokumentu o ID "{subdoc.id}".')
return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': document.id}))
if subdoc.original_lang_error():
messages.error(request,
f'Błąd: Poddokument o ID "{subdoc.id}" posiada tłumacza, ale posiada polski język '
f'oryginału.')
return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': document.id}))
if len(subdoc.chunks.all()) == 0:
messages.error(request,
f'Błąd: Poddokument o ID "{subdoc.id}" nie posiada żadnych akapitów. Usuń go przed '
f'zakończeniem anotacji.')
return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': document.id}))
document.change_processing_status('correct')
document.save()
annotation = Annotation.objects.get(document=document,
user=request.user,
start_time__lte=timezone.now(),
finished=False)
annotation.finish_time = timezone.now()
annotation.finished = True
annotation.save()
messages.success(request, 'Twoja anotacja została zapisana.')
return super().get(request, *args, **kwargs)
class DocSplitView(UpdateView):
model = Document
template_name = 'storage/edit.html'
form_class = DocSplitForm
def form_valid(self, form):
if not self.request.is_ajax():
chunk_beg = form.cleaned_data['chunk_beg'].sequence
chunk_end = form.cleaned_data['chunk_end'].sequence
chunks = self.object.chunks.all().order_by('sequence')
if chunk_end < chunk_beg:
messages.error(self.request, 'Błąd: Nieprawidłowy zakres akapitów.')
return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': self.object.id}))
max_seq_doc = Document.objects.filter(parent=self.object).order_by('-sequence').first()
if max_seq_doc is None:
sequence = 0
else:
sequence = max_seq_doc.sequence
if self.object.parent is None:
parent = self.object
else:
parent = self.object.parent
self.object = get_doc_with_type(self.object.id)
subdocument_class = SUBDOCUMENT_TYPES[type(self.object)][0]
print('sd class: ', subdocument_class)
new_document = subdocument_class.objects.create(name=parent.name,
lang=parent.lang,
original_lang=parent.original_lang,
pipeline=parent.pipeline,
publication_date=None,
path=parent.path,
type='',
status='',
parent=parent,
processing_status=parent.processing_status,
sequence=sequence + 1)
new_document.chunks.set(chunks.filter(sequence__range=(chunk_beg, chunk_end)))
new_document.save()
self.object.chunks.set(chunks.filter(sequence__lt=chunk_beg) | chunks.filter(sequence__gt=chunk_end))
self.object.changed = True
self.object.save()
return HttpResponseRedirect(self.get_success_url())
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Podział dokumentu'
context['submit_btn_text'] = 'Podziel'
return context
def get_form_kwargs(self):
kwargs = super(DocSplitView, self).get_form_kwargs()
kwargs['chunks'] = self.object.chunks.all().order_by('sequence')
return kwargs
def get_success_url(self):
return reverse_lazy('annotation', kwargs={'doc_id': self.object.id})
def move_subdoc(request, subdoc_id, direction):
subdoc = Document.objects.get(id=subdoc_id)
subdoc_seq = subdoc.sequence
parent_doc = Document.objects.get(id=subdoc_id).parent
max_seq_doc = Document.objects.filter(parent=parent_doc).order_by('-sequence').first()
if max_seq_doc is not None:
max_seq = max_seq_doc.sequence
temp_seq = max_seq + 10
else:
max_seq = 0
temp_seq = 10
swap_subdocs(direction, subdoc, subdoc_seq, temp_seq, max_seq, parent_doc)
return HttpResponseRedirect(reverse('annotation', kwargs={'doc_id': parent_doc.id}))
class RevertSubdocDivisionView(DeleteView):
model = Document
template_name = 'storage/delete.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Cofanie podziału dokumentu'
context['msg'] = 'Czy jesteś pewien, że chcesz cofnąć podział dokumentu? Spowoduje to powrót wszystkich ' \
'akapitów do dokumentu głównego i utratę wszystkich metadanych przypisanych do tego dokumentu.'
context['submit_btn_text'] = 'Cofnij podział'
return context
def get_success_url(self):
return reverse_lazy('annotation', kwargs={'doc_id': self.object.parent.id})
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
chunks = handle_chunks_seq_uniqueness(self.object.chunks.all(), self.object.parent.chunks.all())
self.object.parent.chunks.add(*chunks)
response = super(RevertSubdocDivisionView, self).delete(request, *args, **kwargs)
return response
def download_source(request, doc_id, redirect_view):
source_dir = Document.objects.get(id=doc_id).path
filenames = os.listdir(source_dir)
for filename in filenames:
if os.path.splitext(filename)[1] in ['.pdf', '.doc', '.rtf']:
filepath = source_dir + '/' + filename
path = open(filepath, 'rb')
mime_type, _ = mimetypes.guess_type(filepath)
response = HttpResponse(path, content_type=mime_type)
response['Content-Disposition'] = "attachment; filename=%s" % filename
return response
messages.error(request, 'Błąd: Wybrany dokument nie posiada źródła.')
return HttpResponseRedirect(reverse(redirect_view, kwargs={'doc_id': doc_id}))
class DocumentListView(ListView):
model = Document
paginate_by = 100
def get_context_data(self, **kwargs):
context = super(DocumentListView, self).get_context_data(**kwargs)
context['filter'] = self.request.GET.get('filter', '')
context['orderby'] = self.request.GET.get('orderby', 'id')
context['order'] = self.request.GET.get('order', 'asc')
return context
def get_queryset(self):
filter_val = self.request.GET.get('filter', '')
order_by = self.request.GET.get('orderby', 'id')
order = self.request.GET.get('order', 'asc')
if filter_val != '':
docs = Document.objects.filter(Q(name__icontains=filter_val) |
Q(id__istartswith=filter_val) |
Q(annotations__user__username__icontains=filter_val),
parent__isnull=True,
broken_source=False)
else:
docs = Document.objects.filter(parent__isnull=True,
broken_source=False)
if order == 'desc':
docs = docs.order_by("-" + order_by)
else:
docs = docs.order_by(order_by)
return docs
class RetakeDocForAnnoView(UserPassesTestMixin, RedirectView):
def get_redirect_url(self, *args, **kwargs):
return reverse('document_list')
def get(self, request, *args, **kwargs):
return render(self.request, 'storage/delete.html', {'msg': 'Czy na pewno chcesz zwrócić dokument do ponownej '
'anotacji?',
'submit_btn_text': 'Zwróć'})
def post(self, request, *args, **kwargs):
document = Document.objects.get(id=kwargs['doc_id'])
document.change_processing_status('to_correct')
document.save()
return super().get(request, *args, **kwargs)
def test_func(self):
return self.request.user.groups.filter(name='Editors').exists()
def handle_no_permission(self):
return HttpResponse('Wymagana jest rola redaktora, aby uzyskać dostęp do tej strony.')
class RetakeDocForAnnoForPrevAnnotView(UserPassesTestMixin, RedirectView):
def get_redirect_url(self, *args, **kwargs):
return reverse('document_list')
def get(self, request, *args, **kwargs):
return render(self.request, 'storage/delete.html', {'msg': 'Czy na pewno chcesz zwrócić dokument do ponownej '
'anotacji dla poprzedniego anotatora?',
'submit_btn_text': 'Zwróć'})
def post(self, request, *args, **kwargs):
document = Document.objects.get(id=kwargs['doc_id'])
document.change_processing_status('to_correct')
last_annotator = document.annotations.order_by('-finish_time')[0].user
Annotation.objects.create(user=last_annotator, document=document)
document.save()
return super().get(request, *args, **kwargs)
def test_func(self):
return self.request.user.groups.filter(name='Editors').exists()
def handle_no_permission(self):
return HttpResponse('Wymagana jest rola redaktora, aby uzyskać dostęp do tej strony.')
class DraftListView(ListView):
model = Document
template_name = 'storage/draft_list.html'
def get_context_data(self, **kwargs):
context = super(DraftListView, self).get_context_data(**kwargs)
drafts = [ann.document for ann in
Annotation.objects.filter(user=self.request.user, finished=False).order_by('start_time')
]
context['drafts'] = drafts
return context
def change_doc_type(doc, target_type):
new_doc = target_type.objects.create(name=doc.name,
source_id=doc.source_id,
lang=doc.lang,
original_lang=doc.original_lang,
pipeline=doc.pipeline,
image=doc.image,
broken_source=doc.broken_source,
in_effect=doc.in_effect,
indexed=doc.indexed,
changed=doc.changed,
title=doc.title,
publication_date=doc.publication_date,
publication_place=doc.publication_place,
creation_time=doc.creation_time,
meta_url=doc.meta_url,
source_url=doc.source_url,
file_url=doc.file_url,
path=doc.path,
channel=doc.channel,
type=doc.type,
text_origin=doc.text_origin,
status=doc.status,
processing_status=doc.processing_status,
new=doc.new,
unk_coverage=doc.unk_coverage,
sequence=doc.sequence)
new_doc.keywords.add(*doc.keywords.all())
subdocs = Document.objects.filter(parent=doc)
for subdoc in subdocs:
subdoc.parent = new_doc
subdoc.save()
for ch in doc.chunks.all():
ch.document = new_doc
ch.save()
for m in doc.metadata.all():
m.document = new_doc
m.save()
for p in doc.participants.all():
p.document = new_doc
p.save()
for a in doc.annotations.all():
a.document = new_doc
a.save()
doc.delete()
return new_doc
class DocTypeView(UpdateView):
model = Document
template_name = 'storage/edit.html'
form_class = DocTypeForm
def form_valid(self, form):
if not self.request.is_ajax():
doc_type = form.cleaned_data['doc_type']
new_doc = change_doc_type(self.object, getattr(modules[__name__], doc_type))
return HttpResponseRedirect(self.get_success_url(new_doc))
return render(self.request, self.template_name, {'form': form})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Zmiana typu dokumentu'
context['submit_btn_text'] = 'Zmień'
messages.warning(self.request, 'Uwaga! Zmiana typu dokumentu może spowodować usunięcie danych typowych dla '
'tego typu.')
return context
def get_form_kwargs(self):
kwargs = super(DocTypeView, self).get_form_kwargs()
self.object = get_doc_with_type(self.object.id)
if self.object.parent is None:
doc_types = get_remaining_doc_types_tuple(type(self.object))
else:
doc_types = get_remaining_subdoc_types_tuple(type(get_doc_with_type(self.object.parent.id)),
type(self.object))
if not doc_types:
messages.error(self.request, 'Nie jest możliwa zmiana typu dokumentu. Dla tego typu dokumentu głównego nie '
'ma dostępnych innych typów dokumentów.')
kwargs['doc_types'] = doc_types
return kwargs
def get_success_url(self, new_doc):
return reverse_lazy('annotation', kwargs={'doc_id': new_doc.id})
# ****************************** autocomplete ******************************
def md_name_autocomplete(request):
mds = Metadata.objects.filter(name__istartswith=request.GET.get('term'))
names = list(set([md.name for md in mds]))
return JsonResponse(names, safe=False)
def md_value_autocomplete(request):
mds = Metadata.objects.filter(value__istartswith=request.GET.get('term'))
values = list(set([md.value for md in mds]))
return JsonResponse(values, safe=False)
def kw_label_autocomplete(request):
kws = Keyword.objects.filter(label__istartswith=request.GET.get('term'))
labels = list(set([kw.label for kw in kws]))
return JsonResponse(labels, safe=False)
def details_title_autocomplete(request):
docs = Document.objects.filter(title__istartswith=request.GET.get('term'))
titles = list(set([doc.title for doc in docs]))
return JsonResponse(titles, safe=False)
def details_pub_place_autocomplete(request):
docs = Document.objects.filter(publication_place__istartswith=request.GET.get('term'))
pub_places = list(set([doc.publication_place for doc in docs]))
return JsonResponse(pub_places, safe=False)
def details_channel_autocomplete(request):
docs = Document.objects.filter(channel__istartswith=request.GET.get('term'))
channels = list(set([doc.channel for doc in docs]))
return JsonResponse(channels, safe=False)
def details_type_autocomplete(request):
docs = Document.objects.filter(type__istartswith=request.GET.get('term'))
types = list(set([doc.type for doc in docs]))
return JsonResponse(types, safe=False)
def details_text_origin_autocomplete(request):
docs = Document.objects.filter(text_origin__istartswith=request.GET.get('term'))
origins = list(set([doc.text_origin for doc in docs]))
return JsonResponse(origins, safe=False)
def author_autocomplete(request):
authors = Participant.objects.filter(name__istartswith=request.GET.get('term'))
authors_names = list(set([author.name for author in authors]))
return JsonResponse(authors_names, safe=False)