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_apps.n_published() + cached_apps.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_apps.n_count(c['short_name']) data['categories'].append(datum) # Add Featured datum = dict() datum['featured'] = cached_apps.n_count('featured') data['categories'].append(datum) # Add Draft datum = dict() datum['draft'] = cached_apps.n_count('draft') data['categories'].append(datum) return Response(json.dumps(data), 200, mimetype='application/json')
def get(self, id): """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_apps.n_published() + cached_apps.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_apps.n_count(c['short_name']) data['categories'].append(datum) # Add Featured datum = dict() datum['featured'] = cached_apps.n_count('featured') data['categories'].append(datum) # Add Draft datum = dict() datum['draft'] = cached_apps.n_count('draft') data['categories'].append(datum) return Response(json.dumps(data), 200, mimetype='application/json')
def categories(): """List Categories""" try: if request.method == 'GET': require.category.read() form = CategoryForm() if request.method == 'POST': require.category.create() form = CategoryForm(request.form) if form.validate(): slug = form.name.data.lower().replace(" ", "") category = model.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_apps_per_category = dict() for c in categories: n_apps_per_category[c.short_name] = cached_apps.n_count( c.short_name) return render_template('admin/categories.html', title=gettext('Categories'), categories=categories, n_apps_per_category=n_apps_per_category, form=form) except Exception as e: # pragma: no cover current_app.logger.error(e) return abort(500)
def app_index(page, lookup, category, fallback, use_count): """Show apps of app_type""" per_page = 5 # @FC to come back to project homepage from the application detail if 'cat_byapp_' in category: category = get_app_category(category.replace("cat_byapp_", "")) # @FC apps, count = lookup(category, page, per_page) data = [] #for app in apps: # if 'tutorial' in app['short_name']: # data.append(dict(app=app, n_tasks=cached_apps.n_tasks(app['id']), # overall_progress=cached_apps.overall_progress(app['id']), # last_activity=cached_apps.last_activity(app['id']))) for app in apps: if 'tutorial' not in app['short_name']: data.append(dict(app=app, n_tasks=cached_apps.n_tasks(app['id']), overall_progress=cached_apps.overall_progress(app['id']), last_activity=cached_apps.last_activity(app['id']))) if fallback and not apps: return redirect(url_for('.published')) pagination = Pagination(page, per_page, count) categories = cached_cat.get_all() # Check for pre-defined categories featured and draft featured_cat = model.Category(name='Featured', short_name='featured', description='Featured applications') if category == 'featured': active_cat = featured_cat elif category == 'draft': active_cat = model.Category(name='Draft', short_name='draft', description='Draft applications') else: active_cat = db.session.query(model.Category)\ .filter_by(short_name=category).first() # Check if we have to add the section Featured to local nav if cached_apps.n_featured() > 0: categories.insert(0, featured_cat) template_args = { "apps": data, "title": gettext("Applications"), "pagination": pagination, "active_cat": active_cat, "n_apps_per_category": None, "categories": categories} n_apps_per_category = dict() for c in categories: n_apps_per_category[c.short_name] = cached_apps.n_count(c.short_name) template_args['n_apps_per_category'] = n_apps_per_category if use_count: template_args.update({"count": count}) return render_template('/applications/index.html', **template_args)
def index(): """Return Global Statistics for the site""" title = "Global Statistics" n_auth = n_auth_users() n_anon = n_anon_users() n_total_users = n_anon + n_auth n_published_apps = cached_apps.n_published() n_draft_apps = cached_apps.n_count('draft') n_total_apps = n_published_apps + n_draft_apps n_tasks = n_tasks_site() n_task_runs = n_task_runs_site() top5_apps_24_hours = get_top5_apps_24_hours() top5_users_24_hours = get_top5_users_24_hours() locs = 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_apps=n_published_apps, n_draft_apps=n_draft_apps, n_total_apps=n_total_apps, 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])]) apps = dict(label="Apps Statistics", values=[ dict(label='Published', value=[0, n_published_apps]), dict(label='Draft', value=[0, n_draft_apps])]) 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), apps=json.dumps(apps), tasks=json.dumps(tasks), locs=json.dumps(locs), show_locs=show_locs, top5_users_24_hours=top5_users_24_hours, top5_apps_24_hours=top5_apps_24_hours, stats=stats)
def categories(): """List Categories""" try: if request.method == 'GET': require.category.read() form = CategoryForm() if request.method == 'POST': require.category.create() form = CategoryForm(request.form) if form.validate(): slug = form.name.data.lower().replace(" ", "") category = model.category.Category(name=form.name.data, short_name=slug, description=form.description.data) db.session.add(category) db.session.commit() cached_cat.reset() msg = gettext("Category added") flash(msg, 'success') else: flash(gettext('Please correct the errors'), 'error') categories = cached_cat.get_all() n_apps_per_category = dict() for c in categories: n_apps_per_category[c.short_name] = cached_apps.n_count(c.short_name) return render_template('admin/categories.html', title=gettext('Categories'), categories=categories, n_apps_per_category=n_apps_per_category, form=form) except Exception as e: # pragma: no cover current_app.logger.error(e) return abort(500)
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_app_with_tasks(1, 0) n_projects = cached_apps.n_count('nocategory') assert n_projects == 0, n_projects
def home(): """ Render home page with the cached apps and users""" d = {'featured': cached_apps.get_featured_front_page(), 'top_apps': cached_apps.get_top(), 'top_users': None, 'categories': None, 'apps': None, 'n_apps_per_category': None} if app.config['ENFORCE_PRIVACY'] and current_user.is_authenticated(): if current_user.admin: d['top_users'] = cached_users.get_top() if not app.config['ENFORCE_PRIVACY']: d['top_users'] = cached_users.get_top() # @FC categories = cached_cat.get_all() n_apps_per_category = dict() apps = dict() for c in categories: n_apps_per_category[c.short_name] = cached_apps.n_count(c.short_name) apps[c.short_name],count = cached_apps.get(c.short_name,1,1) d['categories'] = categories d['n_apps_per_category'] = n_apps_per_category d['apps'] = apps # Current user Survey System if current_user.is_authenticated(): sql = text('''SELECT COUNT(task_run.id) AS task_run FROM task_run WHERE :cur_user_id=task_run.user_id''') results = db.engine.execute(sql,cur_user_id=current_user.id) for row in results: num_run_task=row.task_run if current_user.is_authenticated() and current_user.survey_check!= "None" and current_user.survey_check == "2": if num_run_task>=30: d['survey_three'] = True new_profile = model.User(id=current_user.id, survey_check="3") db.session.query(model.User).filter(model.User.id == current_user.id).first() db.session.merge(new_profile) db.session.commit() cached_users.delete_user_summary(current_user.name) elif current_user.is_authenticated() and current_user.survey_check!= "None" and current_user.survey_check == "1": if num_run_task>=1: d['survey_two'] = True new_profile = model.User(id=current_user.id, survey_check="2") db.session.query(model.User).filter(model.User.id == current_user.id).first() db.session.merge(new_profile) db.session.commit() cached_users.delete_user_summary(current_user.name) elif current_user.is_authenticated() and current_user.survey_check!= "None" and current_user.survey_check == "0": d['survey_one'] = True new_profile = model.User(id=current_user.id, survey_check="1") db.session.query(model.User).filter(model.User.id == current_user.id).first() db.session.merge(new_profile) db.session.commit() cached_users.delete_user_summary(current_user.name) else: d['survey_one'] = False # @FC return render_template('/home/index.html', **d)
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_app_with_tasks(1, 0) #create a non published project too AppFactory.create() n_projects = cached_apps.n_count(project.category.short_name) assert n_projects == 1, n_projects
def featured(app_id=None): """List featured apps of PyBossa""" try: categories = cached_cat.get_all() if request.method == 'GET': apps = {} for c in categories: n_apps = cached_apps.n_count(category=c.short_name) apps[c.short_name], n_apps = cached_apps.get(category=c.short_name, page=1, per_page=n_apps) return render_template('/admin/applications.html', apps=apps, categories=categories) elif app_id: if request.method == 'POST': cached_apps.reset() f = model.Featured() f.app_id = app_id app = db.session.query(model.App).get(app_id) require.app.update(app) # Check if the app is already in this table tmp = db.session.query(model.Featured)\ .filter(model.Featured.app_id == app_id)\ .first() if (tmp is None): db.session.add(f) db.session.commit() return json.dumps(f.dictize()) else: msg = "App.id %s alreay in Featured table" % app_id return format_error(msg, 415) if request.method == 'DELETE': cached_apps.reset() f = db.session.query(model.Featured)\ .filter(model.Featured.app_id == app_id)\ .first() if (f): db.session.delete(f) db.session.commit() return "", 204 else: msg = 'App.id %s is not in Featured table' % app_id return format_error(msg, 404) else: msg = ('App.id is missing for %s action in featured method' % request.method) return format_error(msg, 415) except HTTPException: return abort(403) except Exception as e: current_app.logger.error(e) return abort(500)
def featured(app_id=None): """List featured apps of PyBossa""" try: if request.method == 'GET': categories = cached_cat.get_all() apps = {} for c in categories: n_apps = cached_apps.n_count(category=c.short_name) apps[c.short_name] = cached_apps.get(category=c.short_name, page=1, per_page=n_apps) return render_template('/admin/applications.html', apps=apps, categories=categories) else: app = db.session.query(model.app.App).get(app_id) if app: require.app.update(app) if request.method == 'POST': cached_apps.reset() if not app.featured: app.featured = True db.session.add(app) db.session.commit() return json.dumps(app.dictize()) else: msg = "App.id %s already featured" % app_id return format_error(msg, 415) if request.method == 'DELETE': cached_apps.reset() if app.featured: app.featured = False db.session.add(app) db.session.commit() return json.dumps(app.dictize()) else: msg = 'App.id %s is not featured' % app_id return format_error(msg, 415) else: msg = 'App.id %s not found' % app_id return format_error(msg, 404) except Exception as e: # pragma: no cover current_app.logger.error(e) return abort(500)
def home(): """ Render home page with the cached apps and users""" d = {'featured': cached_apps.get_featured_front_page(), 'top_apps': cached_apps.get_top(), 'top_users': None, 'categories': None, 'apps': None, 'n_apps_per_category': None} if app.config['ENFORCE_PRIVACY'] and current_user.is_authenticated(): if current_user.admin: d['top_users'] = cached_users.get_top() if not app.config['ENFORCE_PRIVACY']: d['top_users'] = cached_users.get_top() # @FC categories = cached_cat.get_all() n_apps_per_category = dict() apps = dict() for c in categories: n_apps_per_category[c.short_name] = cached_apps.n_count(c.short_name) apps[c.short_name],count = cached_apps.get(c.short_name,1,1) d['categories'] = categories d['n_apps_per_category'] = n_apps_per_category d['apps'] = apps # Current user Survey System print current_user if current_user.is_authenticated(): # Check if survey_check is None # That is the case of a first time registration if current_user.survey_check == "None" : return redirect(url_for('survey_check')) if current_user.survey_check == "YES" : sql = text('''SELECT COUNT(task_run.id) AS task_run FROM task_run WHERE :cur_user_id=task_run.user_id''') results = db.engine.execute(sql,cur_user_id=current_user.id) num_run_task = 0 for row in results: num_run_task = row.task_run print "Number of tasks run : ",num_run_task if num_run_task > 30: return render_template('/survey_check/survey_check_complete.html', **d) # @FC return render_template('/home/index.html', **d)
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_apps.n_count('draft') _n_draft.assert_called_with()
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_apps = cached_apps.n_published() n_draft_apps = cached_apps.n_count('draft') n_total_apps = n_published_apps + n_draft_apps n_tasks = site_stats.n_tasks_site() n_task_runs = site_stats.n_task_runs_site() top5_apps_24_hours = site_stats.get_top5_apps_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_apps=n_published_apps, n_draft_apps=n_draft_apps, n_total_apps=n_total_apps, 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]) ]) apps = dict(label="Apps Statistics", values=[ dict(label='Published', value=[0, n_published_apps]), dict(label='Draft', value=[0, n_draft_apps]) ]) 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), apps=json.dumps(apps), tasks=json.dumps(tasks), locs=json.dumps(locs), show_locs=show_locs, top5_users_24_hours=top5_users_24_hours, top5_apps_24_hours=top5_apps_24_hours, stats=stats)
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_apps.n_count('featured') _n_featured.assert_called_with()