Exemple #1
0
def edit():

    session_token = request.cookies.get("session_token")

    user = db.query(User).filter_by(session_token=session_token).first()

    if not user:
        return redirect(url_for("home"))

    if request.method == "POST":
        name = request.form.get("name")
        email = request.form.get("email")
        password = request.form.get("pasword")

        if name:
            user.name = name

        if email:
            user.email = email

        if password:
            hashed_pwd = hashlib.sha256(password.encode()).hexdigest()
            user.password = hashed_pwd

        db.add(user)
        db.commit()

        return redirect(url_for("profile"))

    return render_template(
        "edit.html",
        user=user,
    )
Exemple #2
0
def login():
    name = request.form.get("name")
    password = request.form.get("password")

    hashed_pwd = hashlib.sha256(password.encode()).hexdigest()
    user = db.query(User).filter_by(name=name).first()

    if not user:
        flash(f"User doesn't exists. Please register.")
        return redirect(url_for("reg"))

    if hashed_pwd != user.password:
        flash(f"Wrong user name, email or password. Try again please.")
        return redirect(url_for("home"))

    else:
        session_token = str(uuid4())
        user.session_token = session_token
        user = User(online=True, offline=False)
        db.add(user)
        db.commit()

        response = make_response(redirect(url_for("home")))
        response.set_cookie("session_token", session_token)

        return response
Exemple #3
0
def register():

    name = request.form.get("name")
    email = request.form.get("email")
    password = request.form.get("password")

    hashed_pwd = hashlib.sha256(password.encode()).hexdigest()

    secret_number = random.randint(1, 30)

    games = 0

    wins = 0

    losses = 0

    score = 0

    user = db.query(User).filter_by(name=name).first()

    if name and email and password:

        if not user:

            user = User(
                name=name,
                email=email,
                password=hashed_pwd,
                secret_number=secret_number,
                games=games,
                wins=wins,
                losses=losses,
                score=score,
                online=True,
                offline=False,
            )

            session_token = str(uuid4())
            user.session_token = session_token

            db.add(user)
            db.commit()

            response = make_response(redirect(url_for("home")))
            response.set_cookie(
                "session_token",
                session_token,
            )

        return response
    else:
        flash(f"Please fill all data to complete registration")
        return render_template("register.html", user=user)
Exemple #4
0
def give_up():
    session_token = request.cookies.get("session_token")

    user = db.query(User).filter_by(session_token=session_token).first()

    user.games += 1
    user.losses += 1
    user.score -= 1

    db.add(user)
    db.commit()

    return render_template("index.html", user=user)
Exemple #5
0
def logout():
    session_token = request.cookies.get("session_token")

    user = db.query(User).filter_by(session_token=session_token).first()

    if user:

        user.session_token = ""
        user.online = False
        user.offline = True

        db.add(user)
        db.commit()

    response = make_response(redirect(url_for("home")))
    response.set_cookie("session_token", "")

    return response
Exemple #6
0
def result():
    guess = int(request.form.get("guess"))

    session_token = request.cookies.get("session_token")

    user = db.query(User).filter_by(session_token=session_token).first()

    if guess == user.secret_number:

        flash(f"Congralutions! You guessed it. The secret number was {guess}")

        user.games += 1

        user.wins += 1

        user.score += 2

        new_secret = random.randint(1, 30)

        user.secret_number = new_secret

        db.add(user)
        db.commit()

        return render_template("success.html", user=user)

    elif guess > user.secret_number:

        flash(f"Wrong. Try a smaller one")

    elif guess < user.secret_number:

        flash(f"Wrong. Try a bigger one")

    else:
        user.games += 1
        user.losses += 1
        user.score -= 1
        db.add(user)
        db.commit()

    return render_template("playGame.html", user=user)
Exemple #7
0
def delete():
    session_token = request.cookies.get("session_token")

    user = db.query(User).filter_by(session_token=session_token).first()

    if not user:
        return render_template(
            "index.html",
            user=user,
        )

    if request.method == "POST":
        if user:
            db.delete(user)
            db.commit()

        response = make_response(redirect(url_for("home")))
        response.set_cookie("session_token", "")

        return response

    return render_template("profile.html", user=user)
Exemple #8
0
        elif self.dbtype == GADFLY:
            self.insert_log_data_gadfly(batch_list, cursor)

        parse_secs = time.time() - start_time

        if self.dbtype == MYSQL:
            if major_version > 3: self.enable_keys(cursor)
            self.unlock_tables(cursor)
            try:
                os.remove(self.prefs.get("TMP_FILE"))
            except Exception, e:
                print "WARNING: Could not delete tmp_file:", self.prefs.get(
                    "TMP_FILE")
        elif self.dbtype in (SQLITE, GADFLY):
            if self.__verbose: "Committing data"
            db.commit()

        print
        print ">> Processing summary:"
        print ".. Parsed lines             :", total_lines
        print ".. Parse time               : %.2f" % parse_secs
        try:
            print ".. Parsed lines/sec         : %.2f" % (float(total_lines) /
                                                          parse_secs)
        except:
            pass
        nsecs = time.time() - start_time
        print ".. Completion time (seconds): %.2f" % nsecs
        try:
            print ".. Lines per second         : %.2f" % (float(total_lines) /
                                                          nsecs)