From 8b99731eb3a4091bd17d88a64302649fa575a98b Mon Sep 17 00:00:00 2001 From: janmax <j.michal@stud.uni-goettingen.de> Date: Wed, 29 Nov 2017 23:28:52 +0100 Subject: [PATCH] Adding whitenoise so serving static files becomes less painful --- Dockerfile | 2 +- backend/core/permissions.py | 5 +---- backend/grady/settings/default.py | 24 +++++++++--------------- backend/requirements.txt | 1 + docker-compose.yml | 20 +++++++++++++------- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/Dockerfile b/Dockerfile index acec75a1..6fa59a78 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.6 as python COPY backend/requirements.txt . RUN pip install -r requirements.txt -RUN curl https://gitlab.gwdg.de/snippets/51/raw --output words +RUN curl -s https://gitlab.gwdg.de/snippets/51/raw --output words FROM node:carbon as node diff --git a/backend/core/permissions.py b/backend/core/permissions.py index e9b87940..bb478558 100644 --- a/backend/core/permissions.py +++ b/backend/core/permissions.py @@ -16,9 +16,6 @@ class IsUserGenericPermission(permissions.BasePermission): def has_permission(self, request: HttpRequest, view: View) -> bool: """ required by BasePermission. Check if user is instance of any of the models provided in class' models attribute """ - log.warn("Checking permission of request %s on view %s for user %s", - request, view, request.user) - assert self.models is not None, ( "'%s' has to include a `models` attribute" % self.__class__.__name__ @@ -29,7 +26,7 @@ class IsUserGenericPermission(permissions.BasePermission): user.get_associated_user(), models) for models in self.models) if not is_authorized: - log.warn('User %s has no permission to view %s', + log.warn('User "%s" has no permission to view %s', user.username, view.__class__.__name__) return is_authorized diff --git a/backend/grady/settings/default.py b/backend/grady/settings/default.py index f839d3ef..fce62a6f 100644 --- a/backend/grady/settings/default.py +++ b/backend/grady/settings/default.py @@ -55,6 +55,7 @@ MIDDLEWARE = [ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'whitenoise.middleware.WhiteNoiseMiddleware', ] ROOT_URLCONF = 'grady.urls' @@ -106,13 +107,14 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.10/howto/static-files/ -STATICFILES_DIRS = ( - os.path.join(BASE_DIR, 'node_modules'), -) STATIC_URL = '/static/' -STATIC_ROOT = os.path.join(BASE_DIR, 'static/') +STATIC_ROOT = os.path.join(BASE_DIR, 'static') +STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' -FIXTURE_DIRS = ['/core/fixtures/'] +STATICFILES_FINDERS = ( + 'django.contrib.staticfiles.finders.FileSystemFinder', + 'django.contrib.staticfiles.finders.AppDirectoriesFinder', +) GRAPH_MODELS = { 'all_applications': True, @@ -131,10 +133,6 @@ MESSAGE_TAGS = { messages.ERROR: 'alert-danger', } -STATICFILES_FINDERS = ( - 'django.contrib.staticfiles.finders.FileSystemFinder', - 'django.contrib.staticfiles.finders.AppDirectoriesFinder', -) AUTH_USER_MODEL = 'core.UserAccount' AUTH_PASSWORD_VALIDATORS = [] @@ -163,13 +161,9 @@ LOGGING = { "version": 1, "disable_existing_loggers": False, "formatters": { - 'django.server': { - 'datefmt': '%d/%b/%Y %H:%M:%S', - 'format': '[%(asctime)s] %(levelname)-10s %(name)-20s %(message)s', - }, 'core': { - 'datefmt': '%d/%b/%Y %H:%M:%S', - 'format': '[%(asctime)s] %(levelname)-10s %(name)-20s "%(message)s"', + 'datefmt': '%Y-%m-%d %H:%M:%S', + 'format': '[%(asctime)s] [%(levelname)s] %(name)-20s %(message)s', }, }, 'filters': { diff --git a/backend/requirements.txt b/backend/requirements.txt index bd5a9cac..e367d256 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -9,3 +9,4 @@ xlrd~=1.0.0 pytest-cov~=2.5.1 pytest-django~=3.1.2 prospector~=0.12.7 +whitenoise~=3.3.1 diff --git a/docker-compose.yml b/docker-compose.yml index d85cfb63..7a866aa4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,19 +1,25 @@ version: '3' services: + postgres: image: postgres:9.5 - web: - build: . + restart: always + + grady: + image: docker.gitlab.gwdg.de/j.michal/grady:master command: - /bin/sh - -c - | + sleep 5 + python manage.py collectstatic --noinput python manage.py migrate --noinput - gunicorn --bind 0.0.0.0:8000 grady.wsgi:application & - cd static/ && python -m http.server 8080 - ports: - - "8000:8000" - - "8080:8080" + gunicorn --bind 0.0.0.0:8000 grady.wsgi:application depends_on: - postgres + restart: always + networks: + - default + expose: + - "8000" -- GitLab