def test_n_count_calls_n_featuredt(self, _n_featured, pickle):
        """Test CACHE PROJECTS n_count calls _n_featured when called with
        argument 'featured'"""
        pickle.dumps.return_value = 'str'
        cached_projects.n_count('featured')

        _n_featured.assert_called_with()
示例#2
0
 def get(self, oid=None):
     """Return global stats."""
     n_pending_tasks = stats.n_total_tasks_site() - stats.n_task_runs_site()
     n_users = stats.n_auth_users() + stats.n_anon_users()
     n_projects = cached_projects.n_published() + cached_projects.n_count('draft')
     data = dict(n_projects=n_projects,
                 n_users=n_users,
                 n_task_runs=stats.n_task_runs_site(),
                 n_pending_tasks=n_pending_tasks,
                 categories=[])
     # Add Categories
     categories = cached_categories.get_used()
     for c in categories:
         datum = dict()
         datum[c['short_name']] = cached_projects.n_count(c['short_name'])
         data['categories'].append(datum)
     # Add Featured
     datum = dict()
     datum['featured'] = cached_projects.n_count('featured')
     data['categories'].append(datum)
     # Add Draft
     datum = dict()
     datum['draft'] = cached_projects.n_count('draft')
     data['categories'].append(datum)
     return Response(json.dumps(data), 200, mimetype='application/json')
示例#3
0
 def get(self, oid=None):
     """Return global stats."""
     n_pending_tasks = stats.n_total_tasks_site() - stats.n_task_runs_site()
     n_users = stats.n_auth_users() + stats.n_anon_users()
     n_projects = cached_projects.n_published() + cached_projects.n_count(
         'draft')
     data = dict(n_projects=n_projects,
                 n_users=n_users,
                 n_task_runs=stats.n_task_runs_site(),
                 n_pending_tasks=n_pending_tasks,
                 n_results=stats.n_results_site(),
                 categories=[])
     # Add Categories
     categories = cached_categories.get_used()
     for c in categories:
         datum = dict()
         datum[c['short_name']] = cached_projects.n_count(c['short_name'])
         data['categories'].append(datum)
     # Add Featured
     datum = dict()
     datum['featured'] = cached_projects.n_count('featured')
     data['categories'].append(datum)
     # Add Draft
     datum = dict()
     datum['draft'] = cached_projects.n_count('draft')
     data['categories'].append(datum)
     return Response(json.dumps(data), 200, mimetype='application/json')
    def test_n_count_calls_n_draft(self, _n_draft, pickle):
        """Test CACHE PROJECTS n_count calls _n_draft when called with argument
        'draft'"""
        pickle.dumps.return_value = 'str'
        cached_projects.n_count('draft')

        _n_draft.assert_called_with()
示例#5
0
def categories():
    """List Categories."""
    try:
        if request.method == 'GET':
            ensure_authorized_to('read', Category)
            form = CategoryForm()
        if request.method == 'POST':
            ensure_authorized_to('create', Category)
            form = CategoryForm(request.form)
            del form.id
            if form.validate():
                slug = form.name.data.lower().replace(" ", "")
                category = Category(name=form.name.data,
                                    short_name=slug,
                                    description=form.description.data)
                project_repo.save_category(category)
                cached_cat.reset()
                msg = gettext("Category added")
                flash(msg, 'success')
            else:
                flash(gettext('Please correct the errors'), 'error')
        categories = cached_cat.get_all()
        n_projects_per_category = dict()
        for c in categories:
            n_projects_per_category[c.short_name] = \
                cached_projects.n_count(c.short_name)

        return render_template('admin/categories.html',
                               title=gettext('Categories'),
                               categories=categories,
                               n_projects_per_category=n_projects_per_category,
                               form=form)
    except Exception as e:  # pragma: no cover
        current_app.logger.error(e)
        return abort(500)
示例#6
0
def index():
    """Return Global Statistics for the site."""
    title = "Global Statistics"

    n_auth = site_stats.n_auth_users()

    n_anon = site_stats.n_anon_users()

    n_total_users = n_anon + n_auth

    n_published_projects = cached_projects.n_published()
    n_draft_projects = cached_projects.n_count('draft')
    n_total_projects = n_published_projects + n_draft_projects

    n_tasks = site_stats.n_tasks_site()

    n_task_runs = site_stats.n_task_runs_site()

    top5_projects_24_hours = site_stats.get_top5_projects_24_hours()

    top5_users_24_hours = site_stats.get_top5_users_24_hours()

    stats = dict(n_total_users=n_total_users,
                 n_auth=n_auth,
                 n_anon=n_anon,
                 n_published_projects=n_published_projects,
                 n_draft_projects=n_draft_projects,
                 n_total_projects=n_total_projects,
                 n_tasks=n_tasks,
                 n_task_runs=n_task_runs)

    users = dict(label="User Statistics",
                 values=[
                     dict(label='Anonymous', value=[0, n_anon]),
                     dict(label='Authenticated', value=[0, n_auth])
                 ])

    projects = dict(label="Projects Statistics",
                    values=[
                        dict(label='Published',
                             value=[0, n_published_projects]),
                        dict(label='Draft', value=[0, n_draft_projects])
                    ])

    tasks = dict(label="Task and Task Run Statistics",
                 values=[
                     dict(label='Tasks', value=[0, n_tasks]),
                     dict(label='Answers', value=[1, n_task_runs])
                 ])

    response = dict(template='/stats/global.html',
                    title=title,
                    users=json.dumps(users),
                    projects=json.dumps(projects),
                    tasks=json.dumps(tasks),
                    show_locs=False,
                    top5_users_24_hours=top5_users_24_hours,
                    top5_projects_24_hours=top5_projects_24_hours,
                    stats=stats)
    return handle_content_type(response)
示例#7
0
def index():
    """Return Global Statistics for the site."""
    title = "Global Statistics"

    n_auth = site_stats.n_auth_users()

    n_anon = site_stats.n_anon_users()

    n_total_users = n_anon + n_auth

    n_published_projects = cached_projects.n_published()
    n_draft_projects = cached_projects.n_count('draft')
    n_total_projects = n_published_projects + n_draft_projects

    n_tasks = site_stats.n_tasks_site()

    n_task_runs = site_stats.n_task_runs_site()

    top5_projects_24_hours = site_stats.get_top5_projects_24_hours()

    top5_users_24_hours = site_stats.get_top5_users_24_hours()

    locs = site_stats.get_locs()

    show_locs = False
    if len(locs) > 0:
        show_locs = True

    stats = dict(n_total_users=n_total_users, n_auth=n_auth, n_anon=n_anon,
                 n_published_projects=n_published_projects,
                 n_draft_projects=n_draft_projects,
                 n_total_projects=n_total_projects,
                 n_tasks=n_tasks,
                 n_task_runs=n_task_runs)

    users = dict(label="User Statistics",
                 values=[
                     dict(label='Anonymous', value=[0, n_anon]),
                     dict(label='Authenticated', value=[0, n_auth])])

    projects = dict(label="Projects Statistics",
                    values=[
                        dict(label='Published',
                             value=[0, n_published_projects]),
                        dict(label='Draft', value=[0, n_draft_projects])])

    tasks = dict(label="Task and Task Run Statistics",
                 values=[
                     dict(label='Tasks', value=[0, n_tasks]),
                     dict(label='Answers', value=[1, n_task_runs])])

    return render_template('/stats/global.html', title=title,
                           users=json.dumps(users),
                           projects=json.dumps(projects),
                           tasks=json.dumps(tasks),
                           locs=json.dumps(locs),
                           show_locs=show_locs,
                           top5_users_24_hours=top5_users_24_hours,
                           top5_projects_24_hours=top5_projects_24_hours,
                           stats=stats)
示例#8
0
    def test_n_count_with_different_category(self):
        """Test CACHE PROJECTS n_count returns 0 if there are no published
        projects from requested category"""
        project = self.create_project_with_tasks(1, 0)

        n_projects = cached_projects.n_count('nocategory')

        assert n_projects == 0, n_projects
    def test_n_count_with_different_category(self):
        """Test CACHE PROJECTS n_count returns 0 if there are no published
        projects from requested category"""
        project = self.create_project_with_tasks(1, 0)

        n_projects = cached_projects.n_count('nocategory')

        assert n_projects == 0, n_projects
示例#10
0
    def test_n_count_with_password_protected_projects(self):
        """Test CACHE PROJECTS n_count returns the number of published projects
        of a given category, excluding projects with a password"""
        project = ProjectFactory.create(published=True, info={'passwd_hash': '2'})
        ProjectFactory.create(category=project.category, published=True)

        n_projects = cached_projects.n_count(project.category.short_name)

        assert n_projects == 1, n_projects
示例#11
0
    def test_n_count_with_published_projects(self):
        """Test CACHE PROJECTS n_count returns the number of published projects
        of a given category"""
        project = ProjectFactory.create(published=True)
        ProjectFactory.create(published=True)
        ProjectFactory.create(category=project.category, published=False)

        n_projects = cached_projects.n_count(project.category.short_name)

        assert n_projects == 1, n_projects
示例#12
0
    def test_n_count_with_published_projects(self):
        """Test CACHE PROJECTS n_count returns the number of published projects
        of a given category"""
        project = self.create_project_with_tasks(1, 0)
        #create a non published project too
        ProjectFactory.create()

        n_projects = cached_projects.n_count(project.category.short_name)

        assert n_projects == 1, n_projects
示例#13
0
    def test_n_count_with_published_projects(self):
        """Test CACHE PROJECTS n_count returns the number of published projects
        of a given category"""
        project = ProjectFactory.create(published=True)
        ProjectFactory.create(published=True)
        ProjectFactory.create(category=project.category, published=False)

        n_projects = cached_projects.n_count(project.category.short_name)

        assert n_projects == 1, n_projects
示例#14
0
    def test_n_count_with_password_protected_projects(self):
        """Test CACHE PROJECTS n_count returns the number of published projects
        of a given category, excluding projects with a password"""
        project = ProjectFactory.create(published=True,
                                        info={'passwd_hash': '2'})
        ProjectFactory.create(category=project.category, published=True)

        n_projects = cached_projects.n_count(project.category.short_name)

        assert n_projects == 1, n_projects
    def test_n_count_with_published_projects(self):
        """Test CACHE PROJECTS n_count returns the number of published projects
        of a given category"""
        project = self.create_project_with_tasks(1, 0)
        #create a non published project too
        ProjectFactory.create()

        n_projects = cached_projects.n_count(project.category.short_name)

        assert n_projects == 1, n_projects
示例#16
0
def index():
    """List admin actions."""
    categories = cached_cat.get_all()
    projects = {}
    for c in categories:
        n_projects = cached_projects.n_count(category=c.short_name)
        projects[c.short_name] = cached_projects.get(category=c.short_name,
                                                     page=1,
                                                     per_page=n_projects)
    return render_template('/admin/index.html',
                           projects=projects,
                           categories=categories)
示例#17
0
def index():
    """Return the Data page."""
    categories = cached_cat.get_all()
    projects = {}
    for c in categories:
        n_projects = cached_projects.n_count(category=c.short_name)
        projects[c.short_name] = cached_projects.get(category=c.short_name,
                                                     page=1,
                                                     per_page=n_projects)
        for p in projects[c.short_name]:
            p['n_task_runs'] = cached_projects.n_task_runs(p['id'])
            p['n_results'] = cached_projects.n_results(p['id'])

    return render_template('/index.html', projects=projects,
                           categories=categories, title="Data")
示例#18
0
def featured(project_id=None):
    """List featured projects of PYBOSSA."""
    try:
        if request.method == 'GET':
            categories = cached_cat.get_all()
            projects = {}
            for c in categories:
                n_projects = cached_projects.n_count(category=c.short_name)
                projects[c.short_name] = cached_projects.get(
                    category=c.short_name,
                    page=1,
                    per_page=n_projects)
            response = dict(template = '/admin/projects.html',
                            projects=projects,
                            categories=categories,
                            form=dict(csrf=generate_csrf()))
            return handle_content_type(response)
        else:
            project = project_repo.get(project_id)
            if project:
                ensure_authorized_to('update', project)
                if request.method == 'POST':
                    if project.featured is True:
                        msg = "Project.id %s already featured" % project_id
                        return format_error(msg, 415)
                    cached_projects.reset()
                    project.featured = True
                    project_repo.update(project)
                    return json.dumps(project.dictize())

                if request.method == 'DELETE':
                    if project.featured is False:
                        msg = 'Project.id %s is not featured' % project_id
                        return format_error(msg, 415)
                    cached_projects.reset()
                    project.featured = False
                    project_repo.update(project)
                    return json.dumps(project.dictize())
            else:
                msg = 'Project.id %s not found' % project_id
                return format_error(msg, 404)
    except Exception as e:  # pragma: no cover
        current_app.logger.error(e)
        return abort(500)
示例#19
0
文件: admin.py 项目: fiorda/pybossa
def featured(project_id=None):
    """List featured projects of PYBOSSA."""
    try:
        if request.method == 'GET':
            categories = cached_cat.get_all()
            projects = {}
            for c in categories:
                n_projects = cached_projects.n_count(category=c.short_name)
                projects[c.short_name] = cached_projects.get(
                    category=c.short_name,
                    page=1,
                    per_page=n_projects)
            response = dict(template = '/admin/projects.html',
                            projects=projects,
                            categories=categories,
                            form=dict(csrf=generate_csrf()))
            return handle_content_type(response)
        else:
            project = project_repo.get(project_id)
            if project:
                ensure_authorized_to('update', project)
                if request.method == 'POST':
                    if project.featured is True:
                        msg = "Project.id %s already featured" % project_id
                        return format_error(msg, 415)
                    cached_projects.reset()
                    project.featured = True
                    project_repo.update(project)
                    return json.dumps(project.dictize())

                if request.method == 'DELETE':
                    if project.featured is False:
                        msg = 'Project.id %s is not featured' % project_id
                        return format_error(msg, 415)
                    cached_projects.reset()
                    project.featured = False
                    project_repo.update(project)
                    return json.dumps(project.dictize())
            else:
                msg = 'Project.id %s not found' % project_id
                return format_error(msg, 404)
    except Exception as e:  # pragma: no cover
        current_app.logger.error(e)
        return abort(500)
    def test_n_count_calls_n_featuredt(self, _n_featured, pickle):
        """Test CACHE PROJECTS n_count calls _n_featured when called with
        argument 'featured'"""
        cached_projects.n_count('featured')

        _n_featured.assert_called_with()
    def test_n_count_calls_n_draft(self, _n_draft, pickle):
        """Test CACHE PROJECTS n_count calls _n_draft when called with argument
        'draft'"""
        cached_projects.n_count('draft')

        _n_draft.assert_called_with()
示例#22
0
def get_visible():
    """Return visible categories"""
    data = session.query(model.category.Category).all()
    return [c for c in data if cached_projects.n_count(c.short_name) > 0]