Skip to content
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

Merged
merged 17 commits into from
Jul 15, 2024
Merged

Upload headshots to S3 via django-storages #1134

merged 17 commits into from
Jul 15, 2024

Conversation

hancush
Copy link
Collaborator

@hancush hancush commented Jul 8, 2024

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):

  • Clean up files from old deployment strategies
  • Update README to reflect current stack and development practices
  • Document environment variables in app.json
  • Default environment variables to development settings

Connects #1073

Notes

Depends on new release of django-councilmatic. Current draft pinned to branch with change.

datamade/django-councilmatic#292

Testing Instructions

  • Log in to the review app using the staging credentials in Bitwarden.
  • View the board member listing and confirm headshots are rendering. (Most are placeholders. Janice Hahn has a real one.)
  • Navigate to a board member and upload a new headshot. Confirm that it appears on their detail page and the listing page.

@fgregg fgregg temporarily deployed to la-metro-cou-hcg-headsh-5e7gzb July 8, 2024 21:08 Inactive
@fgregg fgregg temporarily deployed to la-metro-cou-hcg-headsh-hwoimk July 8, 2024 21:17 Inactive
@hancush hancush temporarily deployed to la-metro-cou-hcg-headsh-hwoimk July 8, 2024 21:18 Inactive
@hancush hancush temporarily deployed to la-metro-cou-hcg-headsh-hwoimk July 8, 2024 21:20 Inactive
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
Copy link
Collaborator Author

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.

@fgregg fgregg temporarily deployed to la-metro-cou-hcg-headsh-3ut4cn July 9, 2024 14:53 Inactive
@hancush hancush temporarily deployed to la-metro-cou-hcg-headsh-3ut4cn July 9, 2024 15:19 Inactive
@hancush hancush temporarily deployed to la-metro-cou-hcg-headsh-3ut4cn July 9, 2024 15:24 Inactive
@hancush hancush temporarily deployed to la-metro-cou-hcg-headsh-3ut4cn July 9, 2024 15:32 Inactive
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 && \
Copy link
Collaborator Author

@hancush hancush Jul 9, 2024

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),
Copy link
Collaborator Author

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")
Copy link
Collaborator Author

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.

Comment on lines +213 to +215
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")
Copy link
Collaborator Author

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.

Copy link
Collaborator

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
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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.

@hancush hancush marked this pull request as ready for review July 9, 2024 15:45
Copy link
Collaborator

@xmedr xmedr left a 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

Comment on lines +213 to +215
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")
Copy link
Collaborator

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
Copy link
Collaborator

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:

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).

Copy link
Collaborator Author

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.

@hancush hancush temporarily deployed to la-metro-cou-hcg-headsh-3ut4cn July 10, 2024 18:17 Inactive
@hancush
Copy link
Collaborator Author

hancush commented Jul 10, 2024

@xmedr Yes, those config var changes are a good idea. Best to have everything standardized.

Copy link
Collaborator

@antidipyramid antidipyramid left a 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!

Copy link
Collaborator

@xmedr xmedr left a 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!

@hancush hancush temporarily deployed to la-metro-cou-hcg-headsh-3ut4cn July 15, 2024 15:44 Inactive
@hancush hancush merged commit 16a70ba into main Jul 15, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants