Exemple #1
0
 def get_visible_images(item_partid, request, visible_images=None):
     if visible_images is None:
         ret = Image.objects.filter(item_part_id=item_partid)
         ret = Image.filter_permissions_from_request(ret, request, True)
         ret = Image.sort_query_set_by_locus(ret)
         visible_images = ret
     return visible_images
Exemple #2
0
 def get_visible_images(item_partid, request, visible_images=None):
     if visible_images is None:
         ret = Image.objects.filter(item_part_id=item_partid)
         ret = Image.filter_permissions_from_request(ret, request, True)
         ret = Image.sort_query_set_by_locus(ret)
         visible_images = ret
     return visible_images
Exemple #3
0
def text_api_view_search(request, item_partid, content_type, location_type, location, max_size=None):
    '''
        location = an identifier for the image. Relative to the item part
                    '#1000' => image with id = 1000
                    '1r'    => image with locus = 1r attached to selected item part
    '''
    ret = {}

    from digipal.templatetags.html_escape import iip_img
    from digipal.models import Image

    # return the locus of the images under this item part
    # return #ID for images which have no locus
    if location_type == 'default' or utils.get_int_from_request_var(request, 'load_locations'):
        ret['locations'] = OrderedDict()
        ret['locations']['locus'] = ['%s' % (rec[0] or '#%s' % rec[1]) for rec in Image.sort_query_set_by_locus(
            Image.objects.filter(item_part_id=item_partid)).values_list('locus', 'id')]

    # resolve 'default' location request
    location_type, location = resolve_default_location(
        location_type, location, ret)

    query = request.GET.get('query', '')
    entries = ''
    hit_count = 0
    if query:
        tcx = TextContentXML.objects.filter(
            text_content__type__slug='translation', text_content__item_part__id=item_partid).first()
        if tcx:
            for hit in get_entries_from_query(query):
                hit_count += 1
                entries += '<li><a data-location-type="entry" href="%s">%s</a><br/>%s</li>' % (
                    hit['entryid'], hit['entryid'], hit['snippets'])

            # import re
            # match = re.search()
            # tcx.content
            # print tcx

    # e.g. if the requested location is 'default' we resolve it
    from django.utils.html import escape
    ret['location_type'] = location_type
    ret['location'] = location

    ret['content'] = ur'''<form class="text-search-form" method="GET" style="margin:0.2em">
        <p>Query: <input type="text" class="control" name="query" value="%s"/><input type="submit" name="s" value="Search"/></p>
        <p>%s entries</p>
        <ul>
            %s
        </ul>
    </form>''' % (escape(query), hit_count, entries)

    return ret
Exemple #4
0
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))
Exemple #5
0
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))
Exemple #6
0
def text_api_view_image(request, item_partid, content_type, location_type, location, content_type_record, max_size=None, ignore_sublocation=False):
    '''
        location = an identifier for the image. Relative to the item part
                    '#1000' => image with id = 1000
                    '1r'    => image with locus = 1r attached to selected item part
    '''
    ret = {}

    from digipal.models import Image

    # ##
    # The sub_location can override or contradict (location_type, location)
    # e.g. text: whole -> image: synced with text
    #      user clicks on entry in the text => we need to fetch that part
    if not ignore_sublocation:
        sub_location = get_sub_location_from_request(request)
        new_address = get_address_from_sub_location(sub_location)
        if new_address:
            location_type, location = new_address
    # ##

    request.visible_images = None

    visible_images = None

    def get_visible_images(item_partid, request, visible_images=None):
        if visible_images is None:
            ret = Image.objects.filter(item_part_id=item_partid)
            ret = Image.filter_permissions_from_request(ret, request, True)
            ret = Image.sort_query_set_by_locus(ret)
            visible_images = ret
        return visible_images

    # return the locus of the images under this item part
    # return #ID for images which have no locus
    if location_type == 'default' or utils.get_int_from_request_var(request, 'load_locations'):
        recs = Image.sort_query_set_by_locus(get_visible_images(
            item_partid, request, visible_images)).values_list('locus', 'id')
        ret['locations'] = OrderedDict()
        if recs:
            ret['locations']['locus'] = ['%s' %
                                         (rec[0] or '#%s' % rec[1]) for rec in recs]

    # resolve 'default' location request
    location_type, location = resolve_default_location(
        location_type, location, ret)

    # find the image
    image = find_image(request, item_partid, location_type,
                       location, get_visible_images, visible_images)

    if not image:
        set_message(ret, 'Image not found')
        ret['location_type'] = location_type
        ret['location'] = location
    else:
        if location_type == 'entry':
            # user asked for entry, we can only return a locus
            # so we add the entry as a sublocation
            ret['sub_location'] = ['', 'location'], [
                'loctype', 'entry'], ['@text', location]

        if request.method == 'POST':
            # deal with writing annotations
            update_text_image_link(request, image, ret)
        else:
            # display settings
            ret['presentation_options'] = [
                ["highlight", "Highlight Text Units"]]

            # image dimensions
            options = {}
            layout = dputils.get_request_var(request, 'layout', '')
            if layout == 'width':
                options['width'] = dputils.get_request_var(
                    request, 'width', '100')

            # we return the location of the returned fragment
            # this may not be the same as the requested location
            # e.g. if the requested location is 'default' we resolve it
            # ret['location_type'] = location_type
            ret['location_type'] = 'locus'
            ret['location'] = image.locus if image else location

            if image:
                # ret['content'] = iip_img(image, **options)
                ret['zoomify_url'] = image.zoomify()
                ret['width'] = image.width
                ret['height'] = image.height

                # add all the elements found on that page in the transcription
                # ret['text_elements'] = get_text_elements_from_image(request, item_partid, getattr(settings, 'TEXT_IMAGE_MASTER_CONTENT_TYPE', 'transcription'), location_type, location)
                ret['text_elements'] = get_text_elements_from_image(request, item_partid, getattr(
                    settings, 'TEXT_IMAGE_MASTER_CONTENT_TYPE', 'transcription'), 'locus', get_locus_from_location(location_type, location))

                # print ret['text_elements']

                # add all the non-graph annotations
                ret.update(get_annotations_from_image(image))

    return ret
Exemple #7
0
 def get_queryset(self, *args, **kwargs):
     qs = super(SortedChangeList,
                self).get_queryset(*args, **kwargs)
     return Image.sort_query_set_by_locus(qs).prefetch_related(
         'annotation_set', 'hands').select_related('item_part')
Exemple #8
0
def text_api_view_image(request,
                        item_partid,
                        content_type,
                        location_type,
                        location,
                        content_type_record,
                        max_size=None,
                        ignore_sublocation=False):
    '''
        location = an identifier for the image. Relative to the item part
                    '#1000' => image with id = 1000
                    '1r'    => image with locus = 1r attached to selected item part
    '''
    ret = {}

    from digipal.models import Image

    # ##
    # The sub_location can override or contradict (location_type, location)
    # e.g. text: whole -> image: synced with text
    #      user clicks on entry in the text => we need to fetch that part
    if not ignore_sublocation:
        sub_location = get_sub_location_from_request(request)
        new_address = get_address_from_sub_location(sub_location)
        if new_address:
            location_type, location = new_address
    # ##

    request.visible_images = None

    visible_images = None

    def get_visible_images(item_partid, request, visible_images=None):
        if visible_images is None:
            ret = Image.objects.filter(item_part_id=item_partid)
            ret = Image.filter_permissions_from_request(ret, request, True)
            ret = Image.sort_query_set_by_locus(ret)
            visible_images = ret
        return visible_images

    # return the locus of the images under this item part
    # return #ID for images which have no locus
    if location_type == 'default' or utils.get_int_from_request_var(
            request, 'load_locations'):
        recs = Image.sort_query_set_by_locus(
            get_visible_images(item_partid, request,
                               visible_images)).values_list('locus', 'id')
        ret['locations'] = OrderedDict()
        if recs:
            ret['locations']['locus'] = [
                '%s' % (rec[0] or '#%s' % rec[1]) for rec in recs
            ]

    # resolve 'default' location request
    location_type, location = resolve_default_location(location_type, location,
                                                       ret)

    # find the image
    image = find_image(request, item_partid, location_type, location,
                       get_visible_images, visible_images)

    if not image:
        set_message(ret, 'Image not found')
        ret['location_type'] = location_type
        ret['location'] = location
    else:
        if location_type == 'entry':
            # user asked for entry, we can only return a locus
            # so we add the entry as a sublocation
            ret['sub_location'] = ['',
                                   'location'], ['loctype',
                                                 'entry'], ['@text', location]

        if request.method == 'POST':
            # deal with writing annotations
            update_text_image_link(request, image, ret)
        else:
            # display settings
            ret['presentation_options'] = [[
                "highlight", "Highlight Text Units"
            ]]

            # image dimensions
            options = {}
            layout = dputils.get_request_var(request, 'layout', '')
            if layout == 'width':
                options['width'] = dputils.get_request_var(
                    request, 'width', '100')

            # we return the location of the returned fragment
            # this may not be the same as the requested location
            # e.g. if the requested location is 'default' we resolve it
            # ret['location_type'] = location_type
            ret['location_type'] = 'locus'
            ret['location'] = image.locus if image else location

            if image:
                # ret['content'] = iip_img(image, **options)
                ret['zoomify_url'] = image.zoomify()
                ret['width'] = image.width
                ret['height'] = image.height

                # add all the elements found on that page in the transcription
                # ret['text_elements'] = get_text_elements_from_image(request, item_partid, getattr(settings, 'TEXT_IMAGE_MASTER_CONTENT_TYPE', 'transcription'), location_type, location)
                ret['text_elements'] = get_text_elements_from_image(
                    request, item_partid,
                    getattr(settings, 'TEXT_IMAGE_MASTER_CONTENT_TYPE',
                            'transcription'), 'locus',
                    get_locus_from_location(location_type, location))

                # print ret['text_elements']

                # add all the non-graph annotations
                ret.update(get_annotations_from_image(image))

    return ret
Exemple #9
0
def text_api_view_search(request,
                         item_partid,
                         content_type,
                         location_type,
                         location,
                         max_size=None):
    '''
        location = an identifier for the image. Relative to the item part
                    '#1000' => image with id = 1000
                    '1r'    => image with locus = 1r attached to selected item part
    '''
    ret = {}

    from digipal.templatetags.html_escape import iip_img
    from digipal.models import Image

    # return the locus of the images under this item part
    # return #ID for images which have no locus
    if location_type == 'default' or utils.get_int_from_request_var(
            request, 'load_locations'):
        ret['locations'] = OrderedDict()
        ret['locations']['locus'] = [
            '%s' % (rec[0] or '#%s' % rec[1])
            for rec in Image.sort_query_set_by_locus(
                Image.objects.filter(
                    item_part_id=item_partid)).values_list('locus', 'id')
        ]

    # resolve 'default' location request
    location_type, location = resolve_default_location(location_type, location,
                                                       ret)

    query = request.GET.get('query', '')
    entries = ''
    hit_count = 0
    if query:
        tcx = TextContentXML.objects.filter(
            text_content__type__slug='translation',
            text_content__item_part__id=item_partid).first()
        if tcx:
            for hit in get_entries_from_query(query):
                hit_count += 1
                entries += '<li><a data-location-type="entry" href="%s">%s</a><br/>%s</li>' % (
                    hit['entryid'], hit['entryid'], hit['snippets'])

            # import re
            # match = re.search()
            # tcx.content
            # print tcx

    # e.g. if the requested location is 'default' we resolve it
    from django.utils.html import escape
    ret['location_type'] = location_type
    ret['location'] = location

    ret['content'] = ur'''<form class="text-search-form" method="GET" style="margin:0.2em">
        <p>Query: <input type="text" class="control" name="query" value="%s"/><input type="submit" name="s" value="Search"/></p>
        <p>%s entries</p>
        <ul>
            %s
        </ul>
    </form>''' % (escape(query), hit_count, entries)

    return ret