Django

Code

Changeset 292

Show
Ignore:
Timestamp:
07/22/05 08:02:27 (3 years ago)
Author:
adrian
Message:

Fixed #146 -- Changed order_by and ordering parameters to be less verbose. The old syntax is still supported but will not be supported by first release.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/comments/models/comments.py

    r230 r292  
    4343        'IS_PUBLIC': 'ip', 
    4444    } 
    45     ordering = (('submit_date', 'DESC'),) 
     45    ordering = ('-submit_date',) 
    4646    admin = meta.Admin( 
    4747        fields = ( 
     
    171171        meta.ForeignKey(core.Site), 
    172172    ) 
    173     ordering = (('submit_date', 'DESC'),) 
     173    ordering = ('-submit_date',) 
    174174    admin = meta.Admin( 
    175175        fields = ( 
  • django/trunk/django/contrib/comments/templatetags/comments.py

    r246 r292  
    149149            'site_id__exact': SITE_ID, 
    150150            'select_related': True, 
    151             'order_by': (('submit_date', self.ordering),), 
     151            'order_by': (self.ordering + 'submit_date',), 
    152152        } 
    153153        if not self.free and COMMENTS_BANNED_USERS_GROUP: 
     
    171171class DoCommentForm: 
    172172    """ 
    173     Displays a comment form for the given params.  
    174      
     173    Displays a comment form for the given params. 
     174 
    175175    Syntax:: 
    176      
     176 
    177177        {% comment_form for [pkg].[py_module_name] [context_var_containing_obj_id] with [list of options] %} 
    178      
     178 
    179179    Example usage:: 
    180      
     180 
    181181        {% comment_form for lcom.eventtimes event.id with is_public yes photos_optional thumbs,200,400 ratings_optional scale:1-5|first_option|second_option %} 
    182      
     182 
    183183    ``[context_var_containing_obj_id]`` can be a hard-coded integer or a variable containing the ID. 
    184184    """ 
     
    247247    Gets comment count for the given params and populates the template context 
    248248    with a variable containing that value, whose name is defined by the 'as' 
    249     clause.  
    250      
     249    clause. 
     250 
    251251    Syntax:: 
    252          
     252 
    253253        {% get_comment_count for [pkg].[py_module_name] [context_var_containing_obj_id] as [varname]  %} 
    254      
     254 
    255255    Example usage:: 
    256      
     256 
    257257        {% get_comment_count for lcom.eventtimes event.id as comment_count %} 
    258          
     258 
    259259    Note: ``[context_var_containing_obj_id]`` can also be a hard-coded integer, like this:: 
    260      
     260 
    261261        {% get_comment_count for lcom.eventtimes 23 as comment_count %} 
    262262    """ 
     
    298298    special comment_package variable, whose name is defined by the ``as`` 
    299299    clause. 
    300      
     300 
    301301    Syntax:: 
    302      
     302 
    303303        {% get_comment_list for [pkg].[py_module_name] [context_var_containing_obj_id] as [varname] (reversed) %} 
    304      
     304 
    305305    Example usage:: 
    306      
     306 
    307307        {% get_comment_list for lcom.eventtimes event.id as comment_list %} 
    308      
     308 
    309309    Note: ``[context_var_containing_obj_id]`` can also be a hard-coded integer, like this:: 
    310      
     310 
    311311        {% get_comment_list for lcom.eventtimes 23 as comment_list %} 
    312          
    313     To get a list of comments in reverse order -- that is, most recent first --  
     312 
     313    To get a list of comments in reverse order -- that is, most recent first -- 
    314314    pass ``reversed`` as the last param:: 
    315      
     315 
    316316        {% get_comment_list for lcom.eventtimes event.id as comment_list reversed %} 
    317317    """ 
     
    349349            if tokens[6] != 'reversed': 
    350350                raise template.TemplateSyntaxError, "Final argument in '%s' must be 'reversed' if given" % self.tag_name 
    351             ordering = "DESC
    352         else: 
    353             ordering = "ASC
     351            ordering = "-
     352        else: 
     353            ordering = "
    354354        return CommentListNode(package, module, var_name, obj_id, tokens[5], self.free, ordering) 
    355355 
  • django/trunk/django/core/meta.py

    r265 r292  
    5252# returns the <ul> class for a given radio_admin value 
    5353get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '') 
     54 
     55# Django currently supports two forms of ordering. 
     56# Form 1 (deprecated) example: 
     57#     order_by=(('pub_date', 'DESC'), ('headline', 'ASC'), (None, 'RANDOM')) 
     58# Form 2 (new-style) example: 
     59#     order_by=('-pub_date', 'headline', '?') 
     60# Form 1 is deprecated and will no longer be supported for Django's first 
     61# official release. The following code converts from Form 1 to Form 2. 
     62 
     63LEGACY_ORDERING_MAPPING = {'ASC': '_', 'DESC': '-_', 'RANDOM': '?'} 
     64 
     65def handle_legacy_orderlist(order_list): 
     66    if not order_list or isinstance(order_list[0], basestring): 
     67        return order_list 
     68    else: 
     69#         import warnings 
     70        new_order_list = [LEGACY_ORDERING_MAPPING[j.upper()].replace('_', str(i)) for i, j in order_list] 
     71#         warnings.warn("%r ordering syntax is deprecated. Use %r instead." % (order_list, new_order_list), DeprecationWarning) 
     72        return new_order_list 
     73 
     74def orderlist2sql(order_list, prefix=''): 
     75    output = [] 
     76    for f in handle_legacy_orderlist(order_list): 
     77        if f.startswith('-'): 
     78            output.append('%s%s DESC' % (prefix, f[1:])) 
     79        elif f == '?': 
     80            output.append('RANDOM()') 
     81        else: 
     82            output.append('%s%s ASC' % (prefix, f)) 
     83    return ', '.join(output) 
    5484 
    5585def curry(*args, **kwargs): 
     
    176206        if order_with_respect_to: 
    177207            self.order_with_respect_to = self.get_field(order_with_respect_to) 
    178             self.ordering = (('_order', 'ASC'),) 
     208            self.ordering = ('_order',) 
    179209        else: 
    180210            self.order_with_respect_to = None 
     
    232262        if not self.ordering: return '' 
    233263        pre = table_prefix and (table_prefix + '.') or '' 
    234         return 'ORDER BY ' + ','.join(['%s%s %s' % (pre, f, order) for f, order in self.ordering]
     264        return 'ORDER BY ' + orderlist2sql(self.ordering, pre
    235265 
    236266    def get_add_permission(self): 
     
    771801def method_get_next_in_order(opts, order_field, self): 
    772802    if not hasattr(self, '_next_in_order_cache'): 
    773         self._next_in_order_cache = opts.get_model_module().get_object(order_by=(('_order', 'ASC'),), 
     803        self._next_in_order_cache = opts.get_model_module().get_object(order_by=('_order',), 
    774804            where=['_order > (SELECT _order FROM %s WHERE %s=%%s)' % (opts.db_table, opts.pk.name), 
    775805                '%s=%%s' % order_field.name], limit=1, 
     
    779809def method_get_previous_in_order(opts, order_field, self): 
    780810    if not hasattr(self, '_previous_in_order_cache'): 
    781         self._previous_in_order_cache = opts.get_model_module().get_object(order_by=(('_order', 'DESC'),), 
     811        self._previous_in_order_cache = opts.get_model_module().get_object(order_by=('-_order',), 
    782812            where=['_order < (SELECT _order FROM %s WHERE %s=%%s)' % (opts.db_table, opts.pk.name), 
    783813                '%s=%%s' % order_field.name], limit=1, 
     
    909939    kwargs.setdefault('where', []).append('%s %s %%s' % (field.name, (is_next and '>' or '<'))) 
    910940    kwargs.setdefault('params', []).append(str(getattr(self, field.name))) 
    911     kwargs['order_by'] = ((field.name, (is_next and 'ASC' or 'DESC')),) 
     941    kwargs['order_by'] = [(not is_next and '-' or '') + field.name] 
    912942    kwargs['limit'] = 1 
    913943    return get_object_func(**kwargs) 
     
    12171247    # ORDER BY clause 
    12181248    order_by = [] 
    1219     for i, j in kwargs.get('order_by', opts.ordering): 
    1220         if j == "RANDOM": 
    1221             order_by.append("RANDOM()"
     1249    for f in handle_legacy_orderlist(kwargs.get('order_by', opts.ordering)): 
     1250        if f == '?': # Special case. 
     1251            order_by.append('RANDOM()'
    12221252        else: 
    1223             # Append the database table as a column prefix if it wasn't given, 
     1253            # Use the database table as a column prefix if it wasn't given, 
    12241254            # and if the requested column isn't a custom SELECT. 
    1225             if "." not in i and i not in [k[0] for k in kwargs.get('select', [])]: 
    1226                 order_by.append("%s.%s %s" % (opts.db_table, i, j)) 
     1255            if "." not in f and f not in [k[0] for k in kwargs.get('select', [])]: 
     1256                table_prefix = opts.db_table + '.' 
    12271257            else: 
    1228                 order_by.append("%s %s" % (i, j)) 
     1258                table_prefix = '' 
     1259            if f.startswith('-'): 
     1260                order_by.append('%s%s DESC' % (table_prefix, f[1:])) 
     1261            else: 
     1262                order_by.append('%s%s ASC' % (table_prefix, f)) 
    12291263    order_by = ", ".join(order_by) 
    12301264 
     
    12471281 
    12481282def function_get_latest(opts, klass, does_not_exist_exception, **kwargs): 
    1249     kwargs['order_by'] = ((opts.get_latest_by, "DESC"),) 
     1283    kwargs['order_by'] = ('-' + opts.get_latest_by,) 
    12501284    kwargs['limit'] = 1 
    12511285    return function_get_object(opts, klass, does_not_exist_exception, **kwargs) 
  • django/trunk/django/models/auth.py

    r265 r292  
    99    ) 
    1010    unique_together = (('package', 'codename'),) 
    11     ordering = (('package', 'ASC'), ('codename', 'ASC')
     11    ordering = ('package', 'codename'
    1212 
    1313    def __repr__(self): 
     
    1919        meta.ManyToManyField(Permission, blank=True, filter_interface=meta.HORIZONTAL), 
    2020    ) 
    21     ordering = (('name', 'ASC'),) 
     21    ordering = ('name',) 
    2222    admin = meta.Admin( 
    2323        search_fields = ('name',), 
     
    4545        meta.ManyToManyField(Permission, name='user_permissions', blank=True, filter_interface=meta.HORIZONTAL), 
    4646    ) 
    47     ordering = (('username', 'ASC'),) 
     47    ordering = ('username',) 
    4848    exceptions = ('SiteProfileNotAvailable',) 
    4949    admin = meta.Admin( 
     
    254254        meta.TextField('change_message', 'change message', blank=True), 
    255255    ) 
    256     ordering = (('action_time', 'DESC'),) 
     256    ordering = ('-action_time',) 
    257257    module_constants = { 
    258258        'ADDITION': 1, 
  • django/trunk/django/models/core.py

    r265 r292  
    77        meta.CharField('name', 'display name', maxlength=50), 
    88    ) 
    9     ordering = (('domain', 'ASC'),) 
     9    ordering = ('domain',) 
    1010 
    1111    def __repr__(self): 
     
    2323        meta.CharField('name', 'name', maxlength=30, unique=True), 
    2424    ) 
    25     ordering = (('name', 'ASC'),) 
     25    ordering = ('name',) 
    2626 
    2727    def __repr__(self): 
     
    3535        meta.CharField('python_module_name', 'Python module name', maxlength=50), 
    3636    ) 
    37     ordering = (('package', 'ASC'), ('name', 'ASC'),
     37    ordering = ('package', 'name'
    3838    unique_together = (('package', 'python_module_name'),) 
    3939 
     
    6464    ) 
    6565    unique_together=(('site_id', 'old_path'),) 
    66     ordering = (('old_path', 'ASC'),) 
     66    ordering = ('old_path',) 
    6767    admin = meta.Admin( 
    6868        list_display = ('__repr__',), 
     
    8888        meta.ManyToManyField(Site), 
    8989    ) 
    90     ordering = (('url', 'ASC'),) 
     90    ordering = ('url',) 
    9191    admin = meta.Admin( 
    9292        fields = ( 
  • django/trunk/django/views/admin/main.py

    r290 r292  
    9595    # order descending by ID by default. Finally, look for manually-specified 
    9696    # ordering from the query string. 
    97     if lookup_opts.admin.ordering is not None: 
    98         order_field, order_type = lookup_opts.admin.ordering 
    99     elif lookup_opts.ordering: 
    100         order_field, order_type = lookup_opts.ordering[0] 
     97    ordering = lookup_opts.admin.ordering or lookup_opts.ordering or ('-' + lookup_opts.pk.name) 
     98 
     99    # Normalize it to new-style ordering. 
     100    ordering = meta.handle_legacy_orderlist(ordering) 
     101 
     102    if ordering[0].startswith('-'): 
     103        order_field, order_type = ordering[0][1:], 'DESC' 
    101104    else: 
    102         order_field, order_type = lookup_opts.pk.name, 'DESC' 
     105        order_field, order_type = ordering[0], 'ASC' 
    103106    if params.has_key(ORDER_VAR): 
    104107        try: 
     
    10721075    mod, opts = _get_mod_opts(app_label, module_name) 
    10731076    action_list = log.get_list(object_id__exact=object_id, content_type_id__exact=opts.get_content_type_id(), 
    1074         order_by=(("action_time", "ASC"),), select_related=True) 
     1077        order_by=("action_time",), select_related=True) 
    10751078    # If no history was found, see whether this object even exists. 
    10761079    try: 
  • django/trunk/django/views/generic/date_based.py

    r268 r292  
    2828        lookup_kwargs.update({ 
    2929            'limit': num_latest, 
    30             'order_by': ((date_field, 'DESC'),), 
     30            'order_by': ('-' + date_field,), 
    3131        }) 
    3232        latest = mod.get_list(**lookup_kwargs)