API: Added A Route To Send Validation Emails.

See /api/help
This commit is contained in:
Sagi Dayan 2015-06-10 18:47:53 +03:00
parent 7830feb887
commit 3c5eba4fea
2 changed files with 82 additions and 3 deletions

View file

@ -41,6 +41,39 @@ def page_not_found(e):
def wellcomePage(): def wellcomePage():
return app.send_static_file('index.html') return app.send_static_file('index.html')
@app.route('/api/validatation/sendmail/<string:token>', methods=['POST'])
@auto.doc()
def send_activation(token):
"""
This Method Will Send An Email To The User - To Confirm his Account
:param token: - seToken
:payload: JSON - {email: 'academic@email.ac.com'}
:return:
200 - Email Sent - No Response
400 - Bad Request
403 - Invalid Token
"""
if not request.data:
return Response(response=json.dumps({'message': 'Bad Request'}),
status=400,
mimetype="application/json")
payload = json.loads(request.data)
if not is_user_token_valid(token):
return Response(response=json.dumps({'message': 'Not A Valid Token!'}),
status=403,
mimetype="application/json")
query = User.all()
query.filter('seToken =', token)
for u in query.run(limit=1):
try:
send_validation_email(token=token, name=u.username, email=payload["email"])
except Exception:
return Response(response=json.dumps({'message': 'Bad Request'}),
status=400,
mimetype="application/json")
return Response(status=200)
@app.route('/api/help') @app.route('/api/help')
def documentation(): def documentation():
return auto.html() return auto.html()
@ -135,7 +168,7 @@ def get_campuses(token):
.... ....
{ {
... ...
} }req
] ]
code 403: Forbidden - Invalid Token code 403: Forbidden - Invalid Token

View file

@ -1,6 +1,8 @@
__author__ = 'sagi' __author__ = 'sagi'
from google.appengine.ext import db from google.appengine.ext import db
from models.User import User from models.User import User
from google.appengine.api import mail
def is_user_token_valid(token): def is_user_token_valid(token):
query = User.all() query = User.all()
@ -9,3 +11,47 @@ def is_user_token_valid(token):
for u in query.run(): for u in query.run():
return True return True
return False return False
def send_validation_email(token, email, name):
message = mail.EmailMessage(sender="SE-Hub Support <se-hub@appspot.gserviceaccount.com>",
subject="SE-Hub Activate Account")
message.to = email
message.body = """
Dear """+name+""":
To Activate your SE-Hub Account please click on the link below:<br>
http://se-hub.appspot.com/api/validatation/confirm/"""+token+"""
to access you virtual class.
Please let us know if you have any questions.
SE-Hub (c) 2015 niptop Team.
"""
message.html = """
<html><head></head><body>
<div>
<center>
<img src='https://cloud.githubusercontent.com/assets/2984053/6825467/7c9d0402-d303-11e4-9827-62a6d66f937a.png'>
</center>
</div>
<div style='width:70%'>
<h1>Hey """+name+"""- Just one More Step...</h1>
<h3>Dear """+name+""":</h3>
To Activate your SE-Hub Account please click on the link below:<br>
http://se-hub.appspot.com/api/validatation/confirm/"""+token+"""<br><br>
to access you virtual class.
</div>
<br><br>
Please let us know if you have any questions.
<br>
SE-Hub (c) 2015 niptop Team.
</body>
</html>
"""
message.send()