Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize logo and background image #1738

Closed
jnm opened this issue Apr 22, 2018 · 4 comments
Closed

Customize logo and background image #1738

jnm opened this issue Apr 22, 2018 · 4 comments
Assignees
Labels
enhancement Ideas, improvements and features

Comments

@jnm
Copy link
Member

jnm commented Apr 22, 2018

via settings in the Django admin interface

@jnm jnm added the enhancement Ideas, improvements and features label Apr 22, 2018
@jnm jnm self-assigned this Apr 22, 2018
@jnm
Copy link
Member Author

jnm commented Apr 23, 2018

django-constance does have some immature support for ImageFields, but it doesn't work for S3 / respect the storage class: https://github.com/jazzband/django-constance/blob/05ec0e9f1dc41be63f42bf75707a9d583c295bfa/constance/admin.py#L137-L144
On the upload side, this isn't terrible to fix with something like:

diff --git a/constance/admin.py b/constance/admin.py
index a2285cc..0f55566 100644
--- a/constance/admin.py
+++ b/constance/admin.py
@@ -13,6 +13,7 @@ from django.contrib import admin, messages
 from django.contrib.admin import widgets
 from django.contrib.admin.options import csrf_protect_m
 from django.core.exceptions import PermissionDenied, ImproperlyConfigured
+from django.core.files.storage import DefaultStorage
 from django.forms import fields
 from django.http import HttpResponseRedirect
 from django.template.response import TemplateResponse
@@ -135,13 +136,13 @@ class ConstanceForm(forms.Form):
         self.initial['version'] = version_hash.hexdigest()
 
     def save(self):
+        storage_instance = DefaultStorage()
         for file_field in self.files:
-            file = self.cleaned_data[file_field]
-            file_path = os.path.join(django_settings.MEDIA_ROOT, file.name)
-            with open(file_path, 'wb+') as destination:
-                for chunk in file.chunks():
-                    destination.write(chunk)
-                self.cleaned_data[file_field] = file.name
+            uploaded_file = self.cleaned_data[file_field]
+            # TODO: max_length
+            actual_name = storage_instance.save(uploaded_file.name,
+                                                uploaded_file)
+            self.cleaned_data[file_field] = actual_name
 
         for name in settings.CONFIG:
             if getattr(config, name) != self.cleaned_data[name]:

...but the file can't be accessed in the admin interface, nor does the context processor provide a way to retrieve the media file's URL. Other issues:

  • "Reset to default" doesn't work for images
  • Images left unselected ("No file selected") cause an IntegrityError

Question: do we work on django-constance to fix these issues or create our own model for custom images? The latter would use more built-in Django machinery and probably take less time and effort.

@pmusaraj
Copy link
Contributor

@jnm we could also consider storing the overrides as CSS (with image as base64).

@jnm
Copy link
Member Author

jnm commented May 13, 2018

Good point. The background image is fairly large, though, and its base64-encoded version is about 135% the size of the original (325504 vs 240955 bytes). I'm going to try a quick Django model

@jnm jnm closed this as completed in 2e8bcaf May 23, 2018
jnm added a commit that referenced this issue Jun 17, 2018
when custom logos are provided. Closes #1738.
Does not affect the logo displayed in Enketo.
@jnm
Copy link
Member Author

jnm commented Feb 9, 2021

reopening to fix for local storage
see also kobotoolbox/kobo-docker#175

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Ideas, improvements and features
Projects
None yet
Development

No branches or pull requests

3 participants