Skip to content

Commit

Permalink
Send email to user after activation by administrator.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlorenzetti committed Sep 22, 2023
1 parent e8a3379 commit 451d2da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
46 changes: 46 additions & 0 deletions g3w-admin/usersmanage/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
from django.conf import settings
from django.dispatch import receiver
from django_registration.signals import user_registered
from django.template.loader import render_to_string
from django.contrib.sites.shortcuts import get_current_site
from usersmanage.models import Userbackend, USER_BACKEND_DEFAULT, Group as AuthGroup, Userdata
from usersmanage.configs import G3W_VIEWER1
from usersmanage.signals import after_save_user_form
import logging

logger = logging.getLogger('django.request')
Expand Down Expand Up @@ -52,3 +55,46 @@ def set_user_backend(sender, **kwargs):
# By default, add user to Viewer Level 1 group
AuthGroup.objects.get(name=G3W_VIEWER1).user_set.add(kwargs['user'])


@receiver(after_save_user_form)
def send_email_to_user(sender, **kwargs):
"""
Check if user is activated by administrator and send an email
of `activation confirmation`
"""

user = kwargs['user']

if (not hasattr(user, 'userdata') or
not sender.request.user.is_superuser or
user.userdata.activated_by_admin):
return

# Set activated by user
if user.is_active:
user.userdata.activated_by_admin = True
user.userdata.save()

# Send email to user
# ----------------------------------------------------------
scheme = "https" if sender.request.is_secure() else "http"
context = {
"scheme": scheme,
"site": get_current_site(sender.request),
"user": user
}
subject = render_to_string(
template_name='django_registration/activated_by_admin_email_subject.txt',
context=context,
request=sender.request,
)
# Force subject to a single line to avoid header-injection
# issues.
subject = "".join(subject.splitlines())
message = render_to_string(
template_name='django_registration/activated_by_admin_email_body.txt',
context=context,
request=sender.request,
)

user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Dear {{user.username}},

your profile was activated by administrators.

Now you can make the login!

{{ scheme }}://{{ site }}{% url 'login' %}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[G3W-SUITE] {{user.username}} your profile is active.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ Dear administrator,

a new user has registered, to activate the new user, follow or copy the link in your browser:

{{ scheme }}://{{ site }}{% url 'user-detail' user.pk %}
{{ scheme }}://{{ site }}{% url 'user-update' user.pk %}

0 comments on commit 451d2da

Please sign in to comment.