-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
305 lines (248 loc) · 9.62 KB
/
server.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import jwt
import base64
import os
import uuid
from collections import Counter
from datetime import datetime, timedelta
from functools import wraps
from flask import Flask, jsonify, render_template, request, _request_ctx_stack
from flask_sqlalchemy import SQLAlchemy
from flask.ext.cors import cross_origin
from werkzeug.local import LocalProxy
from dotenv import Dotenv
from auth import authenticate, requires_auth
from languages import languages
from translation.random_translation import translate
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.sqlite')
db = SQLAlchemy(app)
env = None
try:
env = Dotenv('./.env')
client_id = env["AUTH0_CLIENT_ID"]
client_secret = env["AUTH0_CLIENT_SECRET"]
except IOError:
env = os.environ
# Authentication annotation
current_user = LocalProxy(lambda: _request_ctx_stack.top.current_user)
def verify_or_create_user():
id_service, user_id = current_user['sub'].split('|')
user = User.query.filter_by(auth0_id=user_id).first()
if user is None:
user = User(user_id)
db.session.add(user)
db.session.commit()
return user
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
auth0_id = db.Column(db.Integer)
points = db.Column(db.Integer)
def __init__(self, auth0_id):
self.auth0_id = auth0_id
self.points = 0
@property
def serialize(self):
return {
'id': self.id,
'auth0Id': self.auth0_id,
'points': self.points
}
class Language_Pair(db.Model):
__tablename__ = 'languages'
id = db.Column(db.Integer, primary_key=True)
from_language = db.Column(db.String(80))
to_language = db.Column(db.String(80))
owner = db.Column(db.Integer)
def __init__(self, to_language, from_language, owner):
self.to_language = to_language
self.from_language = from_language
self.owner = owner
@property
def serialize(self):
return {
'id': self.id,
'toLanguage': self.to_language,
'fromLanguage': self.from_language,
'owner': self.owner,
}
class Lexeme(db.Model):
__tablename__ = 'lexicon'
id = db.Column(db.Integer, primary_key=True)
lexeme = db.Column(db.Text)
from_language = db.Column(db.String(80))
to_language = db.Column(db.String(80))
translation = db.Column(db.Text)
def __init__(self, lexeme, from_language, to_language):
self.lexeme = lexeme
self.from_language = from_language
self.to_language = to_language
self.translation = translate(lexeme, from_language, to_language)
@property
def serialize(self):
return {
'id': self.id,
'lexeme': self.lexeme,
'translation': self.translation,
'fromLanguage': self.from_language,
'toLanguage': self.to_language
}
class User_Lexeme(db.Model):
__tablename__ = 'user_lexicon'
id = db.Column(db.Integer, primary_key=True)
lexeme = db.Column(db.Text)
from_language = db.Column(db.String(80))
to_language = db.Column(db.String(80))
translation = db.Column(db.Text)
lexeme_count = db.Column(db.Integer)
success_count = db.Column(db.Integer)
owner = db.Column(db.Integer)
created_at = db.Column(db.DateTime)
active_after = db.Column(db.DateTime)
last_success = db.Column(db.DateTime)
def __init__(self, lexeme, from_language, to_language, translation, lexeme_count, owner):
utc_now = datetime.utcnow()
self.lexeme = lexeme
self.translation = translation
self.from_language = from_language
self.to_language = to_language
self.lexeme_count = lexeme_count
self.success_count = 0
self.owner = owner
self.created_at = utc_now
self.last_success = utc_now
self.active_after = utc_now
@property
def serialize(self):
return {
'id': self.id,
'lexeme': self.lexeme,
'translation': self.translation,
'fromLanguage': self.from_language,
'toLanguage': self.to_language,
'lexemeCount': self.lexeme_count,
'successCount': self.success_count,
#'createdAt': self.created_at,
#'lastSuccess': self.last_success,
'activeAfter': self.active_after
}
# the base route which renders a template
@app.route('/')
def index ():
return render_template('index.html')
def flashcard_deck (user):
now = datetime.utcnow()
deck_size = 10
lexemes = User_Lexeme \
.query \
.filter(User_Lexeme.owner==user.id) \
.filter(User_Lexeme.active_after<=now) \
.order_by(User_Lexeme.lexeme_count.desc()) \
.limit(deck_size)
return [lexeme.serialize for lexeme in lexemes]
@app.route('/flashcards', methods=['GET'])
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def get_flashcards ():
user = verify_or_create_user()
flashcards = flashcard_deck(user)
return jsonify({'flashcards': flashcards})
@app.route('/flashcards', methods=['POST'])
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def verify_flashcards ():
user = verify_or_create_user()
payload = request.get_json()
lexeme_ids = [lexeme['id'] for lexeme in payload['lexemes']]
for id in lexeme_ids:
lexeme = User_Lexeme.query.filter(User_Lexeme.id==id).first()
User_Lexeme \
.query \
.filter(User_Lexeme.id==id) \
.update({"success_count": lexeme.success_count + 1, "active_after": datetime.utcnow() + timedelta(minutes=5**lexeme.success_count + 2)}, synchronize_session=False)
db.session.commit()
user.points = user.points + 10
db.session.commit()
flashcards = flashcard_deck(user)
return jsonify({'flashcards': flashcards})
@app.route('/languages')
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def get_languages ():
user = verify_or_create_user()
langs = [{"name": lang, "abbreviation": languages[lang]} for lang in languages]
user_langs = [language.from_language for language in Language_Pair.query.filter_by(owner=user.id).all()]
for lang in langs:
if lang['abbreviation'] in user_langs:
lang['active'] = True
else:
lang['active'] = False
return jsonify({"languages": langs})
@app.route('/languages/create', methods=['POST'])
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def create_language ():
user = verify_or_create_user()
payload = request.get_json()
from_language = payload['fromLanguage']
to_language = payload['toLanguage']
if not from_language in languages and to_language in languages:
# Should actually return a 400 or maybe 412
return jsonify({'success': False})
prior = Language_Pair.query.filter_by(to_language=to_language, from_language=from_language, owner=user.id).first()
if not prior:
new_language_pair = Language_Pair(to_language, from_language, user.id)
db.session.add(new_language_pair)
db.session.commit()
return jsonify({'success': True})
@app.route('/lexemes')
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def get_lexemes ():
user = verify_or_create_user()
lexemes = [lexeme.serialize for lexeme in User_Lexeme.query.filter_by(owner=user.id).order_by(User_Lexeme.lexeme_count.desc()).all()]
return jsonify({'lexemes': lexemes})
@app.route('/lexemes/<lexeme_id>')
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def get_lexeme (lexeme_id):
"""Return a Lexeme of a given ID or 404"""
lexeme = Lexeme.query.get(lexeme_id)
if lexeme:
return jsonify({'lexeme': lexeme.serialize})
# Should actually return a 404 not None
return jsonify({'lexeme': None})
@app.route('/lexemes/create', methods=['POST'])
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def create_lexeme ():
user = verify_or_create_user()
payload = request.get_json()
lexemes = [lex.strip().lower() for lex in payload['lexemes'].replace('.', '').split(" ") if lex is not '']
counted_lexemes = Counter(lexemes)
to_language = 'english'
from_language = 'spanish'
if not lexemes[0]:
# Should actually return a 400 or maybe 412
return jsonify({'success': False})
for lex in counted_lexemes:
prior = Lexeme.query.filter_by(lexeme=lex, from_language=from_language, to_language=to_language).first()
if not prior:
new_lexeme = Lexeme(lex, from_language, to_language)
db.session.add(new_lexeme)
db.session.commit()
user_lexemes = [User_Lexeme(lex, to_language, from_language, translate(lex, from_language, to_language), counted_lexemes[lex], user.id) for lex in counted_lexemes]
# this is about the worst way imaginable to do this, but it'll work for the time being
# do not deploy to prod before rewriting this db interaction!
for lex in user_lexemes:
prior = User_Lexeme.query.filter_by(lexeme=lex.lexeme, owner=user.id, from_language=lex.from_language, to_language=lex.to_language).first()
if not prior:
# we should actually pull the lexeme table query into this conditional branch
db.session.add(lex)
else:
prior.lexeme_count = prior.lexeme_count + lex.lexeme_count
db.session.commit()
return jsonify({'success': True})
if __name__ == '__main__':
app.run(debug=True)