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