# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with PyBossa. If not, see <http://www.gnu.org/licenses/>. """Cache module for projects.""" from sqlalchemy.sql import text from pybossa.core import db, timeouts from pybossa.model.project import Project from pybossa.util import pretty_date from pybossa.cache import memoize, cache, delete_memoized, delete_cached session = db.slave_session @memoize(timeout=timeouts.get('APP_TIMEOUT')) def get_project(short_name): """Get project by short_name.""" project = session.query(Project).filter_by(short_name=short_name).first() return project @cache(timeout=timeouts.get('STATS_FRONTPAGE_TIMEOUT'), key_prefix="front_page_top_projects") def get_top(n=4): """Return top n=4 projects.""" sql = text( '''SELECT project.id, project.name, project.short_name, project.description, project.info, COUNT(project_id) AS total FROM task_run, project
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with PyBossa. If not, see <http://www.gnu.org/licenses/>. from sqlalchemy.sql import text from pybossa.cache import cache, delete_cached from pybossa.core import db, timeouts import pybossa.model as model session = db.slave_session @cache(key_prefix="categories_all", timeout=timeouts.get('CATEGORY_TIMEOUT')) def get_all(): """Return all categories""" data = session.query(model.category.Category).all() return data @cache(key_prefix="categories_used", timeout=timeouts.get('CATEGORY_TIMEOUT')) def get_used(): """Return categories only used by apps""" sql = text(''' SELECT category.* FROM category, app WHERE app.category_id=category.id GROUP BY category.id ''') results = session.execute(sql)
class RackspaceUploader(Uploader): """Rackspace Cloud Files uploader class.""" cf = None cont_name = 'pybossa' def init_app(self, app, cont_name=None): """Init method to create a generic uploader.""" super(self.__class__, self).init_app(app) try: pyrax.set_setting("identity_type", "rackspace") pyrax.set_credentials(username=app.config['RACKSPACE_USERNAME'], api_key=app.config['RACKSPACE_API_KEY'], region=app.config['RACKSPACE_REGION']) self.cf = pyrax.cloudfiles if cont_name: self.cont_name = cont_name return self.cf.get_container(self.cont_name) except pyrax.exceptions.NoSuchContainer: c = self.cf.create_container(self.cont_name) self.cf.make_container_public(self.cont_name) return c def get_container(self, name): """Create a container for the given asset.""" try: return self.cf.get_container(name) except pyrax.exceptions.NoSuchContainer: c = self.cf.create_container(name) self.cf.make_container_public(name) return c def _upload_file_to_rackspace(self, file, container, attempt=0): """Upload file to rackspace.""" try: chksum = pyrax.utils.get_checksum(file) self.cf.upload_file(container, file, obj_name=secure_filename(file.filename), etag=chksum) return True except Exception as e: print "Uploader exception" # TODO: Log this! traceback.print_exc() attempt += 1 if (attempt < 3): time.sleep(1) # Wait one second and try again return self._upload_file_to_rackspace(file, container, attempt) else: print "Tried to upload two times. Failed!" # TODO: Log this! raise def _upload_file(self, file, container): """Upload a file into a container.""" try: cnt = self.get_container(container) obj = cnt.get_object(file.filename) obj.delete() return self._upload_file_to_rackspace(file, container) except pyrax.exceptions.NoSuchObject: return self._upload_file_to_rackspace(file, container) except pyrax.exceptions.UploadFailed: return False @memoize(timeout=timeouts.get('AVATAR_TIMEOUT')) def _lookup_url(self, endpoint, values): """Return Rackspace URL for object.""" try: # Create failover urls for avatars if '_avatar' in values['filename']: failover_url = url_for('static', filename='img/placeholder.user.png') else: failover_url = url_for('static', filename='img/placeholder.project.png') cont = self.get_container(values['container']) if cont.cdn_enabled: return "%s/%s" % (cont.cdn_ssl_uri, values['filename']) else: msg = ("Rackspace Container %s was not public" % values['container']) current_app.logger.warning(msg) cont.make_public() return "%s/%s" % (cont.cdn_ssl_uri, values['filename']) except: current_app.logger.error(traceback.print_exc()) return failover_url def delete_file(self, name, container): """Delete file from container.""" try: cnt = self.get_container(container) obj = cnt.get_object(name) obj.delete() return True except: return False def file_exists(self, name, container): """Check if a file exists in a container""" try: cnt = self.get_container(container) obj = cnt.get_object(name) return obj is not None except pyrax.exceptions.NoSuchObject: return False
# # PyBossa is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with PyBossa. If not, see <http://www.gnu.org/licenses/>. from sqlalchemy.sql import text from pybossa.cache import cache, delete_cached from pybossa.core import db, timeouts, get_session import pybossa.model as model @cache(key_prefix="categories_all", timeout=timeouts.get('CATEGORY_TIMEOUT')) def get_all(): """Return all categories""" try: session = get_session(db, bind='slave') data = session.query(model.category.Category).all() return data except: # pragma: no cover session.rollback() raise finally: session.close() @cache(key_prefix="categories_used", timeout=timeouts.get('CATEGORY_TIMEOUT')) def get_used():
session = db.slave_session def get_leaderboard(n, user_id=None, window=0, info=None): """Return the top n users with their rank.""" try: return gl(top_users=n, user_id=user_id, window=window, info=info) except ProgrammingError: db.session.rollback() lb(info=info) return gl(top_users=n, user_id=user_id, window=window, info=info) @memoize(timeout=timeouts.get('USER_TIMEOUT')) def n_projects_contributed(user_id): """Return number of projects user has contributed to.""" sql = text(''' WITH projects_contributed AS (SELECT DISTINCT project_id FROM task_run WHERE user_id =:user_id) SELECT COUNT(*) AS total_projects_contributed FROM projects_contributed; ''') results = session.execute(sql, dict(user_id=user_id)) total_projects_contributed = 0 for row in results: total_projects_contributed = row.total_projects_contributed return total_projects_contributed
from pybossa.model.featured import Featured from pybossa.model.app import App from pybossa.model.task import Task from pybossa.model.task_run import TaskRun from pybossa.util import pretty_date from pybossa.cache import memoize, cache, delete_memoized, delete_cached import json import string import operator import datetime import time from datetime import timedelta @memoize(timeout=timeouts.get('APP_TIMEOUT')) def get_app(short_name): try: session = get_session(db, bind='slave') app = session.query(App).filter_by(short_name=short_name).first() return app except: # pragma: no cover session.rollback() raise finally: session.close() @cache(timeout=timeouts.get('STATS_FRONTPAGE_TIMEOUT'), key_prefix="front_page_featured_apps") def get_featured_front_page():
# You should have received a copy of the GNU Affero General Public License # along with PyBossa. If not, see <http://www.gnu.org/licenses/>. """Cache module for users.""" from sqlalchemy.sql import text from pybossa.core import db, timeouts from pybossa.cache import cache, memoize, delete_memoized from pybossa.util import pretty_date from pybossa.model.user import User from pybossa.cache.projects import overall_progress, n_tasks, n_volunteers import json session = db.slave_session @memoize(timeout=timeouts.get('USER_TIMEOUT')) def get_leaderboard(n, user_id): """Return the top n users with their rank.""" sql = text(''' WITH global_rank AS ( WITH scores AS ( SELECT user_id, COUNT(*) AS score FROM task_run WHERE user_id IS NOT NULL GROUP BY user_id) SELECT user_id, score, rank() OVER (ORDER BY score desc) FROM scores) SELECT rank, id, name, fullname, email_addr, info, score FROM global_rank JOIN public."user" on (user_id=public."user".id) ORDER BY rank LIMIT :limit; ''')
# PYBOSSA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>. from sqlalchemy.sql import text from pybossa.cache import cache, delete_cached from pybossa.core import db, timeouts import pybossa.model as model session = db.slave_session @cache(key_prefix="categories_all", timeout=timeouts.get('CATEGORY_TIMEOUT')) def get_all(): """Return all categories""" data = session.query(model.category.Category).all() return data @cache(key_prefix="categories_used", timeout=timeouts.get('CATEGORY_TIMEOUT')) def get_used(): """Return categories only used by projects""" sql = text(''' SELECT category.* FROM category, project WHERE project.category_id=category.id GROUP BY category.id ''') results = session.execute(sql)
# GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with PyBossa. If not, see <http://www.gnu.org/licenses/>. """Cache module for users.""" from sqlalchemy.sql import text from pybossa.core import db, timeouts from pybossa.cache import cache, memoize, delete_memoized from pybossa.util import pretty_date from pybossa.model.user import User from pybossa.cache.projects import overall_progress, n_tasks, n_volunteers session = db.slave_session @memoize(timeout=timeouts.get('USER_TIMEOUT_LEADERBOARD')) def get_leaderboard(n, user_id=None, project_id=None, anonymize=False): """Return the top n users with their rank.""" sql = None results = None if project_id is not None: sql = text(''' WITH global_rank AS ( WITH scores AS ( SELECT user_id, COUNT(*) AS score FROM task_run WHERE user_id IS NOT NULL AND project_id=:project_id GROUP BY user_id) SELECT user_id, score, rank() OVER (ORDER BY score desc) FROM scores) SELECT rank, id, name, fullname, email_addr, info, created,
# along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>. """Cache module for projects.""" from sqlalchemy.sql import text from pybossa.core import db, timeouts from pybossa.model.project import Project from pybossa.util import pretty_date, static_vars, convert_utc_to_est from pybossa.cache import memoize, cache, delete_memoized, delete_cached, \ memoize_essentials, delete_memoized_essential, delete_cache_group from pybossa.cache.task_browse_helpers import get_task_filters, allowed_fields import app_settings session = db.slave_session @cache(timeout=timeouts.get('STATS_FRONTPAGE_TIMEOUT'), key_prefix="front_page_top_projects") def get_top(n=4): """Return top n=4 projects.""" sql = text('''SELECT project.id, project.name, project.short_name, project.description, project.info, COUNT(project_id) AS total FROM task_run, project WHERE project_id IS NOT NULL AND project.id=project_id AND (project.info->>'passwd_hash') IS NULL GROUP BY project.id ORDER BY total DESC LIMIT :limit;''') results = session.execute(sql, dict(limit=n)) top_projects = [] for row in results: project = dict(id=row.id, name=row.name, short_name=row.short_name,
def decorator(cls): def rt_handler(self, key): method = request.method if not key: return error.format_exception(BadRequest('Invalid Key'), cls.__name__.lower(), method) oid = key_to_oid(key) if not oid: return error.format_exception(NotFound('Object not found'), cls.__name__.lower(), method) return getattr(super(cls, self), method.lower())(oid) for action in ('get', 'put', 'delete'): setattr(cls, action, rt_handler) return cls return decorator @memoize(timeout=timeouts.get('APP_TIMEOUT')) def project_name_to_oid(shortname): project = project_repo.get_by_shortname(shortname) return project.id if project else None @methodview_altkey(project_name_to_oid) class ProjectByNameAPI(ProjectAPI): pass
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with PyBossa. If not, see <http://www.gnu.org/licenses/>. from sqlalchemy.sql import text from pybossa.cache import cache, delete_cached from pybossa.core import db, timeouts import pybossa.model as model session = db.slave_session @cache(key_prefix="categories_all", timeout=timeouts.get('CATEGORY_TIMEOUT')) def get_all(): """Return all categories""" data = session.query(model.category.Category).all() return data @cache(key_prefix="categories_used", timeout=timeouts.get('CATEGORY_TIMEOUT')) def get_used(): """Return categories only used by projects""" sql = text(''' SELECT category.* FROM category, project WHERE project.category_id=category.id GROUP BY category.id ''') results = session.execute(sql)
# GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with PyBossa. If not, see <http://www.gnu.org/licenses/>. """Cache module for projects.""" from sqlalchemy.sql import text from pybossa.core import db, timeouts from pybossa.model.project import Project from pybossa.util import pretty_date from pybossa.cache import memoize, cache, delete_memoized, delete_cached session = db.slave_session @memoize(timeout=timeouts.get("APP_TIMEOUT")) def get_project(short_name): """Get project by short_name.""" project = session.query(Project).filter_by(short_name=short_name).first() return project @cache(timeout=timeouts.get("STATS_FRONTPAGE_TIMEOUT"), key_prefix="front_page_top_projects") def get_top(n=4): """Return top n=4 projects.""" sql = text( """SELECT project.id, project.name, project.short_name, project.description, project.info, COUNT(project_id) AS total FROM task_run, project WHERE project_id IS NOT NULL AND project.id=project_id AND project.hidden=0 GROUP BY project.id ORDER BY total DESC LIMIT :limit;"""
# # You should have received a copy of the GNU Affero General Public License # along with PyBossa. If not, see <http://www.gnu.org/licenses/>. """Cache module for users.""" from sqlalchemy.sql import text from pybossa.core import db, timeouts from pybossa.cache import cache, memoize, delete_memoized from pybossa.util import pretty_date from pybossa.model.user import User from pybossa.cache.projects import overall_progress, n_tasks, n_volunteers session = db.slave_session @memoize(timeout=timeouts.get("USER_TIMEOUT")) def get_leaderboard(n, user_id=None): """Return the top n users with their rank.""" sql = text( """ WITH global_rank AS ( WITH scores AS ( SELECT user_id, COUNT(*) AS score FROM task_run WHERE user_id IS NOT NULL GROUP BY user_id) SELECT user_id, score, rank() OVER (ORDER BY score desc) FROM scores) SELECT rank, id, name, fullname, email_addr, info, created, score FROM global_rank JOIN public."user" on (user_id=public."user".id) ORDER BY rank LIMIT :limit; """