Ejemplo n.º 1
0
def warm_cache():  # pragma: no cover
    """Background job to warm cache."""
    from pybossa.core import create_app
    app = create_app(run_as_server=False)
    # Cache 3 pages
    projects_cached = []
    pages = range(1, 4)
    import pybossa.cache.projects as cached_projects
    import pybossa.cache.categories as cached_cat
    import pybossa.cache.users as cached_users
    import pybossa.cache.project_stats as stats

    def warm_project(_id, short_name, featured=False):
        if _id not in projects_cached:
            cached_projects.get_project(short_name)
            cached_projects.n_tasks(_id)
            n_task_runs = cached_projects.n_task_runs(_id)
            cached_projects.overall_progress(_id)
            cached_projects.last_activity(_id)
            cached_projects.n_completed_tasks(_id)
            cached_projects.n_volunteers(_id)
            if n_task_runs >= 1000 or featured:
                # print ("Getting stats for %s as it has %s task runs" %
                #        (short_name, n_task_runs))
                stats.get_stats(_id, app.config.get('GEO'))
            projects_cached.append(_id)

    # Cache top projects
    projects = cached_projects.get_top()
    for p in projects:
        warm_project(p['id'], p['short_name'])
    for page in pages:
        projects = cached_projects.get_featured('featured', page,
                                                app.config['APPS_PER_PAGE'])
        for p in projects:
            warm_project(p['id'], p['short_name'], featured=True)

    # Categories
    categories = cached_cat.get_used()
    for c in categories:
        for page in pages:
            projects = cached_projects.get(c['short_name'], page,
                                           app.config['APPS_PER_PAGE'])
            for p in projects:
                warm_project(p['id'], p['short_name'])
    # Users
    users = cached_users.get_leaderboard(app.config['LEADERBOARD'],
                                         'anonymous')
    for user in users:
        # print "Getting stats for %s" % user['name']
        cached_users.get_user_summary(user['name'])
        cached_users.projects_contributed_cached(user['id'])
        cached_users.published_projects_cached(user['id'])
        cached_users.draft_projects_cached(user['id'])

    cached_users.get_top()

    return True
Ejemplo n.º 2
0
def warm_cache():  # pragma: no cover
    """Background job to warm cache."""
    from pybossa.core import create_app
    app = create_app(run_as_server=False)
    projects_cached = []
    import pybossa.cache.projects as cached_projects
    import pybossa.cache.categories as cached_cat
    import pybossa.cache.users as cached_users
    import pybossa.cache.project_stats as stats
    from pybossa.util import rank
    from pybossa.core import user_repo

    def warm_project(_id, short_name, featured=False):
        if _id not in projects_cached:
            #cached_projects.get_project(short_name)
            #cached_projects.n_tasks(_id)
            #n_task_runs = cached_projects.n_task_runs(_id)
            #cached_projects.overall_progress(_id)
            #cached_projects.last_activity(_id)
            #cached_projects.n_completed_tasks(_id)
            #cached_projects.n_volunteers(_id)
            #cached_projects.browse_tasks(_id)
            #if n_task_runs >= 1000 or featured:
            #    # print ("Getting stats for %s as it has %s task runs" %
            #    #        (short_name, n_task_runs))
            stats.update_stats(_id, app.config.get('GEO'))
            projects_cached.append(_id)

    # Cache top projects
    projects = cached_projects.get_top()
    for p in projects:
        warm_project(p['id'], p['short_name'])

    # Cache 3 pages
    to_cache = 3 * app.config['APPS_PER_PAGE']
    projects = rank(cached_projects.get_all_featured('featured'))[:to_cache]
    for p in projects:
        warm_project(p['id'], p['short_name'], featured=True)

    # Categories
    categories = cached_cat.get_used()
    for c in categories:
        projects = rank(cached_projects.get_all(c['short_name']))[:to_cache]
        for p in projects:
            warm_project(p['id'], p['short_name'])
    # Users
    users = cached_users.get_leaderboard(app.config['LEADERBOARD'])
    for user in users:
        # print "Getting stats for %s" % user['name']
        print user_repo
        u = user_repo.get_by_name(user['name'])
        cached_users.get_user_summary(user['name'])
        cached_users.projects_contributed_cached(u.id)
        cached_users.published_projects_cached(u.id)
        cached_users.draft_projects_cached(u.id)

    return True
Ejemplo n.º 3
0
def warm_cache():  # pragma: no cover
    """Background job to warm cache."""
    from pybossa.core import create_app
    app = create_app(run_as_server=False)
    projects_cached = []
    import pybossa.cache.projects as cached_projects
    import pybossa.cache.categories as cached_cat
    import pybossa.cache.users as cached_users
    import pybossa.cache.project_stats as stats
    from pybossa.util import rank
    from pybossa.core import user_repo

    def warm_project(_id, short_name, featured=False):
        if _id not in projects_cached:
            #cached_projects.get_project(short_name)
            #cached_projects.n_tasks(_id)
            #n_task_runs = cached_projects.n_task_runs(_id)
            #cached_projects.overall_progress(_id)
            #cached_projects.last_activity(_id)
            #cached_projects.n_completed_tasks(_id)
            #cached_projects.n_volunteers(_id)
            #cached_projects.browse_tasks(_id)
            #if n_task_runs >= 1000 or featured:
            #    # print ("Getting stats for %s as it has %s task runs" %
            #    #        (short_name, n_task_runs))
            stats.update_stats(_id, app.config.get('GEO'))
            projects_cached.append(_id)

    # Cache top projects
    projects = cached_projects.get_top()
    for p in projects:
        warm_project(p['id'], p['short_name'])

    # Cache 3 pages
    to_cache = 3 * app.config['APPS_PER_PAGE']
    projects = rank(cached_projects.get_all_featured('featured'))[:to_cache]
    for p in projects:
        warm_project(p['id'], p['short_name'], featured=True)

    # Categories
    categories = cached_cat.get_used()
    for c in categories:
        projects = rank(cached_projects.get_all(c['short_name']))[:to_cache]
        for p in projects:
            warm_project(p['id'], p['short_name'])
    # Users
    users = cached_users.get_leaderboard(app.config['LEADERBOARD'])
    for user in users:
        # print "Getting stats for %s" % user['name']
        print user_repo
        u = user_repo.get_by_name(user['name'])
        cached_users.get_user_summary(user['name'])
        cached_users.projects_contributed_cached(u.id)
        cached_users.published_projects_cached(u.id)
        cached_users.draft_projects_cached(u.id)

    return True
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    def test_projects_contributed_no_contributions_cached(self):
        """Test CACHE USERS projects_contributed_cached returns empty list if the user has
        not contributed to any project"""
        user = UserFactory.create()

        projects_contributed = cached_users.projects_contributed_cached(user.id)

        assert projects_contributed == [], projects_contributed
Ejemplo n.º 6
0
    def test_projects_contributed_no_contributions_cached(self):
        """Test CACHE USERS projects_contributed_cached returns empty list if the user has
        not contributed to any project"""
        user = UserFactory.create()

        projects_contributed = cached_users.projects_contributed_cached(user.id)

        assert projects_contributed == [], projects_contributed
Ejemplo n.º 7
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))
Ejemplo n.º 8
0
def _show_public_profile(user):
    user_dict = cached_users.get_user_summary(user.name)
    projects_contributed = cached_users.projects_contributed_cached(user.id)
    projects_created = cached_users.published_projects_cached(user.id)
    if current_user.is_authenticated() and current_user.admin:
        projects_hidden = cached_users.hidden_projects(user.id)
        projects_created.extend(projects_hidden)
    title = "%s · User Profile" % user_dict['fullname']
    return render_template('/account/public_profile.html',
                           title=title,
                           user=user_dict,
                           projects=projects_contributed,
                           projects_created=projects_created)
Ejemplo n.º 9
0
def _show_public_profile(user):
    user_dict = cached_users.get_user_summary(user.name)
    projects_contributed = cached_users.projects_contributed_cached(user.id)
    projects_created = cached_users.published_projects_cached(user.id)
    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']
    return render_template('/account/public_profile.html',
                           title=title,
                           user=user_dict,
                           projects=projects_contributed,
                           projects_created=projects_created)
Ejemplo n.º 10
0
    def test_projects_contributed_contributions_cached(self):
        """Test CACHE USERS projects_contributed_cached returns a list of projects that has
        contributed to"""
        user = UserFactory.create()
        project_contributed = ProjectFactory.create()
        task = TaskFactory.create(project=project_contributed)
        TaskRunFactory.create(task=task, user=user)
        another_project = ProjectFactory.create()

        projects_contributed = cached_users.projects_contributed_cached(user.id)

        assert len(projects_contributed) == 1
        assert projects_contributed[0]['short_name'] == project_contributed.short_name, projects_contributed
Ejemplo n.º 11
0
    def test_projects_contributed_contributions_cached(self):
        """Test CACHE USERS projects_contributed_cached returns a list of projects that has
        contributed to"""
        user = UserFactory.create()
        project_contributed = ProjectFactory.create()
        task = TaskFactory.create(project=project_contributed)
        TaskRunFactory.create(task=task, user=user)
        another_project = ProjectFactory.create()

        projects_contributed = cached_users.projects_contributed_cached(user.id)

        assert len(projects_contributed) == 1
        assert projects_contributed[0]['short_name'] == project_contributed.short_name, projects_contributed
Ejemplo n.º 12
0
def _show_own_profile(user):
    rank_and_score = cached_users.rank_and_score(user.id)
    user.rank = rank_and_score['rank']
    user.score = rank_and_score['score']
    user.total = cached_users.get_total_users()
    projects_contributed = cached_users.projects_contributed_cached(user.id)
    projects_published, projects_draft = _get_user_projects(user.id)
    cached_users.get_user_summary(user.name)

    return render_template('account/profile.html', title=gettext("Profile"),
                           projects_contrib=projects_contributed,
                           projects_published=projects_published,
                           projects_draft=projects_draft,
                           user=user)
Ejemplo n.º 13
0
def _show_own_profile(user):
    rank_and_score = cached_users.rank_and_score(user.id)
    user.rank = rank_and_score['rank']
    user.score = rank_and_score['score']
    user.total = cached_users.get_total_users()
    projects_contributed = cached_users.projects_contributed_cached(user.id)
    projects_published, projects_draft = _get_user_projects(user.id)
    cached_users.get_user_summary(user.name)

    return render_template('account/profile.html', title=gettext("Profile"),
                           projects_contrib=projects_contributed,
                           projects_published=projects_published,
                           projects_draft=projects_draft,
                           user=user)
Ejemplo n.º 14
0
def add_metadata(name):
    """
    Admin can add metadata for selected user

    Redirects to public profile page for selected user.

    """
    user = user_repo.get_by_name(name=name)
    if not can_update_user_info(current_user, user):
        abort(403)
    form = MetadataForm(request.form)
    if not any(value for value in form.data.values()):
        user.info['metadata'] = {}
        user.user_pref = {}
    elif form.validate():
        metadata = dict(admin=current_user.name, time_stamp=time.ctime(),
                        user_type=form.user_type.data, start_time=form.start_time.data,
                        end_time=form.end_time.data, review=form.review.data,
                        timezone=form.timezone.data, profile_name=user.name)
        user.info['metadata'] = metadata
        user_pref = {}
        if form.languages.data:
            user_pref["languages"] = form.languages.data
        if form.locations.data:
            user_pref["locations"] = form.locations.data

        user.user_pref = user_pref
    else:
        projects_contributed = cached_users.projects_contributed_cached(user.id)
        projects_created = cached_users.published_projects_cached(user.id)
        metadata = cached_users.get_metadata(user.name)
        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, metadata=metadata,
                               projects=projects_contributed, form=form,
                               projects_created=projects_created,
                               input_form=True)
    user_repo.update(user)
    cached_users.delete_user_metadata(user.name)
    delete_memoized(get_user_preferences, user.id)
    flash("Input saved successfully", "info")
    return redirect(url_for('account.profile', name=name))
Ejemplo n.º 15
0
def add_metadata(name):
    """
    Admin can save metadata for selected user
    Redirects to public profile page for selected user.
    """
    user = user_repo.get_by_name(name=name)
    form = UserPrefMetadataForm(request.form)
    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)
        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')
        can_update = current_user.admin
        return render_template('/account/public_profile.html',
                               title=title,
                               user=user_dict,
                               projects=projects_contributed,
                               projects_created=projects_created,
                               form=form,
                               can_update=can_update,
                               input_form=True)

    user_pref, metadata = get_user_pref_and_metadata(name, form)
    user.info['metadata'] = metadata
    user.user_pref = user_pref
    user_repo.update(user)
    cached_users.delete_user_pref_metadata(user.name)
    flash("Input saved successfully", "info")
    return redirect(url_for('account.profile', name=name))
Ejemplo n.º 16
0
def add_metadata(name):
    """
    Admin can save metadata for selected user
    Redirects to public profile page for selected user.
    """
    user = user_repo.get_by_name(name=name)
    form = UserPrefMetadataForm(request.form)
    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)
        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')
        can_update = current_user.admin
        return render_template('/account/public_profile.html',
                               title=title,
                               user=user_dict,
                               projects=projects_contributed,
                               projects_created=projects_created,
                               form=form,
                               can_update=can_update,
                               input_form=True)

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