def sales_calc(self, request): filter_class = self.filter_class f = filter_class(request.GET, queryset=SalesRecord.objects.all()).qs if request.query_params.get("search"): f = filters.SearchFilter().filter_queryset( self.request, SalesRecord.objects.all(), self) qs = f calc_result = dict( bussiness_count=qs.values('business_id').distinct().count(), store_count=qs.values('store_id').distinct().count(), product_count=qs.values('store_id').distinct().count(), retail_total_sales=qs.aggregate(Sum('retail_sales')).get( 'retail_sales__sum', 0), retail_total_price=qs.aggregate(Sum('retail_price')).get( 'retail_price__sum', 0), project_total_sales=qs.aggregate(Sum('project_sales')).get( 'project_sales__sum', 0), project_total_price=qs.aggregate(Sum('project_price')).get( 'project_price__sum', 0), wholesale_total_sales=qs.aggregate(Sum('wholesale_sales')).get( 'wholesale_sales__sum', 0), wholesale_total_price=qs.aggregate(Sum('wholesale_price')).get( 'wholesale_price__sum', 0), online_total_sales=qs.aggregate(Sum('online_sales')).get( 'online_sales__sum', 0), online_total_price=qs.aggregate(Sum('online_price')).get( 'online_price__sum', 0), ) serializer = SalesCalcSerializer(calc_result) return Response(serializer.data, status=status.HTTP_200_OK)
def test_search_field_with_null_characters(self): view = generics.GenericAPIView() request = factory.get('/?search=\0as%00d\x00f') request = view.initialize_request(request) terms = filters.SearchFilter().get_search_terms(request) assert terms == ['asdf']
def test_must_call_distinct_restores_meta_for_each_field(self): # In this test case the attribute of the fk model comes first in the # list of search fields. filter_ = filters.SearchFilter() prefixes = [''] + list(filter_.lookup_prefixes) for prefix in prefixes: assert not filter_.must_call_distinct( SearchFilterModelFk._meta, ["%sattribute__label" % prefix, "%stitle" % prefix])
def test_must_call_distinct(self): filter_ = filters.SearchFilter() prefixes = [''] + list(filter_.lookup_prefixes) for prefix in prefixes: assert not filter_.must_call_distinct(SearchFilterModelFk._meta, ["%stitle" % prefix]) assert not filter_.must_call_distinct( SearchFilterModelFk._meta, ["%stitle" % prefix, "%sattribute__label" % prefix])
def test_must_call_distinct_subsequent_m2m_fields(self): f = filters.SearchFilter() queryset = SearchFilterModelM2M.objects.annotate(title_text=Upper( Concat(models.F('title'), models.F('text')))).all() # Sanity check that m2m must call distinct assert f.must_call_distinct(queryset, ['attributes']) # Annotated field should not prevent m2m must call distinct assert f.must_call_distinct(queryset, ['title_text', 'attributes'])
def test_must_call_distinct(self): filter_ = filters.SearchFilter() prefixes = [''] + list(filter_.lookup_prefixes) for prefix in prefixes: self.assertFalse( filter_.must_call_distinct(SearchFilterModelM2M._meta, ["%stitle" % prefix])) self.assertTrue( filter_.must_call_distinct( SearchFilterModelM2M._meta, ["%stitle" % prefix, "%sattributes__label" % prefix]))
def filter_queryset(self, request, queryset, view): """ Filter the given queryset to results which match the search term in the request parameter. :param request: The request :param queryset: The queryset to filter :param view: The current view """ search_term = request.query_params.get(api_settings.SEARCH_PARAM, "") if search_term: return self.search_queryset(queryset, search_term) return filters.SearchFilter().filter_queryset(request, queryset, view)
def get(self, request): """ Builds the representation for the GET method. """ # Get all HE InVisualizations queryset = HistoricalEventsInVisualizations.objects.all() # Perform a search queryset = filters.SearchFilter().filter_queryset( self.request, queryset, self) # Order the set accordingly to query parameters queryset = filters.OrderingFilter().filter_queryset( self.request, queryset, self) # Filter the set by potential filters queryset = VisualizationsLinkedByEventFilter(request.GET, queryset=queryset) # Set the pagination # set defaul page_size = 10 # get url param request_page_size = request.QUERY_PARAMS.get('page_size') if request_page_size: try: page_size = strict_positive_int(request_page_size) except (KeyError, ValueError): pass # Set the pagination paginator = Paginator(queryset, page_size) page = request.QUERY_PARAMS.get('page') try: eventsinvisualizations = paginator.page(page) except PageNotAnInteger: eventsinvisualizations = paginator.page(1) except EmptyPage: eventsinvisualizations = paginator.page(paginator.num_pages) # Serialize the data serializer = PaginatedListVisualizationLinkedByEventSerializer( eventsinvisualizations) # log.info(paginator.page_range) # return set_jsonschema_link_header(Response(serializer.data), 'visualization_collection', request) return Response(serializer.data)
def get(self, request): """ Builds the representation for the GET method. """ # Get all Datasets In Visualizations # queryset = MetricsInVisualizations.objects.all() queryset = DatasetsInVisualizations.objects.all() # Perform a search queryset = filters.SearchFilter().filter_queryset( self.request, queryset, self) # Order the set accordingly to query parameters queryset = filters.OrderingFilter().filter_queryset( self.request, queryset, self) # Filter the set by potential filters queryset = VisualizationsLinkedByDatasetFilter(request.GET, queryset=queryset) # Set the pagination page_size = 10 request_page_size = request.QUERY_PARAMS.get('page_size') if request_page_size: try: page_size = strict_positive_int(request_page_size) except (KeyError, ValueError): pass # Set the pagination paginator = Paginator(queryset, page_size) page = request.QUERY_PARAMS.get('page') try: datasetsinvisualizations = paginator.page(page) except PageNotAnInteger: datasetsinvisualizations = paginator.page(1) except EmptyPage: datasetsinvisualizations = paginator.page(paginator.num_pages) # Serialize the data serializer = PaginatedListVisualizationLinkedByDatasetSerializer( datasetsinvisualizations) return Response(serializer.data)
def get_queryset(self, pk=None): """All videos except for null ones.""" queryset = Video.objects.filter(is_unlisted=False).values() request = self.request fields = [x.name for x in Video._meta.fields] for f in VIDEO_FIELDS: fields.remove(f) def get_score_annotation(user_preferences_vector): """Returns an sql object annotating queries with the video ratings (sclar product).""" return sum( [F(f) * v for f, v in zip(VIDEO_FIELDS, user_preferences_vector)]) features = self.get_features_from_request() default_features = [constants['DEFAULT_PREFS_VAL'] for _ in VIDEO_FIELDS] search_username = self.need_scores_for_username() # computing score inside the database if search_username: fields_exclude = set(Video.COMPUTED_PROPERTIES) fields = [f for f in fields if f not in fields_exclude] queryset = queryset.values(*fields) queryset = queryset.annotate(**{key: F(f'videorating__{key}') for key in VIDEO_FIELDS}, user=F( 'videorating__user__user__username')).filter( user=search_username) # for myself, allow showing public/non-public videos if search_username == request.user.username: is_public = request.query_params.get('show_all_my_videos', 'true') == 'false' print(is_public) else: # for other people, only show public videos is_public = True # keeping only public videos if is_public: queryset = VideoRatingPrivacy._annotate_privacy( queryset, prefix='videoratingprivacy', field_user=None, filter_add={'videoratingprivacy__user__user__username': search_username} ) queryset = queryset.filter(_is_public=True) queryset = queryset.annotate(rating_n_experts=Value(1, IntegerField())) q1 = Q(expertrating_video_1__user__user__username=search_username) q2 = Q(expertrating_video_2__user__user__username=search_username) c1 = Count('expertrating_video_1', q1, distinct=True) c2 = Count('expertrating_video_2', q2, distinct=True) queryset = queryset.annotate(rating_n_ratings=c1 + c2) queryset = queryset.annotate(n_public_experts=Value(1, IntegerField())) queryset = queryset.annotate(n_private_experts=Value(0, IntegerField())) # TODO: a hack. improve this queryset = queryset.annotate( public_experts=Value("", CharField())) # logging model usage in search if self.request.user.is_authenticated: RepresentativeModelUsage.objects.get_or_create( viewer=UserPreferences.objects.get(user__username=self.request.user.username), model=UserPreferences.objects.get(user__username=search_username) ) queryset = queryset.annotate( score_preferences_term=get_score_annotation(features)) queryset = queryset.annotate( tournesol_score=get_score_annotation(default_features)) queryset = queryset.annotate( score_search_term_=Value( 0.0, FloatField())) if request.query_params.get('search'): # computing the postgres score for search if connection.vendor.startswith('postgres'): s_query = request.query_params.get('search', '') def word_to_query(w): """Convert one word into a query.""" queries = [] queries.append(SearchQuery(w, search_type='raw')) queries.append(SearchQuery(w + ':*', search_type='raw')) return reduce(lambda x, y: x | y, queries) def words_to_query(s_query, max_len=100, max_word_len=20): """Convert a string with words into a SearchQuery.""" s_query = s_query[:max_len] s_query = s_query.split(' ') s_query = [''.join(filter(str.isalnum, x)) for x in s_query] s_query = [x for x in s_query if 1 <= len(x) <= max_word_len] s_query = [word_to_query(x) for x in s_query] if not s_query: return SearchQuery('') return reduce(lambda x, y: x & y, s_query) s_query = words_to_query(s_query) s_vectors = [SearchVector(f, weight=w) for f, w in zip(self.search_fields, self.search_weights)] s_vector = reduce(lambda x, y: x + y, s_vectors) queryset = queryset.annotate( score_search_term_=SearchRank(s_vector, s_query)) else: # in other databases, using basic filtering queryset = filters_.SearchFilter().filter_queryset(self.request, queryset, self) queryset = queryset.annotate( score_search_term_=Value( 1.0, FloatField())) queryset = queryset.annotate( score_search_term=F('score_search_term_') * VideoSearchEngine.VIDEO_SEARCH_COEFF) queryset = queryset.annotate( score=F('score_preferences_term') + F('score_search_term')) return queryset
def filter_queryset(self, request, queryset, view): search_term = request.query_params.get(api_settings.SEARCH_PARAM, "") if search_term: return self.search_queryset(queryset, search_term) return filters.SearchFilter().filter_queryset(request, queryset, view)
class NestedInformationPackageSerializer(DynamicHyperlinkedModelSerializer): responsible = UserSerializer(read_only=True) package_type = serializers.ChoiceField( choices=InformationPackage.PACKAGE_TYPE_CHOICES) package_type_display = serializers.SerializerMethodField() information_packages = serializers.SerializerMethodField() aic = serializers.PrimaryKeyRelatedField( queryset=InformationPackage.objects.all()) submission_agreement = serializers.PrimaryKeyRelatedField( queryset=SubmissionAgreement.objects.all()) workarea = serializers.SerializerMethodField() first_generation = serializers.SerializerMethodField() last_generation = serializers.SerializerMethodField() new_version_in_progress = serializers.SerializerMethodField() permissions = serializers.SerializerMethodField() agents = serializers.SerializerMethodField() search_filter = filters.SearchFilter() def get_package_type_display(self, obj): return obj.get_package_type_display() def get_permissions(self, obj): user = getattr(self.context.get('request'), 'user', None) checker = self.context.get('perm_checker') return obj.get_permissions(user=user, checker=checker) def get_agents(self, obj): try: agent_objs = obj.prefetched_agents except AttributeError: agent_objs = obj.agents.all() agents = AgentSerializer(agent_objs, many=True).data return { '{role}_{type}'.format(role=a['role'], type=a['type']): a for a in agents } def get_information_packages(self, obj): request = self.context['request'] return InformationPackageSerializer( obj.related_ips(), many=True, context={ 'request': request, 'perm_checker': self.context.get('perm_checker') }).data def get_workarea(self, obj): try: workareas = obj.prefetched_workareas except AttributeError: request = self.context.get('request') see_all = request.user.has_perm('ip.see_all_in_workspaces') workareas = obj.workareas.all() if not see_all: workareas = workareas.filter(user=request.user) return WorkareaSerializer(workareas, many=True, context=self.context).data def get_first_generation(self, obj): if hasattr(obj, 'first_generation'): return obj.first_generation return obj.is_first_generation() def get_last_generation(self, obj): if hasattr(obj, 'last_generation'): return obj.last_generation return obj.is_last_generation() def get_new_version_in_progress(self, obj): new = obj.new_version_in_progress() if new is None: return None return WorkareaSerializer(new, context=self.context).data class Meta: model = InformationPackage fields = ( 'url', 'id', 'label', 'object_identifier_value', 'package_type', 'package_type_display', 'responsible', 'create_date', 'entry_date', 'state', 'status', 'step_state', 'archived', 'cached', 'aic', 'information_packages', 'generation', 'policy', 'message_digest', 'agents', 'message_digest_algorithm', 'submission_agreement', 'submission_agreement_locked', 'workarea', 'object_size', 'first_generation', 'last_generation', 'start_date', 'end_date', 'new_version_in_progress', 'appraisal_date', 'permissions', ) extra_kwargs = { 'id': { 'read_only': False, 'validators': [], }, 'object_identifier_value': { 'read_only': False, 'validators': [], }, }