def test_search_clips(self): user = User(username=USER_NAME, password=USER_PASSWORD, email=USER_EMAIL) tag1 = Tag(name="tag-1") tag2 = Tag(name="tag-2") clip = Clip(title=CLIP_TITLE, description=CLIP_DESCRIPTION, url=CLIP_URL_TWITTER) clip.user = user clip.tags = [tag1, tag2] db.session.add(clip) db.session.commit() search.create_index(self.index_dir) search.add_clip(clip) results = search.search_clips(CLIP_TITLE, 1) self.assertEqual(results.length, 1) self.assertEqual(results.clips[0].title, CLIP_TITLE) results = search.search_clips(CLIP_DESCRIPTION, 1) self.assertEqual(results.length, 1) self.assertEqual(results.clips[0].title, CLIP_TITLE) results = search.search_clips("tag 1", 1) self.assertEqual(results.length, 1) self.assertEqual(results.clips[0].title, CLIP_TITLE) results = search.search_clips(USER_NAME, 1) self.assertEqual(results.length, 1) self.assertEqual(results.clips[0].title, CLIP_TITLE)
def rebuild_index(): # pragma: no cover from speeddb.models.clips import Clip click.echo('Removing whoosh directory...') shutil.rmtree(app.config['WHOOSH_INDEX']) click.echo('Recreating index...') search.create_index(app.config['WHOOSH_INDEX']) click.echo('Adding clips to index...') clips = Clip.query.all() search.add_clips(clips) click.echo('Done!')
def main(): app = create_app() app.app_context().push() fake = Faker() search.create_index(app.config['WHOOSH_INDEX']) user = User(username=fake.user_name(), password=fake.password(), email=fake.email(), active=True) print('Created user') tag = Tag.query.filter_by(name='test-data').first() if tag == None: tag = Tag(name='test-data') db.session.add(tag) db.session.add(user) db.session.commit() print('Creating clips...') clips = [] for i in range(NUM_OF_ENTRIES): if i % 1000 == 0: print('Clip %d of %d' % (i, NUM_OF_ENTRIES)) clip = Clip( title=fake.sentence(), description=fake.text(), url='https://www.youtube.com/watch?v=Zxv5rE1TLJg', # Lost Control 2 user_id=user.id, tags=[tag]) db.session.add(clip) clips.append(clip) print('Committing to db') db.session.commit() print('Adding to search') search.add_clips(clips) print('Done!')
def test_create_index_already_exists(self, mock_exists_in, mock_open_dir): mock_exists_in.return_value = True search.create_index(self.index_dir) mock_open_dir.assert_called()
def test_create_index(self, mock_create_in): search.create_index(self.index_dir) mock_create_in.assert_called()
def create_app(extra_config_options={}): # Create the flask app app = Flask(__name__, instance_relative_config=True) app.config.from_object('speeddb.default_settings') app.config.from_pyfile('application.cfg', silent=True) app.config.from_mapping(extra_config_options) if app.config['ENABLE_LOGGING'] and not app.config[ 'TESTING']: # pragma: no cover mail_handler = SMTPHandler(mailhost=app.config['MAIL_SERVER'], fromaddr=app.config['MAIL_ERROR_SENDER'], toaddrs=[app.config['MAIL_ERROR_RECV']], subject=app.config['MAIL_ERROR_SUBJECT'], credentials=(app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])) mail_handler.setLevel(logging.ERROR) mail_handler.setFormatter( logging.Formatter( '[%(asctime)s] %(levelname)s in %(module)s: %(message)s')) app.logger.addHandler(mail_handler) file_handler = TimedRotatingFileHandler(app.config['LOG_FILENAME'], when='midnight') file_handler.setLevel(logging.WARNING) app.logger.addHandler(file_handler) global statsd statsd = StatsClient(host=app.config['STATSD_HOST'], port=app.config['STATSD_PORT'], prefix=app.config['STATSD_PREFIX']) # Setup mail mail.init_app(app) # Create the database db.init_app(app) # Create CSRF protection csrf.init_app(app) # Initialize the oembed cache import speeddb.oembed_cache as oembed_cache oembed_cache.init_cache( cache_type=app.config['OEMBED_CACHE_TYPE'], cache_dir=app.config['OEMBED_CACHE_FILE_DIRECTORY'], timeout=app.config['OEMBED_CACHE_TIMEOUT']) # Create the search index import speeddb.search as search search.create_index(app.config['WHOOSH_INDEX']) from speeddb.views import clip, index, report, search, user, tags from speeddb.views import blueprint app.register_blueprint(blueprint) from speeddb.models.user import User import speeddb.models.tags import speeddb.models.clips # Register the user class db_adapter = SQLAlchemyAdapter(db, User) user_manager = UserManager(db_adapter, app, register_form=forms.RecaptchaRegisterForm, login_form=forms.LoginFormWithBans) @app.before_request def before_request(): g.user = current_user g.user_is_admin = util.is_admin(current_user) @app.cli.command() def init_db(): # pragma: no cover click.echo('Creating the db...') db.create_all() click.echo('Done!') @app.cli.command() def rebuild_index(): # pragma: no cover from speeddb.models.clips import Clip click.echo('Removing whoosh directory...') shutil.rmtree(app.config['WHOOSH_INDEX']) click.echo('Recreating index...') search.create_index(app.config['WHOOSH_INDEX']) click.echo('Adding clips to index...') clips = Clip.query.all() search.add_clips(clips) click.echo('Done!') @app.cli.command() @click.argument('name') def add_role(name): #pragma: no cover if len(name) <= 0 or len(name) > cn.ROLE_NAME_LENGTH: click.echo('Name must be between 0 and %d characters' % cn.ROLE_NAME_LENGTH) return from speeddb.models.user import Role role = Role(name=name) db.session.add(role) db.session.commit() click.echo('Created role "%s" (id: %d)' % (role.name, role.id)) @app.cli.command() @click.argument('role_id') @click.argument('user_id') def add_role_to_user(role_id, user_id): #pragma: no cover from speeddb.models.user import Role user = User.query.get(user_id) if user == None: click.echo('User not found') return role = Role.query.get(role_id) if role == None: click.echo('Role not found') click.echo('Adding role %s to %s' % (role.name, user.username)) user.roles.append(role) db.session.add(user) db.session.commit() click.echo('Done!') @app.cli.command() @click.argument('name') def add_tag(name): #pragma no cover from speeddb.models.tags import Tag tag = Tag.query.filter_by(name=name).first() if tag != None: click.echo('%s already exists' % name) return click.echo('Added tag "%s" to the database' % name) tag = Tag(name=name) db.session.add(tag) db.session.commit() click.echo('Done!') return app