def admin():
    logger.info("Got an admin panel page request: %s" % request)
    form = CAdmin()
    db = AndrewDB()
    g.role = 'admin'
    logger.info("Validating the Create admin form")
    form.csrf_enabled = False
    if request.method == 'POST' and form.validate_on_submit():
        hash_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user_id = db.insert_sys_user_get_id(form.email.data, hash_password)
        if user_id is None:
            flash('User with this email already registered')
            logger.info(
                "User with this email already registered, Redirecting to admin page"
            )
            return redirect(url_for('admin-panel'))
        db.insert_admin(str(user_id), form.first_name.data,
                        form.last_name.data, form.telephone.data)
        flash("Admin was added")
        logger.info("Admin was added, Redirecting to admin page")
        return redirect(url_for('admin-panel'))
    hotels = db.get_all_hotels()
    users = db.get_all_system_users()
    db_stat = db.get_db_statistics()
    admins = db.get_all_admins()
    g.db.commit()
    logger.info("Rendering the admin_panel page")
    return render_template('admin_panel.html',
                           hotels=hotels,
                           users=users,
                           db_stat=db_stat,
                           form=form,
                           admins=admins)
Beispiel #2
0
def test_get_sys_users(mock_connect):
    """Get all existing users from database"""
    with allure.step('Get all system users'):
        with app.app_context():
            db = AndrewDB()
            expected = ['user1', 'user2']
            mock_connect(
            ).cursor.return_value.fetchone.return_value.__getitem__(
                'users').return_value = expected
            result = db.get_all_system_users().return_value
        assert result == expected