summaryrefslogtreecommitdiff
path: root/postgresqleu/util/widgets.py
diff options
context:
space:
mode:
authorMagnus Hagander2019-08-21 10:58:40 +0000
committerMagnus Hagander2019-08-21 14:30:12 +0000
commit8c78c13fd5ab6b90a03764c35f3d74e4d58410c0 (patch)
tree447307206847d9540d20782d7a4a7e52849060df /postgresqleu/util/widgets.py
parentd7d078a501ff3a0e4dd9b1e373570fb242c4c01e (diff)
Add a field+widgets for uploading images into a bytea column
Bypasses the annoying django handling and also doesn't require a special table. PostgreSQL has no problem storing small things inline and toast makes it transparent...
Diffstat (limited to 'postgresqleu/util/widgets.py')
-rw-r--r--postgresqleu/util/widgets.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/postgresqleu/util/widgets.py b/postgresqleu/util/widgets.py
index 0488bc78..9a27a08e 100644
--- a/postgresqleu/util/widgets.py
+++ b/postgresqleu/util/widgets.py
@@ -1,8 +1,10 @@
from django import forms
from django.forms.widgets import TextInput
+from django.core.files.uploadedfile import InMemoryUploadedFile
from django.utils.safestring import mark_safe
from django.template import loader
+import base64
import datetime
import json
@@ -75,6 +77,16 @@ class PhotoUploadWidget(forms.ClearableFileInput):
return mark_safe(loader.render_to_string('confreg/widgets/photo_upload_widget.html', context))
+class InlineImageUploadWidget(forms.ClearableFileInput):
+ clear_checkbox_label = "Remove image"
+
+ def render(self, name, value, attrs=None, renderer=None):
+ context = self.get_context(name, value, attrs)
+ if value and not isinstance(value, InMemoryUploadedFile):
+ context['widget']['value'] = base64.b64encode(value)
+ return mark_safe(loader.render_to_string('confreg/widgets/inline_photo_upload_widget.html', context))
+
+
class AdminJsonWidget(PrettyPrintJsonWidget):
def render(self, name, value, attrs=None, renderer=None):
attrs['cols'] = 100