SHARE
TWEET

Untitled

a guest Jul 11th, 2019 143 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. def sendThatEmail(eRecipient, eSubject, eMessage):
  3.     #this is where we send the welcome email
  4.     message = Mail(
  5.         from_email = DEVELOPER_EMAIL,
  6.         to_emails = eRecipient,
  7.         subject = eSubject,
  8.         html_content = eMessage)
  9.    
  10.     sg = SendGridAPIClient(SENDGRID_API_KEY)
  11.     response = sg.send(message)
  12.     return True
  13.  
  14. @app.route('/')
  15. def home():
  16.     return render_template('home.html')
  17.  
  18. @app.route('/resetpassword', methods = ['GET','POST'])
  19. def resetPassword():
  20.     if request.method == 'POST':
  21.         e = request.form['resetemail']
  22.         t = (e)
  23.         cursor = db.cursor()
  24.         query = "SELECT u_password FROM users WHERE u_email = %s"
  25.         cursor.execute(query, t)
  26.         results = cursor.fetchall()
  27.         totalCount = len(results)
  28.  
  29.         if totalCount == 1:
  30.             #we generate a new password here
  31.             np = str(random.random())
  32.             np = np[10:]
  33.  
  34.             t = (np, e)
  35.             cursor = db.cursor()
  36.             query = "UPDATE users SET u_password = %s WHERE u_email = %s"
  37.             cursor.execute(query, t)
  38.             db.commit()
  39.             #p = results[0][0] #we get the password from the result set
  40.             #we send the email here
  41.             sendThatEmail(e, 'Your Requested Password', 'Your password is: ' + np)
  42.             flash('Check your inbox for your password.')
  43.         else:
  44.             flash('Your email address does not exist.')
  45.    
  46.     return redirect('/login')
  47.  
  48. @app.route('/login', methods = ['POST','GET'])
  49. def login():
  50.     if request.method == 'POST':
  51.         #We process login username and password
  52.         u = request.form['username']
  53.         p = request.form['password']
  54.  
  55.         #t = (u, p)
  56.         t = (u, u, p)
  57.  
  58.         cursor = db.cursor()
  59.         #query = "SELECT * FROM users WHERE u_username = %s AND u_password = %s"
  60.         query = "SELECT * FROM users WHERE (u_username = %s OR u_email = %s) AND u_password = %s"
  61.         cursor.execute(query, t)
  62.         results = cursor.fetchall()
  63.  
  64.         totalCount = len(results)   #if total results returned is 1, successful login
  65.  
  66.         if totalCount == 1:
  67.             session['username'] = u
  68.             flash('Login was successful. Welcome ' + u + '! ')
  69.             return redirect('/dashboard')
  70.         else:
  71.             flash('Bad credentials. Please try again.')
  72.             return redirect('/login')
  73.  
  74.     return render_template('login.html')
  75.  
  76. @app.route('/dashboard', methods = ['GET','POST'])
  77. def dashboard():
  78.     if 'username' in session:
  79.         '''
  80.         if request.method == 'GET':
  81.             sdDate = request['gamedate']
  82.         else:
  83.             sdDate = 'No Date Selected'
  84.         '''
  85.         #we connect to the sportsdata.io API here to get data for the dashboard
  86.         URL = 'https://api.sportsdata.io/v3/nba/scores/json/GamesByDate/2019-FEB-1?key=ee023fa1e20045ed854d1abfdb5214a5'
  87.         response = requests.get(URL)
  88.         sd = json.loads(response.text)
  89.         #end connection to sportsdata.io
  90.         return render_template('dashboard.html', **locals())
  91.     else:
  92.         return redirect('/login')
  93.    
  94.     return redirect('/login')
  95.  
  96. @app.route('/logout')
  97. def logout():
  98.     session.pop('username', None)
  99.     flash('You were logged out successfully.')
  100.     return redirect('/')
  101.  
  102. @app.route('/signup')
  103. def signup():
  104.     if 'username' in session:
  105.         redirect('/dashboard')
  106.    
  107.     return render_template('signup.html')
  108.  
  109. @app.route('/register', methods = ['POST','GET'])
  110. def register():
  111.     #we do form processing here
  112.     if request.method == 'POST':
  113.         e = request.form['email']
  114.         u = request.form['username']
  115.         p = request.form['password']
  116.         #we will save the info above into the DB
  117.         t = (e, u)
  118.         cursor = db.cursor()
  119.         query = "SELECT * FROM users WHERE u_email = %s OR u_username = %s"
  120.         cursor.execute(query, t)
  121.         results = cursor.fetchall()
  122.  
  123.         verifyCount = len(results)
  124.        
  125.         if verifyCount == 0:
  126.             t = (e, u, p)
  127.             cursor = db.cursor()
  128.             query = "INSERT INTO users (u_email, u_username, u_password ) VALUES (%s, %s, %s)"
  129.             cursor.execute(query, t)
  130.             db.commit() #this function makes the change permanent to the table
  131.             flash('You were registered successfully. Please login.')
  132.            
  133.             sendThatEmail(e, 'Welcome to The Oracle', 'Welcome! <br/> Your registration was successful.')
  134.             flash('Check your email inbox for a welcome email.')
  135.             #end of sending the welcome email
  136.  
  137.             return redirect('/login')
  138.         else:
  139.             flash('There was a duplicate email or username. Please try again.')
  140.  
  141.     else:
  142.         flash('Your registration failed.')
  143.    
  144.     return redirect('/signup')
  145.  
  146. if __name__ == '__main__':
  147.     app.run(debug=True)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top