Guest User

Untitled

a guest
Feb 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from __future__ import unicode_literals
  4.  
  5.  
  6. class GroupJSONPresenter(object):
  7. """Present a group in the JSON format returned by API requests."""
  8.  
  9. def __init__(self, group, route_url=None):
  10. self.group = group
  11. self._route_url = route_url
  12.  
  13. def asdict(self):
  14. model = {
  15. 'name': self.group.name,
  16. 'id': self.group.pubid,
  17. 'public': self.group.is_public,
  18. 'scoped': False, # TODO
  19. 'type': 'open' if self.group.is_public else 'private' # TODO
  20. }
  21. model = self._inject_urls(model)
  22. return model
  23.  
  24. def _inject_urls(self, model):
  25. model['urls'] = {}
  26. if not self._route_url:
  27. return model
  28.  
  29. model['url'] = self._route_url('group_read',
  30. pubid=self.group.pubid,
  31. slug=self.group.slug)
  32. model['urls']['group'] = model['url']
  33. return model
  34.  
  35.  
  36. class GroupsJSONPresenter(GroupJSONPresenter):
  37. """Present a list of groups as JSON"""
  38.  
  39. def __init__(self, groups, route_url=None):
  40. self.groups = groups
  41. self._route_url = route_url
  42.  
  43. def asdicts(self):
  44. return [self.asdict(group) for group in self.groups]
Add Comment
Please, Sign In to add comment