def filter_queryset(self, request): params = request.query_params.dict() # Since this is being passed in as a query string, the object ends up # coming through as a string. params['filter_params'] = json.loads(params.get('filter_params', '{}')) inventory_type = params.pop('inventory_type', None) params = search.process_search_params( params=params, user=request.user, is_api_request=True, ) queryset = search.inventory_search_filter_sort( inventory_type, params=params, user=request.user, ) if 'selected' in request.data: # Return labels limited to the 'selected' list. Otherwise, if selected is empty, return all if request.data['selected']: return queryset.filter(id__in=request.data['selected'], ) return queryset
def filter_queryset(self, request): params = request.query_params.dict() # Since this is being passed in as a query string, the object ends up # coming through as a string. params['filter_params'] = json.loads(params.get('filter_params', '{}')) inventory_type = params.pop('inventory_type', None) params = search.process_search_params( params=params, user=request.user, is_api_request=True, ) queryset = search.inventory_search_filter_sort( inventory_type, params=params, user=request.user, ) if 'selected' in request.data: # Return labels limited to the 'selected' list. Otherwise, if selected is empty, return all if request.data['selected']: return queryset.filter( id__in=request.data['selected'], ) return queryset
def filter_labels_for_inv_type(request, inventory_type=None): """ Method used to filter labels by inventory type and return is_applied inventory id's Method was initially built as a class defined function above to handle more parameters. """ params = request.query_params.dict() # Since this is being passed in as a query string, the object ends up # coming through as a string. params['filter_params'] = json.loads(params.get('filter_params', '{}')) params = search.process_search_params( params=params, user=request.user, is_api_request=True, ) queryset = search.inventory_search_filter_sort( inventory_type, params=params, user=request.user, ) if 'selected' in request.data: # Return labels limited to the 'selected' list. Otherwise, if selected is empty, return all if request.data['selected']: return queryset.filter(id__in=request.data['selected'], ) return queryset
def add(self, request, pk): """ Add inventory to project :PUT: Expects organization_id in query string. --- parameters: - name: organization_id description: ID of organization to associate new project with type: integer required: true - name: inventory_type description: type of inventory to add: 'property' or 'taxlot' type: string required: true paramType: query - name: project slug or pk description: The project slug identifier or primary key for this project required: true paramType: path - name: selected description: ids of property or taxlot views to add type: array[int] required: true Returns: { 'status': 'success', 'added': [list of property/taxlot view ids added] } """ error = None inventory = None status_code = status.HTTP_200_OK inventory_type = request.query_params.get( 'inventory_type', request.data.get('inventory_type', None) ) if not inventory_type: error, status_code = self.get_error( 'missing param', 'inventory_type' ) else: key = self.get_key(pk) project = self.get_project(key, pk) if not project: error, status_code = self.get_error( 'not found', key=key, val=pk ) if not error: project = project[0] view_type = "{}_view".format(inventory_type) request.data['inventory_type'] = view_type params = search.process_search_params( request.data, request.user, is_api_request=False ) organization_id = self.get_organization() params['organization_id'] = organization_id qs = search.inventory_search_filter_sort( view_type, params=params, user=request.user ) if request.data.get('selected', None) \ and isinstance(request.data.get('selected'), list): inventory = qs.filter(pk__in=request.data.get('selected')) # TODO is this still relevant elif request.data.get('select_all_checkbox', None): inventory = qs if not inventory: error, status_code = self.get_error( 'missing inventory', key=inventory_type ) if error: result = {'status': 'error', 'message': error} else: Model = self.ProjectViewModels[inventory_type] new_project_views = [ self.project_view_factory(inventory_type, project.id, view.id) for view in inventory ] Model.objects.bulk_create(new_project_views) added = [view.id for view in inventory] project.last_modified_by = request.user project.save() result = {'status': 'success', 'added': added} return Response(result, status=status_code)
def add(self, request, pk): """ Add inventory to project :PUT: Expects organization_id in query string. --- parameters: - name: organization_id description: ID of organization to associate new project with type: integer required: true - name: inventory_type description: type of inventory to add: 'property' or 'taxlot' type: string required: true paramType: query - name: project slug or pk description: The project slug identifier or primary key for this project required: true paramType: path - name: selected description: ids of property or taxlot views to add type: array[int] required: true Returns: { 'status': 'success', 'added': [list of property/taxlot view ids added] } """ error = None inventory = None status_code = status.HTTP_200_OK inventory_type = request.query_params.get( 'inventory_type', request.data.get('inventory_type', None) ) if not inventory_type: error, status_code = self.get_error( 'missing param', 'inventory_type' ) else: key = self.get_key(pk) project = self.get_project(key, pk) if not project: error, status_code = self.get_error( 'not found', key=key, val=pk ) if not error: project = project[0] view_type = "{}_view".format(inventory_type) request.data['inventory_type'] = view_type params = search.process_search_params( request.data, request.user, is_api_request=False ) organization_id = self.get_organization() params['organization_id'] = organization_id qs = search.inventory_search_filter_sort( view_type, params=params, user=request.user ) if request.data.get('selected', None)\ and isinstance(request.data.get('selected'), list): inventory = qs.filter(pk__in=request.data.get('selected')) # TODO is this still relevant elif request.data.get('select_all_checkbox', None): inventory = qs if not inventory: error, status_code = self.get_error( 'missing inventory', key=inventory_type ) if error: result = {'status': 'error', 'message': error} else: Model = self.ProjectViewModels[inventory_type] new_project_views = [ self.project_view_factory(inventory_type, project.id, view.id) for view in inventory ] Model.objects.bulk_create(new_project_views) added = [view.id for view in inventory] project.last_modified_by = request.user project.save() result = {'status': 'success', 'added': added} return Response(result, status=status_code)