Skip to content
Merged
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
25 changes: 25 additions & 0 deletions bats_ai/core/models/user_profile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string


class UserProfile(models.Model):
Expand All @@ -13,3 +16,25 @@ class UserProfile(models.Model):
def _create_new_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)


@receiver(post_save, sender=User)
def _notify_admins_new_user(sender, instance, created, **kwargs):
admins = User.objects.filter(is_superuser=True)
current_site = Site.objects.get_current()
recipient_list = [admin.email for admin in admins]
Comment on lines +23 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, if you wanted to hyper-optimize this, you could use .values_list() to directly get the desired list of strings:

Suggested change
admins = User.objects.filter(is_superuser=True)
current_site = Site.objects.get_current()
recipient_list = [admin.email for admin in admins]
recipient_list = User.objects.filter(is_superuser=True).values_list('email', flat=True)
current_site = Site.objects.get_current()

However, given the size of the QuerySet here (~3 admin users), there's no problem with constructing the intermediate User model and accessing the .email property. Your way gives better type checking / autocomplete, better debug-ability when stepping through the code, and ought to look more obvious to a Django developer.

I just wanted to ensure you're aware of the alternative.


if recipient_list:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch of a corner case. 😃

If you want to de-indent everything below, you could also invert:

Suggested change
if recipient_list:
if not recipient_list:
return

Totally up to you, whichever you find more readable.

email_content = render_to_string(
'core/new_user_signup.txt',
{
'user': instance,
'site': current_site,
},
)
send_mail(
subject=f'{current_site.name}: New user signup',
message=email_content,
from_email=None,
recipient_list=recipient_list,
)
5 changes: 5 additions & 0 deletions bats_ai/core/templates/core/new_user_signup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is an automated notification from {{ site.name }} to alert admins that a new user {{ user.email }} has signed up.

To see additional details about the user, and take actions like verifying the user's profile, go to

https://{{ site.domain }}{% url 'admin:auth_user_change' user.pk %}
16 changes: 16 additions & 0 deletions bats_ai/core/tests/test_user_profile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from django.contrib.auth.models import User
from django.urls import reverse
import pytest

from bats_ai.core.models import UserProfile

from .factories import SuperuserFactory


@pytest.mark.django_db
def test_profile_creation():
Expand All @@ -11,3 +14,16 @@ def test_profile_creation():
user = User.objects.create()
profile = UserProfile.objects.get(user=user)
assert not profile.verified


@pytest.mark.django_db
def test_new_user_signup_email_sent(mailoutbox):
superuser = SuperuserFactory()
new_user = User.objects.create(
username='foo',
email='foo@bar.com',
)
message = next(filter(lambda message: 'New user signup' in message.subject, mailoutbox), None)
assert message is not None
assert superuser.email in message.to
assert reverse('admin:auth_user_change', args=[new_user.pk]) in message.body