Django

Code

Changeset 1885

Show
Ignore:
Timestamp:
01/09/06 08:34:17 (3 years ago)
Author:
russellm
Message:

magic-removal: Cleaned up get_in_bulk() and get_date_list() DB queries to match documentation and improve error checking.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/db/models/manager.py

    r1884 r1885  
    190190        return obj_list[0] 
    191191 
    192     def get_in_bulk(self, *args, **kwargs): 
    193         # Separate any list arguments: the first list will be used as the id list; subsequent 
    194         # lists will be ignored. 
    195         id_args = filter(lambda arg: isinstance(arg, list), args) 
    196         # Separate any non-list arguments: these are assumed to be query arguments 
    197         sql_args = filter(lambda arg: not isinstance(arg, list), args) 
    198  
    199         id_list = id_args and id_args[0] or kwargs.get('id_list', []) 
     192    def get_in_bulk(self, id_list, *args, **kwargs): 
     193        assert isinstance(id_list, list), "get_in_bulk() must be provided with a list of IDs." 
    200194        assert id_list != [], "get_in_bulk() cannot be passed an empty ID list."         
    201195        kwargs['where'] = ["%s.%s IN (%s)" % (backend.quote_name(self.klass._meta.db_table), backend.quote_name(self.klass._meta.pk.column), ",".join(['%s'] * len(id_list)))] 
    202196        kwargs['params'] = id_list 
    203         obj_list = self.get_list(*sql_args, **kwargs) 
     197        obj_list = self.get_list(*args, **kwargs) 
    204198        return dict([(getattr(o, self.klass._meta.pk.attname), o) for o in obj_list]) 
    205199 
     
    234228        return self.get_object(*args, **kwargs) 
    235229 
    236     def __get_date_list(self, field, *args, **kwargs): 
    237         # Separate any string arguments: the first will be used as the kind 
    238         kind_args = filter(lambda arg: isinstance(arg, str), args) 
    239         # Separate any non-list arguments: these are assumed to be query arguments 
    240         sql_args = filter(lambda arg: not isinstance(arg, str), args) 
    241          
     230    def __get_date_list(self, field, kind, *args, **kwargs): 
    242231        from django.db.backends.util import typecast_timestamp 
    243         kind = kind_args and kind_args[0] or kwargs.get(['kind'],"") 
    244232        assert kind in ("month", "year", "day"), "'kind' must be one of 'year', 'month' or 'day'." 
    245233        order = 'ASC' 
     
    252240            kwargs.setdefault('where', []).append('%s.%s IS NOT NULL' % \ 
    253241                (backend.quote_name(self.klass._meta.db_table), backend.quote_name(field.column))) 
    254         select, sql, params = self._get_sql_clause(*sql_args, **kwargs) 
     242        select, sql, params = self._get_sql_clause(*args, **kwargs) 
    255243        sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1 %s' % \ 
    256244            (backend.get_date_trunc_sql(kind, '%s.%s' % (backend.quote_name(self.klass._meta.db_table), 
  • django/branches/magic-removal/tests/modeltests/basic/models.py

    r1631 r1885  
    181181[datetime.datetime(2005, 7, 31, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 28, 0, 0)] 
    182182 
     183# Try some bad arguments to __get_date_list 
     184>>> Article.objects.get_pub_date_list('badarg') 
     185Traceback (most recent call last): 
     186    ... 
     187AssertionError: 'kind' must be one of 'year', 'month' or 'day'. 
     188>>> Article.objects.get_pub_date_list(order='ASC') 
     189Traceback (most recent call last): 
     190    ... 
     191TypeError: __get_date_list() takes at least 3 non-keyword arguments (2 given) 
     192 
     193 
    183194# An Article instance doesn't have access to the "objects" attribute. 
    184195# That is only available as a class method. 
  • django/branches/magic-removal/tests/modeltests/lookup/models.py

    r1884 r1885  
    7070    ... 
    7171AssertionError: get_in_bulk() cannot be passed an empty ID list. 
     72>>> Article.objects.get_in_bulk('foo') 
     73Traceback (most recent call last): 
     74    ... 
     75AssertionError: get_in_bulk() must be provided with a list of IDs. 
     76>>> Article.objects.get_in_bulk() 
     77Traceback (most recent call last): 
     78    ... 
     79TypeError: get_in_bulk() takes at least 2 arguments (1 given) 
     80>>> Article.objects.get_in_bulk(headline__startswith='Blah') 
     81Traceback (most recent call last): 
     82    ... 
     83TypeError: get_in_bulk() takes at least 2 non-keyword arguments (1 given) 
    7284 
    7385# get_values() is just like get_list(), except it returns a list of