Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Forms.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from wtforms import Form, EmailField, validators, PasswordField, StringField, IntegerField
from wtforms import Form, validators
from wtforms.fields import PasswordField,StringField, IntegerField


class CreateUserForm(Form):
email = EmailField('Email', [validators.DataRequired(), validators.Email()])
email = StringField('Email', [validators.DataRequired(), validators.Email()])
full_name = StringField('Full Name', validators=[validators.DataRequired()])
username = StringField('Username', validators=[validators.DataRequired()])
phone_number = IntegerField('Phone Number', [validators.DataRequired(),
validators.NumberRange(min=00000000, max=99999999)])
password = PasswordField('Password', [validators.DataRequired(), validators.length(min=8, max=30)])
confirm_password = PasswordField('Confirm Password', [validators.DataRequired(), validators.length(min=8, max=30)])


class LoginForm(Form):
email = StringField('Email', [validators.DataRequired(), validators.Email()])
password = PasswordField('Password', [validators.DataRequired(), validators.length(min=8, max=30)])
91 changes: 40 additions & 51 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,30 @@
from flask import Flask, render_template, request, redirect, url_for
from Forms import CreateUserForm
from flask import Flask, render_template, request, session, redirect, url_for
from Forms import CreateUserForm, LoginForm
import hashlib
from dotenv import load_dotenv, find_dotenv
import os
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import exists
import mysql.connector
# import mysql.connector
# db_2 = mysql.connector.connect(
# host="localhost",
# user="root",
# password="EcoWheels123",
# database="eco_wheels"
# )
from model import *

app = Flask(__name__)
db = SQLAlchemy()
load_dotenv(find_dotenv())
db_2 = mysql.connector.connect(
host="localhost",
user="root",
password="EcoWheels123",
database="eco_wheels"
)

# def create_app():
# app = Flask(__name__)
#
# app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("DATABASE_URI2")
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# app.config['SECRET_KEY'] = os.environ.get("SECRET_KEY")
#
#
# db.init_app(app)
#
# with app.app_context():
# import model
# db.create_all() # Create sql tables
#
# return app
# app = create_app()


#JIAYINGG the following 4 lines of commented code is ursss!
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URI")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY")
db = SQLAlchemy()

db.init_app(app)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("DATABASE_URI")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = os.environ.get("SECRET_KEY")

with app.app_context():
import model

db.create_all()
db.init_app(app)
db.create_all() # Create sql tables


@app.route('/')
Expand Down Expand Up @@ -74,9 +53,9 @@ def sign_up():
confirm_password = create_user_form.confirm_password.data

# Check if the user already exists (This is called IntegrityError)
user_exists = db.session.query(exists().where(model.User.username == username)).scalar()
email_exists = db.session.query(exists().where(model.User.email == email)).scalar()
phone_number_exists = db.session.query(exists().where(model.User.phone_number == phone_number)).scalar()
user_exists = db.session.query(exists().where(User.username == username)).scalar()
email_exists = db.session.query(exists().where(User.email == email)).scalar()
phone_number_exists = db.session.query(exists().where(User.phone_number == phone_number)).scalar()

if user_exists:
error = "Username already exists!"
Expand All @@ -95,30 +74,40 @@ def sign_up():

if error is None:
# Create a new user
new_user = model.User(full_name=full_name, username=username, email=email, phone_number=phone_number, password_hash=hashed_password)
new_user = User(full_name=full_name, username=username, email=email, phone_number=phone_number,
password_hash=hashed_password)
db.session.add(new_user)
db.session.commit()
print("User created!")
print("User created!")
return redirect(url_for('login'))
return render_template("customer/sign_up.html", form=create_user_form, error=error)


@app.route('/test_sign_up')
def test_create_user():
new_user = model.User(id=1, full_name="John Doe", username="johndoe", email="johndoe@gmail.com", phone_number="12345678", password_hash="password")
db.session.add(new_user)
db.session.commit()
return "User created!"

@app.route('/login', methods=['GET', 'POST'])
def login():
return render_template("customer/login.html")
error = None
login_form = LoginForm(request.form)
if request.method == 'POST' and login_form.validate():
email = login_form.email.data
password = login_form.password.data
password_bytes = password.encode('utf-8')
entered_password_hash = hashlib.sha256(password_bytes).hexdigest()
user = db.session.query(User).filter_by(email=email).first()

if user and user.password_hash == entered_password_hash:
session['user_id'] = user.id
return redirect(url_for('home'))
else:
error = "Invalid email or password. Please try again."

return render_template("customer/login.html", form=login_form, error=error)


@app.route('/payment')
def payment():
return render_template("customer/payment.html")


@app.route('/confirmation')
def confirmation():
# Render a simple confirmation page
Expand Down
8 changes: 0 additions & 8 deletions model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash

from __init__ import db


Expand All @@ -11,15 +9,9 @@ class User(db.Model):
full_name = db.Column(db.String(64), nullable=False)
username = db.Column(db.String(64), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
phone_number = db.Column(db.String(8), nullable=False)
phone_number = db.Column(db.String(8), unique=True, nullable=False)
password_hash = db.Column(db.String(128))

def set_password(self, password):
self.password_hash = generate_password_hash(password)

def check_password(self, password):
return check_password_hash(self.password_hash, password)

class Order(db.Model):
__tablename__ = 'orders'
Expand Down
7 changes: 0 additions & 7 deletions run.py

This file was deleted.

12 changes: 7 additions & 5 deletions templates/customer/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% block title %}EcoWheels{% endblock %}

{% block content %}
{% from "_formHelper.html" import render_field %}

<link rel="stylesheet" href="{{ url_for('static', filename='css/login.css') }}" xmlns="http://www.w3.org/1999/html">
<a href="/">
Expand All @@ -12,16 +13,17 @@
<p>Login</p>
</div>

{% if error %}
<p style="color: red; text-align: center;">{{ error }}</p>
{% endif %}

<form method="POST" action="" autocomplete="off">
<div class="user_details">
<div class="input_box">
<label for="email">Email</label>
<input type="email" id="email" placeholder="example@gmail.com" required>
{{ render_field(form.email, class="form-control", id="email", placeholder="example@gmail.com") }}
</div>
<div class="input_box">
<label for="pass">Password</label>
<input type="password" id="pass" pattern="^(?=.*[A-Z])(?=.*[\W_]).{8,}$" placeholder="Enter your password" required
title="Password must contain at least 8 characters, including at least one special character and one uppercase letter.">
{{ render_field(form.password, class="form-control", id="pass", placeholder="Enter your password") }}
</div>
</div>
<div class="reg_btn">
Expand Down