def choices(self, cl):
        yield {'selected': not len(self.lookup_val),
               'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
               'display': _('All')}

        for val in self.lookup_choices:
            pk_val = [smart_unicode(getattr(val, f.attname)) for f in self.field.rel.to._meta.pks]
            # TODO: fix for composites
            yield {'selected': self.lookup_val == pk_val,
                   'query_string': cl.get_query_string({self.lookup_kwarg: PRIMARY_KEY_URL_SEPARATOR.join(pk_val)}),
                   'display': val}
Example #2
0
    def choices(self, cl):
        yield {
            'selected': not len(self.lookup_val),
            'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
            'display': _('All')
        }

        for val in self.lookup_choices:
            pk_val = [
                smart_unicode(getattr(val, f.attname))
                for f in self.field.rel.to._meta.pks
            ]
            # TODO: fix for composites
            yield {
                'selected':
                self.lookup_val == pk_val,
                'query_string':
                cl.get_query_string({
                    self.lookup_kwarg:
                    PRIMARY_KEY_URL_SEPARATOR.join(pk_val)
                }),
                'display':
                val
            }
Example #3
0
 def url(self):
     return mark_safe(
         '%s%s/%s/objects/%s/' %
         (self.model.site.root_url, self.model.model._meta.app_label,
          self.model.model._meta.module_name,
          PRIMARY_KEY_URL_SEPARATOR.join(iri_to_uri(self.pk()))))
Example #4
0
 def url_for_result(self, result):
     return "%s/" % PRIMARY_KEY_URL_SEPARATOR.join([quote(force_unicode(getattr(result, pk))) for pk in self.pk.attnames])
Example #5
0
 def url_for_result(self, result):
     return "%s/" % PRIMARY_KEY_URL_SEPARATOR.join([
         quote(force_unicode(getattr(result, pk)))
         for pk in self.pk.attnames
     ])
Example #6
0
def items_for_result(cl, result):
    first = True
    for field_name in cl.list_display:
        row_class = ''
        try:
            f = cl.lookup_opts.get_field(field_name)
        except models.FieldDoesNotExist:
            # For non-field list_display values, the value is either a method,
            # property or returned via a callable.
            try:
                if callable(field_name):
                    attr = field_name
                    value = attr(result)
                elif hasattr(cl.model_admin, field_name) and \
                   not field_name == '__str__' and not field_name == '__unicode__':
                    attr = getattr(cl.model_admin, field_name)
                    value = attr(result)
                else:
                    attr = getattr(result, field_name)
                    if callable(attr):
                        value = attr()
                    else:
                        value = attr
                allow_tags = getattr(attr, 'allow_tags', False)
                boolean = getattr(attr, 'boolean', False)
                if boolean:
                    allow_tags = True
                    result_repr = _boolean_icon(value)
                else:
                    result_repr = smart_unicode(value)
            except (AttributeError, ObjectDoesNotExist):
                result_repr = EMPTY_CHANGELIST_VALUE
            else:
                # Strip HTML tags in the resulting text, except if the
                # function has an "allow_tags" attribute set to True.
                if not allow_tags:
                    result_repr = escape(result_repr)
                else:
                    result_repr = mark_safe(result_repr)
        else:
            field_val = getattr(result, f.attname)

            if isinstance(f.rel, models.ManyToOneRel):
                if field_val is not None:
                    result_repr = escape(getattr(result, f.name))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Dates and times are special: They're formatted in a certain way.
            elif isinstance(f, models.DateField) or isinstance(
                    f, models.TimeField):
                if field_val:
                    (date_format, datetime_format,
                     time_format) = get_date_formats()
                    if isinstance(f, models.DateTimeField):
                        result_repr = capfirst(
                            dateformat.format(field_val, datetime_format))
                    elif isinstance(f, models.TimeField):
                        result_repr = capfirst(
                            dateformat.time_format(field_val, time_format))
                    else:
                        result_repr = capfirst(
                            dateformat.format(field_val, date_format))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
                row_class = ' class="nowrap"'
            # Booleans are special: We use images.
            elif isinstance(f, models.BooleanField) or isinstance(
                    f, models.NullBooleanField):
                result_repr = _boolean_icon(field_val)
            # DecimalFields are special: Zero-pad the decimals.
            elif isinstance(f, models.DecimalField):
                if field_val is not None:
                    result_repr = ('%%.%sf' % f.decimal_places) % field_val
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Fields with choices are special: Use the representation
            # of the choice.
            elif f.choices:
                result_repr = dict(f.choices).get(field_val,
                                                  EMPTY_CHANGELIST_VALUE)
            else:
                result_repr = escape(field_val)
        if force_unicode(result_repr) == '':
            result_repr = mark_safe(' ')
        # If list_display_links not defined, add the link tag to the first field
        if (first and not cl.list_display_links
            ) or field_name in cl.list_display_links:
            table_tag = {True: 'th', False: 'td'}[first]
            first = False
            url = cl.url_for_result(result)
            # Convert the pk to something that can be used in Javascript.
            # Problem cases are long ints (23L) and non-ASCII strings.

            if cl.to_field:
                attr = tuple(cl.to_field)
            else:
                attr = cl.pk.attnames

            result_id = repr(
                PRIMARY_KEY_URL_SEPARATOR.join(
                    [force_unicode(getattr(result, pk)) for pk in attr]))[1:]
            yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \
                (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))
        else:
            yield mark_safe(u'<td%s>%s</td>' %
                            (row_class, conditional_escape(result_repr)))
def items_for_result(cl, result):
    first = True
    for field_name in cl.list_display:
        row_class = ""
        try:
            f = cl.lookup_opts.get_field(field_name)
        except models.FieldDoesNotExist:
            # For non-field list_display values, the value is either a method,
            # property or returned via a callable.
            try:
                if callable(field_name):
                    attr = field_name
                    value = attr(result)
                elif (
                    hasattr(cl.model_admin, field_name)
                    and not field_name == "__str__"
                    and not field_name == "__unicode__"
                ):
                    attr = getattr(cl.model_admin, field_name)
                    value = attr(result)
                else:
                    attr = getattr(result, field_name)
                    if callable(attr):
                        value = attr()
                    else:
                        value = attr
                allow_tags = getattr(attr, "allow_tags", False)
                boolean = getattr(attr, "boolean", False)
                if boolean:
                    allow_tags = True
                    result_repr = _boolean_icon(value)
                else:
                    result_repr = smart_unicode(value)
            except (AttributeError, ObjectDoesNotExist):
                result_repr = EMPTY_CHANGELIST_VALUE
            else:
                # Strip HTML tags in the resulting text, except if the
                # function has an "allow_tags" attribute set to True.
                if not allow_tags:
                    result_repr = escape(result_repr)
                else:
                    result_repr = mark_safe(result_repr)
        else:
            field_val = getattr(result, f.attname)

            if isinstance(f.rel, models.ManyToOneRel):
                if field_val is not None:
                    result_repr = escape(getattr(result, f.name))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Dates and times are special: They're formatted in a certain way.
            elif isinstance(f, models.DateField) or isinstance(f, models.TimeField):
                if field_val:
                    (date_format, datetime_format, time_format) = get_date_formats()
                    if isinstance(f, models.DateTimeField):
                        result_repr = capfirst(dateformat.format(field_val, datetime_format))
                    elif isinstance(f, models.TimeField):
                        result_repr = capfirst(dateformat.time_format(field_val, time_format))
                    else:
                        result_repr = capfirst(dateformat.format(field_val, date_format))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
                row_class = ' class="nowrap"'
            # Booleans are special: We use images.
            elif isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField):
                result_repr = _boolean_icon(field_val)
            # DecimalFields are special: Zero-pad the decimals.
            elif isinstance(f, models.DecimalField):
                if field_val is not None:
                    result_repr = ("%%.%sf" % f.decimal_places) % field_val
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Fields with choices are special: Use the representation
            # of the choice.
            elif f.choices:
                result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE)
            else:
                result_repr = escape(field_val)
        if force_unicode(result_repr) == "":
            result_repr = mark_safe("&nbsp;")
        # If list_display_links not defined, add the link tag to the first field
        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
            table_tag = {True: "th", False: "td"}[first]
            first = False
            url = cl.url_for_result(result)
            # Convert the pk to something that can be used in Javascript.
            # Problem cases are long ints (23L) and non-ASCII strings.

            if cl.to_field:
                attr = tuple(cl.to_field)
            else:
                attr = cl.pk.attnames

            result_id = repr(PRIMARY_KEY_URL_SEPARATOR.join([force_unicode(getattr(result, pk)) for pk in attr]))[1:]
            yield mark_safe(
                u'<%s%s><a href="%s"%s>%s</a></%s>'
                % (
                    table_tag,
                    row_class,
                    url,
                    (
                        cl.is_popup
                        and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id
                        or ""
                    ),
                    conditional_escape(result_repr),
                    table_tag,
                )
            )
        else:
            yield mark_safe(u"<td%s>%s</td>" % (row_class, conditional_escape(result_repr)))
 def url(self):
     return mark_safe('%s%s/%s/objects/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, PRIMARY_KEY_URL_SEPARATOR.join(iri_to_uri(self.pk()))))