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_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_panel_accession_list_count(request, panel_id): 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.inner_join(AccessionPanel, accessionpanel=int(panel_id)) results = {'count': cq.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_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_panel_batch_list(request, panel_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 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.cursor(cursor, order_by) cq.inner_join(BatchPanel, batchpanel=int(panel_id)) cq.order_by(order_by).limit(limit) batch_items = [] for batch in cq: b = { 'id': batch.pk, 'name': batch.name, 'accession': batch.accession_id, 'layout': batch.layout_id, 'descriptors': batch.descriptors, } batch_items.append(b) results = { 'perms': [], 'items': batch_items, '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_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 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_classification_entry_related(request, cls_id): """ Return the list of related classification entries 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.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'])) 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 modify_panel_accessions(request, panel_id): action = request.data['action'] selection = request.data['selection']['select'] acc_panel = AccessionPanel.objects.get(id=int_arg(panel_id)) from main.cursor import CursorQuery cq = CursorQuery(Accession) if request.data['selection'].get('search'): search = json.loads(request.GET['search']) cq.filter(request.data['selection'].get('search')) if request.data['selection'].get('filters'): filters = json.loads(request.GET['filters']) cq.filter(request.data['selection'].get('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) if request.data['selection'].get('from'): related_entity = request.data['selection']['from'] 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'])}) if action == 'remove': try: with transaction.atomic(): if isinstance(selection, bool): if selection is True: acc_panel.accessions.remove(*cq) elif selection['op'] == 'in': acc_panel.accessions.remove(*cq.filter( id__in=selection['value'])) elif selection['op'] == 'notin': acc_panel.accessions.remove(*cq.filter( id__notin=selection['value'])) acc_panel.save() except IntegrityError as e: Descriptor.integrity_except(AccessionPanel, e) elif action == 'add': try: with transaction.atomic(): if isinstance(selection, bool): if selection is True: acc_panel.accessions.add(*cq) elif selection['op'] == 'in': acc_panel.accessions.add(*cq.filter( id__in=selection['value'])) elif selection['op'] == 'notin': acc_panel.accessions.add(*cq.filter( id__notin=selection['value'])) acc_panel.save() except IntegrityError as e: Descriptor.integrity_except(AccessionPanel, e) else: raise SuspiciousOperation('Invalid action') return HttpResponseRest(request, {})
def get_panel_accession_list(request, panel_id): # check permission on this panel panel = get_object_or_404(AccessionPanel, id=int(panel_id)) # check permission on this object from permission.utils import get_permissions_for perms = get_permissions_for(request.user, panel.content_type.app_label, panel.content_type.model, panel.pk) if 'accession.get_accessionpanel' not in perms: raise PermissionDenied(_('Invalid permission to access to this panel')) 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.inner_join(AccessionPanel, accessionpanel=int(panel_id)) 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)
def create_panel(request): name = request.data['name'] selection = request.data['selection']['select'] related_entity = request.data['selection']['from'] search = request.data['selection']['search'] filters = request.data['selection']['filters'] layout_id = request.data['layout'] descriptors = request.data['descriptors'] layout = None # check uniqueness of the name if AccessionPanel.objects.filter(name=name).exists(): raise SuspiciousOperation(_("The name of the panel is already used")) if layout_id is not None: content_type = get_object_or_404(ContentType, app_label="accession", model="accessionpanel") layout = get_object_or_404(Layout, id=int_arg(layout_id), target=content_type) from main.cursor import CursorQuery cq = CursorQuery(Accession) if search: cq.filter(search) if 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) 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(): acc_panel = AccessionPanel(name=name) acc_panel.layout = layout acc_panel.count = 0 # descriptors descriptors_builder = DescriptorsBuilder(acc_panel) if layout: descriptors_builder.check_and_update(layout, descriptors) acc_panel.descriptors = descriptors_builder.descriptors acc_panel.save() # update owner on external descriptors descriptors_builder.update_associations() if isinstance(selection, bool): if selection is True: acc_panel.accessions.add(*cq) acc_panel.count = cq.count() elif selection['op'] == 'in': acc_panel.accessions.add(*cq.filter(id__in=selection['value'])) acc_panel.count = cq.filter(id__in=selection['value']).count() elif selection['op'] == 'notin': acc_panel.accessions.add(*cq.filter( id__notin=selection['value'])) acc_panel.count = cq.filter( id__notin=selection['value']).count() except IntegrityError as e: Descriptor.integrity_except(AccessionPanel, e) response = { 'id': acc_panel.pk, 'name': acc_panel.name, 'layout': acc_panel.layout.pk if acc_panel.layout else None, 'descriptors': acc_panel.descriptors, 'accessions_amount': acc_panel.count } return HttpResponseRest(request, response)
def modify_panel_batches(request, panel_id): action = request.data['action'] selection = request.data['selection']['select'] panel = BatchPanel.objects.get(id=int_arg(panel_id)) from main.cursor import CursorQuery cq = CursorQuery(Batch) if request.data['selection'].get('filters'): cq.filter(request.data['selection'].get('filters')) if request.data['selection'].get('search'): cq.filter(request.data['selection'].get('search')) if request.data['selection'].get('from'): related_entity = request.data['selection']['from'] 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'])}) if action == 'remove': try: with transaction.atomic(): if isinstance(selection, bool): if selection is True: panel.batches.remove(*cq) elif selection['op'] == 'in': panel.batches.remove(*cq.filter(id__in=selection['value'])) elif selection['op'] == 'notin': panel.batches.remove(*cq.filter( id__notin=selection['value'])) panel.save() except IntegrityError as e: Descriptor.integrity_except(BatchPanel, e) elif action == 'add': try: with transaction.atomic(): if isinstance(selection, bool): if selection is True: panel.batches.add(*cq) elif selection['op'] == 'in': panel.batches.add(*cq.filter(id__in=selection['value'])) elif selection['op'] == 'notin': panel.batches.add(*cq.filter(id__notin=selection['value'])) panel.save() except IntegrityError as e: Descriptor.integrity_except(BatchPanel, e) else: raise SuspiciousOperation('Invalid action') return HttpResponseRest(request, {})