-
Notifications
You must be signed in to change notification settings - Fork 2
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
Upload headshots to S3 via django-storages #1134
Conversation
requirements.txt
Outdated
@@ -1,4 +1,5 @@ | |||
django-councilmatic[convert_docs]>=3.0 | |||
# django-councilmatic[convert_docs]>=3.0 | |||
git+https://github.com/datamade/django-councilmatic.git@hcg/configurable-headshot-backend#egg=django-councilmatic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Update this to new django-councilmatic
release once it becomes available.
FROM python:3.10-slim-bullseye | ||
LABEL maintainer "DataMade <[email protected]>" | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y libpq-dev gcc gdal-bin gnupg && \ | ||
apt-get install -y libxml2-dev libxslt1-dev antiword unrtf poppler-utils \ | ||
tesseract-ocr flac ffmpeg lame libmad0 libsox-fmt-mp3 \ | ||
sox libjpeg-dev swig libpulse-dev curl && \ | ||
sox libjpeg-dev swig libpulse-dev curl git && \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Need git
to install django-councilmatic
from the branch, will remove when new release is cut.
@@ -8,8 +8,33 @@ | |||
|
|||
env = environ.Env( | |||
# Set default values | |||
DEBUG=(bool, False), | |||
LOCAL_DOCKER=(bool, False), | |||
LOCAL_DOCKER=(bool, True), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We default to development settings in other apps. Think it's a good idea to preserve this pattern and keep the size of the local env file small, since these are overridden in deployment already.
@@ -19,7 +44,7 @@ | |||
environ.Env.read_env(os.path.join(BASE_DIR, ".env.local")) | |||
|
|||
SECRET_KEY = env("DJANGO_SECRET_KEY") | |||
DEBUG = env.bool("DEBUG") | |||
DEBUG = env.bool("DJANGO_DEBUG") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's conventional to prefix settings with the app they're relevant to.
AWS_S3_ACCESS_KEY_ID = env("AWS_S3_ACCESS_KEY_ID") | ||
AWS_S3_SECRET_ACCESS_KEY = env("AWS_S3_SECRET_ACCESS_KEY") | ||
AWS_STORAGE_BUCKET_NAME = env("AWS_STORAGE_BUCKET_NAME") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Django Storages automatically discovers these variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neat! that makes things easier. if we're removing these non-S3 specific vars here, what do you think of these heroku config var adjustments as well?
staging - in addition to AWS_KEY
and AWS_SECRET
, there's also AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
which seem to hold duplicate values of their S3 counterparts. i imagine these can all be removed
production - the S3 versions do not exist, so we can likely change AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
to match the S3 var names. django storages also finds these automatically, but i'm unsure if the mismatch would cause issues considering these settings
are looking for the S3 specific versions.
review apps are using only the S3 specific variables, so no change there
# Returning here solves that. | ||
if "headshot_placeholder.png" not in self.headshot.path: | ||
return self.headshot | ||
return self.headshot.url |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We previously needed to load all images with static()
because django-councilmatic
was storing them with static files. Now, since we've told django-councilmatic
to upload headshots to S3, if there is a headshot, we can return its URL directly, and only get the manual headshots and placeholder from static.
@@ -668,7 +667,7 @@ def post(self, request, *args, **kwargs): | |||
self.get_context_data(form=form, headshot_error=error) | |||
) | |||
|
|||
person.headshot = self.get_file_url(request, file_obj) | |||
person.headshot = file_obj |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
person.headshot
is a file field, so we can pass it this file directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the README updates and env var cleanup are very much appreciated! and the headshot uploads work as expected. just a couple questions inline
AWS_S3_ACCESS_KEY_ID = env("AWS_S3_ACCESS_KEY_ID") | ||
AWS_S3_SECRET_ACCESS_KEY = env("AWS_S3_SECRET_ACCESS_KEY") | ||
AWS_STORAGE_BUCKET_NAME = env("AWS_STORAGE_BUCKET_NAME") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neat! that makes things easier. if we're removing these non-S3 specific vars here, what do you think of these heroku config var adjustments as well?
staging - in addition to AWS_KEY
and AWS_SECRET
, there's also AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
which seem to hold duplicate values of their S3 counterparts. i imagine these can all be removed
production - the S3 versions do not exist, so we can likely change AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
to match the S3 var names. django storages also finds these automatically, but i'm unsure if the mismatch would cause issues considering these settings
are looking for the S3 specific versions.
review apps are using only the S3 specific variables, so no change there
README.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 17 here refers to a legacy way of running the app without docker. is this still useful to us? those old docs talk about using Solr which has been removed here, so they're also out of date. If we want to keep the reference to the old magicks, we could maybe change the language to something like:
These days, we run apps in containers for local development. More on that here. For reference, if you'd like to read up on how we used to run this app locally, see the legacy setup instructions.
The line in question:
la-metro-councilmatic/README.md
Line 17 in 04df15e
These days, we run apps in containers for local development. More on that [here](https://github.com/datamade/how-to/docker/local-development.md). Prefer to run the app locally? See the [legacy setup instructions](https://github.com/datamade/la-metro-councilmatic/blob/b8bc14f6d90f1b05e24b5076b1bfcd5e0d37527a/README.md). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great call. Since we don't run apps without Docker any more, I just removed the reference to the old setup instructions.
@xmedr Yes, those config var changes are a good idea. Best to have everything standardized. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works great. Thanks for also including the README updates!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sweet, i just made those config var changes for prod and staging, so this looks good to me!
Overview
This PR makes use of an upstream change to
django-councilmatic
allowing the headshot storage backend to be configured by Councilmatic instances. When credentials are provided, it configures the Metro app to store and retrieve headshots from S3.This PR also performs some housekeeping (#1055):
app.json
Connects #1073
Notes
Depends on new release of django-councilmatic. Current draft pinned to branch with change.
datamade/django-councilmatic#292
Testing Instructions