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} # Get all the categories with apps categories = cached_cat.get_used() d['categories'] = categories d['categories_apps'] = {} for c in categories: tmp_apps, count = cached_apps.get(c['short_name'], per_page=20) d['categories_apps'][str(c['short_name'])] = tmp_apps # Add featured tmp_apps, count = cached_apps.get_featured('featured', per_page=20) if count > 0: featured = model.category.Category(name='Featured', short_name='featured') d['categories'].insert(0,featured) d['categories_apps']['featured'] = tmp_apps 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)
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 } # Get all the categories with apps categories = cached_cat.get_used() d['categories'] = categories d['categories_apps'] = {} for c in categories: tmp_apps, count = cached_apps.get(c['short_name'], per_page=20) d['categories_apps'][str(c['short_name'])] = tmp_apps # Add featured tmp_apps, count = cached_apps.get_featured('featured', per_page=20) if count > 0: featured = model.category.Category(name='Featured', short_name='featured') d['categories'].insert(0, featured) d['categories_apps']['featured'] = tmp_apps 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)
def home(): """ Render home page with the cached apps 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_apps': cached_apps.get_top(), 'top_users': None} # Get all the categories with apps categories = cached_cat.get_used() d['categories'] = categories d['categories_apps'] = {} for c in categories: tmp_apps = cached_apps.get(c['short_name'], page, per_page) d['categories_apps'][c['short_name']] = tmp_apps # Add featured tmp_apps = cached_apps.get_featured('featured', page, per_page) if len(tmp_apps) > 0: featured = Category(name='Featured', short_name='featured') d['categories'].insert(0, featured) d['categories_apps']['featured'] = tmp_apps 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)
def home(): """ Render home page with the cached apps 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_apps': cached_apps.get_top(), 'top_users': None} # Get all the categories with apps categories = cached_cat.get_used() d['categories'] = categories d['categories_apps'] = {} for c in categories: tmp_apps = cached_apps.get(c['short_name'], page, per_page) d['categories_apps'][c['short_name']] = tmp_apps # Add featured tmp_apps = cached_apps.get_featured('featured', page, per_page) if len(tmp_apps) > 0: featured = model.category.Category(name='Featured', short_name='featured') d['categories'].insert(0,featured) d['categories_apps']['featured'] = tmp_apps 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)
def test_get_not_returns_hidden_apps(self): """Test CACHE PROJECTS get does not return hidden projects""" project = self.create_app_with_contributors(1, 0, hidden=1) projects = cached_apps.get(project.category.short_name) assert len(projects) is 0, projects
def test_get_category(self): """Test CACHE PROJECTS get returns projects from given category""" project = self.create_app_with_tasks(1, 0) projects = cached_apps.get(project.category.short_name) assert len(projects) is 1, projects
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 apps_cached = [] pages = range(1, 4) import pybossa.cache.apps 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 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.apps_contributed_cached(user['id']) cached_users.published_apps_cached(user['id']) cached_users.draft_apps_cached(user['id']) cached_users.get_top() return True
def api(): """Render help/api page""" categories = cached_cat.get_used() apps, count = cached_apps.get(categories[0]["short_name"]) if len(apps) > 0: app_id = choice(apps)["id"] else: app_id = None return render_template("help/api.html", title="Help: API", app_id=app_id)
def api(): """Render help/api page""" categories = cached_cat.get_used() apps, count = cached_apps.get(categories[0]['short_name']) if len(apps) > 0: app_id = choice(apps)['id'] else: app_id = None return render_template('help/api.html', title="Help: API", app_id=app_id)
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_get_not_returns_draft_apps(self): """Test CACHE PROJECTS get does not return draft (non-published) projects""" project = self.create_app_with_contributors(1, 0) # Create a project wothout presenter AppFactory.create(info={}, category=project.category) projects = cached_apps.get(project.category.short_name) assert len(projects) is 1, projects
def test_get_only_returns_category_projects(self): """Test CACHE PROJECTS get returns only projects from required category""" project = self.create_app_with_tasks(1, 0) #create a non published project too AppFactory.create() projects = cached_apps.get(project.category.short_name) assert len(projects) is 1, projects
def api(): """Render help/api page""" categories = cached_cat.get_used() apps = cached_apps.get(categories[0]['short_name']) if len(apps) > 0: app_id = choice(apps)['id'] else: # pragma: no cover app_id = None return render_template('help/api.html', title="Help: API", app_id=app_id)
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 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 apps_cached = [] pages = range(1, 4) import pybossa.cache.apps 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() return True
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.apps as cached_apps import pybossa.cache.categories as cached_cat import pybossa.cache.users as cached_users import pybossa.stats as stats def warm_app(id, short_name): 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: print "Getting stats for %s as it has %s" % (id, n_task_runs) stats.get_stats(id, app.config.get('GEO')) apps_cached.append(id) # Cache top projects cached_apps.get_featured_front_page() apps = cached_apps.get_top() for a in apps: warm_app(a['id'], a['short_name']) for page in pages: apps, count = cached_apps.get_featured('featured', page, app.config['APPS_PER_PAGE']) for a in apps: warm_app(a['id'], a['short_name']) # Categories categories = cached_cat.get_used() for c in categories: for page in pages: apps, count = 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_top()
def test_get_returns_required_fields(self): """Test CACHE PROJECTS get returns the required info about each project""" fields = ('id', 'name', 'short_name', 'info', 'created', 'description', 'last_activity', 'last_activity_raw', 'overall_progress', 'n_tasks', 'n_volunteers', 'owner', 'info') project = self.create_app_with_tasks(1, 0) retrieved_project = cached_apps.get(project.category.short_name)[0] for field in fields: assert retrieved_project.has_key(field), "%s not in app info" % field
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)