Skip to content

Instantly share code, notes, and snippets.

@mikeywaites
Last active November 29, 2017 05:53
Show Gist options
  • Save mikeywaites/6a7f5515fad1a71ed1bd60e6fcb99fcf to your computer and use it in GitHub Desktop.
Save mikeywaites/6a7f5515fad1a71ed1bd60e6fcb99fcf to your computer and use it in GitHub Desktop.
from arrested import Resource
from arrested.contrib.kim_arrested import KimEndpoint
from arrested.contrib.sql_alchemy import DBListMixin, DBObjectMixin, DBCreateMixin
from star_wars.models import db, Character, UserCharacterLike
from .mappers import CharacterMapper, UserCharacterLikeMapper
characters_resource = Resource('characters', __name__, url_prefix='/characters')
class UserCharacterLikesIndexEndpoint(KimEndpoint, DBListMixin, DBCreateMixin):
url = '/<string:obj_id>/likes'
name = 'likes'
many = True
mapper_class = UserCharacterLikeMapper
model = UserCharacterLike
def get_character_obj(self):
character_id = self.kwargs['obj_id']
character = db.session.query(Character).filter(
Character.id == character_id
).one_or_none()
if character is None:
payload = {
"message": "Character object not found.",
}
self.return_error(404, payload=payload)
else:
return character
def get_query(self):
character = self.get_character_obj()
stmt = db.session.query(UserCharacterLike).filter(
UserCharacterLike.character_id == character.id
)
return stmt
def save_object(self, obj):
character = self.get_character_obj()
obj.character_id = character.id
return super(UserCharacterLikesIndexEndpoint, self).save_object(obj)
characters_resource.add_endpoint(CharactersIndexEndpoint)
characters_resource.add_endpoint(UserCharacterLikesIndexEndpoint)
characters_resource.add_endpoint(CharacterObjectEndpoint)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment