def player_data(): if not request.json or 'player' not in request.json: print(request, request.json) abort(400) db = get_db() player_data = request.json['player'] friends = player_data['friends'] puzzle_data = player_data['puzzle_data'] del player_data['friends'] del player_data['puzzle_data'] player = Player.get_or_create(player_data) db.session.query(Player).filter( Player.social_id == player.social_id).update(player_data) for friend_social_id in friends: friend = Player.get_or_create({"social_id": friend_social_id}) player.add_friendship(friend) for index, puzzle_id in enumerate(puzzle_data['puzzles']): Score.maybe_update_score({ 'player_social_id': player.social_id, 'puzzle_id': puzzle_id, 'score': puzzle_data['scores'][index], 'stars': puzzle_data['stars'][index]}) db.session.commit() return jsonify( _assemble_response(player))
from sqlalchemy import func from bootstrap import get_db db = get_db() class DnfBase(db.Model): __abstract__ = True def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} friendships = db.Table("friendships", db.Column('friend_social_id', db.String(32), db.ForeignKey('player.social_id'), primary_key=True), db.Column('friend_of_social_id', db.String(32), db.ForeignKey('player.social_id'), primary_key=True)) class Score(DnfBase): player_social_id = db.Column(db.String(32), db.ForeignKey( 'player.social_id'), primary_key=True) puzzle_id = db.Column(db.Integer, primary_key=True) score = db.Column(db.Integer, nullable=False) stars = db.Column(db.Integer, nullable=False) def __init__(self, player_social_id, puzzle_id, score, stars): self.player_social_id = player_social_id