-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
168 lines (141 loc) · 4.85 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from flask import Flask, redirect, url_for, session, request
from flask import render_template
from flask_oauth import OAuth
from models import User, Group, UserGroup
SECRET_KEY = '123'
DEBUG = True
FACEBOOK_APP_ID = '1420298884864869'
FACEBOOK_APP_SECRET = 'f11b46f7a016760722b968de867d7432'
app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY
oauth = OAuth()
facebook = oauth.remote_app('facebook',
base_url='https://graph.facebook.com/',
request_token_url=None,
access_token_url='/oauth/access_token',
authorize_url='https://www.facebook.com/dialog/oauth',
consumer_key=FACEBOOK_APP_ID,
consumer_secret=FACEBOOK_APP_SECRET,
request_token_params={'scope': 'email'}
)
def get_context():
data = session.get('data')
if data:
user = User.get_or_create(email=data['email'])
invite_groups = user.get_invite_groups()
if 'work' in data:
employer = data['work'][0]['employer']['name']
position = data['work'][0]['position']['name']
else:
employer = user.employer
position = user.position
if 'location' in data:
location = data['location']['name']
else:
location = user.location
return {
'user': user,
'email': user.email or data['email'],
'name': user.name or data['name'],
'employer': employer,
'position': position,
'location': location,
'bio': user.bio,
'data': data,
'photo_url': 'https://graph.facebook.com/%s/picture' % data['id'],
}
return {}
@app.route("/profile", methods=['POST', 'GET'])
def profile():
context = get_context()
context.update(dict(success=False))
if request.method == 'POST':
user = User.get_or_create(email=context['email'])
user.employer = request.form['employer']
user.position = request.form['position']
user.location = request.form['location']
user.bio = request.form['bio']
user.save()
context = get_context()
context.update(dict(success=True))
print request.form
return render_template('profile.html', **context)
@app.route("/create", methods=['POST', 'GET'])
def create():
context = get_context()
context.update(dict(success=False))
user = User.get_or_create(email=context['email'])
all_users = User.get_all(exclude_user_id=user.id)
context.update({'all_users': list(all_users)})
if request.method == 'POST':
group = Group.get_or_create(name=request.form['group_name'], status='Pending')
UserGroup.get_or_create(
user_id=user.id,
group_id=group.id,
loan_amount=request.form['loan_amount'],
repaid_amount='0',
)
all_users = User.get_all(exclude_user_id=user.id)
for _user in all_users:
UserGroup.get_or_create(
user_id=_user.id,
group_id=group.id,
loan_amount='0',
repaid_amount='0',
)
context.update(dict(success=True))
return render_template('create.html', **context)
@app.route("/receipt", methods=['POST', 'GET'])
def receipt():
context = get_context()
context.update(dict(success=False))
return render_template('receipt.html', **context)
@app.route("/loans", methods=['POST', 'GET'])
def loans():
context = get_context()
return render_template('loans.html', **context)
@app.route("/requests", methods=['POST', 'GET'])
def requests():
context = get_context()
return render_template('requests.html', **context)
@app.route("/")
def index():
context = get_context()
return render_template('index.html', **context)
@app.route("/iframe")
def iframe():
return render_template('iframe.html')
@facebook.tokengetter
def get_facebook_token():
return session.get('facebook_token')
def pop_login_session():
session.pop('logged_in', None)
session.pop('facebook_token', None)
session.pop('data', None)
@app.route("/facebook_login")
def facebook_login():
return facebook.authorize(callback=url_for('facebook_authorized',
next=request.args.get('next'), _external=True))
@app.route("/facebook_authorized")
@facebook.authorized_handler
def facebook_authorized(resp):
next_url = request.args.get('next') or url_for('index')
if resp is None or 'access_token' not in resp:
return redirect(next_url)
session['logged_in'] = True
session['facebook_token'] = (resp['access_token'], '')
data = facebook.get('/me').data
session['data'] = data
User.get_or_create(
email=data['email'],
name=data['name'],
facebook_id=data['id'],
)
return redirect(url_for('index'))
@app.route("/logout")
def logout():
pop_login_session()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run()