-
Notifications
You must be signed in to change notification settings - Fork 1
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
Issue #149: Extracted email messages to templates. Fixed some bugs, cleaned up some code. #156
Open
lilliputten
wants to merge
8
commits into
main
Choose a base branch
from
149-email-messages-in-template-files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b67263a
Issue #149: Renamed used modules from `billing` to `payment`.
lilliputten a53e42b
Issue #149: Extracting email messages to tamplates (in progress): pas…
lilliputten 0786573
Issue #149: Minor changes.
lilliputten df13fe7
Issue #149: Fixed error 'Unrecognized currency...' (using currencies …
lilliputten 8771cce
Issue #149: Removed 'test' assets.
lilliputten e719fb2
Issue #149: Fixed found errors in sending messages for a registration…
lilliputten c2badf4
Issue #152: Minor linter fixes.
lilliputten 5becad6
Issue #149: Added template for sending receipt.
lilliputten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import re | ||
|
||
|
||
def prepare_email_message_text(text: str) -> str: | ||
# Remove spaces around | ||
text = text.strip() | ||
# Remove spaces before newlines | ||
text = re.sub(r"[ \t]*[\n\r]", "\n", text) | ||
# Leave max two newlines in a row | ||
text = re.sub(r"\n{3,}", "\n\n", text) | ||
return text | ||
|
||
|
||
def parse_email_subject_and_content(text: str) -> list[str]: | ||
""" | ||
Email message should contain a subject in the beginning, separated from the content by double newline. | ||
Returns the list consisted of [subject, content] | ||
""" | ||
text = prepare_email_message_text(text) | ||
results = text.split("\n\n", 1) | ||
if len(results) < 2: | ||
raise Exception( | ||
"Email message should constist of a subject and a content, separated from the content by double newline." | ||
) | ||
return results | ||
|
||
|
||
__all__ = [ | ||
prepare_email_message_text, | ||
parse_email_subject_and_content, | ||
] |
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 |
---|---|---|
|
@@ -4,20 +4,25 @@ | |
import random | ||
import string | ||
from datetime import date | ||
|
||
import requests | ||
|
||
from fpdf import FPDF | ||
|
||
from django.contrib.sites.models import Site | ||
from django.http import HttpRequest | ||
from django.conf import settings | ||
from django.contrib.auth.models import AbstractUser | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.db import models | ||
from django.db.models import Model, Q, QuerySet | ||
from django.urls import reverse | ||
from fpdf import FPDF | ||
from django.template.loader import render_to_string | ||
|
||
from dds_registration.core.constants.payments import ( | ||
site_default_currency, | ||
site_supported_currencies, | ||
) | ||
from .core.helpers.emails import parse_email_subject_and_content | ||
|
||
from .core.constants.date_time_formats import dateFormat | ||
from .core.constants.payments import currency_emojis, payment_details_by_currency | ||
|
@@ -220,7 +225,7 @@ def mark_obsolete(self): | |
) | ||
self.save() | ||
|
||
def mark_paid(self): | ||
def mark_paid(self, request: HttpRequest): | ||
if self.status == "PAID": | ||
return | ||
self.status = "PAID" | ||
|
@@ -235,7 +240,7 @@ def mark_paid(self): | |
) | ||
}, | ||
) | ||
self.email_receipt() | ||
self.email_receipt(request) | ||
self.save() | ||
|
||
@property | ||
|
@@ -285,29 +290,55 @@ def invoice_pdf(self): | |
def receipt_pdf(self): | ||
return create_receipt_pdf_from_payment(self) | ||
|
||
def email_invoice(self): | ||
def email_invoice(self, request: HttpRequest): | ||
user = User.objects.get(id=self.data["user"]["id"]) | ||
# TODO: Issue #149: To extract these (and all other hardcoded here, in `send_email` methods?) texts to template files, with substiting names, urls and emails from settings or preferences values? | ||
context = { | ||
"site": Site.objects.get_current(), | ||
"scheme": "https" if request.is_secure() else "http", | ||
"payment": self, | ||
"user": user, | ||
} | ||
if self.data["kind"] == "membership": | ||
subject = f"DdS Membership Invoice {self.invoice_no}" | ||
message = f"Thanks for signing up for Départ de Sentier membership! Membership fees allow us to write awesome open source code, deploy open infrastructure, and run community events without spending all our time fundraising.\n\nYour membership will run until December 31st, {user.membership.until} (Don't worry, you will get a reminder to renew for another year :).\n\nPlease find attached the membership invoice. Your membership is not in force until the bank transfer is received.\n\nYou can change your invoice details here: https://events.d-d-s.ch{reverse('membership_application')}.\n\nIf you have any questions, please contact [email protected]." | ||
email_template = "dds_registration/payment/emails/invoice_membership.txt.django" | ||
else: | ||
event = Event.objects.get(id=self.data["event"]["id"]) | ||
subject = f"DdS Event {event.title} Registration Invoice {self.invoice_no}" | ||
message = f"Thanks for registering for {event.title}! We look forward to seeing your, in person or virtually.\n\nDépart de Sentier runs its events and schools on a cost-neutral basis - i.e. we don't make a profit off the registration fees. They are used for catering, room, hotel, and equipment rental, AV hosting and technician fees, and guest speaker costs. We literally could not run this event without your support.\n\nYou can view your registration status and apply for membership at https://events.d-d-s.ch/profile.\n\nPlease find attached the registration invoice. Your registration is not finalized until the bank transfer is received.\n\nYou can change your invoice details here: https://events.d-d-s.ch{reverse('event_registration', args=(event.code,))}.\n\nIf you have any questions, please contact [email protected]." | ||
context["event"] = Event.objects.get(id=self.data["event"]["id"]) | ||
email_template = "dds_registration/payment/emails/invoice_membership.txt.django" | ||
# Parse email message template | ||
text = render_to_string( | ||
template_name=email_template, | ||
context=context, | ||
request=request, | ||
) | ||
# Extract a subject and a message from the template | ||
[subject, message] = parse_email_subject_and_content(text) | ||
user.email_user( | ||
subject=subject, | ||
message=message, | ||
attachment_content=self.invoice_pdf(), | ||
attachment_name=f"DdS Invoice {self.invoice_no}.pdf", | ||
) | ||
|
||
def email_receipt(self): | ||
def email_receipt(self, request: HttpRequest): | ||
user = User.objects.get(id=self.data["user"]["id"]) | ||
kind = "Membership" if self.data["kind"] == "membership" else "Event" | ||
context = { | ||
"kind": "Membership" if self.data["kind"] == "membership" else "Event", | ||
"site": Site.objects.get_current(), | ||
"scheme": "https" if request.is_secure() else "http", | ||
"payment": self, | ||
"user": user, | ||
} | ||
email_template = "dds_registration/payment/emails/receipt.txt.django" | ||
# Parse email message template | ||
text = render_to_string( | ||
template_name=email_template, | ||
context=context, | ||
request=request, | ||
) | ||
# Extract a subject and a message from the template | ||
[subject, message] = parse_email_subject_and_content(text) | ||
user.email_user( | ||
subject=f"DdS {kind} Receipt {self.invoice_no}", | ||
message="Thanks! A receipt for your event or membership payment is attached. You can always find more information about your item at your your profile: https://events.d-d-s.ch/profile.\n\nWe really appreciate your support. If you have any questions, please contact [email protected].", | ||
subject=subject, | ||
message=message, | ||
attachment_content=self.receipt_pdf(), | ||
attachment_name=f"DdS receipt {self.invoice_no}.pdf", | ||
) | ||
|
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
15 changes: 15 additions & 0 deletions
15
dds_registration/templates/dds_registration/payment/emails/invoice_event.txt.django
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,15 @@ | ||
DdS Event {{ event.title }} Registration Invoice {{ payment.invoice_no }} | ||
|
||
|
||
Thanks for registering for {{ event.title }}! We look forward to seeing your, in person or virtually. | ||
|
||
Départ de Sentier runs its events and schools on a cost-neutral basis - i.e. we don't make a profit off the registration fees. They are used for catering, room, hotel, and equipment rental, AV hosting and technician fees, and guest speaker costs. We literally could not run this event without your support. | ||
|
||
You can view your registration status and apply for membership at {{ scheme }}://{{ site.domain }}{% url 'profile' %}. | ||
|
||
Please find attached the registration invoice. Your registration is not finalized until the bank transfer is received. | ||
|
||
You can change your invoice details here: {{ scheme }}://{{ site.domain }}{% url 'event_registration' event_code=event.code %}. | ||
|
||
If you have any questions, please contact {{ settings.DEFAULT_CONTACT_EMAIL }}. | ||
|
14 changes: 14 additions & 0 deletions
14
dds_registration/templates/dds_registration/payment/emails/invoice_membership.txt.django
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,14 @@ | ||
DdS Membership Invoice {{ payment.invoice_no }} | ||
|
||
|
||
Thanks for signing up for Départ de Sentier membership! | ||
|
||
Membership fees allow us to write awesome open source code, deploy open infrastructure, and run community events without spending all our time fundraising. | ||
|
||
Your membership will run until December 31st, {{ user.membership.until }} (Don't worry, you will get a reminder to renew for another year :). | ||
|
||
Please find attached the membership invoice. Your membership is not in force until the bank transfer is received. | ||
|
||
You can change your invoice details here: {{ scheme }}://{{ site.domain }}{% url 'membership_application' %}. | ||
|
||
If you have any questions, please contact {{ settings.DEFAULT_CONTACT_EMAIL }}. |
12 changes: 12 additions & 0 deletions
12
dds_registration/templates/dds_registration/payment/emails/receipt.txt.django
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,12 @@ | ||
DdS {{ kind }} Receipt {{ payment.invoice_no }} | ||
|
||
|
||
Thanks! | ||
|
||
A receipt for your event or membership payment is attached. | ||
|
||
You can always find more information about your item at your your profile: {{ scheme }}://{{ site.domain }}{% url 'profile' %}. | ||
|
||
We really appreciate your support. | ||
|
||
If you have any questions, please contact {{ settings.DEFAULT_CONTACT_EMAIL }}. |
File renamed without changes.
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
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 |
---|---|---|
|
@@ -69,7 +69,7 @@ def membership_application(request: HttpRequest): | |
if payment.data["method"] == "INVOICE": | ||
payment.status = "ISSUED" | ||
payment.save() | ||
payment.email_invoice() | ||
payment.email_invoice(request) | ||
messages.success( | ||
request, | ||
f"Your membership has been created! An invoice has been sent to {request.user.email} from [email protected]. The invoice can also be downloaded from your profile. Please note your membership is not in force until the invoice is paid.", | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You had to make a lot of changes elsewhere to get the request here, but I don't understand why it is needed.