def record_view(request, content_type='', objectid='', tabid=''): '''The generic view for any type of record: Hand, Scribe, Manuscript''' context = {'tabid': tabid} template = 'errors/404.html' # We need to do a search to show the next and previous record # Only when we come from the the search image. set_search_results_to_context( request, allowed_type=content_type, context=context) for type in context['types']: if type.key == content_type: # Hide if not set as visible in settings.py from digipal.utils import request_invisible_model request_invisible_model(type.get_model(), request, content_type) # Now find templates and populate context context['id'] = objectid from django.core.exceptions import ObjectDoesNotExist try: template = type.process_record_view_request(context, request) if not template.endswith('.html'): # redirect from django.shortcuts import redirect return redirect(template) except ObjectDoesNotExist: context['title'] = 'This %s record does not exist' % type.label_singular template = 'errors/404.html' break return render_to_response(template, context, context_instance=RequestContext(request))
def record_view(request, content_type='', objectid='', tabid=''): '''The generic view for any type of record: Hand, Scribe, Manuscript''' context = {'tabid': tabid} template = 'errors/404.html' # We need to do a search to show the next and previous record # Only when we come from the the search image. set_search_results_to_context(request, allowed_type=content_type, context=context) for type in context['types']: if type.key == content_type: # Hide if not set as visible in settings.py from digipal.utils import request_invisible_model request_invisible_model(type.get_model(), request, content_type) # Now find templates and populate context context['id'] = objectid from django.core.exceptions import ObjectDoesNotExist try: template = type.process_record_view_request(context, request) if not template.endswith('.html'): # redirect from django.shortcuts import redirect return redirect(template) except ObjectDoesNotExist: context[ 'title'] = 'This %s record does not exist' % type.label_singular template = 'errors/404.html' break return render_to_response(template, context, context_instance=RequestContext(request))
def image(request, image_id): """The view for the front-end annotator page""" from digipal.utils import request_invisible_model, raise_404 try: image = Image.objects.get(id=image_id) except Image.DoesNotExist: raise_404('This Image record does not exist') # 404 if content type Image not visible request_invisible_model(Image, request, 'Image') # 404 if image is private and user not staff if image.is_private_for_user(request): raise_404('This Image is currently not publicly available') is_admin = has_edit_permission(request, Image) # annotations_count = image.annotation_set.all().values('graph').count() # annotations = image.annotation_set.all() annotations = Annotation.objects.filter( image_id=image_id, graph__isnull=False).exclude_hidden(is_admin).select_related( 'graph__hand', 'graph__idiograph__allograph') dimensions = { 'width': image.dimensions()[0], 'height': image.dimensions()[1] } hands = image.hands.count() url = request.path url = url.split('/') url.pop(len(url) - 1) url = url[len(url) - 1] # Check for a vector_id in image referral, if it exists the request has # come via Scribe/allograph route vector_id = request.GET.get('graph', '') or request.GET.get( 'vector_id', '') hands_list = [] hand = {} hands_object = Hand.objects.filter(images=image_id) data_allographs = OrderedDict() for h in hands_object.values(): if h['label'] == None: label = "None" else: label = mark_safe(h['label']) hand = {'id': h['id'], 'name': label.encode('cp1252')} hands_list.append(hand) # annotations by allograph for a in annotations: if a.graph and a.graph.hand: hand_label = a.graph.hand allograph_name = a.graph.idiograph.allograph if hand_label in data_allographs: if allograph_name not in data_allographs[hand_label]: data_allographs[hand_label][allograph_name] = [] else: data_allographs[hand_label] = OrderedDict() data_allographs[hand_label][allograph_name] = [] data_allographs[hand_label][allograph_name].append(a) image_link = urlresolvers.reverse('admin:digipal_image_change', args=(image.id, )) form = ImageAnnotationForm(auto_id=False) form.fields['hand'].queryset = image.hands.all() width, height = image.dimensions() image_server_url = image.zoomify zoom_levels = settings.ANNOTATOR_ZOOM_LEVELS from digipal.models import OntographType from digipal.utils import is_model_visible images = Image.objects.none() if image.item_part: images = image.item_part.images.exclude(id=image.id).prefetch_related( 'hands', 'annotation_set') images = Image.filter_permissions_from_request(images, request) images = Image.sort_query_set_by_locus(images, True) from digipal_text.models import TextContentXML context = { 'form': form.as_ul(), 'dimensions': dimensions, 'images': images, 'image': image, 'height': height, 'width': width, 'image_server_url': image_server_url, 'hands_list': hands_list, 'image_link': image_link, 'annotations': annotations.count(), 'annotations_list': data_allographs, 'url': url, 'hands': hands, 'is_admin': is_admin, 'no_image_reason': image.get_media_unavailability_reason(), # True is the user can edit the database 'can_edit': has_edit_permission(request, Annotation), 'ontograph_types': OntographType.objects.order_by('name'), 'zoom_levels': zoom_levels, 'repositories': Repository.objects.filter(currentitem__itempart__images=image_id), # hide all annotations and all annotation tools from the user 'hide_annotations': int(not is_model_visible('graph', request)), 'PAGE_IMAGE_SHOW_MSDATE': settings.PAGE_IMAGE_SHOW_MSDATE, 'text_content_xmls': TextContentXML.objects.filter(text_content__item_part=image.item_part), } if settings.PAGE_IMAGE_SHOW_MSSUMMARY: context['document_summary'] = image.get_document_summary() context['annotations_switch_initial'] = 1 - int( context['hide_annotations'] or ((request.GET.get( 'annotations', 'true')).strip().lower() in ['0', 'false'])) context[ 'show_image'] = context['can_edit'] or not context['no_image_reason'] if vector_id: context['vector_id'] = vector_id return render_to_response('digipal/image_annotation.html', context, context_instance=RequestContext(request))
def search_ms_image_view(request): '''View for the Browse Image page''' hand_filters.chrono('BROWSE:') from digipal.utils import request_invisible_model request_invisible_model('image', request, 'Image') hand_filters.chrono('search:') hand_filters.chrono('all():') images = Image.objects.all() hand_filters.chrono(':all()') from digipal.forms import FilterManuscriptsImages # Get Buttons context = {} context['view'] = request.GET.get('view', 'images') town_or_city = request.GET.get('town_or_city', '') repository = request.GET.get('repository', '') date = request.GET.get('date', '') set_page_sizes_to_context(request, context, [12, 20, 40, 100]) # Applying filters if town_or_city: images = images.filter( item_part__current_item__repository__place__name=town_or_city) if repository: # repo is in two parts: repo place, repo name (e.g. cambridge, corpus christi college) # but we also support old style URL which have only the name of the repo # if we don't, crawlers like Googlebot could receive a 500 error (see # JIRA DIGIPAL-483) repo_parts = [p.strip() for p in repository.split(',')] if repo_parts: images = images.filter( item_part__current_item__repository__name=repo_parts[-1]) if len(repo_parts) > 1: images = images.filter( item_part__current_item__repository__place__name=repo_parts[0]) if date: images = images.filter(hands__assigned_date__date=date) images = images.filter(item_part_id__gt=0) # not sufficient, see JIRA #552 # images = Image.sort_query_set_by_locus(images) # images = list(images.order_by('id')) # from digipal.utils import natural_sort_key # images = sorted(images, key=lambda im: natural_sort_key(im.display_label, True)) # context['images'] = Image.sort_query_set_by_locus(images.prefetch_related('hands', 'annotation_set')) # permission filter # OPT: on DigiPal prefetch_related of annotation_set takes 20s and it retrieves all the fields even # related to linked tables (allograph, character, etc.) # Same with hands which takes around 2/3 s. # images = Image.filter_permissions_from_request(images.prefetch_related('hands', 'annotation_set'), request) images = Image.filter_permissions_from_request(images, request) hand_filters.chrono(':search') # count hands hand_filters.chrono('hands:') from django.db.models import Count context['images'] = Image.sort_query_set_by_locus(images.select_related( 'item_part__current_item__repository__place').annotate(hand_count=Count('hands'))) hand_filters.chrono('hands:') image_search_form = FilterManuscriptsImages(request.GET) context['image_search_form'] = image_search_form context['query_summary'], context['query_summary_interactive'] = get_query_summary( request, '', True, [image_search_form]) hand_filters.chrono('template:') ret = render_to_response('search/search_ms_image.html', context, context_instance=RequestContext(request)) hand_filters.chrono(':template') hand_filters.chrono(':BROWSE') return ret
def image(request, image_id): """The view for the front-end annotator page""" from digipal.utils import request_invisible_model, raise_404 try: image = Image.objects.get(id=image_id) except Image.DoesNotExist: raise_404('This Image record does not exist') # 404 if content type Image not visible request_invisible_model(Image, request, 'Image') # 404 if image is private and user not staff if image.is_private_for_user(request): raise_404('This Image is currently not publicly available') is_admin = has_edit_permission(request, Image) #annotations_count = image.annotation_set.all().values('graph').count() #annotations = image.annotation_set.all() annotations = Annotation.objects.filter(image_id=image_id, graph__isnull=False).exclude_hidden(is_admin).select_related('graph__hand', 'graph__idiograph__allograph') dimensions = { 'width': image.dimensions()[0], 'height': image.dimensions()[1] } hands = image.hands.count() url = request.path url = url.split('/') url.pop(len(url) - 1) url = url[len(url) - 1] # Check for a vector_id in image referral, if it exists the request has # come via Scribe/allograph route vector_id = request.GET.get('graph', '') or request.GET.get('vector_id', '') hands_list = [] hand = {} hands_object = Hand.objects.filter(images=image_id) data_allographs = SortedDict() for h in hands_object.values(): if h['label'] == None: label = "None" else: label = mark_safe(h['label']) hand = {'id': h['id'], 'name': label.encode('cp1252')} hands_list.append(hand) #annotations by allograph for a in annotations: if a.graph and a.graph.hand: hand_label = a.graph.hand allograph_name = a.graph.idiograph.allograph if hand_label in data_allographs: if allograph_name not in data_allographs[hand_label]: data_allographs[hand_label][allograph_name] = [] else: data_allographs[hand_label] = SortedDict() data_allographs[hand_label][allograph_name] = [] data_allographs[hand_label][allograph_name].append(a) image_link = urlresolvers.reverse('admin:digipal_image_change', args=(image.id,)) form = ImageAnnotationForm(auto_id=False) form.fields['hand'].queryset = image.hands.all() width, height = image.dimensions() image_server_url = image.zoomify zoom_levels = settings.ANNOTATOR_ZOOM_LEVELS from digipal.models import OntographType from digipal.utils import is_model_visible images = image.item_part.images.exclude(id=image.id).prefetch_related('hands', 'annotation_set') images = Image.filter_permissions_from_request(images, request) images = Image.sort_query_set_by_locus(images, True) from digipal_text.models import TextContentXML context = { 'form': form.as_ul(), 'dimensions': dimensions, 'images': images, 'image': image, 'height': height, 'width': width, 'image_server_url': image_server_url, 'hands_list': hands_list, 'image_link': image_link, 'annotations': annotations.count(), 'annotations_list': data_allographs, 'url': url, 'hands': hands, 'is_admin': is_admin, 'no_image_reason': image.get_media_unavailability_reason(), # True is the user can edit the database 'can_edit': has_edit_permission(request, Annotation), 'ontograph_types': OntographType.objects.order_by('name'), 'zoom_levels': zoom_levels, 'repositories': Repository.objects.filter(currentitem__itempart__images=image_id), # hide all annotations and all annotation tools from the user 'hide_annotations': int(not is_model_visible('graph', request)), 'PAGE_IMAGE_SHOW_MSDATE': settings.PAGE_IMAGE_SHOW_MSDATE, 'text_content_xmls': TextContentXML.objects.filter(text_content__item_part=image.item_part), } if settings.PAGE_IMAGE_SHOW_MSSUMMARY: context['document_summary'] = image.get_document_summary() context['annotations_switch_initial'] = 1 - int(context['hide_annotations'] or ((request.REQUEST.get('annotations', 'true')).strip().lower() in ['0', 'false'])) context['show_image'] = context['can_edit'] or not context['no_image_reason'] if vector_id: context['vector_id'] = vector_id return render_to_response('digipal/image_annotation.html', context, context_instance=RequestContext(request))
def search_ms_image_view(request): '''View for the Browse Image page''' hand_filters.chrono('BROWSE:') from digipal.utils import request_invisible_model request_invisible_model('image', request, 'Image') hand_filters.chrono('search:') hand_filters.chrono('all():') images = Image.objects.all() hand_filters.chrono(':all()') from digipal.forms import FilterManuscriptsImages # Get Buttons context = {} context['view'] = request.GET.get('view', 'images') town_or_city = request.GET.get('town_or_city', '') repository = request.GET.get('repository', '') date = request.GET.get('date', '') set_page_sizes_to_context(request, context, [12, 20, 40, 100]) # Applying filters if town_or_city: images = images.filter( item_part__current_item__repository__place__name=town_or_city) if repository: # repo is in two parts: repo place, repo name (e.g. cambridge, corpus christi college) # but we also support old style URL which have only the name of the repo # if we don't, crawlers like Googlebot could receive a 500 error (see # JIRA DIGIPAL-483) repo_parts = [p.strip() for p in repository.split(',')] if repo_parts: images = images.filter( item_part__current_item__repository__name=repo_parts[-1]) if len(repo_parts) > 1: images = images.filter( item_part__current_item__repository__place__name=repo_parts[0]) if date: images = images.filter(hands__assigned_date__date=date) images = images.filter(item_part_id__gt=0) # not sufficient, see JIRA #552 # images = Image.sort_query_set_by_locus(images) # images = list(images.order_by('id')) # from digipal.utils import natural_sort_key # images = sorted(images, key=lambda im: natural_sort_key(im.display_label, True)) # context['images'] = Image.sort_query_set_by_locus(images.prefetch_related('hands', 'annotation_set')) # permission filter # OPT: on DigiPal prefetch_related of annotation_set takes 20s and it retrieves all the fields even # related to linked tables (allograph, character, etc.) # Same with hands which takes around 2/3 s. # images = Image.filter_permissions_from_request(images.prefetch_related('hands', 'annotation_set'), request) images = Image.filter_permissions_from_request(images, request) hand_filters.chrono(':search') # count hands hand_filters.chrono('hands:') from django.db.models import Count context['images'] = Image.sort_query_set_by_locus( images.select_related('item_part__current_item__repository__place'). annotate(hand_count=Count('hands'))) hand_filters.chrono('hands:') image_search_form = FilterManuscriptsImages(request.GET) context['image_search_form'] = image_search_form context['query_summary'], context[ 'query_summary_interactive'] = get_query_summary( request, '', True, [image_search_form]) hand_filters.chrono('template:') ret = render_to_response('search/search_ms_image.html', context, context_instance=RequestContext(request)) hand_filters.chrono(':template') hand_filters.chrono(':BROWSE') return ret