Skip to content

⚡(backend) move email sending to celery #1236

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to
- 🔧(project) change env.d system by using local files #1200
- ⚡️(frontend) improve tree stability #1207
- ⚡️(frontend) improve accessibility #1232
- ⚡(backend) move email sending to celery

### Fixed

Expand Down
12 changes: 7 additions & 5 deletions src/backend/core/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from core import authentication, choices, enums, models
from core.services.ai_services import AIService
from core.services.collaboration_services import CollaborationService
from core.tasks.mail import send_ask_for_access_mail
from core.tasks.mail import send_ask_for_access_mail, send_invitation_mail
from core.utils import extract_attachments, filter_descendants

from . import permissions, serializers, utils
Expand Down Expand Up @@ -1640,10 +1640,11 @@ def perform_create(self, serializer):
access = serializer.save(document_id=self.kwargs["resource_id"])

if access.user:
access.document.send_invitation_email(
send_invitation_mail.delay(
access.document.id,
access.user.email,
access.role,
self.request.user,
self.request.user.id,
access.user.language
or self.request.user.language
or settings.LANGUAGE_CODE,
Expand Down Expand Up @@ -1914,10 +1915,11 @@ def perform_create(self, serializer):
"""Save invitation to a document then send an email to the invited user."""
invitation = serializer.save()

invitation.document.send_invitation_email(
send_invitation_mail.delay(
invitation.document.id,
invitation.email,
invitation.role,
self.request.user,
self.request.user.id,
self.request.user.language or settings.LANGUAGE_CODE,
)

Expand Down
21 changes: 21 additions & 0 deletions src/backend/core/tasks/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,24 @@ def send_ask_for_access_mail(ask_for_access_id):
access.user.email,
access.user.language or settings.LANGUAGE_CODE,
)


@app.task
def send_mail(document_id, subject, emails, context=None, language=None):
"""Send email from a document template."""
try:
document = models.Document.objects.get(id=document_id)
document.send_email(subject, emails, context, language)
except models.Document.DoesNotExist:
pass


@app.task
def send_invitation_mail(document_id, email, role, sender_id, language=None):
"""Send invitation email for a document."""
try:
document = models.Document.objects.get(id=document_id)
sender = models.User.objects.get(id=sender_id)
document.send_invitation_email(email, role, sender, language)
except (models.Document.DoesNotExist, models.User.DoesNotExist):
pass