Skip to content

Commit

Permalink
🧩 Merge master into add-type-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyzhang01 committed Nov 7, 2024
2 parents 4d69e42 + 96a605c commit 848c72b
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 137 deletions.
8 changes: 8 additions & 0 deletions backend/pennmobile/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,11 @@
AWS_QUERYSTRING_AUTH = False
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = "public-read"

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = os.environ.get("SMTP_HOST", "")
EMAIL_USE_TLS = True
EMAIL_PORT = os.environ.get("SMTP_PORT", 587)
EMAIL_HOST_USER = os.environ.get("SMTP_USERNAME", "")
EMAIL_HOST_PASSWORD = os.environ.get("SMTP_PASSWORD", "")
DEFAULT_FROM_EMAIL = os.environ.get("SMTP_FROM_EMAIL", EMAIL_HOST_USER)
20 changes: 20 additions & 0 deletions backend/pennmobile/templates/email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Notification</title>
</head>
<body style="font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 0;">

<div>
<p>{{ message|linebreaksbr }}</p>

<hr style="border: 0; border-top: 1px solid #ddd; margin: 30px 0 20px 0;">

<em style="font-size: 12px; color: #666;">Please do not reply to this email. Replies to this email address are not monitored.</em>

</div>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_years(self, years):
# creates new class year in August in preparation for upcoming school year
if years is None:
return (
[+x for x in range(4)]
[timezone.localtime().year + x for x in range(4)]
if timezone.localtime().month < 8
else [timezone.localtime().year + x for x in range(1, 5)]
)
Expand Down
36 changes: 36 additions & 0 deletions backend/portal/migrations/0016_poll_creator_post_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.2.9 on 2024-04-17 04:44

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("portal", "0015_auto_20240226_2236"),
]

operations = [
migrations.AddField(
model_name="poll",
name="creator",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
migrations.AddField(
model_name="post",
name="creator",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
),
),
]
43 changes: 41 additions & 2 deletions backend/portal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.db.models import Q
from django.utils import timezone

from utils.email import get_backend_manager_emails, send_automated_email


User = get_user_model()

Expand Down Expand Up @@ -48,17 +50,54 @@ class Content(models.Model):
admin_comment = models.CharField(max_length=255, null=True, blank=True)
target_populations = models.ManyToManyField(TargetPopulation, blank=True)
priority = models.IntegerField(default=0)
creator = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)

class Meta:
abstract = True

def _get_email_subject(self):
return f"[Portal] {self.__class__._meta.model_name.capitalize()} #{self.id}"

def _on_create(self):
send_automated_email.delay_on_commit(
self._get_email_subject(),
get_backend_manager_emails(),
(
f"A new {self.__class__._meta.model_name} for {self.club_code}"
f"has been created by {self.creator}."
),
)

def _on_status_change(self):
if email := getattr(self.creator, "email", None):
send_automated_email.delay_on_commit(
self._get_email_subject(),
[email],
f"Your {self.__class__._meta.model_name} status for {self.club_code} has been "
+ f"changed to {self.status}."
+ (
f"\n\nAdmin comment: {self.admin_comment}"
if self.admin_comment and self.status == self.STATUS_REVISION
else ""
),
)

def save(self, *args, **kwargs):
prev = self.__class__.objects.filter(id=self.id).first()
super().save(*args, **kwargs)
if prev is None:
self._on_create()
return
if self.status != prev.status:
self._on_status_change()


class Poll(Content):
question = models.CharField(max_length=255)
multiselect = models.BooleanField(default=False)

def __str__(self):
return f"{self.id} - {self.club_code} - {self.question}"
return self.question


class PollOption(models.Model):
Expand All @@ -85,4 +124,4 @@ class Post(Content):
image = models.ImageField(upload_to="portal/images", null=True, blank=True)

def __str__(self):
return f"{self.id} - {self.club_code} - {self.title}"
return self.title
Loading

0 comments on commit 848c72b

Please sign in to comment.