-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
149 lines (108 loc) · 3.17 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# the above line is to avoid 'SyntaxError: Non-UTF-8 code starting with' error
'''
Created on
Course work:
@author: raja
Source:
https://overiq.com/flask-101/sessions-in-flask/
https://stackoverflow.com/questions/26080872/secret-key-not-set-in-flask-session-using-the-flask-session-extension
https://randomkeygen.com/
https://pythonhosted.org/Flask-Session/ - this is buggy and deprecated
'''
# Import necessary modules
from flask import Flask
import json
from flask import jsonify
from functools import wraps
from flask import Flask, render_template, request, redirect, url_for, flash, make_response, session
app = Flask(__name__)
app.secret_key = "LaEt{B07|MCCtC0.(OGw0B[-AUTs"
SESSION_KEY = "user_session"
def requires_session(f):
@wraps(f)
def decorated(*args, **kwargs):
# print('inside deco')
# check apikey in args
if SESSION_KEY not in session:
# print('session not available')
data = {
'apiresult' : 'Session Not Available',
'apimessage': 1011
}
return jsonify(data)
# print('session is available')
# verify user_session
user_session = session.get(SESSION_KEY)
return f(*args, **kwargs)
return decorated
'''
http://127.0.0.1:5000/add-visit/
'''
@app.route('/add-visit/')
def visits():
if 'visits' in session:
session['visits'] = session.get('visits') + 1 # reading and updating session data
else:
session['visits'] = 1 # setting session data
return "Total visits: {}".format(session.get('visits'))
'''
http://127.0.0.1:5000/delete-visit/
'''
@app.route('/delete-visit/')
def delete_visits():
session.pop('visits', None) # delete visits
return 'Visits deleted'
'''
http://127.0.0.1:5000/visits-counter/
'''
@app.route('/visits-counter/')
def count():
if 'visits' in session:
session['visits'] = session.get('visits') # reading and updating session data
else:
session['visits'] = 0 # setting session data
return "Total visits: {}".format(session.get('visits'))
'''
http://localhost:5000/
'''
@app.route('/')
def entry_point():
return 'Hello World!'
'''
http://localhost:5000/content
'''
@app.route('/content')
@requires_session # Decorator has to be close to the method
def user_content():
return 'User Content'
'''
http://localhost:5000/login
http://localhost:5000/[email protected]&password=abcd
http://localhost:5000/login?email=one&password=two
'''
@app.route('/login')
def login():
email = request.values.get('email')
password = request.values.get('password')
if(email == '[email protected]' and password == 'abcd'):
# set session
session[SESSION_KEY] = 'session_key_raja'
return "Success"
return 'Login Failed'
'''
http://localhost:5000/get/session
'''
@app.route('/get/session')
def get_session():
return session[SESSION_KEY]
'''
http://localhost:5000/logout
'''
@app.route('/logout')
def logout():
del session[SESSION_KEY]
return "Logged out"
if __name__ == '__main__':
app.run(debug=True)