-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.py
155 lines (113 loc) · 3.88 KB
/
Database.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
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
from core import *
MYSQL_USERNAME = 'root'
MYSQL_PASSWORD = ''
MYSQL_DATABASE = 'timeline'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://{}:{}@localhost'.format(MYSQL_USERNAME, MYSQL_PASSWORD)
app.config['SQLALCHEMY_POOL_RECYCLE'] = 3600
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class Penguin(db.Model):
__tablename__ = 'penguins'
__table_args__ = {
'autoload': True,
'schema': MYSQL_DATABASE,
'autoload_with': db.engine
}
class Avatar(db.Model):
__tablename__ = 'avatars'
__table_args__ = {
'autoload': True,
'schema': MYSQL_DATABASE,
'autoload_with': db.engine
}
class Membership(db.Model):
__tablename__ = 'memberships'
__table_args__ = {
'autoload': True,
'schema': MYSQL_DATABASE,
'autoload_with': db.engine
}
class Coin(db.Model):
__tablename__ = 'coins'
__table_args__ = {
'autoload': True,
'schema': MYSQL_DATABASE,
'autoload_with': db.engine
}
class Mail(db.Model):
__tablename__ = 'mails'
__table_args__ = {
'autoload': True,
'schema': MYSQL_DATABASE,
'autoload_with': db.engine
}
class Friend(db.Model):
__tablename__ = 'friends'
__table_args__ = {
'autoload': True,
'schema': MYSQL_DATABASE,
'autoload_with': db.engine
}
class Request(db.Model):
__tablename__ = 'requests'
__table_args__ = {
'autoload': True,
'schema': MYSQL_DATABASE,
'autoload_with': db.engine
}
class Ban(db.Model):
__tablename__ = 'bans'
__table_args__ = {
'autoload': True,
'schema': MYSQL_DATABASE,
'autoload_with': db.engine
}
from Session import *
from Firebase import *
from flask import send_file, request, abort, jsonify, Response
from flask_cors import CORS, cross_origin
import traceback
# rate limit to 1 per 5 seconds
@app.route('/flex/nickname/set/<nicknameKey>', methods=['POST'])
@cross_origin(supports_credentials=True)
@limiter.limit('1/5seconds')
def handleSetNickname(nicknameKey):
if not request.json:
return jsonify({'error': {'request': 'expected json'}})
data = request.json
if 'idToken' not in data or 'nickname' not in data:
return jsonify({'error': {'request.data': 'insufficient data'}})
tkn = data['idToken']
try:
userData = auth.verify_id_token(tkn, check_revoked=True)
userData = auth.get_user(userData['uid'])
user = Penguin.query.filter_by(email=userData.email).first()
if user is None:
raise Exception("User doesn't exist.")
swid = user.swid
key = app.redis.get('nickname_setup_key@{}'.format(swid))
if key is None or key != nicknameKey or user.nickname != '-_-':
# possible key expired?
return jsonify({'error': {'key.expired': 'Key possibly expired. Retry', 'nickname.set': 'failed'}})
errors = {}
# check nickname
data['nickname'] = data['nickname'].strip(' ')
nickname_w_space = data['nickname'].replace(' ', '')
if not nickname_w_space.isalnum():
errors['nickname.syntax'] = 'Nickname can contain only alphabets, numbers and spaces'
if not 3 < len(data['nickname']) < 13:
errors['nickname.length'] = 'Nickname should be a min of 4 and max of 12 letters (incl spaces)'
if len(errors) > 0:
errors['nickname.set'] = 'failed'
return jsonify({'error': errors})
user.nickname = data['nickname']
db.session.commit()
auth.update_user(userData.uid, display_name=data['nickname'])
return jsonify({'success': {'nickname.set': True}})
except Exception, e:
traceback.print_exc()
print 'Unable to verify account existance:', e
return jsonify({'error': {'auth.failed': str(e)}})