Beispiel #1
0
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)
Beispiel #2
0
def accession_batches_list(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', '[]'))

    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'))

    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.filter(accession=accession.id)
    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
        }

        batch_list.append(a)

    results = {
        'perms': [],
        'items': batch_list,
        'prev': cq.prev_cursor,
        'cursor': cursor,
        'next': cq.next_cursor,
    }

    return HttpResponseRest(request, results)
Beispiel #3
0
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)
Beispiel #4
0
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)
Beispiel #5
0
def get_location_list(request):
    results_per_page = int_arg(request.GET.get('more', 0))
    cursor = json.loads(request.GET.get('cursor', 'null'))
    limit = results_per_page
    sort_by = json.loads(request.GET.get('sort_by', '[]'))
    parent_location = request.GET.get('parent', None)

    if parent_location is not None:
        parent_location = int(parent_location)

    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(StorageLocation)
    cq.filter(parent_id=parent_location)

    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.prefetch_related(Prefetch(
        "children",
        queryset=StorageLocation.objects.all()
    ))

    cq.cursor(cursor, order_by)
    cq.order_by(order_by).limit(limit)

    location_list = []

    for storage_location in cq:
        sl = {
            'id': storage_location.id,
            'name': storage_location.name,
            'label': storage_location.get_label(),
            'children_count': storage_location.children.count()
        }

        location_list.append(sl)

    results = {
        'perms': [],
        'items': location_list,
        'prev': cq.prev_cursor,
        'cursor': cursor,
        'next': cq.next_cursor,
    }

    return HttpResponseRest(request, results)
Beispiel #6
0
def get_users_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', '["username"]'))
    order_by = sort_by

    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.cursor(cursor, order_by)
    cq.order_by(order_by).limit(limit)
    cq.set_count('user_permissions')

    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,
            'num_permissions':
            user.user_permissions.all().count()  # optimized by the set_count
        })

    results = {
        'perms': get_permissions_for(request.user, "auth", "user"),
        'items': user_items,
        'prev': cq.prev_cursor,
        'cursor': cursor,
        'next': cq.next_cursor
    }

    return HttpResponseRest(request, results)
Beispiel #7
0
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)
Beispiel #8
0
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)
Beispiel #9
0
def get_groups_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', '["name"]'))
    order_by = sort_by

    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)

    cq.cursor(cursor, order_by)
    cq.order_by(order_by).limit(limit)
    cq.prefetch_related('permissions')
    cq.prefetch_related('user_set')

    # @todo is it prefetch something ?
    prefetch_permissions_for(request.user, cq)

    group_items = []

    for group in cq:
        group_items.append({
            'id':
            group.id,
            'name':
            group.name,
            'num_users':
            group.user_set.all().count(),  # optimized by prefetch_related
            'num_permissions':
            group.permissions.all().count(),  # optimized by prefetch_related
            'perms':
            get_permissions_for(request.user, "auth", "group", group),
        })

    results = {
        'perms': get_permissions_for(request.user, "auth", "group"),
        'items': group_items,
        'prev': cq.prev_cursor,
        'cursor': cursor,
        'next': cq.next_cursor
    }

    return HttpResponseRest(request, results)
Beispiel #10
0
def get_classification_list(request):
    """
    Get the list of classification 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)
    cq.set_count('ranks')

    classification_items = []

    for classification in cq:
        c = {
            'id': classification.id,
            'name': classification.name,
            'can_modify': classification.can_modify,
            'can_delete': classification.can_delete,
            'label': classification.get_label(),
            'description': classification.description,
            'num_classification_ranks': classification.ranks__count
        }

        classification_items.append(c)

    results = {
        'perms': [],
        'items': classification_items,
        'prev': cq.prev_cursor,
        'cursor': cursor,
        'next': cq.next_cursor
    }

    return HttpResponseRest(request, results)
Beispiel #11
0
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)
Beispiel #12
0
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)
Beispiel #13
0
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)
Beispiel #14
0
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)
Beispiel #15
0
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)
Beispiel #16
0
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)
Beispiel #17
0
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)
Beispiel #18
0
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)
Beispiel #19
0
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)
Beispiel #20
0
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)
Beispiel #21
0
def get_organisation_list(request):
    """
    List all organisations.
    """
    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(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']))

    cq.cursor(cursor, order_by)
    cq.order_by(order_by).limit(limit)
    cq.set_count('establishments')
    cq.set_count('grcs')
    # cq.prefetch_related('grcs')

    # for order on char field referencing a descriptor value, to avoid this fixture move type to the descriptors
    lang = translation.get_language()
    cq.query_select.append(
        '"descriptor_descriptorvalue"."value0" AS "type_name"')
    cq.query_from.append(
        'LEFT JOIN "descriptor_descriptorvalue" ON ("organisation_organisation"."type" = "descriptor_descriptorvalue"."code" AND "language" = \'%s\')'
        % lang)
    cq.query_group_by.append('"type_name"')

    organisation_items = []

    for organisation in cq:
        t = {
            'id': organisation.pk,
            'name': organisation.name,
            'type': organisation.type,
            'descriptors': organisation.descriptors,
            'layout': organisation.layout_id,
            'num_establishments': organisation.establishments__count,
            'grc': organisation.
            grcs__count  # [x for x in organisation.grcs.all().values_list('id', flat=True)]
        }

        organisation_items.append(t)

    results = {
        'perms': [],
        'items': organisation_items,
        'prev': cq.prev_cursor,
        'cursor': cursor,
        'next': cq.next_cursor
    }

    return HttpResponseRest(request, results)