Skip to content

Instantly share code, notes, and snippets.

@ak64th
Created June 16, 2016 09:11
Show Gist options
  • Save ak64th/b6e6053e623a36e073089e187ba48bf4 to your computer and use it in GitHub Desktop.
Save ak64th/b6e6053e623a36e073089e187ba48bf4 to your computer and use it in GitHub Desktop.
add custom url to a flask_peewee rest api
# 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