def setUp(self) -> None: with app.app_context(): # Create an user with no stories 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() # Create another user q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'Admin2' example.lastname = 'Admin2' 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 a not recent story by Admin2 example = Story() example.text = 'This is a story about the end of the world' example.date = datetime.datetime.strptime('2012-12-12', '%Y-%m-%d') example.author_id = 2 example.figures = 'story#world' example.is_draft = False db.session.add(example) db.session.commit() # Create a recent story saved as draft by Admin2 example = Story() example.text = 'This story is just a draft' example.date = datetime.datetime.now() example.author_id = 2 example.figures = 'story#draft' example.is_draft = True db.session.add(example) db.session.commit() # Create a recent story by Admin example = Story() example.text = 'Just another story' example.date = datetime.datetime.now() example.author_id = 1 example.figures = 'dice#example' example.is_draft = False db.session.add(example) db.session.commit()
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 setUp(self) -> None: with app.app_context(): # Create admin user (if not present) 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() # Create non admin user (if not present) q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: 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 the first story, default from teacher's code 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.author_id = 1 example.figures = '#example#admin#' example.is_draft = False db.session.add(example) db.session.commit() # Create a story of a different user q = db.session.query(Story).filter(Story.id == 2) story = q.first() if story is None: example = Story() example.text = 'You won\'t modify this story' example.author_id = 2 example.figures = '#modify#story#' example.is_draft = False db.session.add(example) db.session.commit() # Create a draft for the logged user q = db.session.query(Story).filter(Story.id == 3) story = q.first() if story is None: example = Story() example.text = 'This is an example of draft' example.author_id = 1 example.figures = '#example#draft#' example.is_draft = True db.session.add(example) db.session.commit() # Create a draft of a different user q = db.session.query(Story).filter(Story.id == 4) story = q.first() if story is None: example = Story() example.text = 'This is an example of draft that you can\'t modify' example.date = datetime.datetime.strptime('2018-12-30', '%Y-%m-%d') example.author_id = 2 example.figures = '#example#draft#' example.is_draft = True db.session.add(example) db.session.commit() payload = {'email': '*****@*****.**', 'password': '******'} form = LoginForm(data=payload) self.client.post('/users/login', data=form.data, follow_redirects=True)
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)