def __init__(self, field, request, params, model, model_admin, field_path): self.field = field self.field_path = field_path self.used_parameters = {} self.lookup_kwarg_isnull = '%s__isnull' % field_path other_model = get_model_from_relation(field) rel_name = other_model._meta.pk.name if "tags__tag_category__name__iexact" in request.GET: self.lookup_kwarg = '%s__description' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) # getting the first char of values cat_t = request.GET.get("tags__tag_category__name__iexact") cat = TagCategory.objects.get(name__iexact=cat_t) tags = Tag.objects.filter(tag_category__id__exact="%s" % cat.pk) self.lookup_choices = list(tag.description for tag in tags) self.lookup_choices.sort() else: self.lookup_kwarg = '%s__tag_category__name__iexact' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) # getting the first char of values self.lookup_choices = list( cat.name for cat in TagCategory.objects.all()) self.lookup_choices.sort() for p in self.expected_parameters(): if p in params: value = params.pop(p) self.used_parameters[p] = prepare_lookup_value(p, value) # super(RelatedFieldListFilter, self).__init__( # field, request, params, model, model_admin, field_path) self.title = _('tag category')
def populate_state(self): params = self.state.params.copy() self.used_parameters = dict() for p in self.expected_parameters(): if p in params: value = params[p] self.used_parameters[p] = prepare_lookup_value(p, value)
def __init__(self, field, request, params, model, model_admin, field_path): self.field = field self.field_path = field_path self.used_parameters = {} self.lookup_kwarg_isnull = '%s__isnull' % field_path other_model = get_model_from_relation(field) rel_name = other_model._meta.pk.name if "tags__tag_category__name__iexact" in request.GET: self.lookup_kwarg = '%s__description' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) # getting the first char of values cat_t = request.GET.get("tags__tag_category__name__iexact") cat = TagCategory.objects.get(name__iexact=cat_t) tags = Tag.objects.filter(tag_category__id__exact="%s" % cat.pk) self.lookup_choices = list(tag.description for tag in tags) self.lookup_choices.sort() else: self.lookup_kwarg = '%s__tag_category__name__iexact' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) # getting the first char of values self.lookup_choices = list(cat.name for cat in TagCategory.objects.all()) self.lookup_choices.sort() for p in self.expected_parameters(): if p in params: value = params.pop(p) self.used_parameters[p] = prepare_lookup_value(p, value) # super(RelatedFieldListFilter, self).__init__( # field, request, params, model, model_admin, field_path) self.title = _('tag category')
def get_filters(self, request): lookup_params = self.get_filters_params() use_distinct = False # Normalize the types of keys for key, value in lookup_params.items(): if not isinstance(key, str): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. del lookup_params[key] lookup_params[force_str(key)] = value if not self.model_admin.lookup_allowed(key, value): raise DisallowedModelAdminLookup("Filtering by %s not allowed" % key) filter_specs = [] if self.list_filter: for list_filter in self.list_filter: if callable(list_filter): # This is simply a custom list filter class. spec = list_filter(request, lookup_params, self.model, self.model_admin) else: field_path = None if isinstance(list_filter, (tuple, list)): # This is a custom FieldListFilter class for a given field. field, field_list_filter_class = list_filter else: # This is simply a field name, so use the default # FieldListFilter class that has been registered for # the type of the given field. field, field_list_filter_class = list_filter, FieldListFilter.create if not isinstance(field, BaseField): field_path = field field = get_fields_from_path(self.model, field_path)[-1] spec = field_list_filter_class(field, request, lookup_params, self.model, self.model_admin, field_path=field_path) # Check if we need to use distinct() use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, field_path)) if spec and spec.has_output(): filter_specs.append(spec) # At this point, all the parameters used by the various ListFilters # have been removed from lookup_params, which now only contains other # parameters passed via the query string. We now loop through the # remaining parameters both to ensure that all the parameters are valid # fields and to determine if at least one of them needs distinct(). If # the lookup parameters aren't real fields, then bail out. try: for key, value in lookup_params.items(): lookup_params[key] = prepare_lookup_value(key, value) use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, key)) return filter_specs, bool(filter_specs), lookup_params, use_distinct except FieldDoesNotExist as e: six.reraise(IncorrectLookupParameters, IncorrectLookupParameters(e), sys.exc_info()[2])
def get_filters(self, request): lookup_params = self.get_filters_params() use_distinct = False # Normalize the types of keys for key, value in lookup_params.items(): if not isinstance(key, str): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. del lookup_params[key] lookup_params[force_str(key)] = value if not self.model_admin.lookup_allowed(key, value): raise DisallowedModelAdminLookup("Filtering by %s not allowed" % key) filter_specs = [] if self.list_filter: for list_filter in self.list_filter: if callable(list_filter): # This is simply a custom list filter class. spec = list_filter(request, lookup_params, self.model, self.model_admin) else: field_path = None if isinstance(list_filter, (tuple, list)): # This is a custom FieldListFilter class for a given field. field, field_list_filter_class = list_filter else: # This is simply a field name, so use the default # FieldListFilter class that has been registered for # the type of the given field. field, field_list_filter_class = list_filter, FieldListFilter.create if not isinstance(field, models.Field): field_path = field field = get_fields_from_path(self.model, field_path)[-1] spec = field_list_filter_class(field, request, lookup_params, self.model, self.model_admin, field_path=field_path) # Check if we need to use distinct() use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, field_path)) if spec and spec.has_output(): filter_specs.append(spec) # At this point, all the parameters used by the various ListFilters # have been removed from lookup_params, which now only contains other # parameters passed via the query string. We now loop through the # remaining parameters both to ensure that all the parameters are valid # fields and to determine if at least one of them needs distinct(). If # the lookup parameters aren't real fields, then bail out. try: for key, value in lookup_params.items(): lookup_params[key] = prepare_lookup_value(key, value) use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, key)) return filter_specs, bool(filter_specs), lookup_params, use_distinct except FieldDoesNotExist as e: six.reraise(IncorrectLookupParameters, IncorrectLookupParameters(e), sys.exc_info()[2])
def __init__(self, field, request, params, model, model_admin, field_path): self.field = field self.field_path = field_path self.title = getattr(field, "verbose_name", field_path) super(FieldListFilter, self).__init__(request, params, model, model_admin) for p in self.expected_parameters(): if p in params: value = params.pop(p) self.used_parameters[p] = prepare_lookup_value(p, value)
def get_filtered_queryset(self, qs): filters = {} query_string = self.GET.get('query_string', None) if query_string: for item in query_string.split(":"): k, v = item.split("=") filters[smart_text(k)] = prepare_lookup_value(smart_text(k), smart_text(v)) return qs.filter(**filters)
def __init__(self, field, request, params, model, model_admin, field_path): self.field = field self.field_path = field_path self.title = getattr(field, 'verbose_name', field_path) super(FieldListFilter, self).__init__(request, params, model, model_admin) for p in self.expected_parameters(): if p in params: value = params.pop(p) self.used_parameters[p] = prepare_lookup_value(p, value)
def get_filtered_queryset(self, qs): filters = {} query_string = self.GET.get("query_string", None) if query_string: for item in query_string.split("&"): k, v = item.split("=") if k != "t": filters[smart_str(k)] = prepare_lookup_value(smart_str(k), smart_str(v)) return qs.filter(**filters)
def get_filtered_queryset(self, qs): filters = {} query_string = self.GET.get('query_string', None) if query_string: for item in query_string.split("&"): k, v = item.split("=") if k != "t": filters[smart_bytes(k)] = prepare_lookup_value(smart_bytes(k), smart_bytes(v)) return qs.filter(**filters)
def get_filters(self, request): if not request.session.get('use_new_filters'): return super(CustomChangeList, self).get_filters(request) new_filter, created = CustomFilter.objects.get_or_create(user=request.user, model_name=self.model.__name__, app_name=self.model._meta.app_label, default=True) form = CustomFilterForm(request.GET.copy(), custom_filter=new_filter) if len(request.GET) and form.is_valid(): form.save() self.current_filter = CustomFilter.objects.filter(user=request.user, path_info=request.path_info, default=True) # loading filter set params into change list, so they will be applied in queryset if self.current_filter: filter_params, self.exclude_params, self.bundled_params = self.current_filter[0].get_filter_params() self.params.update(**filter_params) lookup_params = self.params.copy() # a dictionary of the query string use_distinct = False # Remove all the parameters that are globally and systematically # ignored. for ignored in IGNORED_PARAMS: if ignored in lookup_params: del lookup_params[ignored] # Normalize the types of keys for key, value in lookup_params.items(): if not isinstance(key, str): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. del lookup_params[key] lookup_params[smart_str(key)] = value if not self.model_admin.lookup_allowed(key, value): raise SuspiciousOperation("Filtering by %s not allowed" % key) filter_specs = [] if self.list_filter: for list_filter in self.list_filter: if callable(list_filter): # This is simply a custom list filter class. spec = list_filter(request, lookup_params, self.model, self.model_admin) else: field_path = None if isinstance(list_filter, (tuple, list)): # This is a custom FieldListFilter class for a given field. field, field_list_filter_class = list_filter else: # This is simply a field name, so use the default # FieldListFilter class that has been registered for # the type of the given field. field, field_list_filter_class = list_filter, FieldListFilter.create if not isinstance(field, models.Field): field_path = field field = get_fields_from_path(self.model, field_path)[-1] spec = field_list_filter_class(field, request, lookup_params, self.model, self.model_admin, field_path=field_path) # Check if we need to use distinct() use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, field_path)) if spec and spec.has_output(): filter_specs.append(spec) # At this point, all the parameters used by the various ListFilters # have been removed from lookup_params, which now only contains other # parameters passed via the query string. We now loop through the # remaining parameters both to ensure that all the parameters are valid # fields and to determine if at least one of them needs distinct(). If # the lookup parameters aren't real fields, then bail out. for key, value in lookup_params.items(): lookup_params[key] = prepare_lookup_value(key, value) try: use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, key)) except FieldDoesNotExist, e: lookup_params.pop(key)
def get_filters(self, request): lookup_params = self.params.copy() # a dictionary of the query string use_distinct = False # Remove all the parameters that are globally and systematically # ignored. for ignored in IGNORED_PARAMS: if ignored in lookup_params: del lookup_params[ignored] # Normalize the types of keys for key, value in lookup_params.items(): if not isinstance(key, str): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. del lookup_params[key] lookup_params[smart_str(key)] = value if not self.model_admin.lookup_allowed(key, value): raise SuspiciousOperation("Filtering by %s not allowed" % key) filter_specs = [] if self.list_filter: for list_filter in self.list_filter: if callable(list_filter): # This is simply a custom list filter class. spec = list_filter(request, lookup_params, self.model, self.model_admin) else: field_path = None if isinstance(list_filter, (tuple, list)): # This is a custom FieldListFilter class for a given field. field, field_list_filter_class = list_filter else: # This is simply a field name, so use the default # FieldListFilter class that has been registered for # the type of the given field. field, field_list_filter_class = list_filter, FieldListFilter.create if not isinstance(field, models.Field): field_path = field field = get_fields_from_path(self.model, field_path)[-1] spec = field_list_filter_class(field, request, lookup_params, self.model, self.model_admin, field_path=field_path) # Check if we need to use distinct() use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, field_path)) if spec and spec.has_output(): filter_specs.append(spec) # At this point, all the parameters used by the various ListFilters # have been removed from lookup_params, which now only contains other # parameters passed via the query string. We now loop through the # remaining parameters both to ensure that all the parameters are valid # fields and to determine if at least one of them needs distinct(). for key, value in lookup_params.items(): lookup_params[key] = prepare_lookup_value(key, value) use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, key)) return filter_specs, bool(filter_specs), lookup_params, use_distinct
def get_filters(self, request): lookup_params = self.params.copy() # a dictionary of the query string use_distinct = False # Remove all the parameters that are globally and systematically # ignored. for ignored in IGNORED_PARAMS: if ignored in lookup_params: del lookup_params[ignored] # Normalize the types of keys for key, value in lookup_params.items(): if not isinstance(key, str): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. del lookup_params[key] lookup_params[smart_str(key)] = value if not self.model_admin.lookup_allowed(key, value): raise SuspiciousOperation("Filtering by %s not allowed" % key) filter_specs = [] if self.list_filter: for list_filter in self.list_filter: if callable(list_filter): # This is simply a custom list filter class. spec = list_filter(request, lookup_params, self.model, self.model_admin) else: field_path = None if isinstance(list_filter, (tuple, list)): # This is a custom FieldListFilter class for a given field. field, field_list_filter_class = list_filter else: # This is simply a field name, so use the default # FieldListFilter class that has been registered for # the type of the given field. field, field_list_filter_class = list_filter, FieldListFilter.create if not isinstance(field, models.Field): field_path = field field = get_fields_from_path(self.model, field_path)[-1] spec = field_list_filter_class(field, request, lookup_params, self.model, self.model_admin, field_path=field_path) # Check if we need to use distinct() use_distinct = (use_distinct or lookup_needs_distinct( self.lookup_opts, field_path)) if spec and spec.has_output(): filter_specs.append(spec) # At this point, all the parameters used by the various ListFilters # have been removed from lookup_params, which now only contains other # parameters passed via the query string. We now loop through the # remaining parameters both to ensure that all the parameters are valid # fields and to determine if at least one of them needs distinct(). for key, value in lookup_params.items(): lookup_params[key] = prepare_lookup_value(key, value) use_distinct = (use_distinct or lookup_needs_distinct(self.lookup_opts, key)) return filter_specs, bool(filter_specs), lookup_params, use_distinct
def get_filters(self, request): if not request.session.get('use_new_filters'): return super(CustomChangeList, self).get_filters(request) new_filter, created = CustomFilter.objects.get_or_create( user=request.user, model_name=self.model.__name__, app_name=self.model._meta.app_label, default=True) form = CustomFilterForm(request.GET.copy(), custom_filter=new_filter) if len(request.GET) and form.is_valid(): form.save() self.current_filter = CustomFilter.objects.filter( user=request.user, path_info=request.path_info, default=True) # loading filter set params into change list, so they will be applied in queryset if self.current_filter: filter_params, self.exclude_params, self.bundled_params = self.current_filter[ 0].get_filter_params() self.params.update(**filter_params) lookup_params = self.params.copy() # a dictionary of the query string use_distinct = False # Remove all the parameters that are globally and systematically # ignored. for ignored in IGNORED_PARAMS: if ignored in lookup_params: del lookup_params[ignored] # Normalize the types of keys for key, value in lookup_params.items(): if not isinstance(key, str): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. del lookup_params[key] lookup_params[smart_str(key)] = value if not self.model_admin.lookup_allowed(key, value): raise SuspiciousOperation("Filtering by %s not allowed" % key) filter_specs = [] if self.list_filter: for list_filter in self.list_filter: if callable(list_filter): # This is simply a custom list filter class. spec = list_filter(request, lookup_params, self.model, self.model_admin) else: field_path = None if isinstance(list_filter, (tuple, list)): # This is a custom FieldListFilter class for a given field. field, field_list_filter_class = list_filter else: # This is simply a field name, so use the default # FieldListFilter class that has been registered for # the type of the given field. field, field_list_filter_class = list_filter, FieldListFilter.create if not isinstance(field, models.Field): field_path = field field = get_fields_from_path(self.model, field_path)[-1] spec = field_list_filter_class(field, request, lookup_params, self.model, self.model_admin, field_path=field_path) # Check if we need to use distinct() use_distinct = (use_distinct or lookup_needs_distinct( self.lookup_opts, field_path)) if spec and spec.has_output(): filter_specs.append(spec) # At this point, all the parameters used by the various ListFilters # have been removed from lookup_params, which now only contains other # parameters passed via the query string. We now loop through the # remaining parameters both to ensure that all the parameters are valid # fields and to determine if at least one of them needs distinct(). If # the lookup parameters aren't real fields, then bail out. for key, value in lookup_params.items(): lookup_params[key] = prepare_lookup_value(key, value) try: use_distinct = (use_distinct or lookup_needs_distinct( self.lookup_opts, key)) except FieldDoesNotExist, e: lookup_params.pop(key)