Browse Source

Adds email functionality

Jose Reyes 6 years ago
parent
commit
2beba4919c
3 changed files with 46 additions and 26 deletions
  1. 24
    25
      app/__init__.py
  2. 9
    0
      app/email.py
  3. 13
    1
      app/index.py

+ 24
- 25
app/__init__.py View File

@@ -1,32 +1,31 @@
1 1
 import os
2 2
 from flask import Flask
3
+from flask_mail import Mail
3 4
 
4
-def create_app(test_config=None):
5
-    app = Flask(__name__, instance_relative_config=True)
6
-    app.config.from_mapping(
7
-        SECRET_KEY='dev',
8
-        DATABASE=os.path.join(app.instance_path, 'upr_espera.sqlite'),
9
-    )
5
+app = Flask(__name__, instance_relative_config=True)
6
+app.config.from_mapping(
7
+    SECRET_KEY=os.environ.get('SECRET_KEY'),
8
+    DATABASE=os.path.join(app.instance_path, 'upr_espera.sqlite'),
9
+    MAIL_SERVER='smtp.googlemail.com',
10
+    MAIL_PORT=587,
11
+    MAIL_USE_TLS=1,
12
+    MAIL_USERNAME=os.environ.get('MAIL_USERNAME'),
13
+    MAIL_PASSWORD=os.environ.get('MAIL_PASSWORD'),
14
+)
15
+mail = Mail(app)
10 16
 
11
-    if test_config is None:
12
-        app.config.from_pyfile('config.py', silent=True)
13
-    else:
14
-        app.config.from_mapping(test_config)
17
+try:
18
+    os.makedirs(app.instance_path)
19
+except OSError:
20
+    pass
15 21
 
16
-    try:
17
-        os.makedirs(app.instance_path)
18
-    except OSError:
19
-        pass
22
+from . import db
23
+db.init_app(app)
20 24
 
21
-    from . import db
22
-    db.init_app(app)
25
+from . import index
26
+app.register_blueprint(index.bp)
27
+app.add_url_rule('/', endpoint='index')
23 28
 
24
-    from . import index
25
-    app.register_blueprint(index.bp)
26
-    app.add_url_rule('/', endpoint='index')
27
-
28
-    from . import admin_insert
29
-    app.register_blueprint(admin_insert.bp)
30
-    app.add_url_rule('/admin_insert', endpoint='admin_insert')
31
-
32
-    return app
29
+from . import admin_insert
30
+app.register_blueprint(admin_insert.bp)
31
+app.add_url_rule('/admin_insert', endpoint='admin_insert')

+ 9
- 0
app/email.py View File

@@ -0,0 +1,9 @@
1
+from flask_mail import Message
2
+from app import mail
3
+from app import app
4
+
5
+def send_email(subject, sender, recipients, text_body, html_body):
6
+    msg = Message(subject, sender=sender, recipients=recipients)
7
+    msg.body = text_body
8
+    msg.html = html_body
9
+    mail.send(msg)

+ 13
- 1
app/index.py View File

@@ -7,6 +7,8 @@ from werkzeug.exceptions import abort
7 7
 
8 8
 from app.db import get_db
9 9
 from app.forms import TurnForm
10
+from app.email import send_email
11
+from app import app
10 12
 
11 13
 bp = Blueprint('index', __name__)
12 14
 
@@ -36,7 +38,17 @@ def index():
36 38
         ).fetchone()
37 39
         db.commit()
38 40
 
39
-        flash('Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'], station_info['oficina'],  turn))
41
+        flash('Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'], station_info['oficina'], turn))
42
+
43
+        send_email('Your UPR turn',
44
+            sender=app.config['MAIL_USERNAME'],
45
+            recipients=[form.cEmail.data],
46
+            text_body='Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'],
47
+                station_info['oficina'],
48
+                turn),
49
+            html_body='<h3>Your turn for {} ({}) is number {}</h3>'.format(station_info['nombre_empleado'],
50
+                station_info['oficina'],
51
+                turn))
40 52
 
41 53
         return redirect(url_for('index'))
42 54