def reload_database(): exit_reload = False try: response = requests.get(f'http://{host}:{port}') print( 'The website seems to be running. Please stop it and run this file again.', file=sys.stderr) exit_reload = True except: pass if exit_reload: exit(11) try: os.remove('flasksite/site.db') print('previous DB file removed') except: print('no previous file found') db.create_all() # creating two users hashed_password = bcrypt.generate_password_hash('testing').decode('utf-8') default_user1 = User(username='******', email='*****@*****.**', image_file='another_pic.jpeg', password=hashed_password) db.session.add(default_user1) hashed_password = bcrypt.generate_password_hash('testing2').decode('utf-8') default_user2 = User(username='******', email='*****@*****.**', image_file='7798432669b8b3ac.jpg', password=hashed_password) db.session.add(default_user2) hashed_password = bcrypt.generate_password_hash('testing3').decode('utf-8') default_user3 = User(username='******', email='*****@*****.**', password=hashed_password) db.session.add(default_user3) # TODO: Here you should include the generation of rows for your database try: db.session.commit() print('\nFinalized - database created successfully!') except Exception as e: print('The operations were not successful. Error:', file=sys.stderr) print(e, file=sys.stderr) db.session.rollback()
def reload_database(): exit_reload = False try: response = requests.get(f'http://{host}:{port}') app.logger.critical( 'The website seems to be running. Please stop it and run this file again.' ) exit_reload = True except: pass if exit_reload: exit(11) try: os.remove('flasksite/site.db') app.logger.info('previous DB file removed') except: app.logger.info('no previous DB file found') assert not os.path.exists( 'flasksite/site.db' ), 'It seems that site.db was not deleted. Please delete it manually!' db.create_all() # creating two users hashed_password = bcrypt.generate_password_hash('testing').decode('utf-8') default_user1 = User(username='******', email='*****@*****.**', image_file='another_pic.jpeg', password=hashed_password) db.session.add(default_user1) hashed_password = bcrypt.generate_password_hash('testing2').decode('utf-8') default_user2 = User(username='******', email='*****@*****.**', image_file='7798432669b8b3ac.jpg', password=hashed_password) db.session.add(default_user2) hashed_password = bcrypt.generate_password_hash('testing3').decode('utf-8') default_user3 = User(username='******', email='*****@*****.**', password=hashed_password) db.session.add(default_user3) try: db.session.commit() except Exception as e: db.session.rollback() app.logger.critical('Error while committing the user insertion.') app.logger.exception(e) # testing if the users were added correctly assert len( User.query.all()) == 3, 'It seems that user failed to be inserted!' # TODO: Here you should include the generation of rows for your database try: db.session.commit() app.logger.info('Finalized - database created successfully!') except Exception as e: db.session.rollback() app.logger.critical('The operations were not successful.') app.logger.exception(e)
import os import csv import secrets import pandas as pd from random import randrange from flasksite import db, app from flasksite.models import User, Park from PIL import Image if __name__ == '__main__': # Create New DB db.create_all() # Load data from csv df_park = pd.read_csv('data/park1.csv') for index, row in df_park.iterrows(): park = Park( name=row['name'], prefecture=row['prefecture'], area=row['area'], lat=row['lat'], lon=row['lon'], capacity=row['capacity'], fake_distance =row['fake_distance'], image_file =row['image_file'], count = randrange(175) ) db.session.add(park) db.session.commit()
import sys import os #Run to create sqlalchemy for the first time. move and run from home, not flasksite #get current path and set path to /env current_path = os.getcwd() path_env = current_path + '/env' sys.path.insert(0, path_env) from migrate.versioning import api from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_MIGRATE_REPO from flasksite import db import os.path db.create_all() if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) else: api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))