Skip to content

Commit

Permalink
Remove signals, apply dry, change naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
varun kumar committed Oct 2, 2023
1 parent f06f8ee commit 8e1770c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 29 deletions.
11 changes: 9 additions & 2 deletions allauth/account/adapter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import html
import json
import warnings
from datetime import timedelta
from datetime import datetime, timedelta
from urllib.parse import urlparse

from django import forms
Expand Down Expand Up @@ -680,7 +680,14 @@ def get_login_stages(self):
return ret

def send_notification_mail(self, template_prefix, user, context):
self.send_mail(template_prefix, user.email, context)
if app_settings.ACCOUNT_EMAIL_NOTIFICATIONS:
context |= {
"timestamp": datetime.now(),
"ip": self.get_client_ip(self.request),
"browser_agent": self.get_browser_user_agent(self.request),
}
print(context)
self.send_mail(template_prefix, user.email, context)


def get_adapter(request=None):
Expand Down
4 changes: 4 additions & 0 deletions allauth/account/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ def PASSWORD_RESET_TOKEN_GENERATOR(self):
def REAUTHENTICATION_TIMEOUT(self):
return self._setting("REAUTHENTICATION_TIMEOUT", 300)

@property
def ACCOUNT_EMAIL_NOTIFICATIONS(self):
return self._setting("ACCOUNT_EMAIL_NOTIFICATIONS", True)


_app_settings = AppSettings("ACCOUNT_")

Expand Down
4 changes: 0 additions & 4 deletions allauth/account/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,3 @@ def ready(self):
raise ImproperlyConfigured(
f"{required_mw} must be added to settings.MIDDLEWARE"
)
from .receivers import send_password_change_notification
from .signals import password_changed

password_changed.connect(send_password_change_notification)
18 changes: 0 additions & 18 deletions allauth/account/receivers.py

This file was deleted.

6 changes: 5 additions & 1 deletion allauth/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,15 @@ def get_form_kwargs(self):
def form_valid(self, form):
form.save()
logout_on_password_change(self.request, form.user)
get_adapter(self.request).add_message(
adapter = get_adapter(self.request)
adapter.add_message(
self.request,
messages.SUCCESS,
"account/messages/password_changed.txt",
)
adapter.send_notification_mail(
"account/email/password_changed", self.request.user, {}
)
signals.password_changed.send(
sender=self.request.user.__class__,
request=self.request,
Expand Down
5 changes: 4 additions & 1 deletion allauth/templates/account/email/base_notification.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% load i18n %}
{% load account %}
{% user_display user as user_display %}

Hello {{ username }},
Hello {{ user_display }},

We wanted to let you know that there has been a recent change to your account information.

Expand All @@ -10,6 +12,7 @@ Here are the details of the change:
{% endblock %}

Change details
Time: {{timestamp}}
IP address : {{ip}}
Browser agent : {{browser_agent}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block content %}{% blocktrans %}Your Password has been changed

Changes made by {{ username }}.{% endblocktrans %}{% endblock content %}
{% block content %}{% blocktrans %}Your Password has been changed{% endblocktrans %}{% endblock content %}

0 comments on commit 8e1770c

Please sign in to comment.