diff options
Diffstat (limited to 'app/utils/widgets.py')
-rw-r--r-- | app/utils/widgets.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/app/utils/widgets.py b/app/utils/widgets.py new file mode 100644 index 0000000..dc0c4e0 --- /dev/null +++ b/app/utils/widgets.py @@ -0,0 +1,39 @@ +import os +from django import forms +from django.contrib.admin.widgets import AdminFileWidget +from django.utils.safestring import mark_safe +from django.conf import settings + + +def thumbnail(image_path): + absolute_url = os.path.join(settings.IMAGES_URL, image_path[7:]) + print(absolute_url) + return '<img style="max-width: 400px" src="%s" alt="%s" />' % (absolute_url, image_path) + + +class AdminImageWidget(AdminFileWidget): + """ + A FileField Widget that displays an image instead of a file path + if the current file is an image. + """ + def render(self, name, value, attrs=None): + output = [] + file_name = str(value) + help_text = '' + if file_name: + file_path = '%s' % (file_name) + if attrs['id'] == 'id_thumbnail': + help_text = '160 wide' + if attrs['id'] == 'id_image': + help_text = '205px high' + output.append('<span>%s</span><a target="_blank" href="%s">%s</a>' % (help_text, file_path, thumbnail(file_name))) + + output.append(super(AdminFileWidget, self).render(name, value, attrs)) + return mark_safe(''.join(output)) + + +class LGEntryForm(forms.ModelForm): + class Meta: + widgets = { + 'body_markdown': forms.Textarea(attrs={'rows': 50, 'cols': 100}), + } |