diff --git a/.env.example b/.env.example index 1a2f1706fd..6111caf343 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/physionet-django/physionet/settings/base.py b/physionet-django/physionet/settings/base.py index 471f329dba..84e24f5822 100644 --- a/physionet-django/physionet/settings/base.py +++ b/physionet-django/physionet/settings/base.py @@ -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( diff --git a/physionet-django/static-overrides/.gitignore b/physionet-django/static-overrides/.gitignore new file mode 100644 index 0000000000..34e87fe9ca --- /dev/null +++ b/physionet-django/static-overrides/.gitignore @@ -0,0 +1 @@ +static-overrides/* \ No newline at end of file diff --git a/physionet-django/static-overrides/.gitkeep b/physionet-django/static-overrides/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/physionet-django/user/management/commands/compilestatic.py b/physionet-django/user/management/commands/compilestatic.py index bb86aa3c6e..a30dbc3ef9 100644 --- a/physionet-django/user/management/commands/compilestatic.py +++ b/physionet-django/user/management/commands/compilestatic.py @@ -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 @@ -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' @@ -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'))