Created
June 16, 2016 09:11
-
-
Save ak64th/b6e6053e623a36e073089e187ba48bf4 to your computer and use it in GitHub Desktop.
add custom url to a flask_peewee rest api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from flask import request, Response | |
from peewee import * | |
from flask_peewee.rest import RestAPI | |
from models import * | |
class IntApi(RestAPI): | |
@property | |
def registry(self): | |
return self._registry | |
def get_urls(self): | |
return [('/activity/<pk>/stats/', self.stats)] | |
def stats(self, pk): | |
query = (Run | |
.select(Run.id, UserInfo.info_field_1, UserInfo.info_field_2, UserInfo.info_field_3, FinalScore.score) | |
.join(UserInfo, JOIN.LEFT_OUTER, | |
on=((Run.uid == UserInfo.uid) & (Run.game == UserInfo.game))) | |
.join(FinalScore, JOIN.LEFT_OUTER, | |
on=((Run.run_id == FinalScore.run_id) & (Run.game == UserInfo.game))) | |
.where((Run.game == pk) & (FinalScore.score > 0)) | |
.order_by(FinalScore.score.desc()) | |
.dicts()) | |
kwargs = {} if request.is_xhr else {'indent': 2} | |
return Response(json.dumps(list(query), **kwargs), mimetype='application/json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment