Beispiel #1
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 #2
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)
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)
Beispiel #5
0
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)
Beispiel #6
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)