Пример #1
0
def _show_public_profile(user, form, can_update):
    if current_user.id == user.id:
        user_dict = cached_users.get_user_summary(user.name)
    else:
        user_dict = cached_users.public_get_user_summary(user.name)
    if current_user.admin:
        user_dict['email_addr'] = user.email_addr
    projects_contributed = cached_users.public_projects_contributed_cached(
        user.id)
    projects_created = cached_users.public_published_projects_cached(user.id)
    total_projects_contributed = '{} / {}'.format(
        cached_users.n_projects_contributed(user.id), n_published())
    percentage_tasks_completed = user_dict['n_answers'] * 100 / (
        n_total_tasks() or 1)

    if current_user.is_authenticated() and current_user.admin:
        draft_projects = cached_users.draft_projects(user.id)
        projects_created.extend(draft_projects)

    title = "%s · User Profile" % user_dict['fullname']
    response = dict(template='/account/public_profile.html',
                    title=title,
                    user=user_dict,
                    projects=projects_contributed,
                    projects_created=projects_created,
                    total_projects_contributed=total_projects_contributed,
                    percentage_tasks_completed=percentage_tasks_completed,
                    form=form,
                    can_update=can_update,
                    private_instance=bool(data_access_levels),
                    upref_mdata_enabled=bool(app_settings.upref_mdata))

    return handle_content_type(response)
Пример #2
0
def _show_public_profile(user):
    user_dict = cached_users.public_get_user_summary(user.name)
    md = cached_users.get_metadata(user.name)
    form = MetadataForm(**md)
    projects_contributed = cached_users.projects_contributed_cached(user.id)
    projects_created = cached_users.published_projects_cached(user.id)
    total_projects_contributed = '{} / {}'.format(cached_users.n_projects_contributed(user.id), n_published())
    percentage_tasks_completed = user_dict['n_answers'] * 100 / (n_total_tasks() or 1)

    if current_user.is_authenticated() and current_user.admin:
        draft_projects = cached_users.draft_projects(user.id)
        projects_created.extend(draft_projects)
    title = "%s · User Profile" % user_dict['fullname']

    response = dict(template='/account/public_profile.html',
                    title=title,
                    user=user_dict,
                    projects=projects_contributed,
                    form=form,
                    projects_created=projects_created,
                    metadata=md,
                    can_update=can_update_user_info(current_user, user),
                    total_projects_contributed=total_projects_contributed,
                    percentage_tasks_completed=percentage_tasks_completed)

    return handle_content_type(response)
Пример #3
0
def add_metadata(name):
    """
    Admin can save metadata for selected user.
    Regular user can save their own metadata.

    Redirects to public profile page for selected user.

    """
    user = user_repo.get_by_name(name=name)
    (can_update, disabled_fields) = can_update_user_info(current_user, user)
    if not can_update:
        abort(403)
    form_data = get_form_data(request, user, disabled_fields)
    form = UserPrefMetadataForm(form_data,
                                can_update=(can_update, disabled_fields))
    form.set_upref_mdata_choices()

    if not form.validate():
        if current_user.id == user.id:
            user_dict = cached_users.get_user_summary(user.name)
        else:
            user_dict = cached_users.public_get_user_summary(user.name)
        projects_contributed = cached_users.projects_contributed_cached(
            user.id)
        projects_created = cached_users.published_projects_cached(user.id)
        total_projects_contributed = '{} / {}'.format(
            cached_users.n_projects_contributed(user.id), n_published())
        percentage_tasks_completed = user_dict['n_answers'] * 100 / (
            n_total_tasks() or 1)
        if current_user.is_authenticated and current_user.admin:
            draft_projects = cached_users.draft_projects(user.id)
            projects_created.extend(draft_projects)
        title = "%s · User Profile" % user.name
        flash("Please fix the errors", 'message')
        return render_template(
            '/account/public_profile.html',
            title=title,
            user=user,
            projects=projects_contributed,
            projects_created=projects_created,
            total_projects_contributed=total_projects_contributed,
            percentage_tasks_completed=percentage_tasks_completed,
            form=form,
            input_form=True,
            can_update=can_update,
            upref_mdata_enabled=bool(app_settings.upref_mdata))

    user_pref, metadata = get_user_pref_and_metadata(name, form)
    user.info['metadata'] = metadata
    ensure_data_access_assignment_from_form(user.info, form)
    user.user_pref = user_pref
    user_repo.update(user)
    cached_users.delete_user_pref_metadata(user.name)
    cached_users.delete_user_access_levels_by_id(user.id)
    delete_memoized(get_user_preferences, user.id)
    flash("Input saved successfully", "info")
    return redirect(url_for('account.profile', name=name))