Skip to content

Commit

Permalink
Switch to using Azure blob storage for media uploads.
Browse files Browse the repository at this point in the history
  • Loading branch information
ropable committed Aug 11, 2023
1 parent 186d419 commit 9ae30df
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 15 deletions.
78 changes: 77 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions prs2/referral/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from azure.core.exceptions import ResourceNotFoundError
from copy import copy
from datetime import date
from dateutil.parser import parse
from django.conf import settings
from django.contrib.auth.signals import user_logged_in
from django.contrib.gis.db import models
from django.contrib.gis.geos import GeometryCollection
from django.core.exceptions import SuspiciousFileOperation
from django.core.mail import EmailMultiAlternatives
from django.core.validators import MaxLengthValidator
from django.db.models import Q
Expand Down Expand Up @@ -1140,27 +1140,24 @@ def filename(self):

@property
def extension(self):
try: # Account for SuspiciousFileOperation exceptions.
if self.uploaded_file and os.path.exists(self.uploaded_file.path):
ext = os.path.splitext(self.uploaded_file.name)[1]
return ext.replace(".", "").upper()
else:
return ""
except SuspiciousFileOperation:
if self.uploaded_file:
ext = os.path.splitext(self.uploaded_file.name)[1]
return ext.replace(".", "").upper()
else:
return ""

@property
def filesize_str(self):
try: # Account for SuspiciousFileOperation exceptions.
if self.uploaded_file and os.path.exists(self.uploaded_file.path):
try:
if self.uploaded_file:
num = self.uploaded_file.size
for x in ["b", "Kb", "Mb", "Gb"]:
if num < 1024.0:
return "{:3.1f}{}".format(num, x)
num /= 1024.0
else:
return ""
except SuspiciousFileOperation:
except ResourceNotFoundError:
return ""

def as_row(self):
Expand Down
11 changes: 8 additions & 3 deletions prs2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@
INTERNAL_IPS = ['127.0.0.1', '::1']
ROOT_URLCONF = 'prs2.urls'
WSGI_APPLICATION = 'prs2.wsgi.application'
# Allow overriding the Django default for FILE_UPLOAD_PERMISSIONS (0o644).
# Required for non-local Azure storage volumes in Kubernetes environment.
FILE_UPLOAD_PERMISSIONS = env('FILE_UPLOAD_PERMISSIONS', None)

# Use Azure blob storage for media uploads.
DEFAULT_FILE_STORAGE = 'storages.backends.azure_storage.AzureStorage'
AZURE_ACCOUNT_NAME = env('AZURE_ACCOUNT_NAME', 'name')
AZURE_ACCOUNT_KEY = env('AZURE_ACCOUNT_KEY', 'key')
AZURE_CONTAINER = env('AZURE_CONTAINER', 'container')

# PRS may deploy its own instance of Geoserver.
PRS_GEOSERVER_WMTS_URL = env('PRS_GEOSERVER_WMTS_URL', '')
PRS_GEOSERVER_WFS_URL = env('PRS_GEOSERVER_WFS_URL', '')
GEOSERVER_WMTS_URL = env('GEOSERVER_WMTS_URL', '')
GEOSERVER_WFS_URL = env('GEOSERVER_WFS_URL', '')
GEOSERVER_SSO_USER = env('GEOSERVER_SSO_USER', 'username')
GEOSERVER_SSO_PASS = env('GEOSERVER_SSO_PASS', 'password')
GEOCODER_URL = env('GEOCODER_URL', '')

INSTALLED_APPS = (
'whitenoise.runserver_nostatic',
'django.contrib.admin',
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ django-celery-results = "2.4.0"
pygeopkg = "0.1.3"
whitenoise = {version = "6.5.0", extras = ["brotli"]}
django-crum = "0.7.9"
django-storages = {version = "1.13.2", extras = ["azure"]}

[tool.poetry.group.dev.dependencies]
ipython = "^8.10.0"
Expand Down

0 comments on commit 9ae30df

Please sign in to comment.