def render(self, record, value): storage = getattr(value, 'storage', None) exists = None url = None if storage: # we'll assume value is a `django.db.models.fields.files.FieldFile` if self.verify_exists: exists = storage.exists(value.name) url = storage.url(value.name) else: if self.verify_exists and hasattr(value, 'name'): # ignore negatives, perhaps the file has a name but it doesn't # represent a local path... better to stay neutral than give a # false negative. exists = os.path.exists(value.name) or exists tag = 'a' if url else 'span' attrs = AttributeDict(self.attrs.get(tag, {})) attrs['title'] = value.name classes = [c for c in attrs.get('class', '').split(' ') if c] if exists is not None: classes.append('exists' if exists else 'missing') attrs['class'] = ' '.join(classes) if url: return self.render_link(url, record=record, value=value, attrs=attrs) else: return format_html( '<span {attrs}>{text}</span>', attrs=attrs.as_html(), text=self.text_value(record, value) )
def render_attrs(attrs, **kwargs): ret = AttributeDict(kwargs) if attrs is not None: ret.update(attrs) return ret.as_html()
def render(self, record, value): attrs = AttributeDict(self.attrs.get("span", {})) classes = [c for c in attrs.get("class", "").split(" ") if c] exists = None storage = getattr(value, "storage", None) if storage: # we'll assume value is a `django.db.models.fields.files.FieldFile` if self.verify_exists: exists = storage.exists(value.name) else: if self.verify_exists and hasattr(value, "name"): # ignore negatives, perhaps the file has a name but it doesn't # represent a local path... better to stay neutral than give a # false negative. exists = os.path.exists(value.name) or exists if exists is not None: classes.append("exists" if exists else "missing") attrs["title"] = value.name attrs["class"] = " ".join(classes) return format_html( "<span {attrs}>{text}</span>", attrs=attrs.as_html(), text=self.text_value(record, value), )
def render(self, value, record, bound_column): if not self.links: raise NotImplementedError('Links not assigned.') if not isinstance(self.links, (list, tuple,dict)): raise NotImplementedError('Links must be an iterable.') links = [] for link in self.links: title = link['title'] url = link['url'] attrs = link['attrs'] if 'attrs' in link else None if 'args' in link: args = [a.resolve(record) if isinstance(a, A) else a for a in link['args']] else: args = None attrs = AttributeDict(attrs if attrs is not None else self.attrs.get('a', {})) try: attrs['href'] = urlresolvers.reverse(url, args=args) except urlresolvers.NoReverseMatch: attrs['href'] = url links.append('<a {attrs}>{text}</a>'.format( attrs=attrs.as_html(), text=mark_safe(title) )) return mark_safe(self.delimiter.join(links))
def render(self, value, record, bound_column): if not self.links: raise NotImplementedError('Links not assigned.') if not isinstance(self.links, (list, tuple, dict)): raise NotImplementedError('Links must be an iterable.') links = [] for link in self.links: title = link['title'] url = link['url'] attrs = link['attrs'] if 'attrs' in link else None if 'args' in link: args = [ a.resolve(record) if isinstance(a, A) else a for a in link['args'] ] else: args = None attrs = AttributeDict( attrs if attrs is not None else self.attrs.get('a', {})) try: attrs['href'] = urlresolvers.reverse(url, args=args) except urlresolvers.NoReverseMatch: attrs['href'] = url links.append('<a {attrs}>{text}</a>'.format(attrs=attrs.as_html(), text=mark_safe(title))) return mark_safe(self.delimiter.join(links))
def header(self): default = {"type": "checkbox", "class": "toggle-all", } general = self.attrs.get('input') specific = self.attrs.get('th__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<input %s /> ' % attrs.as_html())
def header(self): """Render the header cell.""" default = {"type": "checkbox"} general = self.attrs.get("input") specific = self.attrs.get("th__input") attrs = AttributeDict(default, **(specific or general or {})) return mark_safe("<label><input %s/><span></span></label>" % attrs.as_html()) # noqa
def render_link(self, uri, record, value, attrs=None): super(OpenColumn, self).render_link(uri, record, value, attrs) attrs = AttributeDict( attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = uri html = '<a {attrs}><span class="fa fa-{icon}"></span> {text}</a>'.format( attrs=attrs.as_html(), icon=self.icon, text='Open') return mark_safe(html)
def header(self): if self.header_check: default = {'type': 'checkbox'} general = self.attrs.get('input') specific = self.attrs.get('th__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<input %s/>' % attrs.as_html()) else: return super(CheckBoxColumn, self).header
def render(self, value, bound_column, record): default = {"type": "checkbox", "name": bound_column.name, "value": value} if self.is_checked(value, record): default.update({"checked": "checked"}) general = self.attrs.get("input") specific = self.attrs.get("td__input") attrs = AttributeDict(default, **(specific or general or {})) return mark_safe("<input %s/>" % attrs.as_html())
def render(self, value, bound_column): # pylint: disable=W0221 default = { 'type': 'text', 'name': bound_column.name, } general = self.attrs.get('input') specific = self.attrs.get('td__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<input %s/>' % attrs.as_html())
def render_link(self, uri, text, attrs=None): attrs = AttributeDict(attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = uri html = '<a {attrs}>{text}</a>'.format( attrs=attrs.as_html(), text=text.encode('utf-8') ) return mark_safe(html)
def render(self, value, bound_column): # pylint: disable=W0221 default = { 'type': 'checkbox', 'name': bound_column.name, 'value': value } general = self.attrs.get('input') specific = self.attrs.get('td__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<label><input %s/><span class="lbl"></span></label>' % attrs.as_html())
def render_link(self, uri, value, attrs=None): attrs = AttributeDict(attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = uri return format_html( '<a {attrs}>{text}</a>', attrs=attrs.as_html(), text=value, )
def render(self, value, bound_column): default = { 'type': 'checkbox', 'name': bound_column.name, 'value': value } general = self.attrs.get('input') specific = self.attrs.get('td__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<input %s/>' % attrs.as_html())
def render(self, value, record, bound_column): try: url = value.get_absolute_url() except (AttributeError, NoReverseMatch): return force_str(value) attrs = AttributeDict((("href", url),)) return format_html( "<a {attrs}>{text}</a>", attrs=attrs.as_html(), text=value, )
def header(self): default = { 'type': 'checkbox', 'class': "tableflat", 'id': "checkboxall", 'name': 'for_action', } general = self.attrs.get('input') specific = self.attrs.get('th__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<input %s/>' % attrs.as_html())
def render(self, value, bound_column, record): default = {'type': 'radio', 'name': bound_column.name, 'value': value} if self.is_checked(value, record): default.update({ 'checked': 'checked', }) general = self.attrs.get('input') specific = self.attrs.get('td__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<input %s/>' % attrs.as_html())
def render(self, value, record, bound_column): if self.action: url = crud_url(record, self.action) else: url = record.get_absolute_url() attrs = AttributeDict((("href", url),)) return format_html( "<a {attrs}>{text}</a>", attrs=attrs.as_html(), text=self.action_name, )
def render_link(self, uri, text, attrs=None): """ Render a hyperlink. :param uri: URI for the hyperlink :param text: value wrapped in ``<a></a>`` :param attrs: ``<a>`` tag attributes """ attrs = AttributeDict(attrs if attrs is not None else self.attrs.get("a", {})) attrs["href"] = uri html = "<a {attrs}>{text}</a>".format(attrs=attrs.as_html(), text=escape(text)) return mark_safe(html)
def render(self, value, bound_column, record): default = { "type": "checkbox", "name": bound_column.name, "value": value } if self.is_checked(value, record): default.update({"checked": "checked"}) general = self.attrs.get("input") specific = self.attrs.get("td__input") attrs = AttributeDict(default, **(specific or general or {})) return mark_safe("<input %s/>" % attrs.as_html())
def render_link(self, uri, text, attrs=None): """ Render a hyperlink. :param uri: URI for the hyperlink :param text: value wrapped in ``<a></a>`` :param attrs: ``<a>`` tag attributes """ attrs = AttributeDict( attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = uri html = '<a {attrs}>{text}</a>'.format(attrs=attrs.as_html(), text=escape(text)) return mark_safe(html)
def render(self, value, bound_column, record): # pylint: disable=W0221 default = { 'type': 'checkbox', 'name': bound_column.name, 'value': value } if self.is_checked(value, record): default.update({ 'checked': 'checked', }) general = self.attrs.get('input') specific = self.attrs.get('td__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<input %s/>' % attrs.as_html())
def render_link(self, uri, text, attrs=None): """ Render a hyperlink. :param uri: URI for the hyperlink :param text: value wrapped in ``<a></a>`` :param attrs: ``<a>`` tag attributes """ attrs = AttributeDict(attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = uri return format_html('<a {attrs}>{text}</a>', attrs=attrs.as_html(), text=text)
def render_link(self, uri, text, attrs=None, get_params_dict={}): """ Render a hyperlink. :param uri: URI for the hyperlink :param text: value wrapped in ``<a></a>`` :param attrs: ``<a>`` tag attributes """ attrs = AttributeDict(attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = "%s%s" % (uri, '' if not get_params_dict else '?'+"&".join( "%s=%s"%item for item in get_params_dict.items() )) html = '<a {attrs}>{text}</a>'.format( attrs=attrs.as_html(), text=escape(text) ) return mark_safe(html)
def render_link(self, uri, record, value, attrs=None): """ Render a hyperlink. :param uri: URI for the hyperlink :param record: record currently being rendered :param value: value wrapped in ``<a></a>``, might be overridden by ``self.text`` :param attrs: ``<a>`` tag attributes """ attrs = AttributeDict(attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = uri return format_html( '<a {attrs}>{text}</a>', attrs=attrs.as_html(), text=self.text_value(record, value) )
def render_link(self, uri, record, value, attrs=None): ''' Render a hyperlink. Arguments: uri (str): URI for the hyperlink record: record currently being rendered value (str): value to be wrapped in ``<a></a>``, might be overridden by ``self.text`` attrs (dict): ``<a>`` tag attributes ''' attrs = AttributeDict( attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = uri return format_html('<a {attrs}>{text}</a>', attrs=attrs.as_html(), text=self.text_value(record, value))
def render_link(self, uri, record, value, attrs=None): """ Render a link (`<a>`). Arguments: uri (str): URI for the link record: record currently being rendered value (str): value to be wrapped in ``<a></a>``, might be overridden by ``self.text`` attrs (dict): ``<a>`` tag attributes """ attrs = AttributeDict( attrs if attrs is not None else self.attrs.get("a", {})) attrs["href"] = uri return format_html("<a {attrs}>{text}</a>", attrs=attrs.as_html(), text=self.text_value(record, value))
def render_link(self, uri, record, value, attrs=None): ''' Render a hyperlink. Arguments: uri (str): URI for the hyperlink record: record currently being rendered value (str): value to be wrapped in ``<a></a>``, might be overridden by ``self.text`` attrs (dict): ``<a>`` tag attributes ''' attrs = AttributeDict(attrs if attrs is not None else self.attrs.get('a', {})) attrs['href'] = uri return format_html( '<a {attrs}>{text}</a>', attrs=attrs.as_html(), text=self.text_value(record, value) )
def render(self, value, record, bound_column): if not self.links: raise NotImplementedError("Links not assigned.") if not isinstance(self.links, (list, tuple, dict)): raise NotImplementedError("Links must be an iterable.") links = [] for link in self.links: title = link["title"] url = link["url"] urlme = None attrs = link["attrs"] if "attrs" in link else None if "args" in link: args = [a.resolve(record) if isinstance(a, A) else a for a in link["args"]] else: args = None if "url" in link and not isinstance(url, str): urlme = [a.resolve(record) if isinstance(a, A) else a for a in link["url"]][0] else: urlme = None attrs = AttributeDict(attrs if attrs is not None else self.attrs.get("a", {})) try: if isinstance(urlme, str): attrs["href"] = urlresolvers.reverse(urlme, args=args) else: attrs["href"] = urlresolvers.reverse(url, args=args) except urlresolvers.NoReverseMatch: if isinstance(urlme, str): attrs["href"] = urlme else: attrs["href"] = url links.append("<a {attrs}>{text}</a>".format(attrs=attrs.as_html(), text=mark_safe(title))) return mark_safe(self.delimiter.join(links))
def render(self, value): storage = getattr(value, "storage", None) exists = None url = None if storage: # we'll assume value is a `django.db.models.fields.files.FieldFile` if self.verify_exists: exists = storage.exists(value.name) url = storage.url(value.name) else: if self.verify_exists and hasattr(value, "name"): # ignore negatives, perhaps the file has a name but it doesn't # represent a local path... better to stay neutral than give a # false negative. exists = os.path.exists(value.name) or exists tag = 'a' if url else 'span' attrs = AttributeDict(self.attrs.get(tag, {})) attrs['title'] = value.name if url: attrs['href'] = url # add "exists" or "missing" to the class list classes = [c for c in attrs.get('class', '').split(' ') if c] if exists is True: classes.append("exists") elif exists is False: classes.append("missing") attrs['class'] = " ".join(classes) html = '<{tag} {attrs}>{text}</{tag}>'.format( tag=tag, attrs=attrs.as_html(), text=os.path.basename(value.name)) return mark_safe(html)
def render(self, value): storage = getattr(value, "storage", None) exists = None url = None if storage: # we'll assume value is a `django.db.models.fields.files.FieldFile` if self.verify_exists: exists = storage.exists(value.name) url = storage.url(value.name) else: if self.verify_exists and hasattr(value, "name"): # ignore negatives, perhaps the file has a name but it doesn't # represent a local path... better to stay neutral than give a # false negative. exists = os.path.exists(value.name) or exists tag = 'a' if url else 'span' attrs = AttributeDict(self.attrs.get(tag, {})) attrs['title'] = value.name if url: attrs['href'] = url # add "exists" or "missing" to the class list classes = [c for c in attrs.get('class', '').split(' ') if c] if exists is True: classes.append("exists") elif exists is False: classes.append("missing") attrs['class'] = " ".join(classes) html = '<{tag} {attrs}>{text}</{tag}>'.format(tag=tag, attrs=attrs.as_html(), text=os.path.basename( value.name)) return mark_safe(html)
def header(self): default = {'': ''} general = self.attrs.get('input') specific = self.attrs.get('th__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('' % attrs.as_html())
def test_omits_None(self): x = AttributeDict({'x': None}) self.assertEqual(x.as_html(), '')
def header(self): default = {'type': 'checkbox'} general = self.attrs.get('input') specific = self.attrs.get('th__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<label><input name="checkall" %s/><span class="lbl"></span></label>' % attrs.as_html())
def attribute_dict_handles_escaping(): x = AttributeDict({"x": '"\'x&'}) assert x.as_html() == 'x=""'x&"'
def header(self): default = {"type": "checkbox"} general = self.attrs.get("input") specific = self.attrs.get("th__input") attrs = AttributeDict(default, **(specific or general or {})) return mark_safe("<input %s/>" % attrs.as_html())
def test_omits_None(self): x = AttributeDict({'x': None}) assert x.as_html() == ''
def test_attribute_dict_handles_escaping(): x = AttributeDict({'x': '"\'x&'}) assert x.as_html() == 'x=""'x&"'
def test_attribute_dict_omits_None(): x = AttributeDict({'x': None}) assert x.as_html() == ''
def header(self): default = {'type': 'checkbox'} general = self.attrs.get('input') specific = self.attrs.get('th__input') attrs = AttributeDict(default, **(specific or general or {})) return mark_safe('<input %s/>' % attrs.as_html())
def test_handles_escaping(self): x = AttributeDict({'x': '"\'x&'}) self.assertEqual(x.as_html(), 'x=""'x&"')
def test_handles_escaping(self): x = AttributeDict({"x": "\"'x&"}) self.assertEqual(x.as_html(), 'x=""'x&"')
def test_omits_None(self): x = AttributeDict({"x": None}) self.assertEqual(x.as_html(), "")