def new_user(): """ Register a new user. This endpoint is publicly available. """ user = User.create(request.get_json() or {}) if User.query.filter_by(nickname=user.nickname).first() is not None: abort(400) db.session.add(user) db.session.commit() r = jsonify(user.to_dict()) r.status_code = 201 r.headers['Location'] = url_for('get_user', id=user.id) return r
def to_dict(self): """Export message to a dictionary.""" return { 'id': self.id, 'created_at': self.created_at, 'updated_at': self.updated_at, 'source': self.source, 'html': self.html, 'user_id': self.user_id, '_links': { 'self': url_for('get_message', id=self.id), 'user': '******'.format(self.user_id) } }
def to_dict(self): """Export user to a dictionary.""" return { 'id': self.id, 'created_at': self.created_at, 'updated_at': self.updated_at, 'nickname': self.nickname, 'last_seen_at': self.last_seen_at, 'online': self.online, '_links': { 'self': url_for('get_user', id=self.id), 'messages': '/api/messages/{}'.format(self.id), 'tokens': '/api/tokens' } }
def new_game(): """ Register a new game. This endpoint is publicly available. Create game only if none of the names is already playing elsewhere """ logging.info(request.get_json()) in_r = request.get_json() if not ("game_name" in in_r and "names" in in_r and "styles" in in_r): logging.error("Your request needs the fields type(<String>) and names(<[<String>,...])") abort(400) if check_user_playing(in_r["names"]): logging.error("One of the users is already playing....") abort(400) if not check_game_type_valid(in_r["game_name"], len(in_r["names"])): logging.error("I do not know your game... wrong type?, wrong nu names?") abort(400) # TODO Test if the players are online! # shuffle users here (for random seats) random.shuffle(in_r["names"]) # todo types also shuffled in same way! options = get_game_options(in_r["game_name"], in_r["names"], in_r["styles"]) # create game db_game = Game.create(in_r or {}) db_game.start_cards = {} game_obj = schafkopf(options) game_obj.reset() #sort hand cards: for i in range(game_obj.nu_players): db_game.start_cards[game_obj.player_names[i]] = l2s(game_obj.players[i].hand) db.session.add(db_game) db.session.commit() r = jsonify(db_game.to_dict()) print(r)#558 bytes # TODO not all has to be send back!!! r.status_code = 201 r.headers['Location'] = url_for('get_game', id=db_game.id) return r
def to_dict(self): """Export game to a dictionary. links are build e.g. /api/game/{}""" return { 'id': self.id, 'title': self.title, 'game_name': self.game_name, 'names': self.names, 'styles': self.styles, 'comments': self.comments, 'declarations': self.declarations, 'highest_decl': self.highest_decl, 'table_cards': self.table_cards, 'start_cards': self.start_cards, 'final_points': self.final_points, 'final_money': self.final_money, 'updated_at': self.updated_at, 'created_at': self.created_at, '_links': { 'self': url_for('get_game', id=self.id), 'games': '/api/game/{}'.format(self.id), 'tokens': '/api/tokens' } }
def new_message(): """ Post a new message. This endpoint is requires a valid user token. """ msg = Message(user_id=g.jwt_claims['user_id']) msg.from_dict(request.get_json(), partial_update=False) msg.html = '...' db.session.add(msg) db.session.commit() r = jsonify(msg.to_dict()) r.status_code = 201 r.headers['Location'] = url_for('get_message', id=msg.id) # render the markdown and expand the links in a background task if app.config['TESTING']: # for unit tests, render synchronously render_message(msg.id) else: # asynchronous rendering render_thread = threading.Thread(target=render_message, args=(msg.id,)) render_thread.start() return r