Skip to content

Commit

Permalink
👌 IMPROVE: Add functionality to opt-out of mail notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
aottr committed Mar 24, 2024
1 parent 3704b3d commit 04a8a78
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
4 changes: 3 additions & 1 deletion core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class UserChangeForm(forms.Form):
'class': 'grow', 'placeholder': 'Telegram Username'}))
use_darkmode = forms.BooleanField(
required=False, widget=forms.CheckboxInput(attrs={'class': 'toggle toggle-secondary'}))
receive_email_notifications = forms.BooleanField(
required=False, widget=forms.CheckboxInput(attrs={'class': 'toggle toggle-secondary'}))

class Meta:
model = PawUser
fields = ('email', 'profile_picture',
'language', 'telegram_username', 'use_darkmode')
'language', 'telegram_username', 'use_darkmode', 'receive_email_notifications')

USERNAME_REGEX_FIELD = forms.RegexField(
required=True,
Expand Down
18 changes: 18 additions & 0 deletions core/migrations/0008_pawuser_receive_email_notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.3 on 2024-03-23 23:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0007_seed_mailtemplates"),
]

operations = [
migrations.AddField(
model_name="pawuser",
name="receive_email_notifications",
field=models.BooleanField(default=True),
),
]
3 changes: 2 additions & 1 deletion core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PawUser(AbstractUser):
language = models.CharField(max_length=2, default='en')
telegram_username = models.CharField(max_length=50, null=True, blank=True)
use_darkmode = models.BooleanField(default=False)
receive_email_notifications = models.BooleanField(default=True)

def __str__(self):
return self.username
Expand Down Expand Up @@ -52,7 +53,7 @@ def send_mail(self, to, context):
fail_silently=False,
)
except Exception as e:
print(e)
print(f"Error sending email with type {type(e)}: {e}")

def __str__(self):
return f"{self.name} - [{self.event}]"
2 changes: 2 additions & 0 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def settings_view(request):
request.user.language = form.cleaned_data["language"]
request.user.telegram_username = form.cleaned_data["telegram_username"]
request.user.use_darkmode = form.cleaned_data["use_darkmode"]
request.user.receive_email_notifications = form.cleaned_data["receive_email_notifications"]
if form.cleaned_data["profile_picture"]:
request.user.profile_picture = form.cleaned_data["profile_picture"]
request.user.save()
Expand All @@ -134,6 +135,7 @@ def settings_view(request):
"language": request.user.language,
"telegram_username": request.user.telegram_username,
"use_darkmode": request.user.use_darkmode,
"receive_email_notifications": request.user.receive_email_notifications
})

res = render(request, "core/settings.html", {"form": form})
Expand Down
2 changes: 1 addition & 1 deletion paw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import get_version

VERSION = (0, 3, 0, "beta", 3)
VERSION = (0, 4, 0, "beta", 1)

__version__ = get_version(VERSION)
9 changes: 8 additions & 1 deletion paw/templates/core/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ <h1 class="text-2xl font-bold mb-4">{% trans 'Settings' %}</h1>
<span class="label-text font-semibold text-base-content" for="{{ form.use_darkmode.id_for_label }}">{% trans 'Use Darkmode' %}</span>
{{ form.use_darkmode }}
</label>
</div>
</div>

<div class="form-control w-full mb-2">
<label class="label cursor-pointer">
<span class="label-text font-semibold text-base-content" for="{{ form.receive_email_notifications.id_for_label }}">{% trans 'Receive Email Notifications' %}</span>
{{ form.receive_email_notifications }}
</label>
</div>

<label class="form-control w-full mb-2">
<div class="label">
Expand Down
6 changes: 4 additions & 2 deletions ticketing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def update_team_assignment(sender, instance, created, **kwargs):

@receiver(post_save, sender=Ticket, dispatch_uid="mail_notification")
def send_mail_notification(sender, instance, created, **kwargs):
if created:
if created and instance.user.receive_email_notifications:
mail_template = MailTemplate.get_template('new_ticket', instance.user.language)
if not mail_template:
return None
Expand All @@ -126,6 +126,8 @@ def send_mail_notification(sender, instance, created, **kwargs):

@receiver(pre_save, sender=Ticket, dispatch_uid="mail_change_notification")
def send_mail_change_notification(sender, instance, update_fields=None, **kwargs):
if not instance.user.receive_email_notifications:
return None
try:
old_instance = Ticket.objects.get(id=instance.id)
except Ticket.DoesNotExist:
Expand Down Expand Up @@ -159,7 +161,7 @@ def __str__(self):

@receiver(post_save, sender=Comment, dispatch_uid="mail_comment_notification")
def send_mail_comment_notification(sender, instance, created, **kwargs):
if created:
if created and instance.ticket.user.receive_email_notifications:
mail_template = MailTemplate.get_template('new_comment', instance.user.language)
if not mail_template:
return None
Expand Down

0 comments on commit 04a8a78

Please sign in to comment.