-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
317 lines (240 loc) · 9.91 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
from flask import Flask, Blueprint, request, jsonify, make_response, render_template, url_for, flash, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin, expose, AdminIndexView
from flask_admin.contrib.sqla import ModelView
from flask_admin.menu import MenuLink
from flask_login import UserMixin, login_user, LoginManager, logout_user, login_required
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login.utils import current_user
from flask_admin.helpers import get_form_data
from flask_admin.babel import gettext
from markupsafe import Markup
app = Flask(__name__, static_folder='frontend/static', template_folder='frontend')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///pictionary.db'
app.config['SECRET_KEY'] = "bel-pictionary"
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.init_app(app)
class CRUDMixin(object):
@classmethod
def create(cls, **kwargs):
instance = cls(**kwargs)
return instance.save()
def update(self, commit=True, **kwargs):
for attr, value in kwargs.iteritems():
setattr(self, attr, value)
return commit and self.save() or self
def save(self, commit=True):
db.session.add(self)
if commit:
db.session.commit()
return self
def delete(self, commit=True):
db.session.delete(self)
return commit and db.session.commit()
class Model(CRUDMixin, db.Model):
__abstract__ = True
class User(UserMixin, db.Model):
__tablename__ = 'users'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
is_admin = db.Column(db.Boolean)
class Team(CRUDMixin, db.Model):
__tablename__ = 'teams'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
score = db.Column(db.Integer, default=0)
class Player(CRUDMixin, db.Model):
__tablename__ = 'players'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
email = db.Column(db.Text)
team_id = db.Column(db.Integer, db.ForeignKey('teams.id'), nullable=False)
team = db.relationship('Team', backref=db.backref('players', lazy="dynamic"))
active = db.Column(db.Boolean, default=False)
is_playing = db.Column(db.Boolean, default=False)
def __repr__(self):
return '<Player %r>' % self.name
class Word(CRUDMixin, db.Model):
__tablename__ = 'words'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
word = db.Column(db.Text)
score = db.Column(db.Integer, default=0)
guessed = db.Column(db.Boolean)
player_id = db.Column(db.Integer, db.ForeignKey(
'players.id'), nullable=False)
player = db.relationship('Player', backref=db.backref('words', lazy="dynamic"))
team_id = db.Column(db.Integer, db.ForeignKey('teams.id'), nullable=False)
team = db.relationship(
'Team', backref=db.backref('words', lazy="dynamic"))
def __repr__(self):
return '<Word %r>' % self.word
class LastPlay(CRUDMixin, db.Model):
__tablename__ = 'last_play'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
player_id = db.Column(db.Integer, db.ForeignKey(
'players.id'), nullable=False)
player = db.relationship('Player', backref=db.backref('last_play', lazy="dynamic"))
team_id = db.Column(db.Integer, db.ForeignKey(
'teams.id'), nullable=False)
team = db.relationship('Team', backref=db.backref('last_play', lazy="dynamic"))
def __repr__(self):
return '<LastPlay %r>' % self.player.name
class MyView(ModelView):
def is_accessible(self):
return current_user.is_authenticated and current_user.is_admin
def inaccessible_callback(self, name, **kwargs):
flash('Must be logged in to access the admin pages')
return redirect(url_for('login'))
class MyAdminView(AdminIndexView):
def is_accessible(self):
return current_user.is_authenticated and current_user.is_admin
def inaccessible_callback(self, name, **kwargs):
flash('Must be logged in to access the admin pages')
return redirect(url_for('login'))
class WordView(ModelView):
def _format_guessed_now(view, context, model, name):
if model.guessed:
return 'Guessed'
guessed_url = url_for('.guessed_view')
_html = '''
<form action="{guessed_url}" method="POST">
<input id="word_id" name="word_id" type="hidden" value="{word_id}">
<button type='submit'>Set to guessed</button>
</form
'''.format(guessed_url=guessed_url, word_id=model.id)
return Markup(_html)
column_formatters = {
'guessed': _format_guessed_now
}
@expose('guessed_view', methods=['POST'])
def guessed_view(self):
return_url = self.get_url('.index_view')
form = get_form_data()
word_id = form['word_id']
word = self.get_one(word_id)
word.guessed = True
team = word.team
team.score = team.score + word.score
try:
self.session.commit()
flash(gettext('Word ID {word_id} guessed!'.format(
word_id=word_id)))
except Exception as ex:
if not self.handle_view_exception(ex):
raise
flash(gettext('Failed to set word ID {word_id} to guessed.'.format(
word_id=word_id), error=str(ex)), 'error')
return redirect(return_url)
admin = Admin(app, index_view=MyAdminView(), name='sz-pictionary')
admin.add_view(MyView(Team, db.session))
admin.add_view(MyView(Player, db.session))
admin.add_view(WordView(Word, db.session))
admin.add_view(MyView(User, db.session))
admin.add_view(MyView(LastPlay, db.session))
admin.add_link(MenuLink(name='Logout', url='/logout'))
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login')
def login():
if current_user and current_user.is_authenticated and current_user.is_admin :
return redirect(url_for('admin.index'))
else :
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login_post():
username = request.form.get('username')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
user = User.query.filter_by(username=username).first()
if not user or not check_password_hash(user.password, password):
flash('Please check your login details and try again.')
return redirect(url_for('login'))
login_user(user, remember=remember)
return redirect(url_for('admin.index'))
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('Successfully logged out')
return render_template('login.html')
@app.route('/api/set_playing/', methods=['POST'])
def post_playing():
active_player = Player.query.filter(Player.active).first()
active_player.is_playing = True
active_player.save()
message = {"message": f"{active_player.name} is now playing"}
return make_response(jsonify(message), 200)
@app.route('/api/teams/', methods=['GET'])
def get_teams():
teams = Team.query.all()
active_player = Player.query.filter(Player.active).first()
import random
last_play = LastPlay.query.first()
if last_play:
last_team = last_play.team
possible_teams = Team.query.filter(Team.id != last_team.id).all()
active_team = possible_teams[0]
for pt in possible_teams :
if pt != active_team and len(pt.words.all()) < len(active_team.words.all()) :
active_team = pt
else:
active_team = Team.query.first()
if not active_player :
players = active_team.players.all()
random_player = random.choice(players)
active_player = random_player
amount_of_words = len(active_player.words.all())
for player in players:
player_words = len(player.words.all())
if player != active_player and player_words < amount_of_words:
amount_of_words = player_words
active_player = player
active_player.active = True
active_player.save()
teams_info = []
for team in teams:
info = {'id': team.id, 'name': team.name, 'score': team.score, 'players': []}
for player in team.players :
info['players'].append({ 'id': player.id, 'name': player.name, 'email': player.email, 'active': player.active, 'playing': player.is_playing })
teams_info.append(info)
return make_response(jsonify(teams_info), 200)
@app.route('/api/save_word/', methods=['POST'])
def post_word():
json = request.get_json()
word = json['word']
score = json['score']
player_id = json['player_id']
if word and score and player_id :
player = Player.query.get(player_id)
new_word = Word(word=word, score=score, player=player, guessed=0, team=player.team)
new_word.save()
last_play = LastPlay.query.first()
if last_play :
last_play.player = player
last_play.team = player.team
else :
last_play = LastPlay(player=player, team=player.team)
last_play.save()
active_player = Player.query.filter(Player.active).first()
active_player.active = False
active_player.is_playing = False
active_player.save()
message = {"message": f"{word} saved"}
return make_response(jsonify(message), 200)
else :
error = {"error": "incorrect data provided"}
return make_response(jsonify(error), 500)
if __name__ == '__main__':
app.run(threaded=True, port=5000)