From 54c884ae69bad1a23a96e252055802f5ab212a4b Mon Sep 17 00:00:00 2001 From: Oscar Chan Date: Fri, 9 Sep 2022 01:23:25 -0700 Subject: [PATCH 1/6] Django Private Storage --- requirements.txt | 1 + urls.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index ed0998f..3c60208 100644 --- a/requirements.txt +++ b/requirements.txt @@ -39,3 +39,4 @@ tblib==1.7.0 urllib3==1.25.6 vobject==0.9.6.1 whitenoise==4.1.1 +django-private-storage==3.0 diff --git a/urls.py b/urls.py index fc47d13..853f58c 100644 --- a/urls.py +++ b/urls.py @@ -1,12 +1,12 @@ from django.conf import settings -from django.urls import include -from django.urls import path, re_path +from django.urls import include, path, re_path from django.contrib.flatpages import views from django.conf.urls.static import static from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.views.generic import RedirectView +import private_storage.urls admin.autodiscover() @@ -29,6 +29,7 @@ path('newsreel/', include(('newsreel.urls', 'newsreel'), namespace='newsreel')), path('notifications/', include(('notifications.urls', 'notifications'), namespace='notifications')), path('past-presidents/', include(('past_presidents.urls', 'past-presidents'), namespace='past-presidents')), + path('private-media/', include(private_storage.urls)), path('profile/', include(('user_profiles.urls', 'user-profiles'), namespace='user-profiles')), path('project-reports/', include(('project_reports.urls', 'project-reports'), namespace='project-reports')), path('quote-board/', include(('quote_board.urls', 'quote-board'), namespace='quote-board')), From abe56121a30f7106f046eb0fb13e8cafff225368 Mon Sep 17 00:00:00 2001 From: Oscar Chan Date: Fri, 9 Sep 2022 01:23:37 -0700 Subject: [PATCH 2/6] Resumes Private File --- resumes/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resumes/models.py b/resumes/models.py index addb672..721efac 100644 --- a/resumes/models.py +++ b/resumes/models.py @@ -2,7 +2,7 @@ from django.conf import settings from django.db import models - +from private_storage.fields import PrivateFileField class Resume(models.Model): RESUMES_LOCATION = 'resumes' @@ -33,7 +33,7 @@ def rename_file(instance, filename): verbose_name='GPA', max_digits=4, decimal_places=3, help_text='GPA must have three decimal places (ex. 3.750)') full_text = models.TextField(help_text='Full text of the resume') - resume_file = models.FileField( + resume_file = PrivateFileField( upload_to=rename_file, verbose_name='File', help_text='PDF only please') # Each resume must be manually verified by an officer to determine From c8d285e29dd3e4fcbf99388404d5061ffb7d675c Mon Sep 17 00:00:00 2001 From: Oscar Chan Date: Fri, 9 Sep 2022 01:24:18 -0700 Subject: [PATCH 3/6] Move Static Files Dirs to Production only + add Private Storage settings to base --- settings/base.py | 10 +++++----- settings/production.py | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/settings/base.py b/settings/base.py index 28d9784..081e40a 100644 --- a/settings/base.py +++ b/settings/base.py @@ -129,6 +129,10 @@ # Examples: "http://media.lawrence.com/media/", "http://example.com/media/" MEDIA_URL = '/media/' +# Private Storage for Pip package django-private-storage +PRIVATE_STORAGE_ROOT = os.path.join(WORKSPACE_DJANGO_ROOT, 'private-media') +PRIVATE_STORAGE_AUTH_FUNCTION = 'private_storage.permissions.allow_authenticated' + # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. @@ -138,11 +142,6 @@ # URL prefix for static files. STATIC_URL = '/static/' -# Additional locations of static files -STATICFILES_DIRS = [ - os.path.join(WORKSPACE_DJANGO_ROOT, "static"), -] - # List of finder classes that know how to find static files in # various locations. STATICFILES_FINDERS = ( @@ -230,6 +229,7 @@ 'notifications', 'past_presidents', 'project_reports', + 'private_storage', 'quote_board', 'resumes', 'syllabi', diff --git a/settings/production.py b/settings/production.py index b6495f9..4640ba1 100644 --- a/settings/production.py +++ b/settings/production.py @@ -49,6 +49,11 @@ 'tbp.berkeley.edu' ] +# Additional locations of static files +STATICFILES_DIRS = [ + os.path.join(WORKSPACE_DJANGO_ROOT, "static"), +] + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = 'https://www.ocf.berkeley.edu/~tbp/tbpweb/static/' From eae47429c22176ccd48ce69db04ab5785e9b68ea Mon Sep 17 00:00:00 2001 From: Oscar Chan Date: Fri, 9 Sep 2022 01:24:41 -0700 Subject: [PATCH 4/6] Run Django Settings Module --- run | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/run b/run index 5d19003..e97c3d4 100644 --- a/run +++ b/run @@ -22,9 +22,10 @@ fi # PYTHONPATH=~/public_html/tbpweb/src:$PYTHONPATH # TODO ? -# DJANGO_SETTINGS_MODULE=tbpweb.settings # DJANGO_WSGI_MODULE=tbpweb.wsgi +export DJANGO_SETTINGS_MODULE=settings + DJANGO_DIR=~/tbpweb/prod/current VENV=~/tbpweb/prod/shared/venv SOCKFILE=/srv/apps/$(whoami)/tbp.sock From aa9e53604f04136306f143c32d921cbe35ad5e5f Mon Sep 17 00:00:00 2001 From: Oscar Chan Date: Fri, 9 Sep 2022 01:25:02 -0700 Subject: [PATCH 5/6] Hash of Terms --- base/models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/models.py b/base/models.py index edd498d..0aef995 100644 --- a/base/models.py +++ b/base/models.py @@ -193,6 +193,9 @@ def __str__(self): if self.current: name += ' (Current)' return name + + def __hash__(self): + return hash(self.__term_as_int()) def _calculate_pk(self): return self.year * 10 + self.__term_as_int() From 1047f5439cdd1b357cb254f17036159a5f00be5a Mon Sep 17 00:00:00 2001 From: Oscar Chan Date: Fri, 9 Sep 2022 01:33:21 -0700 Subject: [PATCH 6/6] Grant 'run' chmod permission in fabfile --- fabfile.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fabfile.py b/fabfile.py index 3196e63..da18fa4 100644 --- a/fabfile.py +++ b/fabfile.py @@ -116,6 +116,9 @@ def symlink_release(c: Connection): print("-- Symlinking current@ to release") c.run("ln -sfn {} {}".format(c.release_path, c.current_path), echo=True) +def run_permission(c: Connection): + print("-- Granting run file permission") + c.run("chmod +x {}/run".format(c.current_path), echo=True) def systemd_restart(c: Connection): print("-- Restarting systemd unit")