def get(self, uid): ranking = Ranking.get_or_none(id=uid) if ranking is None: return {'message': f'Ranking `{uid}` not found'}, 404 if ranking.user.id != current_user.id: return {'message': f'Ranking belongs to another user'}, 409 ranking_json = {'id': ranking.id, 'name': ranking.name, 'datasource': ranking.datasource, 'item_count': len(ranking.items), 'comp_count': len(ranking.comparisons)} if len(ranking.items) != 0: model = ranking.get_pairwise_model() ranking_json['items'] = [] for item in ranking.items: coeff = model.coeff_by_id(str(item.id)) stderr = coeff[1] if isnan(stderr): stderr = 0 idx = model.coefficients.index(coeff) rating = (idx / len(model.coefficients)) * 10 ranking_json['items'].append({ 'id': item.id, 'label': item.label, 'img_url': item.img_url, 'init_rating': item.init_rating, 'curr_rating': round(rating, 2), 'stderr': round(stderr, 2)}) return jsonify(ranking=ranking_json)
def get(self, ranking_uid): ranking = Ranking.get_or_none(id=ranking_uid) if ranking is None: return {'message': f'Ranking `{uid}` not found.'}, 404 if ranking.user.id != current_user.id: return {'message': f'Ranking belongs to another user.'}, 409 model = ranking.get_pairwise_model() comp = model.next_comparison() item1 = Item.get_or_none(id=UUID(comp[0])) if item1 is None: return {'message': f'Item `{comp[0]}` not found'}, 404 item2 = Item.get_or_none(id=UUID(comp[1])) if item2 is None: return {'message': f'Item `{comp[1]}` not found'}, 404 items = [] items.append({ 'id': item1.id, 'label': item1.label, 'img_url': item1.img_url }) items.append({ 'id': item2.id, 'label': item2.label, 'img_url': item2.img_url }) return jsonify(comparison=items)
def post(self): parser = reqparse.RequestParser(trim=True, bundle_errors=True) parser.add_argument('name') parser.add_argument('source') data = parser.parse_args() if Ranking.get_or_none(Ranking.name == data['name']) is not None: return {'message': f"Ranking with such name already exists"}, 409 ranking = Ranking.create(user=current_user.id, name=data['name'], datasource=data['source']) return jsonify(message=f'Ranking {ranking.name} was created, id={ranking.id}')
def put(self, uid): ranking = Ranking.get_or_none(Ranking.id == uid) if ranking is None: return {'message': f'Ranking `{uid}` not found'}, 404 parser = reqparse.RequestParser(trim=True) parser.add_argument('name') args = parser.parse_args() if 'name' in args: ranking.name = args['name'] ranking.save() return jsonify(message=f'Ranking {ranking.id} was modified')
def post(self, uid): ranking = Ranking.get_or_none(id=uid) if ranking is None: return {'message': f'Ranking `{uid}` not found'}, 404 parser = reqparse.RequestParser(trim=True, bundle_errors=True) parser.add_argument('anilist_username') parser.add_argument('anilist_statuses', action="append") parser.add_argument('steam_id') data = parser.parse_args() if ranking.datasource == 'anilist': ranking.add_items_from_anilist(data['anilist_username'], data['anilist_statuses']) elif ranking.datasource == 'steam': ranking.add_items_from_steam(data['steam_id']) return jsonify(message=f'Now ranking has {len(ranking.items)} items.')
def post(self, ranking_uid): ranking = Ranking.get_or_none(id=ranking_uid) if ranking is None: return {'message': f'Ranking `{uid}` not found'}, 404 parser = reqparse.RequestParser(trim=True, bundle_errors=True) parser.add_argument('winitem') parser.add_argument('loseitem') data = parser.parse_args() id1 = UUID(data['winitem']) item1 = Item.get_or_none(id=id1) if item1 is None: return {'message': f'Item `{id1}` not found'}, 404 id2 = UUID(data['loseitem']) item2 = Item.get_or_none(id=id2) if item2 is None: return {'message': f'Item `{id2}` not found'}, 404 comp = Comparison.compare(item1, item2, item1.id) model = ranking.get_pairwise_model() return jsonify(coeff=str(model.coefficients), comparison_count=len(ranking.comparisons), comp=model_to_dict(comp))