def restart_db_tables(db, app): with app.app_context(): db.init_app(app) db.drop_all(app=app) db.create_all(app=app) example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() example = Story() example.text = 'Trial story of example admin user :)' example.likes = 42 example.author_id = 1 example.roll = { 'dice': ['bike', 'tulip', 'happy', 'cat', 'ladder', 'rain'] } example.date = datetime.datetime(2019, 11, 5) db.session.add(example) db.session.commit()
def create_app(debug=False): app = Flask(__name__) app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY' app.config['SECRET_KEY'] = 'ANOTHER ONE' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///storytellers.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # DEBUGGING AND TESTING app.config['SQLALCHEMY_ECHO'] = False app.config['TESTING'] = debug app.config['LOGIN_DISABLED'] = True app.config['WTF_CSRF_ENABLED'] = False for bp in blueprints: app.register_blueprint(bp) bp.app = app db.init_app(app) login_manager.init_app(app) db.create_all(app=app) # create a first admin user with app.app_context(): q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() q = db.session.query(Story).filter(Story.id == 1) story = q.first() if story is None: example = Story() example.text = 'Trial story of example admin user :)' example.likes = 42 example.author_id = 1 example.dicenumber = 6 example.roll = { 'dice': ['bike', 'tulip', 'happy', 'cat', 'ladder', 'rain'] } example.date = datetime.datetime(2019, 11, 5) db.session.add(example) db.session.commit() return app
def create_app(): app = Flask(__name__) app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY' app.config['SECRET_KEY'] = 'ANOTHER ONE' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///gooutsafe.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False for bp in blueprints: app.register_blueprint(bp) bp.app = app db.init_app(app) login_manager.init_app(app) db.create_all(app=app) # create a first admin user with app.app_context(): q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() q = db.session.query(Restaurant).filter(Restaurant.id == 1) restaurant = q.first() if restaurant is None: example = Restaurant() example.name = 'Trial Restaurant' example.likes = 42 example.phone = 555123456 example.lat = 43.720586 example.lon = 10.408347 db.session.add(example) db.session.commit() return app
def create_app(): app = Flask(__name__) app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY' app.config['SECRET_KEY'] = 'ANOTHER ONE' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///storytellers.db' for bp in blueprints: app.register_blueprint(bp) bp.app = app db.init_app(app) login_manager.init_app(app) db.create_all(app=app) # create a first admin user with app.app_context(): q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() q = db.session.query(Story).filter(Story.id == 1) story = q.first() if story is None: example = Story() example.text = 'Trial story of example admin user :)' example.likes = 42 example.author_id = 1 print(example) db.session.add(example) db.session.commit() return app
def test_json_wall(self): app = test_app.test_client() with app.session_transaction() as sess: db.drop_all() db.create_all() q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'userwall' example.lastname = 'theWall' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('daddysflownacrosstheocean') db.session.add(example) db.session.commit() q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() q = db.session.query(Story).filter(Story.author_id == user.id) story = q.first() if story is None: example = Story() example.text = 'We dont need no education We dont need no...All in all you re just another brick in the wall' example.likes = 42 example.dislikes = 1 example.dicenumber = 6 example.author_id = user.id db.session.add(example) example = Story() example.text = 'Leaving just a memory...Snapshot in the family album...Daddy what else did you leave for me?' example.likes = 42 example.dislikes = 0 example.dicenumber = 4 example.author_id = user.id db.session.add(example) db.session.commit() q = db.session.query(Story).filter(Story.author_id == user.id) stories = [] thewalltest = Wall(user) for s in q: s: Story thewalltest.add_story(s) stories.append({ 'story_id': s.id, 'text': s.text, 'likes': s.likes, 'dislikes': s.dislikes }) reply = app.get('/thewall/' + str(user.id)) body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "id": user.id, "firstname": user.firstname, "lastname": user.lastname, "email": user.email, "stories": stories # thewalltest.stories }) wall_repl = Wall() wall_repl.acquire_from_json(reply) self.assertEqual(thewalltest.id, wall_repl.id, "Json acquire fail")
def create_app(test=False): app = Flask(__name__, static_url_path='/static') app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY' app.config['SECRET_KEY'] = 'ANOTHER ONE' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///storytellers.db' if test: app.config['TESTING'] = True app.config['CELERY_ALWAYS_EAGER'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['WTF_CSRF_ENABLED'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False celery = celeryApp.make_celery(app) celeryApp.celery = celery for bp in blueprints: app.register_blueprint(bp) bp.app = app db.init_app(app) login_manager.init_app(app) db.create_all(app=app) with app.app_context(): # Create first admin user. q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() example = Story() example.title = 'My first story!' example.rolls_outcome = '[["bike", "static/Mountain/bike.PNG"], ["bus", "static/Mountain/bus.PNG"]]' example.text = 'With my bike, I am faster than a bus!!!!' example.theme = 'Mountain' example.published = 1 example.likes = 42 example.dislikes = 5 example.author_id = 1 db.session.add(example) db.session.commit() # Create dice sets if missing. themes = retrieve_themes() if not themes: die1 = Die( ['angry', 'bag', 'bike', 'bird', 'crying', 'moonandstars'], "N/A") die2 = Die(['bus', 'coffee', 'happy', 'letter', 'paws', 'plate'], "N/A") die3 = Die( ['caravan', 'clock', 'drink', 'mouth', 'tulip', 'whale'], "N/A") die4 = Die( ['baloon', 'bananas', 'cat', 'icecream', 'pencil', 'phone'], "N/A") dice_set = DiceSet([die1, die2, die3], "Mountain") store_dice_set(dice_set) dice_set = DiceSet([die2, die3, die4], "Late night") store_dice_set(dice_set) dice_set = DiceSet([die3, die1, die4], "Travelers") store_dice_set(dice_set) dice_set = DiceSet([die2, die1, die4], "Youth") store_dice_set(dice_set) die = Die(["1", "2", "3"], "test_theme") dice_set = DiceSet([die], "test_theme") return app
def setUp(self) -> None: with app.app_context(): # Create admin user example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() # Create non admin user example = User() example.firstname = 'Abc' example.lastname = 'Abc' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2010, 10, 5) example.is_admin = False example.set_password('abc') db.session.add(example) db.session.commit() # Create another non admin user example = User() example.firstname = 'Nini' example.lastname = 'Nini' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2010, 10, 7) example.is_admin = False example.set_password('nini') db.session.add(example) db.session.commit() # Create an account that will have 0 stories example = User() example.firstname = 'No' example.lastname = 'Stories' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2010, 10, 5) example.is_admin = False example.set_password('no') db.session.add(example) db.session.commit() # Create the first story, default from teacher's code example = Story() example.text = 'Trial story of example admin user :)' example.author_id = 1 example.figures = '#example#admin#' example.is_draft = False example.date = datetime.datetime.strptime('2019-10-20', '%Y-%m-%d') db.session.add(example) db.session.commit() # Create a story that shouldn't be seen in /latest example = Story() example.text = 'Old story (dont see this in /latest)' example.date = datetime.datetime.strptime('2019-10-10', '%Y-%m-%d') example.likes = 420 example.author_id = 2 example.is_draft = False example.figures = '#example#abc#' db.session.add(example) db.session.commit() # Create a story that should be seen in /latest example = Story() example.text = 'You should see this one in /latest' example.date = datetime.datetime.strptime('2019-10-13', '%Y-%m-%d') example.likes = 3 example.author_id = 2 example.is_draft = False example.figures = '#example#abc#' db.session.add(example) db.session.commit() # Random draft from a non-admin user example = Story() example.text = 'DRAFT from not admin' example.date = datetime.datetime.strptime('2018-12-30', '%Y-%m-%d') example.likes = 100 example.author_id = 3 example.is_draft = True example.figures = '#example#nini#' db.session.add(example) db.session.commit() # Create a very old story for range searches purpose example = Story() example.text = 'very old story (11 11 2011)' example.date = datetime.datetime.strptime('2011-11-11', '%Y-%m-%d') example.likes = 2 example.author_id = 3 example.is_draft = False example.figures = '#example#nini#' example.date = datetime.datetime(2011, 11, 11) db.session.add(example) db.session.commit() # Add third reaction (love) love = ReactionCatalogue() love.reaction_id = 3 love.reaction_caption = "love" db.session.add(love) db.session.commit() # login payload = {'email': '*****@*****.**', 'password': '******'} form = LoginForm(data=payload) self.client.post('/users/login', data=form.data, follow_redirects=True)