Example #1
0
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))
Example #2
0
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))
Example #3
0
def json_contact(contact, viewing_as=None):
    """
    A suitable representation of the contact that can be turned into JSON.
    If "viewing_as" (a local contact) is supplied, the data visible to that
    contact will be returned; otherwise only public data is visible.
    """
    from pyaspora.post.views import json_part
    resp = {
        'id': contact.id,
        'link': url_for(
            'contacts.profile',
            contact_id=contact.id,
            _external=True
        ),
        'subscriptions': url_for(
            'contacts.subscriptions',
            contact_id=contact.id,
            _external=True
        ),
        'name': contact.realname,
        'bio': '',
        'avatar': None,
        'actions': {
            'add': None,
            'remove': None,
            'post': None,
            'edit': None,
            'edit_groups': None
        },
        'feed': None,
        'tags': [json_tag(t) for t in contact.interests]
    }
    if contact.avatar:
        resp['avatar'] = url_for(
            'contacts.avatar',
            contact_id=contact.id,
            _external=True
        )

    if contact.bio:
        fake_part = FakePart()
        fake_part.inline = True
        fake_part.mime_part = contact.bio
        resp['bio'] = json_part(fake_part)

    if viewing_as:
        if viewing_as.id == contact.id:
            resp['actions'].update({
                'edit': url_for('users.info', _external=True)
            })
        elif viewing_as.contact.subscribed_to(contact):
            resp['actions'].update({
                'remove': url_for(
                    'roster.unsubscribe',
                    contact_id=contact.id,
                    _external=True
                ),
                'post': url_for(
                    'posts.create',
                    target_type='contact',
                    target_id=contact.id,
                    _external=True
                ),
                'edit_groups': url_for(
                    'roster.edit_contact_groups_form',
                    contact_id=contact.id,
                    _external=True
                )
            })
        else:
            resp['actions'].update({
                'add': url_for(
                    'roster.subscribe',
                    contact_id=contact.id,
                    _external=True
                )
            })

    return resp
Example #4
0
def json_contact(contact, viewing_as=None):
    """
    A suitable representation of the contact that can be turned into JSON.
    If "viewing_as" (a local contact) is supplied, the data visible to that
    contact will be returned; otherwise only public data is visible.
    """
    from pyaspora.post.views import json_part
    resp = {
        'id':
        contact.id,
        'link':
        url_for('contacts.profile', contact_id=contact.id, _external=True),
        'subscriptions':
        url_for('contacts.subscriptions',
                contact_id=contact.id,
                _external=True),
        'name':
        contact.realname,
        'bio':
        '',
        'avatar':
        None,
        'actions': {
            'add': None,
            'remove': None,
            'post': None,
            'edit': None,
            'edit_groups': None
        },
        'feed':
        None,
        'tags': [json_tag(t) for t in contact.interests]
    }
    if contact.avatar:
        resp['avatar'] = url_for('contacts.avatar',
                                 contact_id=contact.id,
                                 _external=True)

    if contact.bio:
        fake_part = FakePart()
        fake_part.inline = True
        fake_part.mime_part = contact.bio
        resp['bio'] = json_part(fake_part)

    if viewing_as:
        if viewing_as.id == contact.id:
            resp['actions'].update(
                {'edit': url_for('users.info', _external=True)})
        elif viewing_as.contact.subscribed_to(contact):
            resp['actions'].update({
                'remove':
                url_for('roster.unsubscribe',
                        contact_id=contact.id,
                        _external=True),
                'post':
                url_for('posts.create',
                        target_type='contact',
                        target_id=contact.id,
                        _external=True),
                'edit_groups':
                url_for('roster.edit_contact_groups_form',
                        contact_id=contact.id,
                        _external=True)
            })
        else:
            resp['actions'].update({
                'add':
                url_for('roster.subscribe',
                        contact_id=contact.id,
                        _external=True)
            })

    return resp
Example #5
0
def json_contact(contact, viewing_as=None):
    """
    A suitable representation of the contact that can be turned into JSON.
    If "viewing_as" (a local contact) is supplied, the data visible to that
    contact will be returned; otherwise only public data is visible.
    """
    from pyaspora.post.views import json_part
    resp = {
        'id': contact.id,
        'link': url_for(
            'contacts.profile',
            contact_id=contact.id,
            _external=True
        ),
        'name': contact.realname,
        'bio': '',
        'avatar': None,
        'actions': {
            'add': None,
            'remove': None,
            'post': None,
            'edit': None,
            'edit_groups': None
        },
        'feed': None,
        'tags': [json_tag(t) for t in contact.interests]
    }

    if contact.diasp:
        resp['username'] = contact.diasp.username

    # No point in showing subs for remote users as they're incomplete
    if contact.user:
        resp['subscriptions'] = url_for(
            'contacts.subscriptions',
            contact_id=contact.id,
            _external=True
        )

    if contact.avatar:
        resp['avatar'] = url_for(
            'contacts.avatar',
            contact_id=contact.id,
            _external=True
        )
    elif contact.user and \
            current_app.config.get('FEATURES', {}).get('gravatar', False):
        resp['avatar'] = 'http://www.gravatar.com/avatar/{0}?d=mm'.format(
            md5(contact.user.email.strip().lower()).hexdigest()
        )

    if contact.bio:
        # Need something that feels like a PostPart for the rendering engine.
        fake_part = FakePart()
        fake_part.inline = True
        fake_part.mime_part = contact.bio
        resp['bio'] = json_part(fake_part)

    if viewing_as:
        if viewing_as.id == contact.id:  # Viewing own profile
            resp['actions'].update({
                'edit': url_for('users.info', _external=True)
            })
        elif viewing_as.contact.subscribed_to(contact):  # Friend
            resp['actions'].update({
                'remove': url_for(
                    'roster.unsubscribe',
                    contact_id=contact.id,
                    _external=True
                ),
                'post': url_for(
                    'posts.create',
                    target_type='contact',
                    target_id=contact.id,
                    _external=True
                ),
                'edit_groups': url_for(
                    'roster.edit_contact_groups_form',
                    contact_id=contact.id,
                    _external=True
                )
            })
        else:  # Potential friend? :-)
            resp['actions'].update({
                'add': url_for(
                    'roster.subscribe',
                    contact_id=contact.id,
                    _external=True
                )
            })

    return resp
Example #6
0
def json_contact(contact, viewing_as=None):
    """
    A suitable representation of the contact that can be turned into JSON.
    If "viewing_as" (a local contact) is supplied, the data visible to that
    contact will be returned; otherwise only public data is visible.
    """
    from pyaspora.post.views import json_part
    resp = {
        'id': contact.id,
        'link': url_for('contacts.profile',
                        contact_id=contact.id,
                        _external=True),
        'name': contact.realname,
        'bio': '',
        'avatar': None,
        'actions': {
            'add': None,
            'remove': None,
            'post': None,
            'edit': None,
            'edit_groups': None
        },
        'feed': None,
        'tags': [json_tag(t) for t in contact.interests]
    }

    if contact.diasp:
        resp['username'] = contact.diasp.username

    # No point in showing subs for remote users as they're incomplete
    if contact.user:
        resp['subscriptions'] = url_for('contacts.subscriptions',
                                        contact_id=contact.id,
                                        _external=True)

    if contact.avatar:
        resp['avatar'] = url_for('contacts.avatar',
                                 contact_id=contact.id,
                                 _external=True)
    elif contact.user and \
            current_app.config.get('FEATURES', {}).get('gravatar', False):
        resp['avatar'] = 'http://www.gravatar.com/avatar/{0}?d=mm'.format(
            md5(contact.user.email.strip().lower()).hexdigest())

    if contact.bio:
        # Need something that feels like a PostPart for the rendering engine.
        fake_part = FakePart()
        fake_part.inline = True
        fake_part.mime_part = contact.bio
        resp['bio'] = json_part(fake_part)

    if viewing_as:
        if viewing_as.id == contact.id:  # Viewing own profile
            resp['actions'].update(
                {'edit': url_for('users.info', _external=True)})
        elif viewing_as.contact.subscribed_to(contact):  # Friend
            resp['actions'].update({
                'remove':
                url_for('roster.unsubscribe',
                        contact_id=contact.id,
                        _external=True),
                'post':
                url_for('posts.create',
                        target_type='contact',
                        target_id=contact.id,
                        _external=True),
                'edit_groups':
                url_for('roster.edit_contact_groups_form',
                        contact_id=contact.id,
                        _external=True)
            })
        else:  # Potential friend? :-)
            resp['actions'].update({
                'add':
                url_for('roster.subscribe',
                        contact_id=contact.id,
                        _external=True)
            })

    return resp