Skip to content

Commit

Permalink
WIP: Add API AnnotationJSONPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
chdorner committed Feb 25, 2016
1 parent 7d7d7ef commit 6bcfc40
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions h/api/presenters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,64 @@
import collections


class AnnotationJSONPresenter(object):
def __init__(self, annotation):
self.annotation = annotation

def dict(self):
docpresenter = DocumentJSONPresenter(self.annotation.document)

a = {
'id': self.annotation.id,
'created': self.created,
'updated': self.updated,
'user': self.annotation.userid,
'uri': self.annotation.target_uri,
'text': self.annotation.text,
'tags': self.tags,
'group': self.annotation.groupid,
'permission': self.permission,
'target': self.target,
'document': docpresenter.dict(),
}

if self.annotation.references:
a['references'] = self.annotation.references

return a

@property
def created(self):
return utc_iso8601(self.annotation.created)

@property
def updated(self):
return utc_iso8601(self.annotation.updated)

@property
def tags(self):
if self.annotation.tags:
return self.annotation.tags
else:
return []

@property
def permission(self):
read = self.annotation.userid
if self.annotation.shared:
read = 'group:{}'.format(self.annotation.groupid)

return {'read': [read],
'admin': [self.annotation.userid],
'update': [self.annotation.userid],
'delete': [self.annotation.userid]}

@property
def target(self):
return [{'source': self.annotation.target_uri,
'selector': self.annotation.target_selectors or {}}]


class DocumentJSONPresenter(object):
def __init__(self, document):
self.document = document
Expand Down

0 comments on commit 6bcfc40

Please sign in to comment.