def __init__(self, faceted_search, context, request): # if True, we group all the results by document to avoid stacking # graphs or clauses self.faceted_search = faceted_search self.settings = self.faceted_search.settings.getGlobal('visualisation') self.context = context self.request = request # an index used to detect data point overlap and create stack # [y][[x0, x1], [x0, x1]] self.stack = {} self.cat_hits = {} self.font_size = 12 # margin on each side of a label (in pixels) self.margin = 3 self.font_size_margin = self.font_size + 2 * self.margin self.bar_height = utils.get_int_from_request_var(request, 'vz_bh', 7) self.graph_size = utils.get_int_from_request_var(request, 'vz_gs', 40) context['viz'] = {} context['viz']['vz_bh'] = self.bar_height context['viz']['vz_gs'] = self.graph_size self.mins = [None, None] self.maxs = [None, None] # histogram at the bottom of the chart with the sum of all the ocurrences # above # format = {0: {0: 3, 1: 2}, 1: {0:2, 1:1}, ... } # format = {X/year: {layer index: bar count, layer index: bar count, # ...}, ...} self.histogram = {} self.histogram_height = 0 self.band_width = 1000000
def __init__(self, faceted_search, context, request): # if True, we group all the results by document to avoid stacking graphs or clauses self.faceted_search = faceted_search self.context = context self.request = request # an index used to detect data point overlap and create stack # [y][[x0, x1], [x0, x1]] self.stack = {} self.cat_hits = {} self.font_size = 12 # margin on each side of a label (in pixels) self.margin = 3 self.font_size_margin = self.font_size + 2 * self.margin self.bar_height = utils.get_int_from_request_var(request, 'vz_bh', 7) self.graph_size = utils.get_int_from_request_var(request, 'vz_gs', 40) context['viz'] = {} context['viz']['vz_bh'] = self.bar_height context['viz']['vz_gs'] = self.graph_size self.mins = [None, None] self.maxs = [None, None] # histogram at the bottom of the chart with the sum of all the ocurrences # above # format = {0: {0: 3, 1: 2}, 1: {0:2, 1:1}, ... } # format = {X/year: {layer index: bar count, layer index: bar count, ...}, ...} self.histogram = {} self.histogram_height = 0 self.band_width = 1000000
def text_api_view_location(request, item_partid, content_type, location_type, location, user=None, max_size=MAX_FRAGMENT_SIZE): '''This content type is for the list of all available locations (text, images) Used by the master location widget on top of the Text Viewer web page ''' from digipal.models import ItemPart load_locations = utils.get_int_from_request_var(request, 'load_locations') if load_locations: context = {'item_part': ItemPart.objects.filter( id=item_partid).first()} resolve_master_location(context, location_type, location) ret = { 'location_type': context['master_location_type'], 'location': context['master_location'], 'locations': context['master_locations'], 'toc': context.get('master_toc', {'39a2': 'toc2', '39r': 'toc3', '39a1': 'toc1'}), } else: ret = { 'location_type': location_type, 'location': location, } return ret
def set_from_request(self, request, faceted_search): ''' Query 0: a full search on the currently selected result type (always there) Query 1: the current result set (but if it's a full search, it is invalidated) Query 2, ...: the query saved in the query string. qi_X=Y ''' if self.index < 1: self.is_valid = 1 if self.index == 1: if not faceted_search.is_full_search(): self.is_valid = 1 index = utils.get_int_from_request_var(request, 'qi', 1) if self.index == index: self.is_active = 1 self.request = request self.request_current = request if request.GET.get('q%s_result_type' % (self.index), ''): self.is_valid = 1 self.is_hidden = utils.get_int_from_request_var( request, 'q%s_hidden' % self.index, 0) # if self.is_active: if self.index < 2: self.faceted_search = faceted_search else: from digipal.views.faceted_search.faceted_search import simple_search new_url = '/?' url_is_set = False for k, v in self.request.GET.iteritems(): if k.startswith('q%s_' % self.index): new_url += '&%s=%s' % (k.replace('q%s_' % self.index, ''), v) url_is_set = True if url_is_set: from django.test.client import RequestFactory request = RequestFactory().get(new_url) user = self.request.user self.request = request self.request.user = user self.faceted_search = simple_search(request)
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
def text_api_view_text(request, item_partid, content_type, location_type, location, content_type_record, user=None, max_size=MAX_FRAGMENT_SIZE): ret = {} text_content_xml = None if not user and request: user = request.user # print 'content type %s' % content_type_record # 1. Fetch or Create the necessary DB records to hold this text from digipal.models import ItemPart item_part = ItemPart.objects.filter(id=item_partid).first() if item_part: # print 'item_part %s' % item_part text_content_xml, created, error = get_or_create_text_content_records( item_part, content_type_record) if error: return error if not text_content_xml: return set_message(ret, '%s not found' % content_type.capitalize()) from digipal.utils import is_user_staff if not is_user_staff(user): if text_content_xml.is_private(): if text_content_xml.content and len(text_content_xml.content) > 10: return set_message(ret, 'The %s will be made available at a later stage of the project' % content_type) else: return set_message(ret, '%s not found' % content_type.capitalize()) record_content = text_content_xml.content or '' # 2. Load the list of possible location types and locations # return the locus of the entries if location_type == 'default' or utils.get_int_from_request_var(request, 'load_locations'): # whole ret['locations'] = OrderedDict() # whole if max_size is not None and len(record_content) <= max_size and (content_type != 'codicology'): ret['locations']['whole'] = [] # entry for ltype in ['entry', 'locus']: ret['locations'][ltype] = [] if text_content_xml.content: for entry in re.findall(ur'(?:<span data-dpt="location" data-dpt-loctype="' + ltype + '">)([^<]+)', text_content_xml.content): ret['locations'][ltype].append(entry) if not ret['locations'][ltype]: del ret['locations'][ltype]
def set_from_request(self, request, faceted_search): if self.index < 1: self.is_valid = 1 if self.index == 1: if not faceted_search.is_full_search: self.is_valid = 1 index = utils.get_int_from_request_var(request, 'qi', 1) if self.index == index: self.is_active = 1 self.request = request self.request_current = request if request.REQUEST.get('q%s_result_type'%(self.index), ''): self.is_valid = 1 self.is_hidden = utils.get_int_from_request_var(request, 'q%s_hidden' % self.index, 0) #if self.is_active: if self.index < 2: self.faceted_search = faceted_search else: from digipal.views.faceted_search.faceted_search import simple_search new_url = '/?' for k, v in self.request.GET.iteritems(): if k.startswith('q%s_' % self.index): new_url += '&%s=%s' % (k.replace('q%s_' % self.index, ''), v) from django.test.client import RequestFactory request = RequestFactory().get(new_url) user = self.request.user self.request = request self.request.user = user self.faceted_search = simple_search(request)
def text_api_view_location(request, item_partid, content_type, location_type, location, user=None, max_size=MAX_FRAGMENT_SIZE): '''This content type is for the list of all available locations (text, images) Used by the master location widget on top of the Text Viewer web page ''' from digipal.models import ItemPart load_locations = utils.get_int_from_request_var(request, 'load_locations') if load_locations: context = { 'item_part': ItemPart.objects.filter(id=item_partid).first() } resolve_master_location(context, location_type, location) ret = { 'location_type': context['master_location_type'], 'location': context['master_location'], 'locations': context['master_locations'], 'toc': context.get('master_toc', { '39a2': 'toc2', '39r': 'toc3', '39a1': 'toc1' }), } else: ret = { 'location_type': location_type, 'location': location, } return ret
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
if text_content_xml.content: for entry in re.findall(ur'(?:<span data-dpt="location" data-dpt-loctype="' + ltype + '">)([^<]+)', text_content_xml.content): ret['locations'][ltype].append(entry) if not ret['locations'][ltype]: del ret['locations'][ltype] # resolve 'default' location request location_type, location = resolve_default_location( location_type, location, ret) # 3. Save the user fragment new_fragment = None if request: new_fragment = dputils.get_request_var(request, 'content', None) convert = utils.get_int_from_request_var(request, 'convert') save_copy = utils.get_int_from_request_var(request, 'save_copy') ret['content_status'] = text_content_xml.status.id extent = get_fragment_extent(record_content, location_type, location) ret['message'] = '' dry_run = 0 if extent: # make sure we compare with None, as '' is a different case if new_fragment is not None: ret['message'] = 'Content saved' # insert user fragment len_previous_record_content = len(record_content) # TODO: UNCOMMENT!!!!!!!!!!!!!!!!!!!!!!!!!!
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
def text_api_view_text(request, item_partid, content_type, location_type, location, content_type_record, user=None, max_size=MAX_FRAGMENT_SIZE): ret = {} text_content_xml = None if not user and request: user = request.user # print 'content type %s' % content_type_record # 1. Fetch or Create the necessary DB records to hold this text from digipal.models import ItemPart item_part = ItemPart.objects.filter(id=item_partid).first() if item_part: # print 'item_part %s' % item_part text_content_xml, created, error = get_or_create_text_content_records( item_part, content_type_record) if error: return error if not text_content_xml: return set_message(ret, '%s not found' % content_type.capitalize()) from digipal.utils import is_user_staff if not is_user_staff(user): if text_content_xml.is_private(): if text_content_xml.content and len(text_content_xml.content) > 10: return set_message( ret, 'The %s will be made available at a later stage of the project' % content_type) else: return set_message(ret, '%s not found' % content_type.capitalize()) record_content = text_content_xml.content or '' # 2. Load the list of possible location types and locations # return the locus of the entries if location_type == 'default' or utils.get_int_from_request_var( request, 'load_locations'): # whole ret['locations'] = OrderedDict() # whole if max_size is not None and len(record_content) <= max_size and ( content_type != 'codicology'): ret['locations']['whole'] = [] # entry for ltype in ['entry', 'locus']: ret['locations'][ltype] = [] if text_content_xml.content: for entry in re.findall( ur'(?:<span data-dpt="location" data-dpt-loctype="' + ltype + '">)([^<]+)', text_content_xml.content): ret['locations'][ltype].append(entry) if not ret['locations'][ltype]: del ret['locations'][ltype]
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
ur'(?:<span data-dpt="location" data-dpt-loctype="' + ltype + '">)([^<]+)', text_content_xml.content): ret['locations'][ltype].append(entry) if not ret['locations'][ltype]: del ret['locations'][ltype] # resolve 'default' location request location_type, location = resolve_default_location(location_type, location, ret) # 3. Save the user fragment new_fragment = None if request: new_fragment = dputils.get_request_var(request, 'content', None) convert = utils.get_int_from_request_var(request, 'convert') save_copy = utils.get_int_from_request_var(request, 'save_copy') ret['content_status'] = text_content_xml.status.id extent = get_fragment_extent(record_content, location_type, location) ret['message'] = '' dry_run = 0 if extent: # make sure we compare with None, as '' is a different case if new_fragment is not None: ret['message'] = 'Content saved' # insert user fragment len_previous_record_content = len(record_content) # TODO: UNCOMMENT!!!!!!!!!!!!!!!!!!!!!!!!!!