def get_queryset(self): approval_status = self.request.query_params.get( 'approval_status', None) # org = self.request.query_params.get('org', None) orgs = self.request.query_params.getlist('org', False) enabled = self.request.query_params.get('enabled', None) ordering = self.request.query_params.get('ordering', None) owners_id = self.request.query_params.get('owners_id', None) if enabled: enabled = enabled.lower() if enabled in ['true', '1']: enabled = True else: enabled = False if ordering: ordering = [s.strip() for s in ordering.split(',')] else: # always default to last modified for consistency ordering = ['-edited_date'] listings = model_access.get_listings(self.request.user.username) if owners_id: listings = listings.filter(owners__id=owners_id) if approval_status: listings = listings.filter(approval_status=approval_status) if orgs: listings = listings.filter(agency__short_name__in=orgs) if enabled is not None: listings = listings.filter(is_enabled=enabled) # have to handle this case manually because the ordering includes an app multiple times # if there are multiple owners. We instead do sorting by case insensitive compare of the # app owner that comes first alphabetically param = [ s for s in ordering if 'owners__display_name' == s or '-owners__display_name' == s ] if ordering is not None and param: orderby = 'min' if param[0].startswith('-'): orderby = '-min' listings = listings.annotate( min=Min(Lower('owners__display_name'))).order_by(orderby) self.ordering = None # Django REST filters are canse sensitive by default, so we handle case_insensitive fields # manually. May want to abstract this functionality in an OrderingFilter sub-class case_insensitive_ordering = [ s for s in ordering if s in self.case_insensitive_ordering_fields or s.startswith('-') and s[1:] in self.case_insensitive_ordering_fields ] if ordering is not None and case_insensitive_ordering: for field in case_insensitive_ordering: if field.startswith('-'): listings = listings.order_by(Lower(field[1:])).reverse() else: listings = listings.order_by(Lower(field)) self.ordering = None return listings
def get_queryset(self): approval_status = self.request.query_params.get('approval_status', None) # org = self.request.query_params.get('org', None) orgs = self.request.query_params.getlist('org', False) enabled = self.request.query_params.get('enabled', None) ordering = self.request.query_params.get('ordering', None) owners_id = self.request.query_params.get('owners_id', None) if enabled: enabled = enabled.lower() if enabled in ['true', '1']: enabled = True else: enabled = False if ordering: ordering = [s.strip() for s in ordering.split(',')] else: # always default to last modified for consistency ordering = ['-edited_date'] listings = model_access.get_listings(self.request.user.username) if owners_id: listings = listings.filter(owners__id=owners_id) if approval_status: listings = listings.filter(approval_status=approval_status) if orgs: listings = listings.filter(agency__short_name__in=orgs) if enabled is not None: listings = listings.filter(is_enabled=enabled) # have to handle this case manually because the ordering includes an app multiple times # if there are multiple owners. We instead do sorting by case insensitive compare of the # app owner that comes first alphabetically param = [s for s in ordering if 'owners__display_name' == s or '-owners__display_name' == s] if ordering is not None and param: orderby = 'min' if param[0].startswith('-'): orderby = '-min' listings = listings.annotate(min=Min(Lower('owners__display_name'))).order_by(orderby) self.ordering = None # Django REST filters are canse sensitive by default, so we handle case_insensitive fields # manually. May want to abstract this functionality in an OrderingFilter sub-class case_insensitive_ordering = [s for s in ordering if s in self.case_insensitive_ordering_fields or s.startswith('-') and s[1:] in self.case_insensitive_ordering_fields] if ordering is not None and case_insensitive_ordering: for field in case_insensitive_ordering: if field.startswith('-'): listings = listings.order_by(Lower(field[1:])).reverse() else: listings = listings.order_by(Lower(field)) self.ordering = None return listings
def ApplicationListView(request): """ List of applications """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) listing_root_url = hal.get_abs_url_for_iwc(request) profile = model_access.get_profile(request.user.username) data = hal.create_base_structure( request, hal.generate_content_type(request.accepted_media_type)) applications = listing_model_access.get_listings(profile.user.username) items = [] embedded_items = [] for i in applications: item = { "href": '{0!s}listing/{1!s}/'.format(listing_root_url, i.id), "type": hal.generate_content_type( renderers.ApplicationResourceRenderer.media_type) } items.append(item) embedded = {'_links': {'self': item}} embedded['id'] = i.id embedded['title'] = i.title embedded['unique_name'] = i.unique_name intents = [] for j in i.intents.all(): intent = { 'action': j.action, 'media_type': j.media_type, 'label': j.label, 'icon_id': j.icon.id } intents.append(intent) embedded['intents'] = intents embedded_items.append(embedded) data['_links']['item'] = items data['_embedded']['item'] = embedded_items return Response(data)
def get_queryset(self): approval_status = self.request.query_params.get('approval_status', None) # org = self.request.query_params.get('org', None) orgs = self.request.query_params.getlist('org', False) enabled = self.request.query_params.get('enabled', None) if enabled: enabled = enabled.lower() if enabled in ['true', '1']: enabled = True else: enabled = False listings = model_access.get_listings(self.request.user.username) if approval_status: listings = listings.filter(approval_status=approval_status) if orgs: listings = listings.filter(agency__title__in=orgs) if enabled is not None: listings = listings.filter(is_enabled=enabled) return listings
def ApplicationListView(request): """ List of applications """ if not hal.validate_version(request.META.get('HTTP_ACCEPT')): return Response('Invalid version requested', status=status.HTTP_406_NOT_ACCEPTABLE) listing_root_url = hal.get_abs_url_for_iwc(request) profile = model_access.get_profile(request.user.username) data = hal.create_base_structure(request, hal.generate_content_type( request.accepted_media_type)) applications = listing_model_access.get_listings(profile.user.username) items = [] embedded_items = [] for i in applications: item = {"href": '%slisting/%s/' % (listing_root_url, i.id), "type": hal.generate_content_type(renderers.ApplicationResourceRenderer.media_type)} items.append(item) embedded = {'_links': {'self': item}} embedded['id'] = i.id embedded['title'] = i.title embedded['unique_name'] = i.unique_name intents = [] for j in i.intents.all(): intent = {'action': j.action, 'media_type': j.media_type, 'label': j.label, 'icon_id': j.icon.id} intents.append(intent) embedded['intents'] = intents embedded_items.append(embedded) data['_links']['item'] = items data['_embedded']['item'] = embedded_items return Response(data)
def get_queryset(self): approval_status = self.request.query_params.get( 'approval_status', None) # org = self.request.query_params.get('org', None) orgs = self.request.query_params.getlist('org', False) enabled = self.request.query_params.get('enabled', None) ordering = self.request.query_params.getlist('ordering', None) if enabled: enabled = enabled.lower() if enabled in ['true', '1']: enabled = True else: enabled = False listings = model_access.get_listings(self.request.user.username) if approval_status: listings = listings.filter(approval_status=approval_status) if orgs: listings = listings.filter(agency__title__in=orgs) if enabled is not None: listings = listings.filter(is_enabled=enabled) # have to handle this case manually because the ordering includes an app multiple times # if there are multiple owners. We instead do sorting by case insensitive compare of the # app owner that comes first alphabetically param = [ s for s in ordering if 'owners__display_name' == s or '-owners__display_name' == s ] if ordering is not None and param: orderby = 'min' if param[0].startswith('-'): orderby = '-min' listings = listings.annotate( min=Min(Lower('owners__display_name'))).order_by(orderby) self.ordering = None return listings
def test_get_listings(self): username = '******' listings = model_access.get_listings(username) self.assertTrue(len(listings) >= 2) all_listings = models.Listing.objects.all() self.assertEqual(len(all_listings), 110)
def test_get_listings(self): username = '******' listings = model_access.get_listings(username) self.assertTrue(len(listings) >= 2) all_listings = models.Listing.objects.all() self.assertEqual(len(all_listings), 8)
def test_get_listings_for_user(self): username = '******' listings = model_access.get_listings(username) self.assertTrue(len(listings) >= 2)