-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve album upload error handling (#3649)
* Improve album upload error handling * Delete upload if album is gone
- Loading branch information
Showing
4 changed files
with
86 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,67 @@ | ||
import logging | ||
|
||
from django.db import transaction | ||
from django.dispatch import Signal | ||
|
||
from celery import shared_task | ||
from django_drf_filepond.models import TemporaryUpload | ||
|
||
from mailinglists.services import get_member_email_addresses | ||
from members.models.member import Member | ||
from photos.models import Album | ||
from utils.snippets import send_email | ||
|
||
from .services import extract_archive | ||
|
||
logger = logging.getLogger(__name__) | ||
album_uploaded = Signal() | ||
|
||
|
||
@shared_task | ||
def process_album_upload(archive_upload_id: str, album_id: int): | ||
def process_album_upload( | ||
archive_upload_id: str, album_id: int, uploader_id: int | None = None | ||
): | ||
upload = TemporaryUpload.objects.get(upload_id=archive_upload_id) | ||
archive = upload.file | ||
|
||
try: | ||
album = Album.objects.get(id=album_id) | ||
except Album.DoesNotExist: | ||
return | ||
logger.exception("Album %s does not exist", album_id) | ||
archive.delete() | ||
upload.delete() | ||
|
||
uploader = Member.objects.get(id=uploader_id) if uploader_id is not None else None | ||
|
||
upload = TemporaryUpload.objects.get(upload_id=archive_upload_id) | ||
archive = upload.file | ||
try: | ||
with transaction.atomic(): | ||
# We make the upload atomic separately, so we can keep using the db if it fails. | ||
# See https://docs.djangoproject.com/en/4.2/topics/db/transactions/#handling-exceptions-within-postgresql-transactions. | ||
extract_archive(album, archive) | ||
warnings, count = extract_archive(album, archive) | ||
album.is_processing = False | ||
album.save() | ||
|
||
# Send signal to notify that an album has been uploaded. This is used | ||
# by facedetection, and possibly in the future to notify the uploader. | ||
album_uploaded.send(sender=None, album=album) | ||
|
||
if uploader is not None: | ||
# Notify uploader of the upload result. | ||
send_email( | ||
to=get_member_email_addresses(uploader), | ||
subject=("Album upload processed completed."), | ||
txt_template="photos/email/upload-processed.txt", | ||
context={ | ||
"name": uploader.first_name, | ||
"album": album, | ||
"upload_name": upload.file.name, | ||
"warnings": warnings, | ||
"num_processed": count, | ||
}, | ||
) | ||
except Exception as e: | ||
logger.exception(f"Failed to process album upload: {e}", exc_info=e) | ||
|
||
finally: | ||
archive.delete() | ||
upload.delete() |
16 changes: 16 additions & 0 deletions
16
website/photos/templates/photos/emails/upload-processed.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Hi {{ name }}, | ||
|
||
You have recently uploaded an archive ('{{ upload_name }}') to this album: {{ album }} | ||
You can see it here: {% url "photos:album" album.slug %} | ||
|
||
{{ num_processed }} photos were successfully processed. | ||
{% if warnings %} | ||
The following warnings occurred while processing the upload:{% for file, warning in warnings%} | ||
{{ file }}: {{ warning }} | ||
{% endfor %}{% else %} | ||
There were no issues while processing the upload.</p> | ||
{% endif %} | ||
|
||
Kisses, | ||
|
||
The website |