Example #1
0
def register():
    """Register user"""

    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # Ensure valid username was submitted
        try:
            name = username_valid(request.form.get("username"))
        except TypeError:
            name = None
        if not name:
            return apology("must provide valid username")

        # Querry database and check if the username is already taken
        if User.exists_in_db(db=db, name=name):
            return apology("this username is already taken")

        # Ensure both passwords were submitted
        password = request.form.get("password")
        confirmation = request.form.get("confirmation")
        if not password or not confirmation:
            return apology("must provide password and its confirmation")

        # Ensure passwords match
        if not password == confirmation:
            return apology("passwords do not match")

        # Store a user entry in the database
        password_hash = generate_password_hash(password)
        user = User(name=name, password_hash=password_hash, db=db)

        # Commit the user changes to DB
        try:
            result = user.write_to_db()
        except:
            result = False

        # Log the user in
        return login(first=True)

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        # Forget any user_id
        session.clear()

        return render_template("register.html")
Example #2
0
import sqlite3
from werkzeug.security import generate_password_hash

from backend.DBHandler import DBHandler
from backend.User import User

db = DBHandler()

USER = "******"

try:
    db.create_schema()
    user = User(name=USER,
                password_hash=generate_password_hash(USER),
                is_admin=True,
                db=db)
    user.write_to_db()

    print("New DB initialized successfully, 'admin' user created.")
except sqlite3.OperationalError:
    print("The DB already exists and has a schema. Operation aborted.")