Skip to content

Commit

Permalink
BLUEBUTTON-776 Add logo upload to application ADMIN (#715)
Browse files Browse the repository at this point in the history
* Clean store_media_file to be reusable in model

* Add custom form to app admin w/ logo upload
  • Loading branch information
dtisza1 authored Mar 13, 2019
1 parent dc3e3e0 commit b83b035
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
58 changes: 58 additions & 0 deletions apps/dot_ext/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.contrib import admin
from django.forms import ModelForm
from oauth2_provider.models import AccessToken
from oauth2_provider.models import get_application_model
from .models import ApplicationLabel
from .forms import CustomRegisterApplicationForm


Application = get_application_model()
Expand All @@ -21,8 +23,64 @@ class Meta:
app_label = "bluebutton"


class CustomAdminApplicationForm(CustomRegisterApplicationForm):

def __init__(self, *args, **kwargs):
super(ModelForm, self).__init__(*args, **kwargs)

class Meta:
model = MyApplication
fields = (
'client_id',
'user',
'client_type',
'authorization_grant_type',
'client_secret',
'name',
'skip_authorization',
'scope',
'agree',
'op_tos_uri',
'op_policy_uri',
'client_uri',
'website_uri',
'redirect_uris',
'logo_uri',
'logo_image',
'tos_uri',
'policy_uri',
'software_id',
'contacts',
'support_email',
'support_phone_number',
'description',
'active',
'first_active',
'last_active',
)

def clean(self):
return self.cleaned_data

def clean_name(self):
return super().clean_name()

def clean_agree(self):
return self.cleaned_data.get('agree')

def clean_redirect_uris(self):
return self.cleaned_data.get('redirect_uris')

def clean_logo_image(self):
return super().clean_logo_image()

def save(self, *args, **kwargs):
return super().save(*args, **kwargs)


class MyApplicationAdmin(admin.ModelAdmin):

form = CustomAdminApplicationForm
list_display = ("name", "user", "authorization_grant_type", "client_id",
"skip_authorization", "scopes", "created", "updated")
list_filter = ("name", "user", "client_type", "authorization_grant_type",
Expand Down
12 changes: 7 additions & 5 deletions apps/dot_ext/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from oauth2_provider.forms import AllowForm as DotAllowForm
from oauth2_provider.models import get_application_model
from apps.dot_ext.validators import validate_logo_image
from apps.dot_ext.storage import store_logo_image
import logging


logger = logging.getLogger('hhs_server.%s' % __name__)


class CustomRegisterApplicationForm(forms.ModelForm):

logo_image = forms.ImageField(label='Logo URI Image Upload', required=False,
help_text="Upload your logo image file here in JPEG (.jpg) format! "
"The maximum file size allowed is %sKB and maximum dimensions are %sx%s pixels. "
Expand Down Expand Up @@ -138,11 +138,13 @@ def clean_logo_image(self):

def save(self, *args, **kwargs):
app = self.instance
logmsg = "%s agreed to %s for the application %s" % (app.user, app.op_tos_uri,
app.name)
logger.info(logmsg)
# Only log agreement from a Register form
if app.agree and type(self) == CustomRegisterApplicationForm:
logmsg = "%s agreed to %s for the application %s" % (app.user, app.op_tos_uri,
app.name)
logger.info(logmsg)
app = super().save(*args, **kwargs)
uri = store_logo_image(self.cleaned_data.pop('logo_image', None), app.pk)
uri = app.store_media_file(self.cleaned_data.pop('logo_image', None), "logo.jpg")
if uri:
app.logo_uri = uri
app.save()
Expand Down
14 changes: 14 additions & 0 deletions apps/dot_ext/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from django.conf import settings
from apps.dot_ext.validators import validate_notags
from django.template.defaultfilters import truncatechars
from django.core.files.storage import default_storage


logger = logging.getLogger('hhs_server.%s' % __name__)
Expand Down Expand Up @@ -113,6 +114,19 @@ def get_allowed_schemes(self):
allowed_schemes.append(scheme)
return allowed_schemes

# Save a file to application media storage
def store_media_file(self, file, filename):
uri = None
if file:
if getattr(file, 'name', False):
file_path = "applications/" + hashlib.sha256(str(self.pk).encode('utf-8')).hexdigest() + "/" + filename
if default_storage.exists(file_path):
default_storage.delete(file_path)
default_storage.save(file_path, file)
if default_storage.exists(file_path):
uri = settings.MEDIA_URL + file_path
return uri


class ApplicationLabel(models.Model):
name = models.CharField(max_length=255, unique=True)
Expand Down
17 changes: 0 additions & 17 deletions apps/dot_ext/storage.py

This file was deleted.

0 comments on commit b83b035

Please sign in to comment.