Esempio n. 1
0
def home():
    """Render home page with the cached projects and users."""
    page = 1
    per_page = current_app.config.get('APPS_PER_PAGE')
    if per_page is None:  # pragma: no cover
        per_page = 5
    d = {'top_projects': cached_projects.get_top(),
         'top_users': None}

    # Get all the categories with projects
    categories = cached_cat.get_used()
    d['categories'] = categories
    d['categories_projects'] = {}
    for c in categories:
        tmp_projects = cached_projects.get(c['short_name'], page, per_page)
        d['categories_projects'][c['short_name']] = rank(tmp_projects)

    # Add featured
    tmp_projects = cached_projects.get_featured('featured', page, per_page)
    if len(tmp_projects) > 0:
        featured = Category(name='Featured', short_name='featured')
        d['categories'].insert(0, featured)
        d['categories_projects']['featured'] = rank(tmp_projects)

    if (current_app.config['ENFORCE_PRIVACY']
            and current_user.is_authenticated()):
        if current_user.admin:
            d['top_users'] = cached_users.get_top()
    if not current_app.config['ENFORCE_PRIVACY']:
        d['top_users'] = cached_users.get_top()
    return render_template('/home/index.html', **d)
Esempio n. 2
0
def home():
    """Render home page with the cached projects and users."""
    page = 1
    per_page = current_app.config.get('APPS_PER_PAGE')
    if per_page is None:  # pragma: no cover
        per_page = 5
    d = {'top_projects': cached_projects.get_top(), 'top_users': None}

    # Get all the categories with projects
    categories = cached_cat.get_used()
    d['categories'] = categories
    d['categories_projects'] = {}
    for c in categories:
        tmp_projects = cached_projects.get(c['short_name'], page, per_page)
        d['categories_projects'][c['short_name']] = tmp_projects

    # Add featured
    tmp_projects = cached_projects.get_featured('featured', page, per_page)
    if len(tmp_projects) > 0:
        featured = Category(name='Featured', short_name='featured')
        d['categories'].insert(0, featured)
        d['categories_projects']['featured'] = tmp_projects

    if (current_app.config['ENFORCE_PRIVACY']
            and current_user.is_authenticated()):
        if current_user.admin:
            d['top_users'] = cached_users.get_top()
    if not current_app.config['ENFORCE_PRIVACY']:
        d['top_users'] = cached_users.get_top()
    return render_template('/home/index.html', **d)
Esempio 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)
    # 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
Esempio n. 4
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
Esempio n. 5
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
Esempio n. 6
0
    def test_get_top_respects_limit(self):
        """Test CACHE PROJECTS get_top returns only the top n projects"""

        ranked_3_project = self.create_project_with_contributors(8, 0, name='three')
        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(10, 0, name='one')

        top_projects = cached_projects.get_top(n=2)

        assert len(top_projects) is 2, len(top_projects)
Esempio n. 7
0
    def test_get_top_returns_only_projects_without_password(self):
        """Test CACHE PROJECTS get_top returns projects that don't have a password"""

        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(
            10, 0, name='one', info={'passwd_hash': 'something'})

        top_projects = cached_projects.get_top()

        assert len(top_projects) is 1, len(top_projects)
Esempio n. 8
0
    def test_get_top_respects_limit(self):
        """Test CACHE PROJECTS get_top returns only the top n projects"""

        ranked_3_project = self.create_project_with_contributors(8, 0, name='three')
        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(10, 0, name='one')
        ranked_4_project = self.create_project_with_contributors(7, 0, name='four')

        top_projects = cached_projects.get_top(n=2)

        assert len(top_projects) is 2, len(top_projects)
Esempio n. 9
0
    def test_get_top_returns_only_projects_without_password(self):
        """Test CACHE PROJECTS get_top returns projects that don't have a password"""

        ranked_2_project = self.create_project_with_contributors(9,
                                                                 0,
                                                                 name='two')
        ranked_1_project = self.create_project_with_contributors(
            10, 0, name='one', info={'passwd_hash': 'something'})

        top_projects = cached_projects.get_top()

        assert len(top_projects) is 0, len(top_projects)
Esempio n. 10
0
    def test_get_top_returns_four_projects_by_default(self):
        """Test CACHE PROJECTS get_top returns the top 4 projects by default"""

        ranked_3_project = self.create_project_with_contributors(8, 0, name='three')
        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(10, 0, name='one')
        ranked_4_project = self.create_project_with_contributors(7, 0, name='four')
        ranked_5_project = self.create_project_with_contributors(7, 0, name='five')

        top_projects = cached_projects.get_top()

        assert len(top_projects) is 4, len(top_projects)
Esempio n. 11
0
    def test_get_top_returns_four_projects_by_default(self):
        """Test CACHE PROJECTS get_top returns the top 4 projects by default"""

        ranked_3_project = self.create_project_with_contributors(8, 0, name='three')
        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(10, 0, name='one')
        ranked_4_project = self.create_project_with_contributors(7, 0, name='four')
        ranked_5_project = self.create_project_with_contributors(7, 0, name='five')

        top_projects = cached_projects.get_top()

        assert len(top_projects) is 4, len(top_projects)
Esempio n. 12
0
    def test_get_top_doesnt_return_hidden_projects(self):
        """Test CACHE PROJECTS get_top does not return projects that are hidden"""

        ranked_3_project = self.create_project_with_contributors(8, 0, name='three')
        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(10, 0, name='one')
        hidden_project = self.create_project_with_contributors(11, 0, name='hidden', hidden=1)

        top_projects = cached_projects.get_top()

        assert len(top_projects) is 3, len(top_projects)
        for project in top_projects:
            assert project['name'] != 'hidden', project['name']
Esempio n. 13
0
    def test_get_top_doesnt_return_hidden_projects(self):
        """Test CACHE PROJECTS get_top does not return projects that are hidden"""

        ranked_3_project = self.create_project_with_contributors(8, 0, name='three')
        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(10, 0, name='one')
        hidden_project = self.create_project_with_contributors(11, 0, name='hidden', hidden=1)

        top_projects = cached_projects.get_top()

        assert len(top_projects) is 3, len(top_projects)
        for project in top_projects:
            assert project['name'] != 'hidden', project['name']
Esempio n. 14
0
    def test_get_top_returns_projects_with_most_taskruns(self):
        """Test CACHE PROJECTS get_top returns the projects with most taskruns in order"""

        rankded_3_project = self.create_project_with_contributors(8, 0, name='three')
        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(10, 0, name='one')
        ranked_4_project = self.create_project_with_contributors(7, 0, name='four')

        top_projects = cached_projects.get_top()

        assert top_projects[0]['name'] == 'one', top_projects
        assert top_projects[1]['name'] == 'two', top_projects
        assert top_projects[2]['name'] == 'three', top_projects
        assert top_projects[3]['name'] == 'four', top_projects
Esempio n. 15
0
    def test_get_top_returns_projects_with_most_taskruns(self):
        """Test CACHE PROJECTS get_top returns the projects with most taskruns in order"""

        rankded_3_project = self.create_project_with_contributors(8, 0, name='three')
        ranked_2_project = self.create_project_with_contributors(9, 0, name='two')
        ranked_1_project = self.create_project_with_contributors(10, 0, name='one')
        ranked_4_project = self.create_project_with_contributors(7, 0, name='four')

        top_projects = cached_projects.get_top()

        assert top_projects[0]['name'] == 'one', top_projects
        assert top_projects[1]['name'] == 'two', top_projects
        assert top_projects[2]['name'] == 'three', top_projects
        assert top_projects[3]['name'] == 'four', top_projects
Esempio n. 16
0
    def test_get_top_returns_projects_with_most_taskruns(self):
        """Test CACHE PROJECTS get_top returns the projects with most taskruns in order"""

        rankded_3_project = self.create_project_with_contributors(8, 0, name="three")
        ranked_2_project = self.create_project_with_contributors(9, 0, name="two")
        ranked_1_project = self.create_project_with_contributors(10, 0, name="one")
        ranked_4_project = self.create_project_with_contributors(7, 0, name="four")

        top_projects = cached_projects.get_top()

        assert top_projects[0]["name"] == "one", top_projects
        assert top_projects[1]["name"] == "two", top_projects
        assert top_projects[2]["name"] == "three", top_projects
        assert top_projects[3]["name"] == "four", top_projects
Esempio n. 17
0
def warm_cache():
    '''Warm cache'''
    # Disable cache, so we can refresh the data in Redis
    os.environ['PYBOSSA_REDIS_CACHE_DISABLED'] = '1'
    # Cache 3 pages
    apps_cached = []
    pages = range(1, 4)
    with app.app_context():
        import pybossa.cache.projects as cached_apps
        import pybossa.cache.categories as cached_cat
        import pybossa.cache.users as cached_users
        import pybossa.cache.project_stats  as stats

        def warm_app(id, short_name, featured=False):
            if id not in apps_cached:
                cached_apps.get_app(short_name)
                cached_apps.n_tasks(id)
                n_task_runs = cached_apps.n_task_runs(id)
                cached_apps.overall_progress(id)
                cached_apps.last_activity(id)
                cached_apps.n_completed_tasks(id)
                cached_apps.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'))
                apps_cached.append(id)

        # Cache top projects
        apps = cached_apps.get_top()
        for a in apps:
            warm_app(a['id'], a['short_name'])
        for page in pages:
            apps = cached_apps.get_featured('featured', page,
                                            app.config['APPS_PER_PAGE'])
            for a in apps:
                warm_app(a['id'], a['short_name'], featured=True)

        # Categories
        categories = cached_cat.get_used()
        for c in categories:
            for page in pages:
                 apps = cached_apps.get(c['short_name'],
                                        page,
                                        app.config['APPS_PER_PAGE'])
                 for a in apps:
                     warm_app(a['id'], a['short_name'])
        # Users
        cached_users.get_leaderboard(app.config['LEADERBOARD'], 'anonymous')
        cached_users.get_top()
Esempio n. 18
0
def warm_cache():
    '''Warm cache'''
    # Disable cache, so we can refresh the data in Redis
    os.environ['PYBOSSA_REDIS_CACHE_DISABLED'] = '1'
    # Cache 3 pages
    apps_cached = []
    pages = range(1, 4)
    with app.app_context():
        import pybossa.cache.projects as cached_apps
        import pybossa.cache.categories as cached_cat
        import pybossa.cache.users as cached_users
        import pybossa.cache.project_stats as stats

        def warm_app(id, short_name, featured=False):
            if id not in apps_cached:
                cached_apps.get_app(short_name)
                cached_apps.n_tasks(id)
                n_task_runs = cached_apps.n_task_runs(id)
                cached_apps.overall_progress(id)
                cached_apps.last_activity(id)
                cached_apps.n_completed_tasks(id)
                cached_apps.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'))
                apps_cached.append(id)

        # Cache top projects
        apps = cached_apps.get_top()
        for a in apps:
            warm_app(a['id'], a['short_name'])
        for page in pages:
            apps = cached_apps.get_featured('featured', page,
                                            app.config['APPS_PER_PAGE'])
            for a in apps:
                warm_app(a['id'], a['short_name'], featured=True)

        # Categories
        categories = cached_cat.get_used()
        for c in categories:
            for page in pages:
                apps = cached_apps.get(c['short_name'], page,
                                       app.config['APPS_PER_PAGE'])
                for a in apps:
                    warm_app(a['id'], a['short_name'])
        # Users
        cached_users.get_leaderboard(app.config['LEADERBOARD'], 'anonymous')
        cached_users.get_top()