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
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
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
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