~ubuntuone-pqm-team/django/stable

« back to all changes in this revision

Viewing changes to django/contrib/admin/util.py

  • Committer: Natalia
  • Date: 2014-12-05 15:21:13 UTC
  • Revision ID: natalia.bidart@ubuntu.com-20141205152113-cchtmygjia45gb87
Tags: 1.6.8
- Imported Django 1.6.8 from released tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import datetime
4
4
import decimal
5
5
 
 
6
from django.contrib.auth import get_permission_codename
6
7
from django.db import models
7
8
from django.db.models.constants import LOOKUP_SEP
8
9
from django.db.models.deletion import Collector
37
38
    # if key ends with __in, split parameter into separate values
38
39
    if key.endswith('__in'):
39
40
        value = value.split(',')
40
 
    # if key ends with __isnull, special case '' and false
 
41
    # if key ends with __isnull, special case '' and the string literals 'false' and '0'
41
42
    if key.endswith('__isnull'):
42
 
        if value.lower() in ('', 'false'):
 
43
        if value.lower() in ('', 'false', '0'):
43
44
            value = False
44
45
        else:
45
46
            value = True
88
89
    field_names = []
89
90
    for name, opts in fieldsets:
90
91
        for field in opts['fields']:
91
 
            # type checking feels dirty, but it seems like the best way here
92
 
            if type(field) == tuple:
 
92
            if isinstance(field, (list, tuple)):
93
93
                field_names.extend(field)
94
94
            else:
95
95
                field_names.append(field)
117
117
            admin_url = reverse('%s:%s_%s_change'
118
118
                                % (admin_site.name,
119
119
                                   opts.app_label,
120
 
                                   opts.object_name.lower()),
 
120
                                   opts.model_name),
121
121
                                None, (quote(obj._get_pk_val()),))
122
122
            p = '%s.%s' % (opts.app_label,
123
 
                           opts.get_delete_permission())
 
123
                           get_permission_codename('delete', opts))
124
124
            if not user.has_perm(p):
125
125
                perms_needed.add(opts.verbose_name)
126
126
            # Display a link to the admin page.
267
267
 
268
268
def label_for_field(name, model, model_admin=None, return_attr=False):
269
269
    """
270
 
    Returns a sensible label for a field name. The name can be a callable or the
271
 
    name of an object attributes, as well as a genuine fields. If return_attr is
 
270
    Returns a sensible label for a field name. The name can be a callable,
 
271
    property (but not created with @property decorator) or the name of an
 
272
    object's attribute, as well as a genuine fields. If return_attr is
272
273
    True, the resolved attribute (which could be a callable) is also returned.
273
274
    This will be None if (and only if) the name refers to a field.
274
275
    """
301
302
 
302
303
            if hasattr(attr, "short_description"):
303
304
                label = attr.short_description
 
305
            elif (isinstance(attr, property) and
 
306
                  hasattr(attr, "fget") and
 
307
                  hasattr(attr.fget, "short_description")):
 
308
                label = attr.fget.short_description
304
309
            elif callable(attr):
305
310
                if attr.__name__ == "<lambda>":
306
311
                    label = "--"
313
318
    else:
314
319
        return label
315
320
 
 
321
 
316
322
def help_text_for_field(name, model):
 
323
    help_text = ""
317
324
    try:
318
 
        help_text = model._meta.get_field_by_name(name)[0].help_text
 
325
        field_data = model._meta.get_field_by_name(name)
319
326
    except models.FieldDoesNotExist:
320
 
        help_text = ""
 
327
        pass
 
328
    else:
 
329
        field = field_data[0]
 
330
        if not isinstance(field, RelatedObject):
 
331
            help_text = field.help_text
321
332
    return smart_text(help_text)
322
333
 
323
334
 
368
379
 
369
380
 
370
381
def get_model_from_relation(field):
371
 
    if isinstance(field, models.related.RelatedObject):
 
382
    if hasattr(field, 'get_path_info'):
 
383
        return field.get_path_info()[-1].to_opts.model
 
384
    elif isinstance(field, models.related.RelatedObject):
372
385
        return field.model
373
386
    elif getattr(field, 'rel'): # or isinstance?
374
387
        return field.rel.to