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(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, 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(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)