Ticket #5833: 5833-metapatch.patch

File 5833-metapatch.patch, 2.6 KB (added by Yeago, 14 years ago)

An operator agnostic version of FilterSpec

  • django/contrib/admin/filterspecs.py

    diff -rupN django/contrib/admin/filterspecs.py ../django/contrib/admin/filterspecs.py
    ../ class FilterSpec(object):  
    2626    def title(self):
    2727        raise NotImplementedError()
    2828       
    29     def get_query_set(self, cl, qs):
     29    def get_query_set(self, cl):
    3030        return False
    3131   
    3232    def consumed_params(self):
  • django/contrib/admin/views/main.py

    diff -rupN django/contrib/admin/views/main.py ../django/contrib/admin/views/main.py
    ../ class ChangeList(object):  
    172172            order_type = params[ORDER_TYPE_VAR]
    173173        return order_field, order_type
    174174
     175    def apply_filter_specs(self,qs,filter_dict):
     176        # Here we apply the results of Q objects collected from our FilterSpecs
     177        # The & operator becomes a sensible default which can be overridden in
     178        # particular cases.
     179        return qs.filter(reduce(operator.__and__(filter_dict.values())))
     180
    175181    def get_query_set(self):
    176182        qs = self.root_query_set
    177183        lookup_params = self.params.copy() # a dictionary of the query string
    class ChangeList(object):  
    191197                lookup_params[key] = value.split(',')
    192198               
    193199        # Let every filter spec modify the qs and params to its liking
     200        # Redux to above comment -- Let every filter spec return a Q object
     201
     202        filter_dict = {} # A handy repository for our collected Q objects
    194203        for filter_spec in self.filter_specs:
    195             new_qs = filter_spec.get_query_set(self, qs)
    196             if new_qs is not None and new_qs is not False:
    197                 qs = new_qs
    198                 # Only consume params if we got a new queryset
     204            new_q = filter_spec.get_query_set(self)
     205            if new_q:
     206                filter_dict[filter_spec] = new_q
     207                # Only consume params if we got a new Q obj
    199208                for param in filter_spec.consumed_params():
    200209                    try:
    201210                        del lookup_params[param]
    class ChangeList(object):  
    209218                else:
    210219                    lookup_params[key] = True
    211220
     221        # The below allows the user to override the querying assumption
     222        # among filters (x = y AND z = a AND ...)
     223        if filter_dict:
     224            qs = self.apply_filter_specs(filter_dict)
     225
    212226        # Apply lookup parameters from the query string.
    213227        try:
    214228            qs = qs.filter(**lookup_params)
Back to Top