def json_contact_with_groups(sub, user): """ Enhanced version of json_contact that also details which SubcriptionGroups a user belongs to. """ result = json_contact(sub.to_contact, user) result['groups'] = [json_group(g, sub.to_contact) for g in sub.groups] return result
def json_user(user): """ JSON-serialisable view of a User. """ data = { 'id': user.id, 'email': user.email, } data.update(json_contact(user.contact)) return data
def json_target(cls, user, parent_post=None): if parent_post: user_list = cls._user_bothway_contacts_for_post(user, parent_post) else: user_list = cls._user_bothway_contacts(user) return { 'name': cls.name, 'description': 'Share with one friend', 'targets': [json_contact(c) for c in user_list] }
def json_share(share, cache=None): """ Turn a Share into a sensible format for serialisation. """ contact_repr = _get_cached(cache, 'contact', share.contact_id) if cache \ else json_contact(share.contact) return { 'contact': contact_repr, 'shared_at': share.shared_at.isoformat(), 'public': share.public }
def _fill_cache(c, show_shares=False): # Fill the cache in bulk, which will also fill the entries post_ids = c['post'].keys() if post_ids: for post_tag in PostTag.get_tags_for_posts(post_ids): c['post'][post_tag.post_id]['tags'].append(json_tag(post_tag.tag)) post_parts = PostPart.get_parts_for_posts(post_ids). \ order_by(PostPart.order) for post_part in post_parts: c['post'][post_part.post_id]['parts'].append(json_part(post_part)) if show_shares: for post_share in Share.get_for_posts(post_ids): post_id = post_share.post_id if not c['post'][post_id]['shares']: c['post'][post_id]['shares'] = [] c['post'][post_id]['shares'].append( json_share(post_share, cache=c)) if c['contact']: for contact in Contact.get_many(c['contact'].keys()): c['contact'][contact.id].update(json_contact(contact))
def _fill_cache(c, show_shares=False): # Fill the cache in bulk, which will also fill the entries post_ids = c['post'].keys() if post_ids: for post_tag in PostTag.get_tags_for_posts(post_ids): c['post'][post_tag.post_id]['tags'].append(json_tag(post_tag.tag)) post_parts = PostPart.get_parts_for_posts(post_ids). \ order_by(PostPart.order) for post_part in post_parts: c['post'][post_part.post_id]['parts'].append(json_part(post_part)) if show_shares: for post_share in Share.get_for_posts(post_ids): post_id = post_share.post_id if not c['post'][post_id]['shares']: c['post'][post_id]['shares'] = [] c['post'][post_id]['shares'].append( json_share(post_share, cache=c) ) if c['contact']: for contact in Contact.get_many(c['contact'].keys()): c['contact'][contact.id].update(json_contact(contact))