def __init__(self, app): self.index_path_root = app.config.get('WHOOSHEE_DIR', '') or 'whooshee' self.search_string_min_len = app.config.get('WHOSHEE_MIN_STRING_LEN', 3) self.writer_timeout = app.config.get('WHOOSHEE_WRITER_TIMEOUT', 2) models_committed.connect(self.on_commit, sender=app) if not os.path.exists(self.index_path_root): os.makedirs(self.index_path_root)
def configure_db(app): if not app.config['TESTING']: cfg = Config.getInstance() db_uri = cfg.getSQLAlchemyDatabaseURI() if db_uri is None: raise Exception( "No proper SQLAlchemy store has been configured. Please edit your indico.conf" ) app.config['SQLALCHEMY_DATABASE_URI'] = db_uri # DB options app.config['SQLALCHEMY_ECHO'] = cfg.getSQLAlchemyEcho() app.config[ 'SQLALCHEMY_RECORD_QUERIES'] = cfg.getSQLAlchemyRecordQueries() app.config['SQLALCHEMY_POOL_SIZE'] = cfg.getSQLAlchemyPoolSize() app.config['SQLALCHEMY_POOL_TIMEOUT'] = cfg.getSQLAlchemyPoolTimeout() app.config['SQLALCHEMY_POOL_RECYCLE'] = cfg.getSQLAlchemyPoolRecycle() app.config['SQLALCHEMY_MAX_OVERFLOW'] = cfg.getSQLAlchemyMaxOverflow() import_all_models() db.init_app(app) if not app.config['TESTING']: apply_db_loggers(app.debug) configure_mappers() # Make sure all backrefs are set models_committed.connect(on_models_committed, app)
def test_flask_fail(self): # XXX This fails due to a bug in Flask-SQLAlchemy that affects # Flask-WhooshAlchemy. I submitted a pull request with a fix that is # pending. from flask.ext.sqlalchemy import before_models_committed, models_committed before_models_committed.connect(_after_flush) models_committed.connect(_after_flush) db.session.add(ObjectB(title=u'my title', content=u'hello world')) db.session.add(ObjectA(title=u'a title', content=u'hello world')) db.session.flush() db.session.commit()
def configure(): index_path = app.config['SEARCH_INDEX_PATH'] models_committed.connect(on_models_committed, sender=app) if not os.path.exists(index_path): os.mkdir(index_path) ix = index.create_in(index_path, search_schema) writer = ix.writer() # Index everything since this is the first time for model in [models.JobType, models.JobCategory, models.JobPost]: for ob in model.query.all(): mapping = ob.search_mapping() public = mapping.pop('public') if public: writer.add_document(**mapping) writer.commit()
def configure_db(app): cfg = Config.getInstance() db_uri = cfg.getSQLAlchemyDatabaseURI() if db_uri is None: raise Exception("No proper SQLAlchemy store has been configured." " Please edit your indico.conf") app.config['SQLALCHEMY_DATABASE_URI'] = db_uri # DB options app.config['SQLALCHEMY_ECHO'] = cfg.getSQLAlchemyEcho() app.config['SQLALCHEMY_RECORD_QUERIES'] = cfg.getSQLAlchemyRecordQueries() app.config['SQLALCHEMY_POOL_SIZE'] = cfg.getSQLAlchemyPoolSize() app.config['SQLALCHEMY_POOL_TIMEOUT'] = cfg.getSQLAlchemyPoolTimeout() app.config['SQLALCHEMY_POOL_RECYCLE'] = cfg.getSQLAlchemyPoolRecycle() app.config['SQLALCHEMY_MAX_OVERFLOW'] = cfg.getSQLAlchemyMaxOverflow() app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = False db.init_app(app) apply_db_loggers(app.debug) configure_mappers() # Make sure all backrefs are set models_committed.connect(on_models_committed, app)
def configure(): models_committed.connect(on_models_committed, sender=app)
with index.writer() as writer: primary_field = values[0][1].pure_whoosh.primary_key_name searchable = values[0][1].__searchable__ for update, v in values: if update: attrs = {} for key in searchable: try: attrs[key] = unicode(getattr(v, key)) except AttributeError: raise AttributeError('{0} does not have {1} field {2}' .format(model, __searchable__, key)) attrs[primary_field] = unicode(getattr(v, primary_field)) writer.update_document(**attrs) else: writer.delete_by_term(primary_field, unicode(getattr(v, primary_field))) models_committed.connect(_after_flush) # def init_app(db): # app = db.get_app() # # for table in db.get_tables_for_bind(): # for item in globals(): # # #_create_index(app, table)
from coprs.views.webhooks_ns import webhooks_general from .context_processors import include_banner, inject_fedmenu setup_log() app.register_blueprint(api_ns.api_ns) app.register_blueprint(admin_ns.admin_ns) app.register_blueprint(coprs_ns.coprs_ns) app.register_blueprint(misc.misc) app.register_blueprint(backend_ns.backend_ns) app.register_blueprint(status_ns.status_ns) app.register_blueprint(recent_ns.recent_ns) app.register_blueprint(stats_receiver.stats_rcv_ns) app.register_blueprint(tmp_ns.tmp_ns) app.register_blueprint(groups_ns) app.register_blueprint(webhooks_ns) app.add_url_rule("/", "coprs_ns.coprs_show", coprs_general.coprs_show) app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True from coprs.rest_api import rest_api_bp, register_api_error_handler, URL_PREFIX register_api_error_handler(app) app.register_blueprint(rest_api_bp, url_prefix=URL_PREFIX) # register_api(app, db) from flask.ext.sqlalchemy import models_committed models_committed.connect(coprs.whoosheers.CoprWhoosheer.on_commit, sender=app)
def register_blueprints(app): app.register_blueprint(blueprint) models_committed.connect(send_chart_data, sender=app)
from flask import url_for from datetime import datetime, timedelta from cbbpoll import db, app from cbbpoll.message import send_reddit_pm from itsdangerous import TimedJSONWebSignatureSerializer as Serializer from sqlalchemy import select, desc from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method from flask.ext.sqlalchemy import models_committed from flask.ext.login import AnonymousUserMixin def on_models_committed(sender, changes): for obj, change in changes: if change == 'insert' and hasattr(obj, '__commit_insert__'): obj.__commit_insert__() models_committed.connect(on_models_committed, sender=app) class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key = True, autoincrement=True) nickname = db.Column(db.String(20), index = True) email = db.Column(db.String(120), index = True) emailConfirmed = db.Column(db.Boolean, default=False) role = db.Column(db.Enum('u', 'a'), default = 'u') accessToken = db.Column(db.String(30)) refreshToken = db.Column(db.String(30)) refreshAfter = db.Column(db.DateTime) emailReminders = db.Column(db.Boolean, default=False) pmReminders = db.Column(db.Boolean, default=False) applicationFlag = db.Column(db.Boolean, default=False) flair = db.Column(db.Integer, db.ForeignKey('team.id')) ballots = db.relationship('Ballot', backref = 'voter', lazy = 'dynamic', cascade="all, delete-orphan",
return 'user created' signal_map = { 'insert': { User: user_created, }, } def signal_router(sender, changes): """ Heper method fo routing ORM signals based on it's kind and sender model """ for change in changes: obj, kind = change try: signal_map[kind][obj.__class__](obj) except KeyError as e: pass models_committed.connect(signal_router) def propagate_user(app): if 'uid' in session: g.user = User.get(session['uid']) else: g.user = User(email='*****@*****.**') request_started.connect(propagate_user)
from flask.ext.sqlalchemy import models_committed from app import app, db from package.models import Release from pit.tasks import run_the_gauntlet class TestRun(db.Model): id = db.Column(db.Integer, primary_key=True) release_id = db.Column(db.Integer, db.ForeignKey("release.id"), nullable=False) release = db.relation(Release, primaryjoin=(release_id == Release.id), backref=db.backref("test_runs", order_by=id)) def on_release_commit(sender, changes): for model, change in changes: if isinstance(model, Release): if model: run_the_gauntlet.delay(model) else: print "False? %s" % model models_committed.connect(on_release_commit, sender=app)
# -*- coding: utf-8 -*- from flask.ext.sqlalchemy import models_committed from flask import g from ._base import * from .account import * __author__ = 'Simi' def _clear_cache(sender, changes): for model, operation in changes: if isinstance(model, Account) and operation != 'update': cache.delete('status-account') models_committed.connect(_clear_cache)
with index.writer() as writer: primary_field = values[0][1].pure_whoosh.primary_key_name searchable = values[0][1].__searchable__ for update, v in values: if update: attrs = {} for key in searchable: try: attrs[key] = unicode(getattr(v, key)) except AttributeError: raise AttributeError( '{0} does not have {1} field {2}'.format( model, __searchable__, key)) attrs[primary_field] = unicode(getattr(v, primary_field)) writer.update_document(**attrs) else: writer.delete_by_term(primary_field, unicode(getattr(v, primary_field))) models_committed.connect(_after_flush) # def init_app(db): # app = db.get_app() # # for table in db.get_tables_for_bind(): # for item in globals(): # # #_create_index(app, table)