class TestsWithInitApp(BaseTestCases.BaseTest): def setUp(self): self.wh = Whooshee() self.wh.init_app(self.app) super(TestsWithInitApp, self).setUp()
def setUp(self): self.wh = Whooshee() super(TestsWithInitApp, self).setUp() # we intentionally call `init_app` after creating whoosheers, to test that lazy app # intialization is possible self.wh.init_app(self.app) self.ctx = self.app.app_context() self.ctx.push()
def setUp(self): self.app = Flask(__name__) self.app.config['WHOOSHEE_MEMORY_STORAGE'] = True self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' self.app.config['TESTING'] = True self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False self.db = SQLAlchemy(self.app) self.wh = Whooshee(self.app) class User(self.db.Model): id = self.db.Column(self.db.Integer, primary_key=True) name = self.db.Column(self.db.String) # Need to make sure BigInteger PK's can be created @self.wh.register_model('title', 'content') class Entry(self.db.Model): id = self.db.Column(self.db.BigInteger, primary_key=True) title = self.db.Column(self.db.String) content = self.db.Column(self.db.Text) user = self.db.relationship(User, backref = self.db.backref('entries')) user_id = self.db.Column(self.db.Integer, self.db.ForeignKey('user.id')) self.User = User self.Entry = Entry self.db.create_all(app=self.app) self.u1 = User(name=u'chuck') self.e1 = Entry(id=1000000000000, title=u'chuck nr. 1 article', content=u'blah blah blah', user=self.u1) self.db.session.commit()
class TestsWithInitApp(BaseTestCases.BaseTest): def setUp(self): self.wh = Whooshee() super(TestsWithInitApp, self).setUp() # we intentionally call `init_app` after creating whoosheers, to test that lazy app # intialization is possible self.wh.init_app(self.app) self.ctx = self.app.app_context() self.ctx.push() def tearDown(self): super(TestsWithInitApp, self).tearDown() self.ctx.pop()
def setUp(self): self.app = Flask(__name__) self.app.config['WHOOSHEE_MEMORY_STORAGE'] = True self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' self.app.config['TESTING'] = True self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False self.db = SQLAlchemy(self.app) self.wh = Whooshee(self.app)
def update_indexes_quick(minutes_passed): """ Recreates whoosh indexes for projects for which indexed data were updated in last n minutes. Doesn't update schema. """ index = Whooshee.get_or_create_index(app, CoprWhoosheer) writer = index.writer() query = db.session.query(models.Copr).filter( models.Copr.latest_indexed_data_update >= time.time() - int(minutes_passed) * 60) for copr in query.all(): CoprWhoosheer.update_copr(writer, copr) writer.commit()
def run(self): index = Whooshee.get_or_create_index(app, CoprWhoosheer) writer = index.writer() for copr in coprs_logic.CoprsLogic.get_all(): CoprWhoosheer.delete_copr(writer, copr) writer.commit(optimize=True) writer = index.writer() writer.schema = CoprWhoosheer.schema writer.commit(optimize=True) writer = index.writer() for copr in coprs_logic.CoprsLogic.get_all(): CoprWhoosheer.insert_copr(writer, copr) writer.commit(optimize=True)
def test_fulltext_whooshee_return_all_hits(self, f_users, f_db): # https://bugzilla.redhat.com/show_bug.cgi?id=1153039 self.prefix = u"prefix" self.s_coprs = [] index = Whooshee.get_or_create_index(app, CoprWhoosheer) writer = index.writer() u1_count = 150 for x in range(u1_count): self.s_coprs.append( models.Copr(name=self.prefix + str(x), user=self.u1)) u2_count = 7 for x in range(u2_count): self.s_coprs.append( models.Copr(name=self.prefix + str(x), user=self.u2)) u3_count = 9 for x in range(u3_count): self.s_coprs.append( models.Copr(name=u"_wrong_" + str(x), user=self.u3)) self.db.session.add_all(self.s_coprs) self.db.session.commit() for copr in self.s_coprs: CoprWhoosheer.insert_copr(writer, copr) writer.commit(optimize=True) query = CoprsLogic.get_multiple_fulltext("prefix") pre_query = models.Copr.query.order_by(desc(models.Copr.created_on))\ .join(models.User).filter(models.Copr.deleted == False) query = pre_query.whooshee_search( self.prefix, whoosheer=CoprWhoosheer) # needs flask-whooshee-0.2.0 results = query.all() for obj in results: assert self.prefix in obj.name obtained = len(results) expected = u1_count + u2_count assert obtained == expected
def update_indexes(): """ recreates whoosh indexes for all projects """ index = Whooshee.get_or_create_index(app, CoprWhoosheer) writer = index.writer() for copr in coprs_logic.CoprsLogic.get_all(): CoprWhoosheer.delete_copr(writer, copr) writer.commit(optimize=True) writer = index.writer() writer.schema = CoprWhoosheer.schema writer.commit(optimize=True) writer = index.writer() for copr in coprs_logic.CoprsLogic.get_all(): CoprWhoosheer.insert_copr(writer, copr) writer.commit(optimize=True) WhoosheeStamp.store()
def test_project_list_search(self, f_users, f_mock_chroots, f_coprs, f_db): self.prefix = u"prefix" self.s_coprs = [] c1_username = self.c1.user.username index = Whooshee.get_or_create_index(app, CoprWhoosheer) writer = index.writer() k1 = 3 k2 = 5 for x in range(k1): self.s_coprs.append(Copr(name=self.prefix + str(x), user=self.u1)) for x in range(k2): self.s_coprs.append(Copr(name=self.prefix + str(x), user=self.u2)) self.db.session.add_all(self.s_coprs) self.db.session.commit() for copr in self.s_coprs: CoprWhoosheer.insert_copr(writer, copr) writer.commit(optimize=True) r0 = self.tc.get(u"/api_2/projects?search_query={}".format( self.prefix)) assert r0.status_code == 200 obj = json.loads(r0.data.decode("utf-8")) assert len(obj["projects"]) == k1 + k2 for p in obj["projects"]: assert self.prefix in p["project"]["name"] r1 = self.tc.get(u"/api_2/projects?search_query={}&owner={}".format( self.prefix, c1_username)) assert r1.status_code == 200 obj = json.loads(r1.data.decode("utf-8")) assert len(obj["projects"]) == k1 for p in obj["projects"]: assert self.prefix in p["project"]["name"]
def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # not using sqlalchemy event system, hence disabling it app.config['UPLOADED_IMAGES_DEST'] = 'C:/Users/oem/Desktop/marketplace/marketplace/app/static/frontend/images/' if \ not os.environ.get('UPLOADED_IMAGES_DEST') else os.path.dirname(os.path.realpath(__file__)) + os.environ.get( 'UPLOADED_IMAGES_DEST') app.config['UPLOADED_DOCS_DEST'] = ':/Users/oem/Desktop/marketplace/marketplace/appstatic/docs/' if \ not os.environ.get('UPLOADED_DOCS_DEST') else os.path.dirname(os.path.realpath(__file__)) + os.environ.get( 'UPLOADED_DOCS_DEST') app.config['docs'] = app.config['UPLOADED_DOCS_DEST'] app.config['CKEDITOR_SERVE_LOCAL'] = True app.config['CKEDITOR_HEIGHT'] = 400 app.config['CKEDITOR_FILE_UPLOADER'] = 'upload' app.config['CKEDITOR_ENABLE_CSRF'] = True # if you want to enable CSRF protect, uncomment this line app.config['UPLOADED_PATH'] = os.path.join(basedir, 'uploads') #app.config['WHOOSH_BASE']='whoosh' config[config_name].init_app(app) # Set up extensions mail.init_app(app) db.init_app(app) login_manager.init_app(app) csrf.init_app(app) compress.init_app(app) RQ(app) configure_uploads(app, (images)) configure_uploads(app, docs) ckeditor = CKEditor(app) share.init_app(app) moment.init_app(app) jwt.init_app(app) sess.init_app(app) #whooshee.init_app(app) whooshee = Whooshee(app) # Register Jinja template functions from .utils import register_template_utils register_template_utils(app) # Set up asset pipeline assets_env = Environment(app) dirs = ['assets/styles', 'assets/scripts'] for path in dirs: assets_env.append_path(os.path.join(basedir, path)) assets_env.url_expire = True assets_env.register('app_css', app_css) assets_env.register('app_js', app_js) assets_env.register('vendor_css', vendor_css) assets_env.register('vendor_js', vendor_js) # Configure SSL if platform supports it if not app.debug and not app.testing and not app.config['SSL_DISABLE']: from flask_sslify import SSLify SSLify(app) # Create app blueprints from .blueprints.public import public as public_blueprint app.register_blueprint(public_blueprint) from .blueprints.seo_world import seo_world as seo_world_blueprint app.register_blueprint(seo_world_blueprint) from .blueprints.main import main as main_blueprint app.register_blueprint(main_blueprint) from .blueprints.account import account as account_blueprint app.register_blueprint(account_blueprint, url_prefix='/account') from .blueprints.admin import admin as admin_blueprint app.register_blueprint(admin_blueprint, url_prefix='/admin') from .blueprints.marketplace import marketplace as marketplace_blueprint app.register_blueprint(marketplace_blueprint, url_prefix='/marketplace') from .blueprints.organisations import organisations as organisations_blueprint app.register_blueprint(organisations_blueprint, url_prefix='/organisations') from .blueprints.sitemaps import sitemaps as sitemaps_blueprint app.register_blueprint(sitemaps_blueprint) from .blueprints.api import api as apis_blueprint app.register_blueprint(apis_blueprint, url_prefix='/api') # main_api.init_app(app) app.jinja_env.globals.update(json_load=json_load, image_size=image_size, get_cart=get_cart) @app.before_request def before_request(): try: session['cart_id'] except: u = uuid.uuid4() user_agent = request.headers.get('User-Agent') if user_agent is not None: user_agent = user_agent.encode('utf-8') base = 'cart: {0}|{1}|{2}'.format(_get_remote_addr(), user_agent, u) if str is bytes: base = text_type(base, 'utf-8', errors='replace') # pragma: no cover h = sha512() h.update(base.encode('utf8')) session['cart_id'] = h.hexdigest() @app.cli.command() def reindex(): with app.app_context(): whooshee.reindex() @app.cli.command() def routes(): """'Display registered routes""" rules = [] for rule in app.url_map.iter_rules(): methods = ','.join(sorted(rule.methods)) rules.append((rule.endpoint, methods, str(rule))) sort_by_rule = operator.itemgetter(2) for endpoint, methods, rule in sorted(rules, key=sort_by_rule): route = '{:50s} {:25s} {}'.format(endpoint, methods, rule) print(route) @app.template_filter('product') def product(o): """check if object is user""" from app.models import MProduct return o.__class__ == MProduct return app
class TestMultipleApps(TestCase): def setUp(self): self.db = SQLAlchemy() self.wh = Whooshee() for a in ['app1', 'app2']: app = Flask(a) app.config['WHOOSHEE_DIR'] = tempfile.mkdtemp() app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' app.config['TESTING'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False self.db.init_app(app) self.wh.init_app(app) setattr(self, a, app) class User(self.db.Model): id = self.db.Column(self.db.Integer, primary_key=True) name = self.db.Column(self.db.String) # separate index for just entry @self.wh.register_model('title', 'content') class Entry(self.db.Model): id = self.db.Column(self.db.Integer, primary_key=True) title = self.db.Column(self.db.String) content = self.db.Column(self.db.Text) user = self.db.relationship(User, backref = self.db.backref('entries')) user_id = self.db.Column(self.db.Integer, self.db.ForeignKey('user.id')) # index for both entry and user @self.wh.register_whoosheer class EntryUserWhoosheer(AbstractWhoosheer): schema = whoosh.fields.Schema( entry_id = whoosh.fields.NUMERIC(stored=True, unique=True), user_id = whoosh.fields.NUMERIC(stored=True), username = whoosh.fields.TEXT(), title = whoosh.fields.TEXT(), content = whoosh.fields.TEXT()) models = [Entry, User] @classmethod def update_user(cls, writer, user): pass # TODO: update all users entries @classmethod def update_entry(cls, writer, entry): writer.update_document(entry_id=entry.id, user_id=entry.user.id, username=entry.user.name, title=entry.title, content=entry.content) @classmethod def insert_user(cls, writer, user): # nothing, user doesn't have entries yet pass @classmethod def insert_entry(cls, writer, entry): writer.add_document(entry_id=entry.id, user_id=entry.user.id, username=entry.user.name, title=entry.title, content=entry.content) @classmethod def delete_user(cls, writer, user): # nothing, user doesn't have entries yet pass @classmethod def delete_entry(cls, writer, entry): writer.delete_by_term('entry_id', entry.id) self.User = User self.Entry = Entry self.EntryUserWhoosheer = EntryUserWhoosheer self.db.create_all(app=self.app1) self.db.create_all(app=self.app2) self.u1 = User(name=u'chuck') self.u2 = User(name=u'arnold') self.u3 = User(name=u'silvester') self.e1 = Entry(title=u'chuck nr. 1 article', content=u'blah blah blah', user=self.u1) self.e2 = Entry(title=u'norris nr. 2 article', content=u'spam spam spam', user=self.u1) self.e3 = Entry(title=u'arnold blah', content=u'spam is cool', user=self.u2) self.e4 = Entry(title=u'the less dangerous', content=u'chuck is better', user=self.u3) self.all_inst = [self.u1, self.u2, self.u3, self.e1, self.e2, self.e3, self.e4] def tearDown(self): shutil.rmtree(self.app1.config['WHOOSHEE_DIR'], ignore_errors=True) shutil.rmtree(self.app2.config['WHOOSHEE_DIR'], ignore_errors=True) self.db.drop_all(app=self.app1) self.db.drop_all(app=self.app2) def test_multiple_apps(self): # IIUC, you can't add the same model instance under multiple apps with flask-sqlalchemy # this pretty much reduces the testing to "make sure we used the right app to do # the search" with self.app1.test_request_context(): self.db.session.add_all([self.u2, self.u3, self.e3, self.e4]) self.db.session.commit() with self.app2.test_request_context(): self.db.session.add_all([self.u1, self.e1, self.e2]) self.db.session.commit() # make sure that entities stored only for app1 are only found for app1, same for app2 with self.app1.test_request_context(): q = self.Entry.query.whooshee_search('chuck') self.assertEqual(len(q.all()), 1) self.assertEqual(q[0].title, 'the less dangerous') with self.app2.test_request_context(): q = self.Entry.query.whooshee_search('chuck') self.assertEqual(len(q.all()), 1) self.assertEqual(q[0].title, 'chuck nr. 1 article') # try deleting everything from one app and then searching again with self.app1.test_request_context(): self.Entry.query.delete() q = self.Entry.query.whooshee_search('chuck') self.assertEqual(len(q.all()), 0) with self.app2.test_request_context(): q = self.Entry.query.whooshee_search('chuck') self.assertEqual(len(q.all()), 1) self.assertEqual(q[0].title, 'chuck nr. 1 article')
def setUp(self): self.db = SQLAlchemy() self.wh = Whooshee() for a in ['app1', 'app2']: app = Flask(a) app.config['WHOOSHEE_DIR'] = tempfile.mkdtemp() app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' app.config['TESTING'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False self.db.init_app(app) self.wh.init_app(app) setattr(self, a, app) class User(self.db.Model): id = self.db.Column(self.db.Integer, primary_key=True) name = self.db.Column(self.db.String) # separate index for just entry @self.wh.register_model('title', 'content') class Entry(self.db.Model): id = self.db.Column(self.db.Integer, primary_key=True) title = self.db.Column(self.db.String) content = self.db.Column(self.db.Text) user = self.db.relationship(User, backref = self.db.backref('entries')) user_id = self.db.Column(self.db.Integer, self.db.ForeignKey('user.id')) # index for both entry and user @self.wh.register_whoosheer class EntryUserWhoosheer(AbstractWhoosheer): schema = whoosh.fields.Schema( entry_id = whoosh.fields.NUMERIC(stored=True, unique=True), user_id = whoosh.fields.NUMERIC(stored=True), username = whoosh.fields.TEXT(), title = whoosh.fields.TEXT(), content = whoosh.fields.TEXT()) models = [Entry, User] @classmethod def update_user(cls, writer, user): pass # TODO: update all users entries @classmethod def update_entry(cls, writer, entry): writer.update_document(entry_id=entry.id, user_id=entry.user.id, username=entry.user.name, title=entry.title, content=entry.content) @classmethod def insert_user(cls, writer, user): # nothing, user doesn't have entries yet pass @classmethod def insert_entry(cls, writer, entry): writer.add_document(entry_id=entry.id, user_id=entry.user.id, username=entry.user.name, title=entry.title, content=entry.content) @classmethod def delete_user(cls, writer, user): # nothing, user doesn't have entries yet pass @classmethod def delete_entry(cls, writer, entry): writer.delete_by_term('entry_id', entry.id) self.User = User self.Entry = Entry self.EntryUserWhoosheer = EntryUserWhoosheer self.db.create_all(app=self.app1) self.db.create_all(app=self.app2) self.u1 = User(name=u'chuck') self.u2 = User(name=u'arnold') self.u3 = User(name=u'silvester') self.e1 = Entry(title=u'chuck nr. 1 article', content=u'blah blah blah', user=self.u1) self.e2 = Entry(title=u'norris nr. 2 article', content=u'spam spam spam', user=self.u1) self.e3 = Entry(title=u'arnold blah', content=u'spam is cool', user=self.u2) self.e4 = Entry(title=u'the less dangerous', content=u'chuck is better', user=self.u3) self.all_inst = [self.u1, self.u2, self.u3, self.e1, self.e2, self.e3, self.e4]
def setUp(self): self.wh = Whooshee() self.wh.init_app(self.app) super(TestsWithInitApp, self).setUp()
def setUp(self): self.wh = Whooshee(self.app) super(TestsWithApp, self).setUp()
from config import Config from flask_jwt_extended import JWTManager from flask_cors import CORS import paypalrestsdk app = Flask(__name__) api = Api(app) cors = CORS(app, resources={r"/*": {"origins": "*"}}) app.config.from_object(Config) login = LoginManager(app) jwt = JWTManager(app) login.login_view = 'login' db = SQLAlchemy(app) migrate = Migrate(app, db) whooshee = Whooshee() whooshee.init_app(app) paypalrestsdk.configure({ "mode": 'sandbox', "client_id": app.config['PAYPAL_CREDENTIALS']['id'], "client_secret": app.config['PAYPAL_CREDENTIALS']['secret'] }) @app.before_first_request def init_plans(): from paypalrestsdk import BillingPlan pro_plan = BillingPlan({ "name": "Pro subscription plan",
from flask_bootstrap import Bootstrap from flask_whooshee import Whooshee from flask_wtf import CSRFProtect app = Flask(__name__) app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'dev') app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('SQLALCHEMY_DATABASE_URI') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['WHOOSHEE_MIN_STRING_LEN'] = 1 # pythonanywhere部署时需要设置该值 app.config['SQLALCHEMY_POOL_RECYLE'] = 280 db = SQLAlchemy(app) login_manager = LoginManager(app) bootstrap = Bootstrap(app) whooshee = Whooshee(app) csrf = CSRFProtect(app) @login_manager.user_loader def load_user(user_id): from .models import User user = User.query.get(int(user_id)) return user login_manager.login_view = 'login' # login_manager.login_message = 'Your custom message'
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_admin import Admin from whatsdown.config import name, db_name, password from flask_whooshee import Whooshee from datetime import timedelta app = Flask(__name__) # db and admin config app.secret_key = 'w4lepsze' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://{}:{}@db/{}'.format( name, password, db_name) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True app.config['FLASK_ADMIN_SWATCH'] = 'cerulean' app.config['WHOOSHEE_MIN_STRING_LEN'] = 1 app.config['WHOOSHEE_DIR'] = 'whooshee_indexes' app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=10) db = SQLAlchemy(app) Migrate(app, db) admin = Admin(app) whooshee = Whooshee(app) whooshee.reindex()
from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_whooshee import Whooshee from authomatic import Authomatic from authomatic.providers import oauth2 page = Flask(__name__, static_url_path='/static') page.config.from_object('config') UPLOAD_FOLDER = '/home/manan/Programs/EduTech/app/static/userdata/' page.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER CONFIG = { 'google': { 'class_': oauth2.Google, 'consumer_key': '1005905952791-tfb0dpv4iomdr0edhtdpnl9mnu0t0nsp.apps.googleusercontent.com', 'consumer_secret': 'u5qpMJhO8P-g4HF2wB7z4nwb', 'scope': oauth2.Google.user_info_scope } } authomatic = Authomatic(CONFIG, 'random secret string for session signing') db = SQLAlchemy(page) migrate = Migrate(page, db) whooshee = Whooshee(page) from app import views, models
from flask_bootstrap import Bootstrap from flask_dropzone import Dropzone from flask_login import LoginManager, AnonymousUserMixin from flask_mail import Mail from flask_moment import Moment from flask_sqlalchemy import SQLAlchemy from flask_whooshee import Whooshee from flask_wtf import CSRFProtect bootstrap = Bootstrap() db = SQLAlchemy() login_manager = LoginManager() mail = Mail() dropzone = Dropzone() moment = Moment() whooshee = Whooshee() avatars = Avatars() csrf = CSRFProtect() @login_manager.user_loader def load_user(user_id): from albumy.models import User user = User.query.get(int(user_id)) return user login_manager.login_view = 'auth.login' login_manager.login_message_category = 'warning' login_manager.refresh_view = 'auth.re_authenticate' login_manager.needs_refresh_message_category = 'warning' class Guest(AnonymousUserMixin):
from flask_bootstrap import Bootstrap from flask_dropzone import Dropzone from flask_login import LoginManager, AnonymousUserMixin from flask_mail import Mail from flask_moment import Moment from flask_sqlalchemy import SQLAlchemy from flask_wtf import CSRFProtect from flask_whooshee import Whooshee bootstrap = Bootstrap() db = SQLAlchemy() login_manager = LoginManager() mail = Mail() dropzone = Dropzone() # 图片上传 moment = Moment() whooshee = Whooshee() # 全文搜索 avatars = Avatars() # 头像功能 csrf = CSRFProtect() # 安全模块 @login_manager.user_loader def loader_user(user_id): from photosocial.models import User user = User.query.get(int(user_id)) return user login_manager.login_view = 'auth.login' login_manager.login_message_category = 'warning' login_manager.refresh_view = 'auth.re_authenticate' login_manager.needs_refresh_message_category = 'warning'