Created
February 13, 2018 14:46
-
-
Save lyzadanger/81138dffeb14804aede238609159e33a to your computer and use it in GitHub Desktop.
Sub-classed Presenter?
This file contains hidden or 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 __future__ import unicode_literals | |
class GroupJSONPresenter(object): | |
"""Present a group in the JSON format returned by API requests.""" | |
def __init__(self, group, route_url=None): | |
self.group = group | |
self._route_url = route_url | |
def asdict(self): | |
model = { | |
'name': self.group.name, | |
'id': self.group.pubid, | |
'public': self.group.is_public, | |
'scoped': False, # TODO | |
'type': 'open' if self.group.is_public else 'private' # TODO | |
} | |
model = self._inject_urls(model) | |
return model | |
def _inject_urls(self, model): | |
model['urls'] = {} | |
if not self._route_url: | |
return model | |
model['url'] = self._route_url('group_read', | |
pubid=self.group.pubid, | |
slug=self.group.slug) | |
model['urls']['group'] = model['url'] | |
return model | |
class GroupsJSONPresenter(GroupJSONPresenter): | |
"""Present a list of groups as JSON""" | |
def __init__(self, groups, route_url=None): | |
self.groups = groups | |
self._route_url = route_url | |
def asdicts(self): | |
return [self.asdict(group) for group in self.groups] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment