Procházet zdrojové kódy

tiene admin_remove

Tatiana Castro před 6 roky
rodič
revize
c7bfde9db2
3 změnil soubory, kde provedl 55 přidání a 35 odebrání
  1. 33
    34
      app/__init__.py
  2. 9
    0
      app/email.py
  3. 13
    1
      app/index.py

+ 33
- 34
app/__init__.py Zobrazit soubor

@@ -1,36 +1,35 @@
1 1
 import os
2 2
 from flask import Flask
3
-
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
-    )
10
-
11
-    if test_config is None:
12
-        app.config.from_pyfile('config.py', silent=True)
13
-    else:
14
-        app.config.from_mapping(test_config)
15
-
16
-    try:
17
-        os.makedirs(app.instance_path)
18
-    except OSError:
19
-        pass
20
-
21
-    from . import db
22
-    db.init_app(app)
23
-
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
-    from . import admin_remove
33
-    app.register_blueprint(admin_remove.bp)
34
-    app.add_url_rule('/admin_remove', endpoint='admin_remove')
35
-
36
-    return app
3
+from flask_mail import Mail
4
+
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)
16
+
17
+try:
18
+    os.makedirs(app.instance_path)
19
+except OSError:
20
+    pass
21
+
22
+from . import db
23
+db.init_app(app)
24
+
25
+from . import index
26
+app.register_blueprint(index.bp)
27
+app.add_url_rule('/', endpoint='index')
28
+
29
+from . import admin_insert
30
+app.register_blueprint(admin_insert.bp)
31
+app.add_url_rule('/admin_insert', endpoint='admin_insert')
32
+
33
+from . import admin_remove
34
+app.register_blueprint(admin_remove.bp)
35
+app.add_url_rule('/admin_remove', endpoint='admin_remove')

+ 9
- 0
app/email.py Zobrazit soubor

@@ -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 Zobrazit soubor

@@ -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