This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
932 lines (759 loc) · 35.9 KB
/
views.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
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
#imports of python modules
import json
#import sys
import re
import random
#import os
import sys
#Imports of django modules
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import render
from django.utils import translation
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from apps.utils.decorators import t_login_required_ajax
from apps.utils.utils import crop, t_metadata, t_log
from apps.utils.services import *
from apps.utils.views import *
#Imports from app (library)
import settings
import apps.library.settings
from apps.navigation import navigation
import apps.library.settings
#from .forms import RegisterForm, IngestMetsUrlForm, MetsFileForm, QuickIngestMetsUrlForm
def index(request):
return render(request, 'library/homepage.html' )
#/library
#view that lists available collections for a user
@login_required
def collections(request):
t = request.user.tsdata.t
collections = t.collections(request,{'empty':'true'})
if isinstance(collections,HttpResponse):
return apps.utils.views.error_view(request,collections)
return render(request, 'library/collections.html', {'collections': collections} )
#/library/{colId}
#view that
# - lists documents
# - also lists pages for documents
@login_required
def collection(request, collId):
t = request.user.tsdata.t
#Avoid this sort of nonsense if possible
collections = t.collections(request,{'end':None,'start':None,'empty':'true'})
if isinstance(collections,HttpResponse):
return apps.utils.views.error_view(request,collections)
navdata = navigation.get_nav(collections,collId,'colId','colName')
#if we didn't have a focus before navigation call, we'll have one after
collection = navdata.get("focus")
collIdParam = {'collId': collId}
personParam = {'collId': collId, 'tagName': 'person'}
placeParam = {'collId': collId, 'tagName': 'place'}
dateParam = {'collId': collId, 'tagName': 'date'}
abbrevParam = {'collId': collId, 'tagName': 'abbrev'}
otherParam = {'collId': collId, 'tagName': 'other'}
personCount = t.countCollTags(request,personParam)
if isinstance(personCount,HttpResponse):
return apps.utils.views.error_view(request,personCount)
placeCount = t.countCollTags(request,placeParam)
if isinstance(placeCount,HttpResponse):
return apps.utils.views.error_view(request,placeCount)
dateCount = t.countCollTags(request,dateParam)
if isinstance(dateCount,HttpResponse):
return apps.utils.views.error_view(request,dateCount)
abbrevCount = t.countCollTags(request,abbrevParam)
if isinstance(abbrevCount,HttpResponse):
return apps.utils.views.error_view(request,abbrevCount)
otherCount = t.countCollTags(request,otherParam)
if isinstance(otherCount,HttpResponse):
return apps.utils.views.error_view(request,otherCount)
tagsString = getTagsString(personCount, placeCount, dateCount, abbrevCount, otherCount)
print(tagsString)
collStat = t.collStat(request, collIdParam)
if isinstance(collStat,HttpResponse):
return apps.utils.views.error_view(request,collStat)
if (collection):
print("print collection: " + str(collection))
collection['collStat'] = '%i words' % collStat.get('nrOfWords')
collection['tagsString'] = tagsString
print("print navdata: "+str(navdata))
pagedata = {'collection': collection}
#merge the dictionaries
combidata = pagedata.copy()
combidata.update(navdata)
return render(request, 'library/collection.html', combidata)
#/library/{colId}/{docId}
# view that lists pages in doc and some doc level metadata
@login_required
def document(request, collId, docId, page=None):
t = request.user.tsdata.t
collection = t.collection(request, {'collId': collId})
if isinstance(collection,HttpResponse):
return apps.utils.views.error_view(request,collection)
fulldoc = t.document(request, collId, docId,-1)
if isinstance(fulldoc,HttpResponse):
return apps.utils.views.error_view(request,fulldoc)
nav = navigation.up_next_prev(request,"document",docId,collection,[collId])
navdata = navigation.get_nav(collection,docId,'docId','title')
#if we didn't have a focus before navigation call, we'll have one after
#document = navdata.get("focus")
pagedata = {'document': fulldoc}
#merge the dictionaries
# combidata = pagedata.copy()
# combidata.update(navdata)
#merge the dictionaries
combidata = pagedata.copy()
combidata.update(navdata)
return render(request, 'library/document.html', combidata)
def _get_collection(document, collId):
for collection in document['collectionList']['colList']:
if int(collection['colId']) == int(collId):
return collection
def collection_metadata(request, collId):
t = request.user.tsdata.t
collections = t.collection(request, {'collId': collId})
if isinstance(collections,HttpResponse):
return apps.utils.views.error_view(request,collections)
collection = _get_collection(collections[0], collId)
#Add collection of data on last saved page (aka recent)
recent = t.collection_recent(request,{'collId': collId, 'userid' : request.user.tsdata.userId})
if isinstance(recent,HttpResponse):
recent = None
#Don't stop if we don't get recents back from actions/list
# return apps.utils.views.error_view(request,recent)
#last save data for the doc (generally... not specific to the current user)
recent_save = t.document_recent(request,{'collId': collId})
if isinstance(recent_save,HttpResponse):
recent_save = None
#Don't stop if we don't get recents back from actions/list
# return apps.utils.views.error_view(request,recent_save)
title_desc = ''
title_desc += '<p><b>%s</b> (%s)</p>' % (collection['colName'], collId)
if 'description' in collection:
title_desc += '<p id="long_text_%s">%s</p>' % (collId, collection['description'])
if recent_save :
#recent will be a single item array
if 'time' in recent_save[0]:
title_desc += '<p>%s</p>' % (_('Page %(pageNr)s of %(docName)s last saved on %(time)s</p>') % {'pageNr': recent_save[0].get('pageNr'), 'docName': recent_save[0].get('docName'), 'time': apps.utils.templatetags.str_to_date(recent_save[0].get('time'))})
if recent :
#recent will be a single item array
if 'pageNr' in recent[0]:
title_desc += str(_('<p>Go to <i>your</i> <a href="%s">last saved page</a> in this collection.</p>')) % reverse('edit:correct', args=[collId,recent[0].get('docId'),recent[0].get('pageNr')])
#Add collection stats from metadata call
stats = t.collection_metadata(request,{'collId':collId})
if isinstance(stats,HttpResponse):
return apps.utils.views.error_view(request,stats)
total_pages = stats.get('nrOfNew') + stats.get('nrOfInProgress') + stats.get('nrOfDone') + stats.get('nrOfFinal') + stats.get('nrOfGT')
pc_new = int(round((int(stats.get('nrOfNew'))/total_pages) * 100))
pc_ip = int(round((int(stats.get('nrOfInProgress'))/total_pages) * 100))
pc_done = int(round((int(stats.get('nrOfDone'))/total_pages) * 100))
pc_final = int(round((int(stats.get('nrOfFinal'))/total_pages) * 100))
pc_gt = int(round((int(stats.get('nrOfGT'))/total_pages) * 100))
big_table = ''
big_table += '<table class="center-block"><tr>'
big_table += '<th colspan="2" class="embedded-stats-table-heading">%s</th></tr>' % _('Progress')
big_table += _add_row(pc_new, _('New'))
big_table += _add_row(pc_ip, _('In progress'))
big_table += _add_row(pc_done, _('Done'))
big_table += _add_row(pc_final, _('Final'))
big_table += _add_row(pc_gt, _('Ground truth'))
big_table += '</table>'
return JsonResponse({'titleDesc': title_desc, 'stats': stats, 'big_stats_table': big_table}, safe=False)
def _add_row(val, name):
html = ''
html += '<tr>'
html += '<th>%s</th><td>%s%%</td>' % (name, val)
html += '</tr>'
return html
#Fetch a single thumb url from the document referenced
def document_metadata(request, collId, docId):
import timeit
t = request.user.tsdata.t
#links direct to the various views of the document
view_links = '<ul class="list-unstyled text-center twi-view-link-list">'
view_links += '<li class=""><a href="%s?i=i">%s</a></li>' % (reverse('edit:correct', args=[collId, docId, 1]),_('Image'))
view_links += '<li class=""><a href="%s?i=lbl">%s</a></li>' % (reverse('edit:correct', args=[collId, docId, 1]),_('Line by line'))
# view_links += '<li class=""><a href="%s?i=sbs">%s</a></li>' % (reverse('edit:correct', args=[collId, docId, 1]),_('Side by side'))
# view_links += '<li class=""><a href="%s?i=t">%s</a></li>' % (reverse('edit:correct', args=[collId, docId, 1]),_('Text'))
view_links += '</ul>'
#Get data for tags used in this doc
personCount = t.countDocTags(request,{'collId': collId, 'docId': docId, 'tagName': 'person'})
placeCount = t.countDocTags(request,{'collId': collId, 'docId': docId, 'tagName': 'place'})
dateCount = t.countDocTags(request,{'collId': collId, 'docId': docId, 'tagName': 'date'})
abbrevCount = t.countDocTags(request,{'collId': collId, 'docId': docId, 'tagName': 'abbrev'})
otherCount = t.countDocTags(request,{'collId': collId, 'docId': docId, 'tagName': 'other'})
#Most of the metadata can be got from fulldoc now
fulldoc = t.document(request, collId, docId,-1)
stats = fulldoc.get('md')
#data on last saved page for current user (aka recent)
recent = t.document_recent(request,{'id': docId, 'userid' : request.user.tsdata.userId})
if isinstance(recent,HttpResponse):
#Don't stop if we don't get recents back from actions/list
recent = None
#return apps.utils.views.error_view(request,recent)
#last save data for the doc (generally... not specific to the current user)
recent_save = t.document_recent(request,{'id': docId})
if isinstance(recent_save,HttpResponse):
#Don't stop if we don't get recents back from actions/list
recent_save = None
#return apps.utils.views.error_view(request,recent_save)
#title and description
title_desc = ''
title_desc += '<p><b>%s</b> (%s)</p>' % (stats.get('title'), docId)
if 'desc' in stats:
title_desc += '<p id="long_text_%s">%s</p>' % (docId, stats.get('desc'))
if recent_save :
#recent will be a single item array
if 'time' in recent_save[0]:
title_desc += '<p>%s</p>' % (_('Page %(pageNr)s last saved on %(time)s</p>') % {'pageNr': recent_save[0].get('pageNr'), 'time': apps.utils.templatetags.str_to_date(recent_save[0].get('time'))})
if recent :
#recent will be a single item array
if 'pageNr' in recent[0]:
title_desc += '<p>%s</p>' % (_('Go to <i>your</i> <a href="%(link)s">last saved page</a> in this document') % {'link': reverse('edit:correct', args=[collId,recent[0].get('docId'),recent[0].get('pageNr')])})
#derive proportion of pages in various states
total_pages = stats.get('nrOfNew') + stats.get('nrOfInProgress') + stats.get('nrOfDone') + stats.get('nrOfFinal') + stats.get('nrOfGT')
pc_new = int(round((int(stats.get('nrOfNew'))/total_pages) * 100))
pc_ip = int(round((int(stats.get('nrOfInProgress'))/total_pages) * 100))
pc_done = int(round((int(stats.get('nrOfDone'))/total_pages) * 100))
pc_final = int(round((int(stats.get('nrOfFinal'))/total_pages) * 100))
pc_gt = int(round((int(stats.get('nrOfGT'))/total_pages) * 100))
#build a mini table of the stats to embed in the main table
stats_table = '<table class="embedded-stats-table">'
stats_table += '<tr><th colspan="2" class="embedded-stats-table-heading">%s</th></tr>' % _('Available for editing')
stats_table += '<tr><th>%s</th><td>%s</td></tr>' % (_('Lines'), stats.get('nrOfTranscribedLines'))
stats_table += '<tr><th>%s</th><td>%s</td></tr>' % (_('Words'), stats.get('nrOfWordsInLines'))
stats_table += '<tr><th colspan="2" class="embedded-stats-table-heading">%s</th></tr>' % _('Status of pages')
stats_table += '<tr><th>%s</th><td>%s%%</td></tr>' % (_('New'), pc_new)
stats_table += '<tr><th>%s</th><td>%s%%</td></tr>' % (_('In Progress'), pc_ip)
stats_table += '<tr><th>%s</th><td>%s%%</td></tr>' % (_('Done'), pc_done)
stats_table += '<tr><th>%s</th><td>%s%%</td></tr>' % (_('Final'), pc_final)
stats_table += '<tr><th>%s</th><td>%s%%</td></tr>' % (_('Ground Truth'), pc_gt)
stats_table += '<tr><th colspan="2" class="embedded-stats-table-heading">%s</th></tr>' % _('Tags')
stats_table += '<tr><th>%s</th><td>%s</td></tr>' % (_('People'), personCount)
stats_table += '<tr><th>%s</th><td>%s</td></tr>' % (_('Places'), placeCount)
stats_table += '<tr><th>%s</th><td>%s</td></tr>' % (_('Dates'), dateCount)
stats_table += '<tr><th>%s</th><td>%s</td></tr>' % (_('Abbreviations'), abbrevCount)
stats_table += '<tr><th>%s</th><td>%s</td></tr>' % (_('Other'), otherCount)
stats_table += '</table>'
edit_fields = []
edit_fields += [(_('Lines'), stats.get('nrOfTranscribedLines'))]
edit_fields += [(_('Words'), stats.get('nrOfWords'))]
status_pages = []
status_pages += [(_('New'), pc_new)]
status_pages += [(_('In Progress'), pc_ip)]
status_pages += [(_('Done'), pc_done)]
status_pages += [(_('Final'), pc_final)]
status_pages += [(_('Ground Truth'), pc_gt)]
status_pages = [(name, str(val)+'%') for name, val in status_pages]
tags = []
tags += [(_('People'), personCount)]
tags += [(_('Places'), placeCount)]
tags += [(_('Dates'), dateCount)]
tags += [(_('Abbreviations'), abbrevCount)]
tags += [(_('Other'), otherCount)]
max_vals = max([len(edit_fields), len(status_pages), len(tags)])
edit_fields += [None] * (max_vals-len(edit_fields))
status_pages += [None] * (max_vals-len(status_pages))
tags += [None] * (max_vals-len(tags))
big_table = ''
big_table += '<table class="center-block"><tr>'
big_table += '<th colspan="2" class="embedded-stats-table-heading">%s</th>' % _('Available for editing')
big_table += '<th colspan="2" class="embedded-stats-table-heading">%s</th> ' % _('Status of pages')
big_table += '<th colspan="2" class="embedded-stats-table-heading">%s</th></tr>' % _('Tags')
for edit_row, status_row, tag_row in zip(edit_fields, status_pages, tags):
big_table += '<tr>'
if edit_row:
big_table += '<th>%s</th><td>%s</td>' % edit_row
else:
big_table += '<th></th><td></td>'
if status_row:
big_table += '<th>%s</th><td>%s</td>' % status_row
else:
big_table += '<th></th><td></td>'
if tag_row:
big_table += '<th>%s</th><td>%s</td>' % tag_row
else:
big_table += '<th></th><td></td>'
big_table += '</tr>'
big_table += '</table>'
return JsonResponse({
'titleDesc': title_desc,
'viewLinks': view_links,
'thumbUrl': stats.get('thumbUrl'),
'stats_table': stats_table,
'big_stats_table': big_table,
},safe=False)
@login_required
def document_page(request, collId, docId, page=None):
t = request.user.tsdata.t
collection = t.collection(request, {'collId': collId})
if isinstance(collection,HttpResponse):
return apps.utils.views.error_view(request,collection)
full_doc = t.document(request, collId, docId,-1)
if isinstance(full_doc,HttpResponse):
return apps.utils.views.error_view(request,full_doc)
if (page is None):
page = 1
index = int(page)-1
#extract page data from full_doc (may be better from a separate page data request)
pagedata = full_doc.get('pageList').get('pages')[index]
# transcripts = pagedata.get('tsList').get('transcripts')
sys.stdout.write((str(request)))
sys.stdout.write((str(request)).rsplit('/', 1)[0])
sys.stdout.flush()
startStr = (str(request)).rsplit('/', 1)[0]
#nav = navigation.up_next_prev(startStr,"document",docId,collection,[collId])
navdata = navigation.get_nav(collection,collId,'docId','title')
#
# sys.stdout.write("pagedata url : %s \r\n" % pagedata["url"])
# sys.stdout.flush()
#new for fetching all text regions and text of all pages
textlines = []
current_transcript = t.current_transcript(request, collId, docId, page)
transcript = t.transcript(request, current_transcript.get("tsId"),current_transcript.get("url"))
regions=transcript.get("PcGts").get("Page").get("TextRegion");
if isinstance(regions, dict):
regions = [regions]
#
#
lineList = []
if regions:
sys.stdout.write("number of regions on this page : %s \r\n" % len(regions))
sys.stdout.flush()
for y in regions:
if y is not None:
lines = y.get("TextLine")
#region_width = crop(x.get("Coords").get("@points"), 1).get('w')
if lines:
if isinstance(lines, dict):
#lines['regionWidth'] = region_width
lineList.extend([lines])
else: # Assume that lines is a list of lines
if lines is not None:
for line in lines:
#line['regionWidth'] = region_width
lineList.extend([line])
if lineList:
for line in lineList:
if line.get('TextEquiv') is not None:
unicode_string = line.get('TextEquiv').get('Unicode')
else:
unicode_string = "";
line['Unicode'] = unicode_string
line_crop = crop(line.get("Coords").get("@points"))#,True)
line['crop'] = line_crop
line_id = line.get("@id")
line['id'] = line_id
# paginator = Paginator(full_doc.get('pageList').get('pages'), 10) # Show 5 docs per page
# page = request.GET.get('page')
# try:
# doclist = paginator.page(page)
# except PageNotAnInteger:
# # If page is not an integer, deliver first page.
# doclist = paginator.page(1)
# except EmptyPage:
# # If page is out of range (e.g. 9999), deliver last page of results.
# doclist = paginator.page(paginator.num_pages)
return render(request, 'library/document_page.html', {
'metadata': full_doc.get('md'),
'textlines': lineList,
'pageList': full_doc.get('pageList'),
'collId': int(collId),
'docId': int(docId),
'pageNr': page,
'pagedata': pagedata
# 'nav_up': nav['up'],
# 'nav_next': nav['next'],
# 'nav_prev': nav['prev'],
})
#/library/{colId}/{docId}/{page}
# view that lists transcripts in doc and some page level metadata
@login_required
def page(request, collId, docId, page):
t = request.user.tsdata.t
#call t_document with noOfTranscript=-1 which will return no transcript data
full_doc = t.document(request, collId, docId, -1)
if isinstance(full_doc,HttpResponse):
return apps.utils.views.error_view(request,full_doc)
# big wodge of data from full doc includes data for each page and for each page, each transcript...
index = int(page)-1
#extract page data from full_doc (may be better from a separate page data request)
pagedata = full_doc.get('pageList').get('pages')[index]
transcripts = pagedata.get('tsList').get('transcripts')
# sys.stdout.write("############## PAGEDATA: %s\r\n" % ( pagedata ) )
# the way xmltodict parses multiple instances of tags means that if there is one <transcripts> we get a dict,
# if there is > 1 we get a list. Solution: put dict in list if dict (or get json from transkribus which is
# parsed better, but not yet available)
if isinstance(transcripts, dict):
transcripts = [transcripts]
# sys.stdout.write("############## PAGEDATA.TRANSCRIPTS: %s\r\n" % ( transcripts ) )
nav = navigation.up_next_prev(request,"page",page,full_doc.get("pageList").get("pages"),[collId,docId])
return render(request, 'library/page.html', {
'pagedata': pagedata,
'transcripts': transcripts,
'nav_up': nav['up'],
'nav_next': nav['next'],
'nav_prev': nav['prev'],
'collId': collId,
'docId': docId,
})
#/library/{colId}/{docId}/{page}/{tsId}
# view that lists regions in transcript and some transcript level metadata
@login_required
def transcript(request, collId, docId, page, transcriptId):
t = request.user.tsdata.t
#t_page returns an array of the transcripts for a page
pagedata = t.page(request, collId, docId, page)
if isinstance(pagedata,HttpResponse):
return apps.utils.views.error_view(request,pagedata)
nav = navigation.up_next_prev(request,"transcript",transcriptId,pagedata,[collId,docId,page])
pageXML_url = None;
for x in pagedata:
if int(x.get("tsId")) == int(transcriptId):
pageXML_url = x.get("url")
break
sys.stdout.write("PAGEXML URL : %s \r\n" % (pageXML_url) )
sys.stdout.flush()
if pageXML_url:
transcript = t.transcript(request,transcriptId,pageXML_url)
if isinstance(transcript,HttpResponse):
return apps.utils.views.error_view(request,transcript)
regions=transcript.get("PcGts").get("Page").get("TextRegion");
if isinstance(regions, dict):
regions = [regions]
if regions:
for x in regions:
sys.stdout.write("CUSTOM : %s \r\n" % (x.get("@custom")) )
sys.stdout.flush()
x['md'] = t_metadata(x.get("@custom"))
return render(request, 'library/transcript.html', {
'transcript' : transcript,
'regions' : regions,
'nav_up': nav['up'],
'nav_next': nav['next'],
'nav_prev': nav['prev'],
'collId': collId,
'docId': docId,
'pageId': page, #NB actually the number for now
})
#/library/{colId}/{docId}/{page}/{tsId}/{regionId}
# view that lists lines in region and some region level metadata
@login_required
def region(request, collId, docId, page, transcriptId, regionId):
t = request.user.tsdata.t
# We need to be able to target a transcript (as mentioned elsewhere)
# here there is no need for anything over than the pageXML really
# we could get one transcript from ...{page}/curr, but for completeness would
# rather use transciptId to target a particular transcript
transcripts = t.page(request,collId, docId, page)
if isinstance(transcripts,HttpResponse):
return apps.utils.views.error_view(request,transcripts)
#To get the page image url we need the full_doc (we hope it's been cached)
full_doc = t.document(request, collId, docId, -1)
if isinstance(full_doc,HttpResponse):
return apps.utils.views.error_view(request,full_doc)
index = int(page)-1
# and then extract the correct page from full_doc (may be better from a separate page data request??)
pagedata = full_doc.get('pageList').get('pages')[index]
t_log("############# TRANSCRIPTS: %s" % transcripts )
#we are only using the transcripts to get the pageXML for a particular transcript...
pageXML_url = None;
for x in transcripts:
if int(x.get("tsId")) == int(transcriptId):
t_log("############# transcript id comp: %s" % x.get("tsId") )
t_log("############# transcript id comp: %s" % transcriptId )
pageXML_url = x.get("url")
break
t_log("############# PAGEXML_url: %s" % pageXML_url )
if pageXML_url:
transcript = t.transcript(request,transcriptId,pageXML_url)
if isinstance(transcript,HttpResponse):
return apps.utils.views.error_view(request,transcript)
regions=transcript.get("PcGts").get("Page").get("TextRegion");
if isinstance(regions, dict):
regions = [regions]
for x in regions:
x['key'] = x.get("@id")
if(str(regionId) == str(x.get("@id"))):
region = x
if(region.get("Coords")):
region['crop'] = crop(region.get("Coords").get("@points"),True)
nav = navigation.up_next_prev(request,"region",regionId,regions,[collId,docId,page,transcriptId])
# sys.stdout.write("REGION: %s\r\n" % (region) )
# sys.stdout.flush()
lines = region.get("TextLine")
if isinstance(lines, dict):
lines = [lines]
#parse metadata
if lines:
for x in lines:
x['md'] = t_metadata(x.get("@custom"))
return render(request, 'library/region.html', {
'region' : region,
'lines' : lines,
'nav_up': nav['up'],
'nav_next': nav['next'],
'nav_prev': nav['prev'],
'collId': collId,
'docId': docId,
'pageId': page, #NB actually the number for now
'transcriptId': transcriptId,
'imageUrl' : pagedata.get("url"),
})
#/library/{colId}/{docId}/{page}/{tsId}/{regionId}/{lineId}
# view that lists words in line and some line level metadata
@login_required
def line(request, collId, docId, page, transcriptId, regionId, lineId):
t = request.user.tsdata.t
# We need to be able to target a transcript (as mentioned elsewhere)
# here there is no need for anything over than the pageXML really
# we could get one transcript from ...{page}/curr, but for completeness would
# rather use transciptId to target a particular transcript
transcripts = t.page(request,collId, docId, page)
if isinstance(transcripts,HttpResponse):
return apps.utils.views.error_view(request,transcripts)
#we are only using the transcripts to get the pageXML for a particular
pageXML_url = None;
for x in transcripts:
if int(x.get("tsId")) == int(transcriptId):
pageXML_url = x.get("url")
break
if pageXML_url:
transcript = t.transcript(request,transcriptId,pageXML_url)
if isinstance(transcript,HttpResponse):
return apps.utils.views.error_view(request,transcript)
#To get the page image url we need the full_doc (we hope it's been cached)
full_doc = t.document(request, collId, docId, -1)
if isinstance(full_doc,HttpResponse):
return apps.utils.views.error_view(request,full_doc)
index = int(page)-1
# and then extract the correct page from full_doc (may be better from a separate page data request??)
pagedata = full_doc.get('pageList').get('pages')[index]
#This now officially bonkers....
regions=transcript.get("PcGts").get("Page").get("TextRegion");
if isinstance(regions, dict):
regions = [regions]
for x in regions:
if(str(regionId) == str(x.get("@id"))):
region = x
lines=region.get("TextLine");
if isinstance(lines, dict):
lines = [lines]
for x in lines:
x['key'] = x.get("@id")
if(str(lineId) == str(x.get("@id"))):
line = x
if(line.get("Coords")):
line['crop'] = crop(line.get("Coords").get("@points"),True)
nav = navigation.up_next_prev(request,"line",lineId,lines,[collId,docId,page,transcriptId,regionId])
# sys.stdout.write("REGION: %s\r\n" % (region) )
# sys.stdout.flush()
words = line.get("Word")
if isinstance(words, dict):
words = [words]
#parse metadata
if words:
for x in words:
x['md'] = t_metadata(x.get("@custom"))
return render(request, 'library/line.html', {
'line' : line,
'words' : words,
'nav_up': nav['up'],
'nav_next': nav['next'],
'nav_prev': nav['prev'],
'collId': collId,
'docId': docId,
'pageId': page, #NB actually the number for now
'transcriptId': transcriptId,
'regionId': regionId,
'lineId': lineId,
'imageUrl' : pagedata.get("url"),
})
#/library/{colId}/{docId}/{page}/{tsId}/{regionId}/{lineId}/{wordId}
# view that shows some word level metadata
@login_required
def word(request, collId, docId, page, transcriptId, regionId, lineId, wordId):
t = request.user.tsdata.t
# booo hiss
transcripts = t.page(request, collId, docId, page)
if isinstance(transcripts,HttpResponse):
return apps.utils.views.error_view(request,transcripts)
#we are only using the pagedata to get the pageXML for a particular
pageXML_url = None;
for x in transcripts:
if int(x.get("tsId")) == int(transcriptId):
pageXML_url = x.get("url")
break
if pageXML_url:
transcript = t.transcript(request,transcriptId,pageXML_url)
if isinstance(transcript,HttpResponse):
return apps.utils.views.error_view(request,transcript)
#To get the page image url we need the full_doc (we hope it's been cached)
full_doc = t.document(request, collId, docId, -1)
if isinstance(full_doc,HttpResponse):
return apps.utils.views.error_view(request,full_doc)
index = int(page)-1
# and then extract the correct page from full_doc (may be better from a separate page data request??)
pagedata = full_doc.get('pageList').get('pages')[index]
#This now officially bonkers....
regions=transcript.get("PcGts").get("Page").get("TextRegion");
if isinstance(regions, dict):
regions = [regions]
for x in regions:
if(str(regionId) == str(x.get("@id"))):
region = x
lines=region.get("TextLine");
if isinstance(lines, dict):
lines = [lines]
for x in lines:
if(str(lineId) == str(x.get("@id"))):
line = x
words = line.get("Word")
if isinstance(words, dict):
words = [words]
#parse metadata
for x in words:
x['key'] = x.get("@id")
if(str(wordId) == str(x.get("@id"))):
x['md'] = t_metadata(x.get("@custom"))
word = x
if(word.get("Coords")):
word['crop'] = crop(word.get("Coords").get("@points"),True)
nav = navigation.up_next_prev(request,"word",wordId,words,[collId,docId,page,transcriptId,regionId,lineId])
return render(request, 'library/word.html', {
'word' : word,
'nav_up': nav['up'],
'nav_next': nav['next'],
'nav_prev': nav['prev'],
'collId': collId,
'docId': docId,
'pageId': page, #NB actually the number for now
'transcriptId': transcriptId,
'regionId': regionId,
'lineId': lineId,
'imageUrl' : pagedata.get("url"),
})
# Randomly fetch region/line/word this gives us an awful lot of empty responses
# Ideally we want to filter out the transcripts that don't contain good qulity data
# This may be as simple as isPublished(), rather than any analysis on the content
@login_required
def rand(request, collId, element):
t = request.user.tsdata.t
collection = t.collection(request, {'collId': collId})
if isinstance(collection,HttpResponse):
return apps.utils.views.error_view(request,collection)
doc = random.choice(collection)
collection = None
for x in doc.get("collectionList").get("colList"):
if str(x.get("colId")) == str(collId):
collection = x
pages = t.document(request, collId, doc.get("docId"), 0)
if isinstance(pages,HttpResponse):
return apps.utils.views.error_view(request,pages)
page = random.choice(pages.get("pageList").get("pages"))
sys.stdout.write("RANDOM PAGE: %s\r\n" % (page.get("pageNr")) )
sys.stdout.flush()
#best to avoid a random transcript, so we'll go for the current in the hope that it is best....
current_transcript = t.current_transcript(request, collId, doc.get("docId"), page.get("pageNr"))
transcript = t.transcript(request, current_transcript.get("tsId"),current_transcript.get("url"))
word = None
line = None
region = None
regions = transcript.get("PcGts").get("Page").get("TextRegion")
if isinstance(regions, dict):
regions = [regions]
if regions:
region = random.choice(regions)
if element == "region" :
sys.stdout.write("region I have\r\n" )
lines = region.get("TextLine")
else:
if transcript.get("PcGts").get("Page").get("TextLine"):
# I don't think we ever get here.. need to check with UIBK if Page > TextLine is even possible
sys.stdout.write("I HAVE A LINE DIRECT IN PAGE\r\n" )
sys.stdout.flush()
lines = transcript.get("PcGts").get("Page").get("TextLine")
else:
sys.stdout.write("NO TEXT IN REGION\r\n" )
return render(request, 'library/random.html', {
"level": element,
"text": {},
"collection" : collection,
"document" : doc,
"page" : page,
"transcript" : transcript,
} )
if isinstance(lines, dict):
lines = [lines]
if element in ['line', 'word'] :
if lines:
line = random.choice(lines);
else:
return render(request, 'library/random.html', {
"level": element,
"text": {},
"collection" : collection,
"document" : doc,
"page" : page,
"transcript" : transcript,
} )
sys.stdout.write("LINE: %s\r\n" % ( line ) )
if element == "word" :
words = line.get("Word")
if isinstance(words, dict):
words = [words]
if words:
word = random.choice(words);
else:
return render(request, 'library/random.html', {
"level": element,
"text": {},
"collection" : collection,
"document" : doc,
"page" : page,
"transcript" : transcript,
} )
switcher = {
"region" : display_random(request,element,region,collection,doc,page),
"line" : display_random(request,element,line,collection,doc,page),
"word" : display_random(request,element,word,collection,doc,page),
}
return switcher.get(element, {})
def display_random(request,level,data, collection, doc, page):
text = None
if not data :
text = {}
elif data.get("TextEquiv"):
if data.get("TextEquiv").get("Unicode"):
text = str(data.get("TextEquiv").get("Unicode"))
return render(request, 'library/random.html', {
"level": level,
"text": text,
"collection" : collection,
"document" : doc,
"page" : page,
} )
@login_required
def users(request, collId, userId):
return render(request, 'library/users.html')
def getTagsString(personCount, placeCount, dateCount, abbrevCount, otherCount):
if (personCount>0) or (placeCount>0) or (dateCount>0) or (abbrevCount>0) or (otherCount>0):
tagsStringParts = []
if personCount > 0:
tagsStringParts += ['%i persons' % personCount]
if placeCount > 0:
tagsStringParts += ['%i places' % placeCount]
if dateCount > 0:
tagsStringParts += ['%i dates' % dateCount]
if abbrevCount > 0:
tagsStringParts += ['%i abbrevs ' % abbrevCount]
if otherCount > 0:
tagsStringParts += ['%i others' % otherCount]
return 'Tags: ' + ', '.join(tagsStringParts)
else:
return 'No Tags'