def get_action_type_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', '[]')) # @todo named cache mechanism when no filters and default order # cache_name = 'action_types' # action_types = cache_manager.get('accession', cache_name) # # if action_types: # return HttpResponseRest(request, action_types) 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(ActionType) 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.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) action_types_items = [] for action_type in cq: action_types_items.append({ 'id': action_type.id, 'name': action_type.name, # 'value': action_type.name, 'label': action_type.get_label(), 'format': action_type.format, 'description': action_type.description }) results = { 'perms': [], 'items': action_types_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } # cache for 24h # cache_manager.set('accession', cache_name, action_types, 60*60*24) return HttpResponseRest(request, results)
def accession_batches_list_count(request, acc_id): accession = get_object_or_404(Accession, id=int(acc_id)) # check permission on accession object perms = get_permissions_for(request.user, accession.content_type.app_label, accession.content_type.model, accession.pk) if 'accession.get_accession' not in perms: raise PermissionDenied( _('Invalid permission to access to this accession')) from main.cursor import CursorQuery cq = CursorQuery(Batch) 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.filter(accession=accession.id) count = cq.count() results = {'count': count} return HttpResponseRest(request, results)
def get_count_classification_entry_related(request, cls_id): """ Return the list of related classification entries for the given classification entry. """ classification_entry = get_object_or_404(ClassificationEntry, id=int(cls_id)) from main.cursor import CursorQuery cq = CursorQuery(ClassificationEntry) cq.inner_join(ClassificationEntry, related_name='related', to_related_name='to_classificationentry', from_classificationentry=classification_entry.pk) if request.GET.get('search'): cq.filter(json.loads(request.GET['search'])) if request.GET.get('filters'): cq.filter(json.loads(request.GET['filters'])) count = cq.count() results = {'count': count} return HttpResponseRest(request, results)
def get_panel_list_count(request): from main.cursor import CursorQuery cq = CursorQuery(AccessionPanel) if request.GET.get('filters'): filters = json.loads(request.GET['filters']) cq.filter(filters) count = cq.count() results = {'count': count} return HttpResponseRest(request, results)
def get_action_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', '[]')) # @todo how to manage permission to list only auth actions 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(Action) 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.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) action_list = [] for action in cq: a = { 'id': action.id, 'name': action.name, 'action_type': action.action_type_id, 'data': action.data, 'user': action.user.username, 'completed': action.completed, 'created_date': action.created_date.strftime("%Y-%m-%d %H:%M:%S") } action_list.append(a) results = { 'perms': [], 'items': action_list, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def get_descriptor_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 cq = CursorQuery(Descriptor) if request.GET.get('filters'): cq.filter(json.loads(request.GET['filters'])) if request.GET.get('search'): cq.filter(json.loads(request.GET['search'])) cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) items = [] for descriptor in cq: d = { 'id': descriptor.id, 'name': descriptor.name, 'code': descriptor.code, 'label': descriptor.get_label(), 'group_name': descriptor.group_name, 'description': descriptor.description, 'can_delete': descriptor.can_delete, 'can_modify': descriptor.can_modify, 'format': descriptor.format } items.append(d) results = { 'perms': [], 'items': items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def get_classification_rank_list(request): """ Get a list of classification rank in JSON """ 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', '["name"]')) order_by = sort_by cq = CursorQuery(Classification) 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.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) # @todo with CursorQuery .annotate(Count('classificationentry')): to have a count for a PREFETCH # => COUNT("classification_classificationrank"."id") AS "ranks__count" classification_rank_items = [] for classification_rank in cq: c = { 'id': classification_rank.id, 'name': classification_rank.name, 'label': classification_rank.get_label(), 'level': classification_rank.level, 'num_classification_entries': classification_rank.classificationentry_set.all().count() } classification_rank_items.append(c) results = { 'perms': [], 'items': classification_rank_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor } return HttpResponseRest(request, results)
def get_accession_panels(request, acc_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 accession = Accession.objects.get(id=int(acc_id)) panels = list(accession.panels.all().values_list('id', flat=True)) from main.cursor import CursorQuery cq = CursorQuery(AccessionPanel) cq.filter(id__in=panels) if request.GET.get('filters'): filters = json.loads(request.GET['filters']) cq.filter(filters) cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) panel_items = [] for panel in cq: a = { 'id': panel.pk, 'name': panel.name, 'layout': panel.layout.pk if panel.layout else None, 'descriptors': panel.descriptors, 'accessions_amount': panel.accessions.count() } panel_items.append(a) results = { 'perms': [], 'items': panel_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def get_index_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 cq = CursorQuery(DescriptorIndex) if request.GET.get('filters'): cq.filter(json.loads(request.GET['filters'])) if request.GET.get('search'): cq.filter(json.loads(request.GET['search'])) cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) items = [] for index in cq: d = { 'id': index.id, 'descriptor': index.descriptor.name, 'target': index.target.name.capitalize(), 'type': JSONBFieldIndexType(index.type).name } items.append(d) results = { 'perms': [], 'items': items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def get_classification_list_count(request): from main.cursor import CursorQuery cq = CursorQuery(ClassificationEntry) cq.set_synonym_model(ClassificationEntrySynonym) 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) count = cq.count() results = {'count': count} return HttpResponseRest(request, results)
def get_count_establishment_list_for_organisation(request, org_id): """ Count establishment for an organisation. """ 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'])) results = { 'perms': [], 'count': cq.count() } return HttpResponseRest(request, results)
def get_panel_batch_list_count(request, panel_id): from main.cursor import CursorQuery cq = CursorQuery(Batch) if request.GET.get('filters'): filters = json.loads(request.GET['filters']) cq.filter(filters) cq.inner_join(BatchPanel, batchpanel=int(panel_id)) results = {'count': cq.count()} return HttpResponseRest(request, results)
def get_groups_list_count(request): cq = CursorQuery(Group) 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) results = {'perms': [], 'count': cq.count()} return HttpResponseRest(request, results)
def get_count_organisation_list(request): """ Count organisations. """ cq = CursorQuery(Organisation) if request.GET.get('search'): cq.filter(json.loads(request.GET['search'])) if request.GET.get('filters'): cq.filter(json.loads(request.GET['filters'])) results = {'perms': [], 'count': cq.count()} return HttpResponseRest(request, results)
def count_accession_panels(request, acc_id): accession = Accession.objects.get(id=int(acc_id)) panels = list(accession.panels.all().values_list('id', flat=True)) from main.cursor import CursorQuery cq = CursorQuery(AccessionPanel) cq.filter(id__in=panels) if request.GET.get('filters'): filters = json.loads(request.GET['filters']) cq.filter(filters) results = {'count': cq.count()} return HttpResponseRest(request, results)
def get_panel_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(BatchPanel) if request.GET.get('filters'): filters = json.loads(request.GET['filters']) cq.filter(filters) cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) panel_items = [] for panel in cq: a = { 'id': panel.pk, 'name': panel.name, 'layout': panel.layout.pk if panel.layout else None, 'descriptors': panel.descriptors, 'batches_amount': panel.batches.count() } panel_items.append(a) results = { 'perms': [], 'items': panel_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def get_descriptor_list_count(request): """ Get the count of number of descriptors in JSON """ cq = CursorQuery(Descriptor) if request.GET.get('filters'): cq.filter(json.loads(request.GET['filters'])) if request.GET.get('search'): cq.filter(json.loads(request.GET['search'])) results = {'perms': [], 'count': cq.count()} return HttpResponseRest(request, results)
def get_batch_list_count(request): from main.cursor import CursorQuery cq = CursorQuery(Batch) 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=BatchPanel.batches, selected_field='batchpanel_id', from_related_field='id', to_related_field='batch_id', alias='panels') count = cq.count() results = {'count': count} return HttpResponseRest(request, results)
def get_action_type_list_count(request): from main.cursor import CursorQuery cq = CursorQuery(ActionType) 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) count = cq.count() results = {'count': count} 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_list_count(request, cls_id): """ Return the count of direct children for the given classification entry. """ 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'])) count = cq.count() results = {'count': count} 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_id_classification_entry_list(request, acc_id): """ Get a list of classification for an accession in JSON """ sort_by = json.loads(request.GET.get('sort_by', '[]')) accession = get_object_or_404(Accession, id=int(acc_id)) # check permission on this object perms = get_permissions_for(request.user, accession.content_type.app_label, accession.content_type.model, accession.pk) if 'accession.get_accession' not in perms: raise PermissionDenied(_('Invalid permission to access to this accession')) 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.inner_join( ClassificationEntry, related_name='accession_set', to_related_name='classification_entry', accession=accession.pk) 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.prefetch_related(Prefetch( "synonyms", queryset=ClassificationEntrySynonym.objects.all().order_by('synonym_type', 'language'))) cq.order_by(order_by) 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, 'synonyms': [] } for synonym in classification_entry.synonyms.all(): c['synonyms'].append({ '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 } return HttpResponseRest(request, results)
def get_action_list_for_entity_id_count(request, ent_id): # @todo from main.cursor import CursorQuery cq = CursorQuery(ActionToEntity) # cq = ActionToEntity.objects.filter(entity_id=int(ent_id)) # @todo filter for action relating cq.inner_join(Action, related_name='id', to_related_name='action_id', entity=int(ent_id)) cq.filter(entity=int(ent_id)) 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) # print(cq.sql()) count = cq.count() # cq.filter(input_batches__in=int(bat_id)) results = { 'count': count } return HttpResponseRest(request, results)
def get_action_list_for_entity_id(request, ent_id): # @todo 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', '[]')) # @todo how to manage permission to list only auth actions 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(ActionToEntity) # cq = ActionToEntity.objects.filter(entity_id=int(ent_id)) # @todo filter for action relating cq.inner_join(Action, related_name='id', to_related_name='action_id', entity=int(ent_id)) cq.filter(entity=int(ent_id)) 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.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) # print(cq.sql()) action_list = [] for action in cq: a = { 'id': action.id, 'entity': action.entity_id, 'entity_type': action.entity_type_id, 'type': action.type_id, 'data': action.data, 'completed': action.completed, 'created_date': action.created_date.strftime("%Y-%m-%d %H:%M:%S") } action_list.append(a) results = { 'perms': [], 'items': action_list, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def get_users_list_for_group(request, grp_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', '["username"]')) order_by = sort_by group = get_object_or_404(Group, id=int(grp_id)) cq = CursorQuery(User) 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.inner_join(Group, related_name='user_set', group=group.pk) cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) user_items = [] for user in cq: user_items.append({ 'id': user.id, 'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name, 'email': user.email, 'is_active': user.is_active, 'is_staff': user.is_staff, 'is_superuser': user.is_superuser, }) results = { 'perms': get_permissions_for(request.user, "auth", "group", group), 'items': user_items, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor } return HttpResponseRest(request, results)
def get_batch_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', '[]')) # @todo how to manage permission to list only auth batches 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(Batch) 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=BatchPanel.batches, selected_field='batchpanel_id', from_related_field='id', to_related_field='batch_id', alias='panels') cq.cursor(cursor, order_by) cq.order_by(order_by).limit(limit) batch_list = [] for batch in cq: a = { 'id': batch.pk, 'name': batch.name, 'accession': batch.accession_id, 'layout': batch.layout_id, 'descriptors': batch.descriptors, 'location': batch.location.get_label() if batch.location else None } batch_list.append(a) results = { 'perms': [], 'items': batch_list, 'prev': cq.prev_cursor, 'cursor': cursor, 'next': cq.next_cursor, } return HttpResponseRest(request, results)
def create_batch(request): """ This is the deprecated way of creating a batch. Creation of a batch must be supervised by an action of creation. This method still because of its interest during development process. """ # name = request.data['name'] naming_options = request.data['naming_options'] accession_id = int_arg(request.data['accession']) layout_id = int_arg(request.data['layout']) descriptors = request.data['descriptors'] # batch selection selection = request.data['selection']['select'] related_entity = request.data['selection']['from'] search = request.data['selection']['search'] filters = request.data['selection']['filters'] # naming accession = get_object_or_404(Accession, id=accession_id) naming_variables = { 'ACCESSION_NAME': accession.name, 'ACCESSION_CODE': accession.code } name = NameBuilderManager.get(NameBuilderManager.GLOBAL_BATCH).pick( naming_variables, naming_options) # check uniqueness of the name if Batch.objects.filter(name=name).exists(): raise SuspiciousOperation(_("The name of the batch is already used")) content_type = get_object_or_404(ContentType, app_label="accession", model="batch") layout = get_object_or_404(Layout, id=layout_id, target=content_type) from main.cursor import CursorQuery cq = CursorQuery(Batch) if search: cq.filter(search) if filters: cq.filter(filters) if related_entity: label, model = related_entity['content_type'].split('.') content_type = get_object_or_404(ContentType, app_label=label, model=model) model_class = content_type.model_class() cq.inner_join(model_class, **{model: int_arg(related_entity['id'])}) try: with transaction.atomic(): # common properties batch = Batch() batch.name = name batch.layout = layout # parent accession accession = get_object_or_404(Accession, id=accession_id) batch.accession = accession # descriptors descriptors_builder = DescriptorsBuilder(batch) descriptors_builder.check_and_update(layout, descriptors) batch.descriptors = descriptors_builder.descriptors batch.save() # parent batches if isinstance(selection, bool): if selection is True: batch.batches.add(*cq) elif selection['op'] == 'in': batch.batches.add(*cq.filter(id__in=selection['value'])) elif selection['op'] == 'notin': batch.batches.add(*cq.filter(id__notin=selection['value'])) # update owner on external descriptors descriptors_builder.update_associations() except IntegrityError as e: logger.error(repr(e)) raise SuspiciousOperation(_("Unable to create the batch")) response = { 'id': batch.pk, 'name': batch.name, 'layout': layout.id, 'accession': accession.id, 'descriptors': descriptors } return HttpResponseRest(request, response)
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)