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

[WIP] Customizable theming fix background images issue #2006

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ LIGHT='#007FA3'
GRADIENT_60 = 'rgba(42, 47, 52, 0.6)'
GRADIENT_85 = 'rgba(42, 47, 52, 0.85)'

## physionet background image(local path to image or a download url)
## currently a jpg image is expected
BACKGROUND_IMAGE='static/images/background.jpg'

# Users settings
# maximum number of emails that can be associated to a user model
MAX_EMAILS_PER_USER = 10
Expand Down
5 changes: 4 additions & 1 deletion physionet-django/physionet/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static-overrides'),
os.path.join(BASE_DIR, 'static'),
]
# Google Storge service account credentials
if config('GOOGLE_APPLICATION_CREDENTIALS', default=None):
GOOGLE_APPLICATION_CREDENTIALS = os.path.join(
Expand Down
1 change: 1 addition & 0 deletions physionet-django/static-overrides/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static-overrides/*
Empty file.
38 changes: 38 additions & 0 deletions physionet-django/user/management/commands/compilestatic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Management Command to compile static Sass files
import os
import shutil
import urllib.request
import urllib.error

from django.core.management.base import BaseCommand, CommandError
from decouple import config
from django.core.management import call_command
Expand Down Expand Up @@ -34,6 +38,34 @@ def setup_theme_colors(colors):
return themes_colors


def setup_static_file(source, destination):
"""
Copies a static file from source to destination
:param source: the source file path(local), or a url
:param destination: the destination file path(local)
"""

if source.startswith('http'):
try:
urllib.request.urlretrieve(source, destination)
except urllib.error.HTTPError:
raise CommandError(f'Could not download {source}')
except urllib.error.URLError:
raise CommandError(f'Could not download {source}')
else:
try:
# check if the destination directories exist, if not create them
if not os.path.isdir(os.path.dirname(destination)):
os.makedirs(os.path.dirname(destination))

# copy the file
shutil.copy(source, destination)
except IOError as e:
raise Exception(f'Could not copy {source}. Error: {e.strerror}')
except Exception as e:
raise Exception(f'Unexpected error: {e}')


class Command(BaseCommand):
help = 'Compile static Sass files'

Expand Down Expand Up @@ -65,3 +97,9 @@ def handle(self, *args, **options):
# Demo section for home.css
theme_generator(themes_colors, imports=['../../custom/scss/home'])
call_command('sass', 'static/bootstrap/scss/theme.scss', 'static/custom/css/home.css')

# setup background image
image_source = config('BACKGROUND_IMAGE', default='static/images/background.jpg')
image_destination = 'static-overrides/images/background.jpg'
setup_static_file(image_source, image_destination)
self.stdout.write(self.style.SUCCESS('Successfully setup background image'))