Exemplo n.º 1
0
 def init(self):
     self.label = label_for_field(self.field_name,
                                  self.obj.__class__,
                                  model_admin=self.admin_view,
                                  return_attr=False)
     try:
         f, attr, value = lookup_field(self.field_name, self.obj,
                                       self.admin_view)
     except (AttributeError, ObjectDoesNotExist):
         self.text
     else:
         if f is None:
             self.allow_tags = getattr(attr, 'allow_tags', False)
             boolean = getattr(attr, 'boolean', False)
             if boolean:
                 self.allow_tags = True
                 self.text = boolean_icon(value)
             else:
                 self.text = smart_unicode(value)
         else:
             if isinstance(f.rel, models.ManyToOneRel):
                 self.text = getattr(self.obj, f.name)
             else:
                 self.text = display_for_field(value, f)
         self.field = f
         self.attr = attr
         self.value = value
Exemplo n.º 2
0
 def init(self):
     self.label = label_for_field(self.field_name, self.obj.__class__,
                                  model_admin=self.admin_view,
                                  return_attr=False
                                  )
     try:
         f, attr, value = lookup_field(
             self.field_name, self.obj, self.admin_view)
     except (AttributeError, ObjectDoesNotExist):
         self.text
     else:
         if f is None:
             self.allow_tags = getattr(attr, 'allow_tags', False)
             boolean = getattr(attr, 'boolean', False)
             if boolean:
                 self.allow_tags = True
                 self.text = boolean_icon(value)
             else:
                 self.text = smart_unicode(value)
         else:
             if isinstance(f.rel, models.ManyToOneRel):
                 self.text = getattr(self.obj, f.name)
             else:
                 self.text = display_for_field(value, f)
         self.field = f
         self.attr = attr
         self.value = value
Exemplo n.º 3
0
 def _get_new_field_html(self, field_name):
     try:
         f, attr, value = lookup_field(field_name, self.org_obj, self)
     except (AttributeError, ObjectDoesNotExist):
         return EMPTY_CHANGELIST_VALUE
     else:
         allow_tags = False
         if f is None:
             allow_tags = getattr(attr, 'allow_tags', False)
             boolean = getattr(attr, 'boolean', False)
             if boolean:
                 allow_tags = True
                 text = boolean_icon(value)
             else:
                 text = smart_unicode(value)
         else:
             if isinstance(f.rel, models.ManyToOneRel):
                 field_val = getattr(self.org_obj, f.name)
                 if field_val is None:
                     text = EMPTY_CHANGELIST_VALUE
                 else:
                     text = field_val
             else:
                 text = display_for_field(value, f)
         return mark_safe(text) if allow_tags else conditional_escape(text)
Exemplo n.º 4
0
 def choices(self):
     self.lookup_in_val = (type(self.lookup_in_val)
                           in (tuple, list)) and self.lookup_in_val or list(
                               self.lookup_in_val)
     yield {
         'selected': len(self.lookup_in_val) == 0,
         'query_string': self.query_string({}, [self.lookup_in_name]),
         'display': _('All'),
     }
     for val in self.lookup_choices:
         yield {
             'selected':
             smart_unicode(val) in self.lookup_in_val,
             'query_string':
             self.query_string({
                 self.lookup_in_name:
                 ",".join([val] + self.lookup_in_val),
             }),
             'remove_query_string':
             self.query_string({
                 self.lookup_in_name:
                 ",".join([v for v in self.lookup_in_val if v != val]),
             }),
             'display':
             val,
         }
Exemplo n.º 5
0
 def _get_new_field_html(self, field_name):
     try:
         f, attr, value = lookup_field(field_name, self.org_obj, self)
     except (AttributeError, ObjectDoesNotExist):
         return EMPTY_CHANGELIST_VALUE
     else:
         allow_tags = False
         if f is None:
             allow_tags = getattr(attr, 'allow_tags', False)
             boolean = getattr(attr, 'boolean', False)
             if boolean:
                 allow_tags = True
                 text = boolean_icon(value)
             else:
                 text = smart_unicode(value)
         else:
             if isinstance(f.rel, models.ManyToOneRel):
                 field_val = getattr(self.org_obj, f.name)
                 if field_val is None:
                     text = EMPTY_CHANGELIST_VALUE
                 else:
                     text = field_val
             else:
                 text = display_for_field(value, f)
         return mark_safe(text) if allow_tags else conditional_escape(text)
Exemplo n.º 6
0
 def choices(self):
     yield {
         'selected': (self.lookup_exact_val is ''
                      and self.lookup_isnull_val is ''),
         'query_string':
         self.query_string(
             {}, [self.lookup_exact_name, self.lookup_isnull_name]),
         'display':
         _('All'),
     }
     include_none = False
     for val in self.lookup_choices:
         if val is None:
             include_none = True
             continue
         val = smart_unicode(val)
         yield {
             'selected':
             self.lookup_exact_val == val,
             'query_string':
             self.query_string({self.lookup_exact_name: val},
                               [self.lookup_isnull_name]),
             'display':
             val,
         }
     if include_none:
         yield {
             'selected':
             bool(self.lookup_isnull_val),
             'query_string':
             self.query_string({self.lookup_isnull_name: 'True'},
                               [self.lookup_exact_name]),
             'display':
             EMPTY_CHANGELIST_VALUE,
         }
Exemplo n.º 7
0
 def choices(self):
     yield {
         'selected': self.lookup_exact_val == '' and not self.lookup_isnull_val,
         'query_string': self.query_string({},
                                           [self.lookup_exact_name, self.lookup_isnull_name]),
         'display': _('All'),
     }
     for pk_val, val in self.lookup_choices:
         yield {
             'selected': self.lookup_exact_val == smart_unicode(pk_val),
             'query_string': self.query_string({
                 self.lookup_exact_name: pk_val,
             }, [self.lookup_isnull_name]),
             'display': val,
         }
     if (isinstance(self.field, ForeignObjectRel)
             and self.field.field.null or hasattr(self.field, 'rel')
             and self.field.null):
         yield {
             'selected': bool(self.lookup_isnull_val),
             'query_string': self.query_string({
                 self.lookup_isnull_name: 'True',
             }, [self.lookup_exact_name]),
             'display': EMPTY_CHANGELIST_VALUE,
         }
Exemplo n.º 8
0
 def choices(self):
     yield {
         'selected':
         self.lookup_exact_val == '' and not self.lookup_isnull_val,
         'query_string':
         self.query_string(
             {}, [self.lookup_exact_name, self.lookup_isnull_name]),
         'display':
         _('All'),
     }
     for pk_val, val in self.lookup_choices:
         yield {
             'selected':
             self.lookup_exact_val == smart_unicode(pk_val),
             'query_string':
             self.query_string({
                 self.lookup_exact_name: pk_val,
             }, [self.lookup_isnull_name]),
             'display':
             val,
         }
     if (isinstance(self.field, ForeignObjectRel) and self.field.field.null
             or hasattr(self.field, 'rel') and self.field.null):
         yield {
             'selected':
             bool(self.lookup_isnull_val),
             'query_string':
             self.query_string({
                 self.lookup_isnull_name: 'True',
             }, [self.lookup_exact_name]),
             'display':
             EMPTY_CHANGELIST_VALUE,
         }
Exemplo n.º 9
0
 def choices(self):
     yield {
         'selected': (self.lookup_exact_val is '' and self.lookup_isnull_val is ''),
         'query_string': self.query_string({}, [self.lookup_exact_name, self.lookup_isnull_name]),
         'display': _('All'),
     }
     include_none = False
     for val in self.lookup_choices:
         if val is None:
             include_none = True
             continue
         val = smart_unicode(val)
         yield {
             'selected': self.lookup_exact_val == val,
             'query_string': self.query_string({self.lookup_exact_name: val},
                                               [self.lookup_isnull_name]),
             'display': val,
         }
     if include_none:
         yield {
             'selected': bool(self.lookup_isnull_val),
             'query_string': self.query_string({self.lookup_isnull_name: 'True'},
                                               [self.lookup_exact_name]),
             'display': EMPTY_CHANGELIST_VALUE,
         }
Exemplo n.º 10
0
    def result_item(self, obj, field_name, row):
        """
        Generates the actual list of data.
        """
        item = ResultItem(field_name, row)
        try:
            f, attr, value = lookup_field(field_name, obj, self)
        except (AttributeError, ObjectDoesNotExist):
            item.text = mark_safe("<span class='text-muted'>%s</span>" % EMPTY_CHANGELIST_VALUE)
        else:
            if f is None:
                item.allow_tags = getattr(attr, 'allow_tags', False)
                boolean = getattr(attr, 'boolean', False)
                if boolean:
                    item.allow_tags = True
                    item.text = boolean_icon(value)
                else:
                    item.text = smart_unicode(value)
            else:
                if isinstance(f.rel, models.ManyToOneRel):
                    field_val = getattr(obj, f.name)
                    if field_val is None:
                        item.text = mark_safe("<span class='text-muted'>%s</span>" % EMPTY_CHANGELIST_VALUE)
                    else:
                        item.text = field_val
                else:
                    item.text = display_for_field(value, f)
                if isinstance(f, models.DateField)\
                    or isinstance(f, models.TimeField)\
                        or isinstance(f, models.ForeignKey):
                    item.classes.append('nowrap')

            item.field = f
            item.attr = attr
            item.value = value

        # If list_display_links not defined, add the link tag to the first field
        if (item.row['is_display_first'] and not self.list_display_links) \
                or field_name in self.list_display_links:
            item.row['is_display_first'] = False
            item.is_display_link = True
            if self.list_display_links_details:
                item_res_uri = self.model_admin_url("detail", getattr(obj, self.pk_attname))
                if item_res_uri:
                    if self.has_change_permission(obj):
                        edit_url = self.model_admin_url("change", getattr(obj, self.pk_attname))
                    else:
                        edit_url = ""
                    item.wraps.append('<a data-res-uri="%s" data-edit-uri="%s" class="details-handler" rel="tooltip" title="%s">%%s</a>'
                                     % (item_res_uri, edit_url, _(u'Details of %s') % str(obj)))
            else:
                url = self.url_for_result(obj)
                item.wraps.append(u'<a href="%s">%%s</a>' % url)

        return item
Exemplo n.º 11
0
 def choices(self):
     yield {
         'selected': self.lookup_exact_val is '',
         'query_string': self.query_string({}, [self.lookup_exact_name]),
         'display': _('All')
     }
     for lookup, title in self.field.flatchoices:
         yield {
             'selected': smart_unicode(lookup) == self.lookup_exact_val,
             'query_string': self.query_string({self.lookup_exact_name: lookup}),
             'display': title,
         }
Exemplo n.º 12
0
 def choices(self):
     yield {
         "selected": self.lookup_exact_val is "",
         "query_string": self.query_string({}, [self.lookup_exact_name]),
         "display": _("All"),
     }
     for lookup, title in self.field.flatchoices:
         yield {
             "selected": smart_unicode(lookup) == self.lookup_exact_val,
             "query_string": self.query_string({self.lookup_exact_name: lookup}),
             "display": title,
         }
Exemplo n.º 13
0
 def choices(self):
     yield {
         'selected': self.lookup_exact_val is '',
         'query_string': self.query_string({}, [self.lookup_exact_name]),
         'display': _('All')
     }
     for lookup, title in self.field.flatchoices:
         yield {
             'selected': smart_unicode(lookup) == self.lookup_exact_val,
             'query_string':
             self.query_string({self.lookup_exact_name: lookup}),
             'display': title,
         }
Exemplo n.º 14
0
 def choices(self):
     self.lookup_in_val = (type(self.lookup_in_val) in (tuple,list)) and self.lookup_in_val or list(self.lookup_in_val)
     yield {
         'selected': len(self.lookup_in_val) == 0,
         'query_string': self.query_string({},[self.lookup_in_name]),
         'display': _('All'),
     }
     for val in self.lookup_choices:
         yield {
             'selected': smart_unicode(val) in self.lookup_in_val,
             'query_string': self.query_string({self.lookup_in_name: ",".join([val]+self.lookup_in_val),}),
             'remove_query_string': self.query_string({self.lookup_in_name: ",".join([v for v in self.lookup_in_val if v != val]),}),
             'display': val,
         }
Exemplo n.º 15
0
 def _to_xml(self, xml, data):
     if isinstance(data, (list, tuple)):
         for item in data:
             xml.startElement("row", {})
             self._to_xml(xml, item)
             xml.endElement("row")
     elif isinstance(data, dict):
         for key, value in six.iteritems(data):
             key = key.replace(' ', '_')
             xml.startElement(key, {})
             self._to_xml(xml, value)
             xml.endElement(key)
     else:
         xml.characters(smart_unicode(data))
Exemplo n.º 16
0
 def _to_xml(self, xml, data):
     if isinstance(data, (list, tuple)):
         for item in data:
             xml.startElement("row", {})
             self._to_xml(xml, item)
             xml.endElement("row")
     elif isinstance(data, dict):
         for key, value in six.iteritems(data):
             key = key.replace(' ', '_')
             xml.startElement(key, {})
             self._to_xml(xml, value)
             xml.endElement(key)
     else:
         xml.characters(smart_unicode(data))
Exemplo n.º 17
0
 def default(self, o):
     if isinstance(o, datetime.date):
         return o.strftime('%Y-%m-%d')
     elif isinstance(o, datetime.datetime):
         return o.strftime('%Y-%m-%d %H:%M:%S')
     elif isinstance(o, decimal.Decimal):
         return str(o)
     elif isinstance(o, ModelBase):
         return '%s.%s' % (o._meta.app_label, o._meta.model_name)
     else:
         try:
             return super(JSONEncoder, self).default(o)
         except Exception:
             return smart_unicode(o)
Exemplo n.º 18
0
 def choices(self):
     yield {
         "selected": self.lookup_exact_val == "" and not self.lookup_isnull_val,
         "query_string": self.query_string({}, [self.lookup_exact_name, self.lookup_isnull_name]),
         "display": _("All"),
     }
     for pk_val, val in self.lookup_choices:
         yield {
             "selected": self.lookup_exact_val == smart_unicode(pk_val),
             "query_string": self.query_string({self.lookup_exact_name: pk_val}, [self.lookup_isnull_name]),
             "display": val,
         }
     if is_related_field(self.field) and self.field.field.null or hasattr(self.field, "rel") and self.field.null:
         yield {
             "selected": bool(self.lookup_isnull_val),
             "query_string": self.query_string({self.lookup_isnull_name: "True"}, [self.lookup_exact_name]),
             "display": EMPTY_CHANGELIST_VALUE,
         }
Exemplo n.º 19
0
 def choices(self):
     self.lookup_in_val = (
         (type(self.lookup_in_val) in (tuple, list)) and self.lookup_in_val or list(self.lookup_in_val)
     )
     yield {
         "selected": len(self.lookup_in_val) == 0,
         "query_string": self.query_string({}, [self.lookup_in_name]),
         "display": _("All"),
     }
     for val in self.lookup_choices:
         yield {
             "selected": smart_unicode(val) in self.lookup_in_val,
             "query_string": self.query_string({self.lookup_in_name: ",".join([val] + self.lookup_in_val)}),
             "remove_query_string": self.query_string(
                 {self.lookup_in_name: ",".join([v for v in self.lookup_in_val if v != val])}
             ),
             "display": val,
         }
Exemplo n.º 20
0
 def choices(self):
     yield {
         "selected": (self.lookup_exact_val is "" and self.lookup_isnull_val is ""),
         "query_string": self.query_string({}, [self.lookup_exact_name, self.lookup_isnull_name]),
         "display": _("All"),
     }
     include_none = False
     for val in self.lookup_choices:
         if val is None:
             include_none = True
             continue
         val = smart_unicode(val)
         yield {
             "selected": self.lookup_exact_val == val,
             "query_string": self.query_string({self.lookup_exact_name: val}, [self.lookup_isnull_name]),
             "display": val,
         }
     if include_none:
         yield {
             "selected": bool(self.lookup_isnull_val),
             "query_string": self.query_string({self.lookup_isnull_name: "True"}, [self.lookup_exact_name]),
             "display": EMPTY_CHANGELIST_VALUE,
         }
Exemplo n.º 21
0
 def valid_value(self, value):
     value = self.prepare_value(value)
     for k, v in self.choices:
         if value == smart_unicode(k):
             return True
     return False
Exemplo n.º 22
0
 def valid_value(self, value):
     value = self.prepare_value(value)
     for k, v in self.choices:
         if value == smart_unicode(k):
             return True
     return False