def add_itempart_view(request): from digipal.models import ItemPart, HistoricalItem, CurrentItem, Repository, Place, ItemPartItem, CatalogueNumber fieldset = [ ['Current Item', {'key': 'shelfmark', 'label': 'Shelfmark', 'required': True, 'list': [ci.shelfmark for ci in CurrentItem.objects.all()], 'eg': 'Cotton Domitian vii'}, {'key': 'repository', 'label': 'Repository', 'required': True, 'list': [u'%s, %s' % (repo.place, repo.name) for repo in Repository.objects.all()], 'eg': 'London, British Library'}, ], ['Item Part', {'key': 'locus', 'label': 'Locus', 'eg': 'fols. 15–45'}, ], ['Historical Item', {'key': 'cat_num', 'label': 'Catalogue Number', 'list': [unicode(cn) for cn in CatalogueNumber.objects.all()], 'eg': 'K. 190'}, {'key': 'hi_name', 'label': 'Name', 'list': [hi.name for hi in HistoricalItem.objects.all()], 'eg': 'Durham Liber Vitae'}, ] ] context = {'form': fieldset, 'title': 'Add Item Part'} import json from digipal.utils import sorted_natural for group in fieldset: for field in group: if isinstance(field, dict): if 'list' in field: field['list_json'] = json.dumps(sorted_natural(list(set([n for n in field['list'] if n])), True)) if request.method == "POST": # current item current_item = CurrentItem.get_or_create(shelfmark=request.REQUEST.get('shelfmark', ''), repository=request.REQUEST.get('repository', '')) # create the new records itempart = ItemPart(locus=request.REQUEST.get('locus', ''), current_item=current_item) itempart.save() # historical item historical_item = HistoricalItem.get_or_create(name=request.REQUEST.get('hi_name', ''), cat_num=request.REQUEST.get('cat_num', '')) if historical_item: # item part ItemPartItem(item_part=itempart, historical_item=historical_item).save() # add a confirmation message from django.contrib import messages messages.add_message(request, messages.SUCCESS, 'The item part was added successfully. You may edit it again below.') # redirect to the edit form from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse return HttpResponseRedirect(reverse('admin:digipal_itempart_change', args=(itempart.id,))) pass return render(request, 'admin/digipal/add_itempart.html', context)
def init_bands(self): ''' Initialise the vertical category-bands with a fixed height and label set self.bands = {'agreement': 0, 'brieve': 1000, LABEL: TOPY} ''' #value_rankings, self.bands = self.faceted_search.get_field_value_ranking(self.fields[1]) from digipal.utils import sorted_natural self.bands = sorted_natural(list(set(self.bands))) # eg. {'type1': 0, 'type2': 1000} self.bands = {self.bands[i]: i*self.band_width for i in range(0, len(self.bands))}
def get_units_from_locations(content, location_type, locations, content_xmlid=None): extent = [0, 0] for location in sorted_natural(locations): parts = location.split(':') if len(parts) == 2 and parts[0] != str(content_xmlid): # different XML continue new_extent = get_fragment_extent(content, location_type, parts[-1], extent[1]) if new_extent is not None: extent = new_extent yield {'unitid': extent[2], 'content': content[extent[0]:extent[1]]}
def init_bands(self): ''' Initialise the vertical category-bands with a fixed height and label set self.bands = {'agreement': 0, 'brieve': 1000, LABEL: TOPY} ''' #value_rankings, self.bands = self.faceted_search.get_field_value_ranking(self.fields[1]) bands = sorted_natural(list(set(self.bands))) # eg. {'type1': 0, 'type2': 1000} #self.bands = {self.bands[i]: i*self.band_width for i in range(0, len(self.bands))} self.bands = {} for i in range(0, len(bands)): self.bands[bands[i]] = i * self.band_width
def get_units_from_locations(content, location_type, locations, content_xmlid=None): extent = [0, 0] for location in sorted_natural(locations): parts = location.split(':') if len(parts) == 2 and parts[0] != str(content_xmlid): # different XML continue new_extent = get_fragment_extent(content, location_type, parts[-1], extent[1]) if new_extent is not None: extent = new_extent yield { 'unitid': extent[2], 'content': content[extent[0]:extent[1]] }
class FilterScribes(forms.Form): scribe = get_form_field_from_queryset( Scribe.objects.values_list('name', flat=True).order_by('name').distinct(), 'Scribe') # Was previously called 'scriptorium' scriptorium = get_form_field_from_queryset( Scribe.objects.values_list( 'scriptorium__name', flat=True).order_by('scriptorium__name').distinct(), 'Place') # TODO: order the dates scribe_date = get_form_field_from_queryset( sorted_natural( list( Scribe.objects.filter(date__isnull=False).values_list( 'date', flat=True).order_by('date').distinct())), 'Date') chartype = get_form_field_from_queryset(Scribe.objects.values_list( 'idiographs__allograph__character__ontograph__ontograph_type__name', flat=True).order_by( 'idiographs__allograph__character__ontograph__ontograph_type__name' ).distinct(), 'Character Type', aid='chartype') character = get_form_field_from_queryset(Scribe.objects.values_list( 'idiographs__allograph__character__name', flat=True).order_by( 'idiographs__allograph__character__ontograph__sort_order'). distinct(), 'Character', aid='character') component = get_form_field_from_queryset( Scribe.objects.values_list( 'idiographs__idiographcomponent__component__name', flat=True).order_by( 'idiographs__idiographcomponent__component__name').distinct(), 'Component') feature = get_form_field_from_queryset( Scribe.objects.values_list( 'idiographs__idiographcomponent__features__name', flat=True).order_by( 'idiographs__idiographcomponent__features__name').distinct(), 'Feature')
ret['locus'] = set(context['item_part'].images.all( ).values_list('locus', flat=True).order_by('id')) ret['entry'] = set() # Get entry numbers from texts for tcx in TextContentXML.objects.filter(text_content__item_part=context['item_part']).iterator(): for m in re.findall(ur'<span data-dpt="location" data-dpt-loctype="(.*?)">(.*?)</span>', tcx.content or ''): if m[0] in ret: ret[m[0]].add(m[1]) # sort locations for k, v in ret.iteritems(): if not v: del ret[k] continue locations = sorted_natural(v, roman_numbers=True, is_locus=True) ret[k] = locations return ret # TODO: content_type_record makes this signature non-polymorphic and even incompatible with image # need to use optional parameter for it 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
# Get entry numbers from texts for tcx in TextContentXML.objects.filter( text_content__item_part=context['item_part']).iterator(): for m in re.findall( ur'<span data-dpt="location" data-dpt-loctype="(.*?)">(.*?)</span>', tcx.content or ''): if m[0] in ret: ret[m[0]].add(m[1]) # sort locations for k, v in ret.iteritems(): if not v: del ret[k] continue locations = sorted_natural(v, roman_numbers=True, is_locus=True) ret[k] = locations return ret # TODO: content_type_record makes this signature non-polymorphic and even incompatible with image # need to use optional parameter for it def text_api_view_text(request, item_partid, content_type, location_type, location, content_type_record, user=None, max_size=MAX_FRAGMENT_SIZE):
def add_itempart_view(request): fieldset = [ [ 'Current Item', { 'key': 'repository', 'label': 'Repository', 'required': True, 'list': [ u'%s, %s' % (repo.place, repo.name) for repo in Repository.objects.all() ], 'eg': 'London, British Library', 'help_text': 'The library, archive or institution which currently preserves this item. Format: "City, Repository name"', }, { 'key': 'shelfmark', 'label': 'Shelfmark', 'required': True, 'list': [ci.shelfmark for ci in CurrentItem.objects.all()], 'eg': 'Cotton Domitian vii', 'help_text': 'A short reference used by a repository to help locate the item.', }, ], [ 'Item Part', { 'key': 'locus', 'label': 'Locus', 'eg': 'fols. 15–45', 'help_text': 'The location of this part in the current item (e.g. a folio/page range). Leave blank if only one part in the Current Item.', }, ], [ 'Historical Item', { 'key': 'cat_num', 'label': 'Catalogue Number', 'list': [unicode(cn) for cn in CatalogueNumber.objects.all()], 'eg': 'K. 190', 'help_text': 'Optional. A catalogue number. Format: "CA. CN", Where CA is the catalogue abbbreviation and CN the number or code of this item in that catalogue.', }, { 'key': 'hi_name', 'label': 'Name', 'list': [hi.name for hi in HistoricalItem.objects.all()], 'eg': 'Durham Liber Vitae', 'help_text': 'Optional. A common name for this Item.', }, ] ] import json from digipal.utils import sorted_natural for group in fieldset: for field in group: if isinstance(field, dict): field['value'] = request.POST.get(field['key'], '') if 'list' in field: field['list_json'] = json.dumps( sorted_natural( list(set([n for n in field['list'] if n])), True)) from django.contrib import messages if request.method == "POST": try: itempart = add_itempart(request) except ExceptionAddItemPart, e: messages.add_message(request, messages.ERROR, e.message) else: # add a confirmation message messages.add_message( request, messages.SUCCESS, 'The item part was added successfully. You may edit it again below.' ) # redirect to the edit form from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse return HttpResponseRedirect( reverse('admin:digipal_itempart_change', args=(itempart.id, )))
def add_itempart_view(request): fieldset = [ ['Current Item', { 'key': 'repository', 'label': 'Repository', 'required': True, 'list': [u'%s, %s' % (repo.place, repo.name) for repo in Repository.objects.all()], 'eg': 'London, British Library', 'help_text': 'The library, archive or institution which currently preserves this item. Format: "City, Repository name"', }, { 'key': 'shelfmark', 'label': 'Shelfmark', 'required': True, 'list': [ci.shelfmark for ci in CurrentItem.objects.all()], 'eg': 'Cotton Domitian vii', 'help_text': 'A short reference used by a repository to help locate the item.', }, ], ['Item Part', {'key': 'locus', 'label': 'Locus', 'eg': 'fols. 15–45', 'help_text': 'The location of this part in the current item (e.g. a folio/page range). Leave blank if only one part in the Current Item.', }, ], ['Historical Item', { 'key': 'cat_num', 'label': 'Catalogue Number', 'list': [unicode(cn) for cn in CatalogueNumber.objects.all()], 'eg': 'K. 190', 'help_text': 'Optional. A catalogue number. Format: "CA. CN", Where CA is the catalogue abbbreviation and CN the number or code of this item in that catalogue.', }, { 'key': 'hi_name', 'label': 'Name', 'list': [hi.name for hi in HistoricalItem.objects.all()], 'eg': 'Durham Liber Vitae', 'help_text': 'Optional. A common name for this Item.', }, ] ] import json from digipal.utils import sorted_natural for group in fieldset: for field in group: if isinstance(field, dict): field['value'] = request.POST.get(field['key'], '') if 'list' in field: field['list_json'] = json.dumps(sorted_natural( list(set([n for n in field['list'] if n])), True)) from django.contrib import messages if request.method == "POST": try: itempart = add_itempart(request) except ExceptionAddItemPart, e: messages.add_message(request, messages.ERROR, e.message) else: # add a confirmation message messages.add_message(request, messages.SUCCESS, 'The item part was added successfully. You may edit it again below.') # redirect to the edit form from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse return HttpResponseRedirect(reverse('admin:digipal_itempart_change', args=(itempart.id,)))
class FilterManuscripts(forms.Form): index = get_form_field_from_queryset(sorted_natural( '%s' % cn for cn in CatalogueNumber.objects.filter(historical_item__item_parts__isnull=False).distinct() ), 'Catalogue Number') repository = get_form_field_from_queryset([m.human_readable() for m in Repository.objects.filter(currentitem__itempart__isnull=False).order_by('place__name', 'name').distinct()], 'Repository') ms_date = get_form_field_from_queryset(sorted_natural(list(ItemPart.objects.filter(historical_items__date__isnull=False, historical_items__date__gt='').values_list('historical_items__date', flat=True).order_by('historical_items__date').distinct())), 'Date')