Example #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
Example #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
Example #3
0
def image_icon(count, message, url, template_type=None, request=None):
    '''Return the HTML for showing an image icon with a count as a link to another page
        count is the number of images
        message is the message to show in the tooltip (e.g. 'COUNT image')

        e.g.
        {% image_icon hand.images.count "COUNT image with this hand" hand.get_absolute_url|add:"pages" template_type request %}

        TODO: deal with no request, template type and url
    '''

    ret = u''

    if count:
        from django.db.models.query import QuerySet

        # if count is a QuerySet on Image model, convert it into a int
        if hasattr(count, 'all'):
            from digipal.models import Image
            count = Image.filter_permissions_from_request(count.all(), request).count()

        if count:
            m = re.match(ur'(.*)(COUNT)(\s+)(\w*)(.*)', message)
            if m:
                message = ur'%s%s%s%s%s' % (m.group(1), count, m.group(3), plural(m.group(4), count), m.group(5))
            ret = u'''<span class="result-image-count">
                        (<a data-toggle="tooltip" title="%s" href="%s">%s&nbsp;<i class="fa fa-picture-o"></i></a>)
                      </span>''' % (message, add_query_params(u'%s?result_type=%s' % (url, template_type), request.META['QUERY_STRING']), count)

    return mark_safe(ret)
Example #4
0
def image_icon(count, message, url, template_type=None, request=None):
    '''Return the HTML for showing an image icon with a count as a link to another page
        count is the number of images
        message is the message to show in the tooltip (e.g. 'COUNT image')

        e.g.
        {% image_icon hand.images.count "COUNT image with this hand" hand.get_absolute_url|add:"pages" template_type request %}

        TODO: deal with no request, template type and url
    '''

    ret = u''

    if count:
        from django.db.models.query import QuerySet

        # if count is a QuerySet on Image model, convert it into a int
        if hasattr(count, 'all'):
            from digipal.models import Image
            count = Image.filter_permissions_from_request(
                count.all(), request).count()

        if count:
            m = re.match(ur'(.*)(COUNT)(\s+)(\w*)(.*)', message)
            if m:
                message = ur'%s%s%s%s%s' % (m.group(1), count, m.group(
                    3), plural(m.group(4), count), m.group(5))
            ret = u'''<span class="result-image-count">
                        (<a data-toggle="tooltip" title="%s" href="%s">%s&nbsp;<i class="fa fa-picture-o"></i></a>)
                      </span>''' % (message, add_query_params(u'%s?result_type=%s' % (url, template_type), request.META['QUERY_STRING']), count)

    return mark_safe(ret)
Example #5
0
 def queryset(self, request, queryset):
     if self.value() in ['1', '-1']:
         all_duplicates_ids = Image.get_duplicates_from_ids().keys()
     
     if self.value() == '1':
         return queryset.filter(id__in = all_duplicates_ids).distinct()
     if self.value() == '-1':
         return queryset.exclude(id__in = all_duplicates_ids).distinct()
Example #6
0
    def queryset(self, request, queryset):
        if self.value() in ['1', '-1']:
            all_duplicates_ids = Image.get_duplicates_from_ids().keys()

        if self.value() == '1':
            return queryset.filter(id__in=all_duplicates_ids).distinct()
        if self.value() == '-1':
            return queryset.exclude(id__in=all_duplicates_ids).distinct()
Example #7
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
Example #8
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))
Example #9
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))
Example #10
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
Example #11
0
    def handle(self, *args, **options):

        root = get_image_path()
        if not root:
            raise CommandError('Path variable IMAGE_SERVER_ROOT not set in your settings file.')
        if not isdir(root):
            raise CommandError('Image path not found (%s).' % root)
        if len(args) < 1:
            raise CommandError('Please provide a command. Try "python manage.py help dpdb" for help.')
        command = args[0]
        if options['db'] not in settings.DATABASES:
            raise CommandError('Database settings not found ("%s"). Check DATABASE array in your settings.py.' % options['db'])

        db_settings = settings.DATABASES[options['db']]

        self.args = args

        self.options = options

        known_command = False
        counts = {'online': 0, 'disk': 0, 'disk_only': 0, 'missing': 0}
        if command == 'fetch':
            known_command = True
            self.fetch(*args, **options)

        if command == 'update_dimensions':
            known_command = True
            self.update_dimensions(*args)
            
        if command == 'move_annotations':
            known_command = True
            self.move_annotations()

        if command in ('list', 'upload', 'unstage', 'update', 'remove', 'crop'):
            known_command = True

            for file_info in self.get_all_files(root):
                file_relative = file_info['path']
                found_message = ''

                online = (file_info['image'] is not None)
                imageid = 0
                if online:
                    imageid = file_info['image'].id

                if not self.is_filtered_in(file_info):
                    continue

                if options['offline'] and online:
                    continue

                if options['missing'] and file_info['disk']:
                    continue

                if not online:
                    found_message = '[OFFLINE]'
                elif not file_info['disk']:
                    found_message = '[MISSING FROM DISK]'
                else:
                    found_message = '[ONLINE]'

                if online:
                    counts['online'] += 1
                if file_info['disk']:
                    counts['disk'] += 1
                if file_info['disk'] and not online:
                    counts['disk_only'] += 1
                if not file_info['disk'] and online:
                    counts['missing'] += 1

                processed = False

                if (command == 'crop' and online):
                    self.crop_image(file_info)
                    processed = True

                if (command == 'upload' and not online) or (command == 'update' and online):
                    processed = True

                    file_path, basename = os.path.split(file_relative)
                    new_file_name = os.path.join(file_path, re.sub(r'(\s|,)+', '_' , basename.lower()))
                    if re.search(r'\s|,', new_file_name):
                        found_message = '[FAILED: please remove spaces and commas from the directory names]'
                    else:
                        image = None
                        if command in ('update',):
                            found_message = '[JUST UPDATED]'
                            image = file_info['image']
                        else:
                            found_message = '[JUST UPLOADED]'
                            image = Image()
                            image.iipimage = file_relative
                            image.image = 'x'
                            image.caption = os.path.basename(file_relative)
                            # todo: which rules should we apply here?
                            image.display_label = os.path.basename(file_relative)

                        # convert the image to jp2/tif
                        if command == 'upload':
                            error_message = self.convert_image(image)
                            if error_message:
                                found_message += ' ' + error_message
                                image = None
                            else:
                                found_message += ' [JUST CONVERTED]'

                        if image:
                            image.save()
                            imageid = image.id

                if command == 'remove' and file_info['disk']:
                    file_abs_path = join(settings.IMAGE_SERVER_ROOT, file_relative)
                    print file_abs_path
                    if os.path.exists(file_abs_path):
                        os.unlink(file_abs_path)
                        found_message = '[REMOVED FROM DISK]'
                    else:
                        found_message = '[NOT FOUND]'
                    processed = True

                if command == 'unstage' and online:
                    processed = True

                    found_message = '[JUST REMOVED FROM DB]'
                    file_info['image'].delete()

                if self.is_verbose() or command == 'list' or processed:
                    extra = ''
                    if not file_info['disk'] and online and file_info['image'].image is not None and len(file_info['image'].image.name) > 2:
                        extra = file_info['image'].image.name
                    print '#%s\t%-20s\t%s\t%s' % (imageid, found_message, file_relative, extra)

            print '%s images in DB. %s image on disk. %s on disk only. %s missing from DB.' % (counts['online'], counts['disk'], counts['disk_only'], counts['missing'])

        if command in ['copy', 'originals', 'copy_convert', 'pocket']:
            known_command = True
            self.processOriginals(args, options)

        if not known_command:
            raise CommandError('Unknown command: "%s".' % command)
Example #12
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')
Example #13
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
Example #14
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
Example #15
0
    def handle(self, *args, **options):

        root = get_image_path()
        if not root:
            raise CommandError(
                'Path variable IMAGE_SERVER_ROOT not set in your settings file.'
            )
        if not isdir(root):
            raise CommandError('Image path not found (%s).' % root)
        if len(args) < 1:
            raise CommandError(
                'Please provide a command. Try "python manage.py help dpdb" for help.'
            )
        command = args[0]
        if options['db'] not in settings.DATABASES:
            raise CommandError(
                'Database settings not found ("%s"). Check DATABASE array in your settings.py.'
                % options['db'])

        db_settings = settings.DATABASES[options['db']]

        self.args = args

        self.options = options

        known_command = False
        counts = {'online': 0, 'disk': 0, 'disk_only': 0, 'missing': 0}
        if command == 'fetch':
            known_command = True
            self.fetch(*args, **options)

        if command == 'update_dimensions':
            known_command = True
            self.update_dimensions(*args)

        if command == 'move_annotations':
            known_command = True
            self.move_annotations()

        if command in ('list', 'upload', 'unstage', 'update', 'remove',
                       'crop'):
            known_command = True

            for file_info in self.get_all_files(root):
                file_relative = file_info['path']
                found_message = ''

                online = (file_info['image'] is not None)
                imageid = 0
                if online:
                    imageid = file_info['image'].id

                if not self.is_filtered_in(file_info):
                    continue

                if options['offline'] and online:
                    continue

                if options['missing'] and file_info['disk']:
                    continue

                if not online:
                    found_message = '[OFFLINE]'
                elif not file_info['disk']:
                    found_message = '[MISSING FROM DISK]'
                else:
                    found_message = '[ONLINE]'

                if online:
                    counts['online'] += 1
                if file_info['disk']:
                    counts['disk'] += 1
                if file_info['disk'] and not online:
                    counts['disk_only'] += 1
                if not file_info['disk'] and online:
                    counts['missing'] += 1

                processed = False

                if (command == 'crop' and online):
                    self.crop_image(file_info)
                    processed = True

                if (command == 'upload' and not online) or (command == 'update'
                                                            and online):
                    processed = True

                    file_path, basename = os.path.split(file_relative)
                    new_file_name = os.path.join(
                        file_path, re.sub(r'(\s|,)+', '_', basename.lower()))
                    if re.search(r'\s|,', new_file_name):
                        found_message = '[FAILED: please remove spaces and commas from the directory names]'
                    else:
                        image = None
                        if command in ('update', ):
                            found_message = '[JUST UPDATED]'
                            image = file_info['image']
                        else:
                            found_message = '[JUST UPLOADED]'
                            image = Image()
                            image.iipimage = file_relative
                            image.image = 'x'
                            image.caption = os.path.basename(file_relative)
                            # todo: which rules should we apply here?
                            image.display_label = os.path.basename(
                                file_relative)

                        # convert the image to jp2/tif
                        if command == 'upload':
                            error_message = self.convert_image(image)
                            if error_message:
                                found_message += ' ' + error_message
                                image = None
                            else:
                                found_message += ' [JUST CONVERTED]'

                        if image:
                            image.save()
                            imageid = image.id

                if command == 'remove' and file_info['disk']:
                    file_abs_path = join(settings.IMAGE_SERVER_ROOT,
                                         file_relative)
                    print file_abs_path
                    if os.path.exists(file_abs_path):
                        os.unlink(file_abs_path)
                        found_message = '[REMOVED FROM DISK]'
                    else:
                        found_message = '[NOT FOUND]'
                    processed = True

                if command == 'unstage' and online:
                    processed = True

                    found_message = '[JUST REMOVED FROM DB]'
                    file_info['image'].delete()

                if self.is_verbose() or command == 'list' or processed:
                    extra = ''
                    if not file_info['disk'] and online and file_info[
                            'image'].image is not None and len(
                                file_info['image'].image.name) > 2:
                        extra = file_info['image'].image.name
                    print '#%s\t%-20s\t%s\t%s' % (imageid, found_message,
                                                  file_relative, extra)

            print '%s images in DB. %s image on disk. %s on disk only. %s missing from DB.' % (
                counts['online'], counts['disk'], counts['disk_only'],
                counts['missing'])

        if command in ['copy', 'originals', 'copy_convert', 'pocket']:
            known_command = True
            self.processOriginals(args, options)

        if not known_command:
            raise CommandError('Unknown command: "%s".' % command)