Beispiel #1
0
def admin(password):
    """
    Creates an admin account if one doesn't exist

    :param password: password for the admin account. In the command line interface,
        you can specify this by giving the -p or --password argument, defaults to 'test'
    :type password: str, optional
    """
    usr = User.query.first()
    if not usr:
        # If the first row in the table doesn't exist
        # Creates account with login "admin" and password "test"(both fields may be changed)
        try:
            admin = User(username="******", password=password)
            admin.save_to_db()
            # If everything is okay a message with the login and password from the admin account
            # will be displayed in the console
            print("Successfully created.\nLogin: {0}\nPassword: {1}".format(
                "admin", password))
        except Exception as e:
            # If there is troubles with saving to database
            print("Unable to create: {0}".format(e))
    else:
        # If admin already exists - do nothing
        print("Already exists.")
Beispiel #2
0
def register_view():
    """
    New user registration view.
    Access only for admin.
    If the user tries to enter this route without logging in, he will be automatically redirected to the login page.
    If the user tries to enter this route without permission(user is not admin),
    he will be automatically redirected to the cabinet page.
    Methods: GET, POST
    """
    if current_user.username != "admin":  # If current user is not admin redirect him to the cabinet
        return redirect(url_for("cabinet.cabinet_view"))

    register_form = RegisterForm()
    # Set selector options
    register_form.tariff_select.choices = [
        tariff.value["tariff_name"] for tariff in list(Tariffs)
    ]

    if request.method == "POST":
        data = request.form
        if register_form.validate_on_submit() or (data
                                                  and current_app.testing):
            # if admin click the 'Register' button or there is data in the request while testing app
            # Create new user
            new_user = User(
                name=data.get("name"),
                phone=data.get("phone"),
                email=data.get("email"),
                username=data.get("username"),
                password=data.get("password"),
                tariff=data.get("tariff_select"),
                address=data.get("address"),
                state=State.activated_state.value,
            )

            try:
                # if everything is okay - admin will be redirected to the admin page
                # and user will be created
                new_user.save_to_db()
                flash(messages["success_register"], "info")
                return redirect(url_for("admin.admin_view"))
            except ValueError as e:  # error saving to the database
                # otherwise admin will be redirected to the register page
                # the user will not be created
                current_app.logger.info(
                    "Error while saving new user to the database - {0}".format(
                        e))
                flash(messages["failure"], "warning")
                return redirect(url_for("admin.register_view"))

    return render_template("auth/register.html",
                           title="Register new user",
                           form=register_form)
Beispiel #3
0
def test_saving_deleting_to_db(setup_database):
    """Save and delete user from the database"""
    db = setup_database

    usr = User(username="******", password="******")
    assert not db.session.query(User).filter_by(username="******").first()

    usr.save_to_db()
    assert User.get_user_by_username("susan")

    usr.save_to_db()
    assert len(db.session.query(User).filter_by(username="******").all()) == 1

    usr.delete_from_db()
    assert not User.get_user_by_username("susan")

    prev_num_objects = len(db.session.query(User).all())
    try:
        usr_not_exist = User(username="******", password="******")
        usr_not_exist.delete_from_db()
        assert False  # If user was deleted - something goes wrong
    except ValueError:  # Exception was raised - everything is OK
        assert len(db.session.query(User).all()) == prev_num_objects