def form_dialog(request, image_id): image = Image.objects.get(id=image_id) form = ImageAnnotationForm(auto_id=False) form.fields['hand'].queryset = image.hands.all() return render_to_response('digipal/dialog.html', { 'form': form, 'edit_annotation_shape': getattr(settings, 'EDIT_ANNOTATION_SHAPE', False) }, context_instance=RequestContext(request))
def save_editorial(request, graphs): if settings.ARCHETYPE_API_READ_ONLY: # transaction.rollback() raise Http404 else: data = {'success': False, 'graphs': []} if not graphs or len(graphs) == 0: raise Exception('No data provided') try: graphs = graphs.replace('/"', "'") graphs = json.loads(graphs) for gr in graphs: image = Image.objects.get(id=gr['image']) get_data = request.POST.copy() _id = gr['id'] count = 0 if _id and _id.isdigit(): count = Annotation.objects.filter(image=image, id=_id).count() if 'geoJson' in gr: geo_json = str(gr['geoJson']) else: geo_json = False if count > 0: annotation = Annotation.objects.get(image=image, id=_id) else: annotation = Annotation(image=image, type='editorial') form = ImageAnnotationForm(data=get_data) if form.is_valid(): with transaction.atomic(): clean = form.cleaned_data if geo_json: annotation.geo_json = geo_json # set the note (only if different) - see JIRA # DIGIPAL-477 for f in ['display_note', 'internal_note']: if getattr(annotation, f) != clean[f] and f in get_data: setattr(annotation, f, clean[f]) if not annotation.id: # set the author only when the annotation is # created annotation.author = request.user if geo_json: annotation.set_graph_group() annotation.save() new_graph = [{}] if 'vector_id' in gr: new_graph[0]['vector_id'] = gr['vector_id'] new_graph[0]['annotation_id'] = unicode(annotation.id) if has_edit_permission(request, Annotation): new_graph[0][ 'internal_note'] = annotation.internal_note new_graph[0]['display_note'] = annotation.display_note data['graphs'].append(new_graph[0]) # transaction.commit() data['success'] = True # uncomment this to see the error call stack in the django server output # except ValueError as e: except Exception as e: data['success'] = False data['errors'] = [u'Internal error: %s' % e] # tb = sys.exc_info()[2] return HttpResponse(json.dumps(data), content_type='application/json')
def save(request, graphs): """Saves an annotation and creates a cutout of the annotation.""" if settings.ARCHETYPE_API_READ_ONLY: # transaction.rollback() raise Http404 else: data = {'success': False, 'graphs': []} try: graphs = graphs.replace('/"', "'") graphs = json.loads(graphs) for gr in graphs: graph_object = False if 'id' in gr: graph_object = Graph.objects.get(id=gr['id']) image = Image.objects.get(id=gr['image']) annotation_is_modified = False if graph_object: annotation = graph_object.annotation graph = graph_object else: graph = Graph() annotation = Annotation(image=image) get_data = request.POST.copy() if 'geoJson' in gr: geo_json = str(gr['geoJson']) else: geo_json = False form = ImageAnnotationForm(data=get_data) if form.is_valid(): with transaction.atomic(): clean = form.cleaned_data if geo_json: annotation.geo_json = geo_json annotation_is_modified = True # set the note (only if different) - see JIRA # DIGIPAL-477 for f in ['display_note', 'internal_note']: if getattr(annotation, f) != clean[f]: setattr(annotation, f, clean[f]) annotation_is_modified = True if not annotation.id: # set the author only when the annotation is # created annotation.author = request.user # annotation.before = clean['before'] # annotation.after = clean['after'] allograph = clean['allograph'] hand = clean['hand'] if hand and allograph: scribe = hand.scribe # GN: if this is a new Graph, it has no idiograph # yet, so we test this first if graph.id and (allograph.id != graph.idiograph.allograph.id): graph.graph_components.all().delete() idiograph_list = Idiograph.objects.filter( allograph=allograph, scribe=scribe) if idiograph_list: idiograph = idiograph_list[0] idiograph.id else: idiograph = Idiograph(allograph=allograph, scribe=scribe) idiograph.save() graph.idiograph = idiograph graph.hand = hand graph.save() # error is here feature_list_checked = get_data.getlist('feature') feature_list_unchecked = get_data.getlist('-feature') if feature_list_unchecked: for value in feature_list_unchecked: cid, fid = value.split('::') component = Component.objects.get(id=cid) feature = Feature.objects.get(id=fid) gc_list = GraphComponent.objects.filter( graph=graph, component=component) if gc_list: gc = gc_list[0] gc.features.remove(feature) gc.save() if not gc.features.all(): gc.delete() if feature_list_checked: for value in feature_list_checked: cid, fid = value.split('::') component = Component.objects.get(id=cid) feature = Feature.objects.get(id=fid) gc_list = GraphComponent.objects.filter( graph=graph, component=component) if gc_list: gc = gc_list[0] else: gc = GraphComponent(graph=graph, component=component) gc.save() gc.features.add(feature) gc.save() aspects = get_data.getlist('aspect') aspects_deleted = get_data.getlist('-aspect') if aspects: for aspect in aspects: aspect_model = Aspect.objects.get(id=aspect) graph.aspects.add(aspect_model) if aspects_deleted: for aspect in aspects_deleted: aspect_model = Aspect.objects.get(id=aspect) graph.aspects.remove(aspect_model) graph.save() # Only save the annotation if it has been modified (or new one) # see JIRA DIGIPAL-477 if annotation_is_modified or not annotation.id: annotation.graph = graph annotation.save() # attach the graph to a containing one # cannot be called BEFORE saving the # annotation/graph if geo_json: annotation.set_graph_group() new_graph = json.loads(get_features(graph.id)) if 'vector_id' in gr: new_graph[0]['vector_id'] = gr['vector_id'] if has_edit_permission(request, Annotation): new_graph[0][ 'internal_note'] = annotation.internal_note new_graph[0]['display_note'] = annotation.display_note data['graphs'].append(new_graph[0]) # transaction.commit() data['success'] = True else: # transaction.rollback() data['success'] = False data['errors'] = get_json_error_from_form_errors(form) # uncomment this to see the error call stack in the django server output # except ValueError as e: except Exception as e: data['success'] = False data['errors'] = [u'Internal error: %s' % e] # tb = sys.exc_info()[2] return HttpResponse(json.dumps(data), content_type='application/json')
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 save_editorial(request, graphs): if settings.REJECT_HTTP_API_REQUESTS: # transaction.rollback() raise Http404 else: data = { 'success': False, 'graphs': [] } if not graphs or len(graphs) == 0: raise Exception('No data provided') try: graphs = graphs.replace('/"', "'") graphs = json.loads(graphs) for gr in graphs: image = Image.objects.get(id=gr['image']) get_data = request.POST.copy() _id = gr['id'] count = 0 if _id and _id.isdigit(): count = Annotation.objects.filter(image=image, id=_id).count() if 'geoJson' in gr: geo_json = str(gr['geoJson']) else: geo_json = False if count > 0: annotation = Annotation.objects.get(image=image, id=_id) else: annotation = Annotation(image=image, type='editorial') form = ImageAnnotationForm(data=get_data) if form.is_valid(): with transaction.atomic(): clean = form.cleaned_data if geo_json: annotation.geo_json = geo_json # set the note (only if different) - see JIRA DIGIPAL-477 for f in ['display_note', 'internal_note']: if getattr(annotation, f) != clean[f] and f in get_data: setattr(annotation, f, clean[f]) if not annotation.id: # set the author only when the annotation is created annotation.author = request.user if geo_json: annotation.set_graph_group() annotation.save() new_graph = [{}] if 'vector_id' in gr: new_graph[0]['vector_id'] = gr['vector_id'] new_graph[0]['annotation_id'] = unicode(annotation.id) if has_edit_permission(request, Annotation): new_graph[0]['internal_note'] = annotation.internal_note new_graph[0]['display_note'] = annotation.display_note data['graphs'].append(new_graph[0]) #transaction.commit() data['success'] = True # uncomment this to see the error call stack in the django server output #except ValueError as e: except Exception as e: data['success'] = False data['errors'] = [u'Internal error: %s' % e] #tb = sys.exc_info()[2] return HttpResponse(json.dumps(data), content_type='application/json')
def save(request, graphs): """Saves an annotation and creates a cutout of the annotation.""" if settings.REJECT_HTTP_API_REQUESTS: # transaction.rollback() raise Http404 else: data = { 'success': False, 'graphs': [] } try: graphs = graphs.replace('/"', "'") graphs = json.loads(graphs) for gr in graphs: graph_object = False if 'id' in gr: graph_object = Graph.objects.get(id=gr['id']) image = Image.objects.get(id=gr['image']) annotation_is_modified = False if graph_object: annotation = graph_object.annotation graph = graph_object else: graph = Graph() annotation = Annotation(image=image) get_data = request.POST.copy() if 'geoJson' in gr: geo_json = str(gr['geoJson']) else: geo_json = False form = ImageAnnotationForm(data=get_data) if form.is_valid(): with transaction.atomic(): clean = form.cleaned_data if geo_json: annotation.geo_json = geo_json annotation_is_modified = True # set the note (only if different) - see JIRA DIGIPAL-477 for f in ['display_note', 'internal_note']: if getattr(annotation, f) != clean[f]: setattr(annotation, f, clean[f]) annotation_is_modified = True if not annotation.id: # set the author only when the annotation is created annotation.author = request.user #annotation.before = clean['before'] #annotation.after = clean['after'] allograph = clean['allograph'] hand = clean['hand'] if hand and allograph: scribe = hand.scribe # GN: if this is a new Graph, it has no idiograph yet, so we test this first if graph.id and (allograph.id != graph.idiograph.allograph.id): graph.graph_components.all().delete() idiograph_list = Idiograph.objects.filter(allograph=allograph, scribe=scribe) if idiograph_list: idiograph = idiograph_list[0] idiograph.id else: idiograph = Idiograph(allograph=allograph, scribe=scribe) idiograph.save() graph.idiograph = idiograph graph.hand = hand graph.save() # error is here feature_list_checked = get_data.getlist('feature') feature_list_unchecked = get_data.getlist('-feature') if feature_list_unchecked: for value in feature_list_unchecked: cid, fid = value.split('::') component = Component.objects.get(id=cid) feature = Feature.objects.get(id=fid) gc_list = GraphComponent.objects.filter(graph=graph, component=component) if gc_list: gc = gc_list[0] gc.features.remove(feature) gc.save() if not gc.features.all(): gc.delete() if feature_list_checked: for value in feature_list_checked: cid, fid = value.split('::') component = Component.objects.get(id=cid) feature = Feature.objects.get(id=fid) gc_list = GraphComponent.objects.filter(graph=graph, component=component) if gc_list: gc = gc_list[0] else: gc = GraphComponent(graph=graph, component=component) gc.save() gc.features.add(feature) gc.save() aspects = get_data.getlist('aspect') aspects_deleted = get_data.getlist('-aspect') if aspects: for aspect in aspects: aspect_model = Aspect.objects.get(id=aspect) graph.aspects.add(aspect_model) if aspects_deleted: for aspect in aspects_deleted: aspect_model = Aspect.objects.get(id=aspect) graph.aspects.remove(aspect_model) graph.save() # Only save the annotation if it has been modified (or new one) # see JIRA DIGIPAL-477 if annotation_is_modified or not annotation.id: annotation.graph = graph annotation.save() # attach the graph to a containing one # cannot be called BEFORE saving the annotation/graph if geo_json: annotation.set_graph_group() new_graph = json.loads(get_features(graph.id)) if 'vector_id' in gr: new_graph[0]['vector_id'] = gr['vector_id'] if has_edit_permission(request, Annotation): new_graph[0]['internal_note'] = annotation.internal_note new_graph[0]['display_note'] = annotation.display_note data['graphs'].append(new_graph[0]) #transaction.commit() data['success'] = True else: #transaction.rollback() data['success'] = False data['errors'] = get_json_error_from_form_errors(form) # uncomment this to see the error call stack in the django server output #except ValueError as e: except Exception as e: data['success'] = False data['errors'] = [u'Internal error: %s' % e] #tb = sys.exc_info()[2] return HttpResponse(json.dumps(data), content_type='application/json')
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))