def init_db(): """Creates the database tables.""" with app.app_context(): db = get_db() with app.open_resource('schema.sql', mode='r') as f: db.cursor().executescript(f.read()) db.commit()
def drop_database(): from project import app from project.extensions import db import project.models with app.app_context(): db.drop_all() db.session.commit()
def client(): db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True with app.test_client() as client: with app.app_context(): db.drop_all() db.create_all() # Create a test user and some dummy data test_user = User("klinify", "klinify") now = datetime.now() test_customer_1 = Customers("Alice", "1997-05-09", now) test_customer_2 = Customers("Charlie", "1996-09-27", now) test_customer_3 = Customers("Bobby", "2000-03-01", now) test_customer_4 = Customers("Duke", "1997-01-02", now) db.session.add(test_user) db.session.add(test_customer_1) db.session.add(test_customer_2) db.session.add(test_customer_3) db.session.add(test_customer_4) db.session.commit() print("DB created and populated with dummy data") yield client os.close(db_fd) os.unlink(app.config['DATABASE'])
def send_async_email(msg): with app.app_context(): try: mail.send(msg) print(msg) except Exception as e: print("ERROR") print(e)
def test_spider(self): city = 'kowloon' response = spider(city) self.assertIn('insert_id', response) id = response['insert_id'] with app.app_context(): query = Weather.query.filter_by(id=id).first() self.assertIsInstance(query, Weather)
def send_email(to, subject, template): msg = Message(subject, recipients=[to], html=template, sender=app.config['MAIL_DEFAULT_SENDER']) with app.app_context(): mail.send(msg)
def test_recipe_detail_private_recipe(self): with app.app_context(): self.register_user() self.add_recipes() response = self.app.get('/recipe/3', follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertIn(b'Tacos', response.data) self.assertIn(b'Private', response.data) self.assertIn(b'*****@*****.**', response.data)
def test_recipe_detail_public_recipe(self): with app.app_context(): self.register_user() self.add_recipes() self.logout_user() response = self.app.get('/recipe/1', follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertIn(b'Hamburgers', response.data) self.assertIn(b'Public', response.data) self.assertIn(b'*****@*****.**', response.data)
def test_recipe_detail_private_recipe_invalid_user(self): with app.app_context(): self.register_user() self.add_recipes() self.logout_user() response = self.app.get('/recipe/3', follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertIn( b'Error! Incorrect permissions to access this recipe.', response.data)
def send_email(to, subject, template): msg = Message( subject, recipients=[to], html=template, sender=app.config['MAIL_DEFAULT_SENDER'] ) with app.app_context(): mail.send(msg)
def clear_entries(mode): with app.app_context(): db = get_db() database_request = "drop table if exists " + mode + ";" db.cursor().execute(database_request) db.commit() init_db() flash('All ' + mode + ' entries were successfully cleared') return "OK"
def run_migrations_on_dev_server_restart(): logger.info('Running migrations') current = None from sqlalchemy import create_engine with create_engine( f"postgresql://{CONFIG['DB_USER']}:{CONFIG['DB_PW']}" f"@{CONFIG['DB_HOST']}:{CONFIG['DB_PORT']}/{CONFIG['DB_NAME']}" ).connect() as connection: # If the database was just created from scratch, there will be no # alembic_version table, and current will be set to None. with connection.begin(): current = attempt_to_get_alembic_version(connection) if current: logger.info(f'The current revision is {current}') else: logger.info('Could not find a current revision in the DB') # Get the Flask-Migrate config: config = Migrate(app, db, directory=os.path.join(PROJECT_ROOT, 'migrations')).get_config() # We want to run any migrations that haven't been run yet. First, get # all the revision identifiers (as strings) and store them. revisions = [] script_directory = ScriptDirectory.from_config(config) for revision_script in script_directory.walk_revisions(head='head'): revisions.append(revision_script.revision) # walk_revisions starts from the head and goes backwards. We want to # migrate up from scratch, so we need to reverse the order. revisions.reverse() # False if there is a current revision (in which case, we don't want to # start migrating yet), True if there is none and the database was just # created. migrating = False if current else True with app.app_context(): for revision in revisions: if migrating: logger.info(f'Upgrading to {revision}') command.upgrade(config, revision) # One we reach the current revision, we want to upgrade, one step # at a time, for each subsequent revision. If we don't do this, # queries in migrations (but not DDL) will fail. if current and current == revision: migrating = True logger.info('Migrations finished')
def setUp(self): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \ os.path.join(app.config['BASEDIR'], TEST_DB) self.app = app.test_client() with app.app_context(): db.create_all() mail.init_app(app) self.assertEquals(app.debug, False)
def test_add_recipe(self): with app.app_context(): self.register_user() response = self.app.post( '/add', data=dict( recipe_title='Hamburgers2', recipe_description='Delicious hamburger with pretzel rolls' ), follow_redirects=True) self.assertIn(b'New recipe, Hamburgers2, added!', response.data)
def test_user_recipes_page(self): with app.app_context(): self.register_user() self.add_recipes() response = self.app.get('/recipes', follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertIn(b'Recipes', response.data) self.assertIn(b'Hamburgers', response.data) self.assertIn(b'Mediterranean Chicken', response.data) self.assertIn(b'Tacos', response.data) self.assertIn(b'Homemade Pizza', response.data)
def init_db(): with app.app_context(): db = get_db() for mode in config.modes: database_request = "create table if not exists " \ + mode.get("route") \ + " (" \ " id serial primary key," \ " title text not null," \ " text text not null" \ ");" db.cursor().execute(database_request) db.commit()
def client(): db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True client = app.test_client() with app.app_context(): # Drop all of the existing database tables db.drop_all() # create the database and the database table db.create_all() yield client print("client end") os.close(db_fd) os.unlink(app.config['DATABASE'])
def init_db(): """Creates the database tables.""" with app.app_context(): db = get_db() for mode in config.modes: # create tables if not already created database_request = "create table if not exists " \ + mode.get("route") \ + " (" \ " id integer primary key autoincrement," \ " title text not null," \ " text text not null" \ ");" db.cursor().executescript(database_request) db.commit()
def app(request): # `TESTING` set to True, `DEBUG` to False, # `SQLALCHEMY_DATABASE_URI` leading to testing postgres database _app.config.from_pyfile('testing_config.py') # Not using SQLite for testing, because there are problems with # summing timedelta. test_client = _app.test_client() ctx = _app.app_context() ctx.push() def teardown(): ctx.pop() request.addfinalizer(teardown) return test_client
def ydl_download(self, download_id): with app.app_context(): d = Download.query.filter_by(id=download_id).first() opts = { 'noplaylist': not d.playlist, 'outtmpl': d.outtmpl, 'progress_hooks': [d.progress_hook], 'format': d.df.ydl_format, 'sleep_interval': 60, 'max_sleep_interval': 300, } y = YoutubeDL(params=opts) try: y.download([d.url]) except DownloadError: d.status = DownloadStatus.ERROR d.save()
def render_datatable_html(df_pickle, taxid_file, taxids, html_path, sub_dir_path): taxids_file = open(taxid_file, "r") taxid_list = taxids_file.read().splitlines() taxid_list = ','.join(taxid_list) html_file = open(html_path, "w") # Context is necessary, because template is rendered outside a request. with app.app_context(): reads_datatables = render_template('reads_datatable_blast.html', df_pickle=df_pickle, taxid_list=taxid_list, sub_dir_path=sub_dir_path, taxid=str(taxids[0])) html_file.write(reads_datatables) html_file.close() return None
def createsuperuser(login=None, email=None, password=None): """ Create user with admin rights""" login = login or ask_input('Введите логин') email = email or ask_input('Введите адрес электронной почты') passwd = password or ask_input('Введите пароль', hidden=True) confirmation = password or ask_input('Подтвердите пароль', hidden=True) while passwd != confirmation: print('Пароли не совпадают! Похоже, вы опечатались.') while True: choice = input('Повторить ввод? (y/n) ').lower() if choice in 'yes': passwd = ask_input('Введите пароль', hidden=True) confirmation = ask_input('Подтвердите пароль', hidden=True) break elif choice in 'no': return else: print('Пожалуйста, ответьте y (yes) или n (no)') with disable_csrf(app): form = HelperForm( name='dummy', surname='dummy', password=passwd, confirmation=passwd, ) # dodge filling obj_data , just like browser form filling # (Existence validation comes false positive) form.login.data = login form.email.data = email if not form.validate(): errors = [err for field in form.errors.values() for err in field] for error in errors: print(error) return else: with app.app_context(), perform( name='createsuperuser', before='Creating user', fail='Error occured while creating user', after='Superuser has been succesfully created!', ): User.bl.create_superuser(login, passwd, email)
def insertuser(url, headline, summary, datetime, section): #将数据插入数据库中 global articleurl articleurl = url with app.app_context(): try: new_old = New(url=url, datetime=datetime, headline=headline, summary=summary, section=section) db.session.add(new_old) db.session.commit() new = New.query.filter(New.url == url).first() return new except Exception as ce: logger.error(ce)
def insertarticle(new_id, text): with app.app_context(): try: if text == '': new = New.query.filter(New.id == new_id).first() db.session.delete(new) else: article = Article(article_id=new_id, text=text) db.session.add(article) db.session.commit() ans = "news_url=%s insert succeed" % (article.new.headline) logger.info(ans) time.sleep(3) except Exception as ce: logger.error(ce)
def app(): """Create and configure a new app instance for each test.""" # create a temporary file to isolate the database for each test db_fd, db_path = tempfile.mkstemp() # create the app with common test config app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) # create the database and load test data with app.app_context(): init_db() get_db().executescript(_data_sql) yield app # close and remove the temporary database os.close(db_fd) os.unlink(db_path)
def scan(): folder = Config.VIDEO_BASE_DIR videxts = ['.mkv', '.mpg', '.avi', '.mp4'] walked_videos = [] # Find all relevant media files and directories for root, dirs, files in os.walk(folder): for file in files: # Split off the file extension ext = os.path.splitext(file)[1] # Compare extension with known video extensions. if ext.lower() in videxts: filepath = os.path.join(root, file) if filepath not in walked_videos: walked_videos.append(filepath) with app.app_context(): for video in query_videos: existing_videos.append(video.filepath) if video.filepath in discovered_videos: video.set_active(True) else: video.set_active(False) video.seen() # Determine which discovered files are NEW for filepath in discovered_videos: if filepath not in existing_videos: dir_path = os.path.dirname(filepath) directory = Directory.query.filter_by(path=dir_path).first() if directory != None: video = Video(filepath=filepath, directory_id=directory.id) db.session.add(video) # Commit changes to the database # Note: If ANY error is encountered, commit will NOT execute db.session.commit()
def app(): db_fd, db_path = tempfile.mkstemp() app = Flask(__name__, instance_relative_config=True, static_url_path='/static') app.testing = True app.register_blueprint(bp) # create and configure the app app.config.from_mapping( SECRET_KEY='dev', DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'), ) with app.app_context(): init_db() get_db().executescript( "select Place_ID from PARKING_PLACE order by Place_ID desc") yield app os.close(db_fd) os.unlink(db_path)
def tearDown(self): # Dropping all data from the database with app.app_context(): db.session.remove() db.drop_all()
from sys import argv from connect import create_database from project import app from project.models import User if __name__ == '__main__': if (len(argv) > 1): if (argv[1] == '-db'): with app.app_context(): create_database() app.run()
def send_async_email(msg): with app.app_context(): mail.send(msg)
def test_get_db(app): with app.app_context(): db = get_db() assert db is get_db()
def send_mail(subject, body, sender, recipients, html=''): with app.app_context(): msg = Message(subject, sender=sender, recipients=recipients) msg.html = html msg.body = body mail.send(msg)
def send_async_email(app, msg): with app.app_context(): mail.send(msg)
def test_app(): app.config.from_object('project.config.TestingConfig') with app.app_context(): yield app # testing happens here