-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
131 lines (98 loc) · 3.47 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
KULDEEP SINGH
"""
from flask import Flask, request, g, render_template, Response, jsonify
from flask import session, flash, redirect, url_for, make_response
from flask_pymongo import PyMongo
from flask_mail import Mail, Message
import config_ext
import os
from celery import Celery
app = Flask(__name__)
# --- MongoDB configs. --- #
app.config['MONGO_DBNAME'] = config_ext.mongo_dbname
app.config['MONGO_URI'] = config_ext.mongo_uri
mongo = PyMongo(app)
# Flask-Mail configuration
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
app.config['MAIL_DEFAULT_SENDER'] = '[email protected]'
mail = Mail(app)
# Celery configuration
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
@app.route('/')
def index():
'''
Entry point.
'''
message = ""
return render_template("index.html", message=message)
@app.errorhandler(404)
def not_found(error):
'''
Handles if 404 occurs.
'''
return render_template("404.html")
@celery.task()
def send_async_emails(title, sender, recipients, body):
'''
Method finds subscribed users and sends them a mail about the
changes in the database.
'''
with app.app_context():
msg = Message(title, sender=sender,
recipients=recipients, body=body)
mail.send(msg)
@app.route('/subscribed', methods=['GET', 'POST'])
def subscribe():
'''
Subscription to the email notifications.
'''
if request.method == 'POST':
user_name = request.form['user_name']
user_email = request.form['user_email']
if request.form.getlist('activate_subscription'):
user_activated_subscription = True
else:
user_activated_subscription = False
db = mongo.db.MongoNotify
db.create_index("user_email", unique=True)
try:
db.insert(
{
"user_name": user_name,
"user_email": user_email,
"user_activated_subscription": user_activated_subscription
})
subscribers = db.find({"user_activated_subscription": True})
recipients = []
for s in subscribers:
user_info = list(s.values())
user_email = user_info[2]
recipients.append(user_email)
if recipients:
title = "Hi subscribed user"
sender = '[email protected]'
body = "There was a new entry in the database."
send_async_emails.delay(title, sender, recipients, body)
print (user_activated_subscription)
return render_template("subscribed.html", user_name=user_name,
user_activated_subscription=user_activated_subscription)
except:
message = "Email already taken."
return render_template("index.html", message=message)
return render_template("index.html")
@app.route('/unsubscribe', methods=['GET', 'POST'])
def unsubscribe():
'''
Unsubscribe from the notifications.
'''
return render_template("unsubscribe.html")
if __name__ == "__main__":
app.run()