Advertisement
Guest User

Code

a guest
May 23rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. """
  2. This is the admin dashboard for SwitchCaseHub, and the purpose of this dashboard is for admins to easily manage users and projects
  3. """
  4. # Project Name: SwitchCaseHub Admin
  5. # Project Description: Handle users for SwitchCaseHub
  6. # File for handling view
  7.  
  8. #import all necessary files
  9. import os
  10. import ast
  11. import json
  12. import requests
  13. import jwt
  14. from flask import Blueprint,request,url_for,current_app, render_template, session, redirect, url_for
  15. from flask_json import json_response
  16. from model import Contact
  17. from utils import validate_
  18. from extensions import db
  19.  
  20. web_view = Blueprint('web_view', __name__)#create a blueprint structure of the flask class
  21. is_next_link = 1;
  22. msgs = [];
  23.  
  24. def validate_jwt_identity_token(token):
  25. """This Function is used to verify jwt tokens that re used as a replacment for sessions,
  26. especially for the web """
  27.  
  28. try:
  29.  
  30. key = current_app.config['JWT_SESSIONS']
  31.  
  32. decoded=jwt.decode(token, key, algorithms=['HS256'])
  33.  
  34. print decoded
  35.  
  36. if decoded['name'] and decoded['email'] and decoded['mobile'] and decoded['account_level']:
  37.  
  38. return decoded['name'],decoded['email'],decoded['mobile'], decoded['account_level']
  39.  
  40. except Exception,e:
  41.  
  42. print e
  43. return False
  44.  
  45. @web_view.route("/<string:token>")
  46. def home_page(token):
  47.  
  48. print token
  49. #validate jwt tokens.... and get all the data needed
  50. tok = validate_jwt_identity_token(token)
  51.  
  52. if tok != False:
  53.  
  54. return render_template("master.html",user={"fullname" : tok[0],"token" : token})
  55.  
  56. else:
  57.  
  58. return redirect(url_for("web_view.login_page"))
  59.  
  60. def load_contact_msgs(page=1):
  61. """this function is used to fetch contact pages from d db"""
  62. contact = Contact.query.order_by(Contact.id.desc()).paginate(page,10,error_out=False).items
  63.  
  64. return contact
  65.  
  66. @web_view.route("/mail/<string:token>")
  67. def contact__msg_page(token):
  68.  
  69. print token
  70. #validate jwt tokens.... and get all the data needed
  71. tok = validate_jwt_identity_token(token)
  72. next_contact_page = is_next_link
  73.  
  74. is_next = request.args.get('next') or None
  75.  
  76. if tok != False:
  77.  
  78. #get contact us messages and populate the file
  79. if is_next is not None:
  80.  
  81. print "gtt"
  82. contact_msgs = load_contact_msgs(next_contact_page)
  83.  
  84. for contact_msg in contact_msgs:
  85.  
  86. contact_msg.message = contact_msg.message.replace("\n","<br>")
  87. msgs.append(contact_msg)
  88. print contact_msg.message
  89.  
  90. return render_template("mail.html",user={"fullname" : tok[0],"token" : token}, messages = msgs, num_id = "")
  91.  
  92. else:
  93.  
  94. is_next = None;
  95. is_next_link = 1;
  96.  
  97. contact_msgs = load_contact_msgs(next_contact_page)
  98.  
  99. for contact_msg in contact_msgs:
  100.  
  101. contact_msg.message = contact_msg.message.replace("\n","<br>")
  102. msgs.append(contact_msg)
  103. print contact_msg.message
  104.  
  105. return render_template("mail.html",user={"fullname" : tok[0],"token" : token}, messages = msgs, num_id = is_next_link)
  106.  
  107. else:
  108.  
  109. return redirect(url_for("web_view.login_page",next_page="web_view.contact__msg_page"))
  110.  
  111.  
  112. @web_view.route("/login")
  113. def login_page():
  114.  
  115. next_page = request.args.get('next_page') or "web_view.home_page"
  116. print next_page
  117. return render_template("signin.html",email = "", password = "", next_page = next_page)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement