Created
May 4, 2018 17:55
-
-
Save loisaidasam/b68f7226ce189e530ebeac8402125092 to your computer and use it in GitHub Desktop.
Neat Django admin html decorators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def admin_html_link(short_description=None): | |
"""Neat Django admin html link decorator | |
Example model usage: | |
@util_db.admin_html_link("URL playlist") | |
def url_playlist_html(self): | |
return self.url_playlist | |
or | |
@util_db.admin_html_link("Parent HifiPlaylistHistory") | |
def parent_html(self): | |
if not self.parent: | |
return None | |
return self.parent.admin_url_relative, self.parent | |
""" | |
def decorator(function): | |
def wrapper(obj): | |
result = function(obj) | |
if not result: | |
return "N/A" | |
if isinstance(result, basestring): | |
url, value = result, result | |
else: | |
url, value = result | |
return '<a href="%s">%s</a>' % (url, value) | |
wrapper.allow_tags = True | |
if short_description: | |
wrapper.short_description = short_description | |
return wrapper | |
return decorator | |
def admin_html_image(short_description=None): | |
"""Neat Django admin html image decorator | |
Example model usage: | |
@util_db.admin_html_image("URL image Hifi") | |
def url_image_hifi_html(self): | |
return self.url_image_hifi | |
""" | |
def decorator(function): | |
def wrapper(obj): | |
url = function(obj) | |
if not url: | |
return "N/A" | |
return '<img src="%s" />' % url | |
wrapper.allow_tags = True | |
if short_description: | |
wrapper.short_description = short_description | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment