-
Notifications
You must be signed in to change notification settings - Fork 50
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
Refactor payment mails #309
Draft
falkecarlsen
wants to merge
3
commits into
next
Choose a base branch
from
bugfix-mail
base: next
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.
Draft
Changes from all commits
Commits
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
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,6 +1,8 @@ | ||
import enum | ||
import logging | ||
import re | ||
import smtplib | ||
from enum import Enum | ||
|
||
from django.utils.dateparse import parse_datetime | ||
from email.mime.multipart import MIMEMultipart | ||
|
@@ -60,7 +62,7 @@ def make_room_specific_query(room) -> QuerySet: | |
def make_unprocessed_mobilepayment_query() -> QuerySet: | ||
from stregsystem.models import MobilePayment # import locally to avoid circular import | ||
|
||
return MobilePayment.objects.filter(Q(payment__isnull=True) & Q(status__exact=MobilePayment.UNSET)).order_by( | ||
return MobilePayment.objects.filter(Q(payment__isnull=True) & Q(status__in=[MobilePayment.UNSET, MobilePayment.LOW_AMOUNT])).order_by( | ||
'-timestamp' | ||
) | ||
|
||
|
@@ -93,7 +95,13 @@ def date_to_midnight(date): | |
return timezone.make_aware(timezone.datetime(date.year, date.month, date.day, 0, 0)) | ||
|
||
|
||
def send_payment_mail(member, amount, mobilepay_comment): | ||
class EmailType(Enum): | ||
STANDARD = 0 | ||
SHAME = 1 | ||
LOW_AMOUNT = 2 | ||
|
||
|
||
def send_payment_mail(member, amount, email_type: EmailType, comment=None): | ||
if hasattr(settings, 'TEST_MODE'): | ||
return | ||
msg = MIMEMultipart() | ||
|
@@ -103,7 +111,12 @@ def send_payment_mail(member, amount, mobilepay_comment): | |
|
||
formatted_amount = money(amount) | ||
|
||
normal_html = f""" | ||
from django.utils.html import escape | ||
|
||
if email_type is EmailType.STANDARD: | ||
msg.attach( | ||
MIMEText( | ||
f""" | ||
<html> | ||
<head></head> | ||
<body> | ||
|
@@ -122,18 +135,21 @@ def send_payment_mail(member, amount, mobilepay_comment): | |
</p> | ||
</body> | ||
</html> | ||
""" | ||
|
||
from django.utils.html import escape | ||
|
||
shame_html = f""" | ||
""", | ||
'html', | ||
) | ||
) | ||
elif email_type is EmailType.SHAME: | ||
msg.attach( | ||
MIMEText( | ||
f""" | ||
<html> | ||
<head></head> | ||
<body> | ||
<p> | ||
Hej {member.firstname}!<br><br> | ||
Vi har med stort besvær indsat pokkers {formatted_amount} stregdollars på din konto: "{member.username}". <br><br> | ||
Da du ikke skrev dit brugernavn korrekt, men i stedet skrev '{escape(mobilepay_comment)}' var de stakkels TREOer desværre nødt til at tage flere minutter ud af deres dag for at indsætte dine penge manuelt. | ||
Da du ikke skrev dit brugernavn korrekt, men i stedet skrev '{escape(comment)}' var de stakkels TREOer desværre nødt til at tage flere minutter ud af deres dag for at indsætte dine penge manuelt. | ||
Vil du nyde godt af vores automatiske indbetaling kan du i fremtiden med fordel skrive dit brugernavn korrekt i MobilePay kommentaren: '{member.username}'. | ||
Udnytter du vores QR-kode generator klarer den også denne komplicerede process for dig. | ||
|
||
|
@@ -143,7 +159,7 @@ def send_payment_mail(member, amount, mobilepay_comment): | |
====================================================================<br> | ||
Hello {member.firstname}!<br><br> | ||
We've had great trouble inserting {formatted_amount} stregdollars on your account: "{member.username}". <br><br> | ||
This is due to you not you not writing your username correctly, and instead writing '{escape(mobilepay_comment)}'. The poor TREOs had to take multiple minutes out of their day to insert your money manually. | ||
This is due to you not you not writing your username correctly, and instead writing '{escape(comment)}'. The poor TREOs had to take multiple minutes out of their day to insert your money manually. | ||
If you want to utilise our automatic fill-up in future, you can write your username correctly in the MobilePay comment: '{member.username}' | ||
Our QR-code generator also handles this very complicated process for you. | ||
|
||
|
@@ -153,9 +169,43 @@ def send_payment_mail(member, amount, mobilepay_comment): | |
</p> | ||
</body> | ||
</html> | ||
""" | ||
|
||
msg.attach(MIMEText(shame_html if mobilepay_comment else normal_html, 'html')) | ||
""", | ||
'html', | ||
) | ||
) | ||
elif email_type is EmailType.LOW_AMOUNT: | ||
msg.attach( | ||
MIMEText( | ||
f""" | ||
<html> | ||
<head></head> | ||
<body> | ||
<p> | ||
Hej {member.firstname}!<br><br> | ||
Vi har indsat {formatted_amount} stregdollars på din konto: "{member.username}". <br><br> | ||
<b>Bemærk at du har sendt mindre end minimumsgrænsen (50 DKK) i din indbetaling , hvilket vi ikke tillader på grund af høje gebyrer.</b> | ||
Din indbetaling er ekstraordinært blevet manuelt accepteret, men ved gentagne overførsler under minimumsgrænsen vil vi afvise din indbetaling. | ||
Hvis du ikke ønsker at modtage flere mails som denne kan du skrive en mail til: <a href="mailto:[email protected]?Subject=Klage" target="_top">[email protected]</a><br><br> | ||
Mvh,<br> | ||
TREOen<br> | ||
====================================================================<br> | ||
Hello {member.firstname}!<br><br> | ||
We've inserted {formatted_amount} stregdollars on your account: "{member.username}". <br><br> | ||
<b> Notice that you have sent less than the lower bound for top-ups (50 DKK) in you payment, which is against our rules due to high fees.</b> | ||
Your payment has been extraordinarily manually accepted, but with repeat transactions under the lower bound we may decline your payment. | ||
If you do not desire to receive any more mails of this sort, please file a complaint to: <a href="mailto:[email protected]?Subject=Klage" target="_top">[email protected]</a><br><br> | ||
Kind regards,<br> | ||
TREOen | ||
</p> | ||
</body> | ||
</html> | ||
""", | ||
'html', | ||
) | ||
) | ||
else: | ||
logger.error("Could not match type of payment mail to send user. Skipping") | ||
return | ||
|
||
try: | ||
smtpObj = smtplib.SMTP('localhost', 25) | ||
|
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
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.
Let's remove the code instead
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.
Absolutely, mistakenly didn't mark this PR draft