-
Notifications
You must be signed in to change notification settings - Fork 1
Send email to admin on new user signup #366
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,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): | ||||||||
|
|
@@ -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] | ||||||||
|
|
||||||||
| if recipient_list: | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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( | ||||||||
brianhelba marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| subject=f'{current_site.name}: New user signup', | ||||||||
| message=email_content, | ||||||||
| from_email=None, | ||||||||
| recipient_list=recipient_list, | ||||||||
| ) | ||||||||
This file contains hidden or 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,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 %} |
This file contains hidden or 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.
FYI, if you wanted to hyper-optimize this, you could use
.values_list()to directly get the desired list of strings:However, given the size of the QuerySet here (~3 admin users), there's no problem with constructing the intermediate
Usermodel and accessing the.emailproperty. 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.