def get_establishment_list_for_organisation(request, org_id): """ List all establishments for a specific organisation. """ results_per_page = int_arg(request.GET.get('more', 30)) cursor = json.loads(request.GET.get('cursor', 'null')) limit = results_per_page sort_by = json.loads(request.GET.get('sort_by', '[]')) if not len(sort_by) or sort_by[-1] not in ('id', '+id', '-id'): order_by = sort_by + ['id'] else: order_by = sort_by cq = CursorQuery(Establishment) cq.filter(organisation=int(org_id)) if request.GET.get('search'): cq.filter(json.loads(request.GET['search'])) if request.GET.get('filters'): cq.filter(json.loads(request.GET['filters'])) cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) cq.select_related('organisation->id', 'organisation->name') establishment_items = [] for establishment in cq: t = { 'id': establishment.pk, 'name': establishment.name, 'descriptors': establishment.descriptors, 'layout': establishment.layout, 'organisation': establishment.organisation_id, 'organisation_details': { 'id': establishment.organisation.id, 'name': establishment.organisation.name } } establishment_items.append(t) results = { 'perms': [], 'items': establishment_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor } return HttpResponseRest(request, results)
def get_classification_id_list_count(request, cls_id): from main.cursor import CursorQuery cq = CursorQuery(ClassificationEntry) cq.set_synonym_model(ClassificationEntrySynonym) cq.filter(rank__classification=int_arg(cls_id)) if request.GET.get('filters'): filters = json.loads(request.GET['filters']) cq.filter(filters) cq.select_related('rank->classification') count = cq.count() results = {'count': count} return HttpResponseRest(request, results)
def get_classification_id_entry_list(request, cls_id): results_per_page = int_arg(request.GET.get('more', 30)) cursor = json.loads(request.GET.get('cursor', 'null')) limit = results_per_page sort_by = json.loads(request.GET.get('sort_by', '[]')) if not len(sort_by) or sort_by[-1] not in ('id', '+id', '-id'): order_by = sort_by + ['id'] else: order_by = sort_by cq = CursorQuery(ClassificationEntry) cq.set_synonym_model(ClassificationEntrySynonym) cq.filter(rank__classification=int_arg(cls_id)) if request.GET.get('filters'): filters = json.loads(request.GET['filters']) cq.filter(filters) cq.prefetch_related( Prefetch("synonyms", queryset=ClassificationEntrySynonym.objects.all().order_by( 'synonym_type', 'language'))) cq.select_related('rank->classification') # cq.select_related('parent->name', 'parent->rank') cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) classification_entry_items = [] for classification_entry in cq: c = { 'id': classification_entry.id, 'name': classification_entry.name, 'parent': classification_entry.parent_id, 'rank': classification_entry.rank_id, 'layout': classification_entry.layout_id, 'descriptors': classification_entry.descriptors, 'parent_list': classification_entry.parent_list, # 'parent_details': None, 'synonyms': {} } # if classification_entry.parent: # c['parent_details'] = { # 'id': classification_entry.parent.id, # 'name': classification_entry.parent.name, # 'rank': classification_entry.parent.rank_id # } for synonym in classification_entry.synonyms.all(): synonym_type = EntitySynonymType.objects.get( id=synonym.synonym_type_id) c['synonyms'][synonym_type.name] = { 'id': synonym.id, 'name': synonym.name, 'synonym_type': synonym.synonym_type_id, 'language': synonym.language } classification_entry_items.append(c) results = { 'perms': [], 'items': classification_entry_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def get_classification_entry_children(request, cls_id): """ Return the list of direct children for the given classification entry. """ results_per_page = int_arg(request.GET.get('more', 30)) cursor = json.loads(request.GET.get('cursor', 'null')) limit = results_per_page sort_by = json.loads(request.GET.get('sort_by', '[]')) if not len(sort_by) or sort_by[-1] not in ('id', '+id', '-id'): order_by = sort_by + ['id'] else: order_by = sort_by classification_entry = get_object_or_404(ClassificationEntry, id=int(cls_id)) from main.cursor import CursorQuery cq = CursorQuery(ClassificationEntry) cq.set_synonym_model(ClassificationEntrySynonym) # if only children if request.GET.get('deeply', False): cq.filter(parent_list__in=[classification_entry.id]) else: cq.filter(parent=classification_entry.id) if request.GET.get('filters'): cq.filter(json.loads(request.GET['filters'])) cq.prefetch_related( Prefetch("synonyms", queryset=ClassificationEntrySynonym.objects.all().order_by( 'synonym_type', 'language'))) cq.select_related('parent->name', 'parent->rank') cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) classification_items = [] for classification in cq: c = { 'id': classification.id, 'name': classification.name, 'parent': classification.parent_id, 'rank': classification.rank_id, 'layout': classification.layout_id, 'descriptors': classification.descriptors, 'parent_list': classification.parent_list, 'parent_details': None, 'synonyms': [] } if classification.parent: c['parent_details'] = { 'id': classification.parent.id, 'name': classification.parent.name, 'rank': classification.parent.rank_id } for synonym in classification.synonyms.all(): c['synonyms'].append({ 'id': synonym.id, 'name': synonym.name, 'synonym_type': synonym.synonym_type_id, 'language': synonym.language }) classification_items.append(c) results = { 'perms': [], 'items': classification_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def get_accession_list(request): results_per_page = int_arg(request.GET.get('more', 30)) cursor = json.loads(request.GET.get('cursor', 'null')) limit = results_per_page sort_by = json.loads(request.GET.get('sort_by', '[]')) if not len(sort_by) or sort_by[-1] not in ('id', '+id', '-id'): order_by = sort_by + ['id'] else: order_by = sort_by from main.cursor import CursorQuery cq = CursorQuery(Accession) if request.GET.get('search'): search = json.loads(request.GET['search']) cq.filter(search) if request.GET.get('filters'): filters = json.loads(request.GET['filters']) cq.filter(filters) cq.m2m_to_array_field(relationship=AccessionPanel.accessions, selected_field='accessionpanel_id', from_related_field='id', to_related_field='accession_id', alias='panels') cq.m2m_to_array_field(relationship=Accession.classifications_entries, selected_field='classification_entry_id', from_related_field='id', to_related_field='accession_id', alias='classifications') cq.set_synonym_model(AccessionSynonym) cq.prefetch_related( Prefetch("synonyms", queryset=AccessionSynonym.objects.all().order_by( 'synonym_type', 'language'))) cq.select_related('primary_classification_entry->name', 'primary_classification_entry->rank') cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) accession_items = [] synonym_types = dict( EntitySynonymType.objects.filter( target_model=ContentType.objects.get_for_model( Accession)).values_list('id', 'name')) for accession in cq: a = { 'id': accession.pk, 'name': accession.name, 'code': accession.code, 'primary_classification_entry': accession.primary_classification_entry_id, 'layout': accession.layout_id, 'descriptors': accession.descriptors, 'synonyms': {}, 'primary_classification_entry_details': { 'id': accession.primary_classification_entry.id, 'name': accession.primary_classification_entry.name, 'rank': accession.primary_classification_entry.rank_id, } } for synonym in accession.synonyms.all(): synonym_type_name = synonym_types.get(synonym.synonym_type_id) a['synonyms'][synonym_type_name] = { 'id': synonym.id, 'name': synonym.name, 'synonym_type': synonym.synonym_type_id, 'language': synonym.language } accession_items.append(a) results = { 'perms': [], 'items': accession_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)