Browse Source

[ADD] email notifications

Gogs 7 years ago
parent
commit
11c5b2a118
4 changed files with 55 additions and 0 deletions
  1. 8 0
      .env
  2. 13 0
      api/resources/jwt_resource.py
  3. 26 0
      api/utils/email.py
  4. 8 0
      odoo_control/settings.py

+ 8 - 0
.env

@@ -1,6 +1,14 @@
 SECRET_KEY = '^*&s%i#9p7tq(#%f)--#ki2*qx8=iv5173eg35$qupv8%+fyv7'
 ALLOWED_HOSTS = *
 DEBUG = True
+EMAIL_HOST = 'ns5.serverpy.com'
+EMAIL_PORT = 465
+EMAIL_HOST_USER = 'robert@eiru.com.py'
+EMAIL_HOST_PASSWORD = '@3040/Robert'
+EMAIL_USE_SSL = True
+SEND_EMAIL = True
+EMAIL_FROM = 'admin@eiru.com.py'
+SYSTEM_NAME = 'Sistema de Automatización de Eiru'
 JWT_ACCEPT_HEADER = 'HTTP_AUTHORIZATION'
 JWT_PREFIX_HEADER = 'JWT'
 JWT_SECRET_KEY = '123456789'

+ 13 - 0
api/resources/jwt_resource.py

@@ -10,6 +10,7 @@ from api.utils.logger import (
     info,
     warning
 )
+from api.utils.email import send_email
 import simplejson as json
 
 '''
@@ -56,6 +57,18 @@ class JWTResource(Resource):
 
         info('%s authenticated' % body['username'])
 
+        # Send email
+        email_sended = send_email(
+            'El usuario %s se autenticó al sistema con un nuevo token.\n%s' % (body['username'], token),
+            body['username']
+        )
+
+        if not email_sended:
+            warning('%s email not sended' % body['username'])
+        else:
+            info('%s email sended' % body['username'])
+
+        # Build bundle
         bundle = self.build_bundle(obj={
             'token': token,
             'username': body['username']

+ 26 - 0
api/utils/email.py

@@ -0,0 +1,26 @@
+from django.core.mail import send_mail
+from django.conf import settings
+from django.contrib.auth.models import User
+
+'''
+'''
+def send_email(message, username):
+    if not username:
+        return False
+    
+    if not settings.SEND_EMAIL:
+        return False
+
+    user = User.objects.get(username=username)
+
+    if not user:
+        return False
+
+    send_mail(
+        settings.SYSTEM_NAME,
+        message,
+        settings.EMAIL_FROM,
+        [user.email]
+    )
+
+    return True

+ 8 - 0
odoo_control/settings.py

@@ -82,6 +82,14 @@ ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv())
 CORS_ORIGIN_ALLOW_ALL = config('DEBUG', default=False, cast=bool)
 CORS_ALLOW_CREDENTIALS = True
 DEBUG = config('DEBUG', default=False, cast=bool)
+EMAIL_HOST = config('EMAIL_HOST', 'localhost')
+EMAIL_PORT = config('EMAIL_PORT', 25)
+EMAIL_HOST_USER = config('EMAIL_HOST_USER')
+EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
+EMAIL_USE_SSL = config('EMAIL_USE_SSL', cast=bool)
+SEND_EMAIL = config('SEND_EMAIL', cast=bool)
+EMAIL_FROM = config('EMAIL_FROM')
+SYSTEM_NAME = config('SYSTEM_NAME')
 JWT_ACCEPT_HEADER = config('JWT_ACCEPT_HEADER')
 JWT_PREFIX_HEADER = config('JWT_PREFIX_HEADER')
 JWT_SECRET_KEY = config('JWT_SECRET_KEY')