Example #1
0
def view_embed(organization_id: str):
    if not request.is_xhr:
        return index(organization_id)

    api = pillar_api()

    organization: Organization = Organization.find(organization_id, api=api)

    om = current_app.org_manager
    organization_oid = str2id(organization_id)

    members = om.org_members(organization.members)
    for member in members:
        member['avatar'] = gravatar(member.get('email'))
        member['_id'] = str(member['_id'])

    admin_user = User.find(organization.admin_uid, api=api)

    # Make sure it's never None
    organization.unknown_members = organization.unknown_members or []

    can_super_edit = current_user.has_cap('admin')
    can_edit = can_super_edit or om.user_is_admin(organization_oid)

    csrf = flask_wtf.csrf.generate_csrf()

    return render_template('organizations/view_embed.html',
                           organization=organization,
                           admin_user=admin_user,
                           members=members,
                           can_edit=can_edit,
                           can_super_edit=can_super_edit,
                           seats_used=len(members) +
                           len(organization.unknown_members),
                           csrf=csrf)
Example #2
0
def view_embed(manager_id: str):
    if not request.is_xhr:
        return index(manager_id)

    api = pillar_api()

    manager: Manager = Manager.find(manager_id, api=api)
    linked_projects = manager.linked_projects(api=api)
    linked_project_ids = set(manager.projects or [])

    # TODO: support pagination
    fetched = current_flamenco.flamenco_projects(projection={
        '_id': 1,
        'url': 1,
        'name': 1,
    })
    available_projects = [
        project for project in fetched._items
        if project['_id'] not in linked_project_ids
    ]

    owner_gid = str2id(manager['owner'])
    owners = current_flamenco.manager_manager.owning_users(owner_gid)

    for owner in owners:
        owner['avatar'] = gravatar(owner.get('email'))
        owner['_id'] = str(owner['_id'])

    manager_oid = str2id(manager_id)
    can_edit = current_flamenco.manager_manager.user_is_owner(
        mngr_doc_id=manager_oid)

    csrf = flask_wtf.csrf.generate_csrf()

    return render_template('flamenco/managers/view_manager_embed.html',
                           manager=manager.to_dict(),
                           can_edit=can_edit,
                           available_projects=available_projects,
                           linked_projects=linked_projects,
                           owners=owners,
                           can_abandon_manager=len(owners) > 1,
                           csrf=csrf)
Example #3
0
def notification_parse(notification):
    activities_collection = current_app.data.driver.db['activities']
    activities_subscriptions_collection = \
        current_app.data.driver.db['activities-subscriptions']
    users_collection = current_app.data.driver.db['users']
    nodes_collection = current_app.data.driver.db['nodes']
    activity = activities_collection.find_one({'_id': notification['activity']})

    if activity is None or activity['object_type'] != 'node':
        return
    node = nodes_collection.find_one({'_id': activity['object']})
    if not node:
        # This can happen when a notification is generated and then the
        # node is deleted.
        return

    # Initial support only for node_type comments
    if node['node_type'] != 'comment':
        return
    node['parent'] = nodes_collection.find_one({'_id': node['parent']})
    object_type = 'comment'
    object_name = ''
    object_id = activity['object']

    if node['parent']['user'] == current_user.user_id:
        owner = "your {0}".format(node['parent']['node_type'])
    else:
        parent_comment_user = users_collection.find_one(
            {'_id': node['parent']['user']})
        if parent_comment_user['_id'] == node['user']:
            user_name = 'their'
        else:
            user_name = "{0}'s".format(parent_comment_user['username'])
        owner = "{0} {1}".format(user_name, node['parent']['node_type'])

    context_object_type = node['parent']['node_type']
    context_object_name = owner
    context_object_id = activity['context_object']
    if activity['verb'] == 'replied':
        action = 'replied to'
    elif activity['verb'] == 'commented':
        action = 'left a comment on'
    else:
        action = activity['verb']

    lookup = {
        'user': current_user.user_id,
        'context_object_type': 'node',
        'context_object': context_object_id,
    }
    subscription = activities_subscriptions_collection.find_one(lookup)
    if subscription and subscription['notifications']['web'] == True:
        is_subscribed = True
    else:
        is_subscribed = False

    # Parse user_actor
    actor = users_collection.find_one({'_id': activity['actor_user']})
    if actor:
        parsed_actor = {
            'username': actor['username'],
            'avatar': gravatar(actor['email'])}
    else:
        parsed_actor = None

    updates = dict(
        _id=notification['_id'],
        actor=parsed_actor,
        action=action,
        object_type=object_type,
        object_name=object_name,
        object_id=str(object_id),
        context_object_type=context_object_type,
        context_object_name=context_object_name,
        context_object_id=str(context_object_id),
        date=activity['_created'],
        is_read=('is_read' in notification and notification['is_read']),
        is_subscribed=is_subscribed,
        subscription=subscription['_id']
    )
    notification.update(updates)