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
34 changes: 34 additions & 0 deletions Chapter07_Database/Projects/2024/jeko_kobakhidze/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from flask import Flask, render_template, redirect, url_for, flash
from models import db, User
from forms import RegistrationForm
from config import Config

app = Flask(__name__)
app.config.from_object(Config)

db.init_app(app)

with app.app_context():
db.create_all()


@app.route('/', methods=['GET', 'POST'])
def index():
form = RegistrationForm()
if form.validate_on_submit():
username = form.username.data
email = form.email.data

new_user = User(username=username, email=email)

db.session.add(new_user)
db.session.commit()

flash(f"User {username} has been successfully registered!", 'success')
return redirect(url_for('index'))
users = User.query.all()
return render_template('index.html', form=form, users=users)


if __name__ == "__main__":
app.run(debug=True)
8 changes: 8 additions & 0 deletions Chapter07_Database/Projects/2024/jeko_kobakhidze/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

BASE_DIR = os.path.abspath(os.path.dirname(__file__))

class Config:
SECRET_KEY = 'grgrdhtbg343'
SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(BASE_DIR, 'test.db')}"
SQLALCHEMY_TRACK_MODIFICATIONS = False
8 changes: 8 additions & 0 deletions Chapter07_Database/Projects/2024/jeko_kobakhidze/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, Email

class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired(), Email()])
submit = SubmitField('Register')
11 changes: 11 additions & 0 deletions Chapter07_Database/Projects/2024/jeko_kobakhidze/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)

def __repr__(self):
return f'<User {self.username}>'
83 changes: 83 additions & 0 deletions Chapter07_Database/Projects/2024/jeko_kobakhidze/static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

body {
font-family: Arial, Helvetica, sans-serif;
background-color: #f4f4f9;
color: #333;
line-height: 1.6;
margin: 0;
padding: 0;
}

.container {
max-width: 700px;
margin: 50px auto;
padding: 20px;
background: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

h1, h2 {
text-align: center;
color: #333;
}

form {
display: flex;
flex-direction: column;
}

form p {
margin-bottom: 15px;
}

input[type="text"], input[type="email"] {
width: 100%;
padding: 10px;
margin-top: 5px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 16px;
}

input[type="submit"] {
background-color: #007bff;
color: #fff;
padding: 10px;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}

input[type="submit"]:hover {
background-color: #0056b3;
}

.flash-message {
background-color: #28a745;
color: #fff;
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
text-align: center;
}

ul {
list-style-type: none;
padding: 0;
}

ul li {
background-color: #007bff;
color: #fff;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
text-align: center;
}

ul li:nth-child(odd) {
background-color: #0056b3;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>

<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<div class="container">
<h1>Register New User</h1>

{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="flash-message">{{ message }}</div>
{% endfor %}
{% endif %}
{% endwith %}

<form method="POST">
{{ form.hidden_tag() }}

<p>
{{ form.username.label }}<br>
{{ form.username(class_="form-control") }}
</p>

<p>
{{ form.email.label }}<br>
{{ form.email(class_="form-control") }}
</p>

<p>{{ form.submit(class_="btn btn-primary") }}</p>
</form>

<h2>Registered Users</h2>
<ul>
{% for user in users %}
<li>{{ user.username }} ({{ user.email }})</li>
{% else %}
<p>No users registered yet.</p>
{% endfor %}
</ul>
</div>
</body>
</html>
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions Chapter09_Structuring/Projects/2024/jeko_kobakhidze/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from flask import Flask
from src.ext import db
from src.config import Config
from src.views.auth.routes import auth_bp
from src.views.main.routes import main_bp
from src.views.products.routes import products_bp

app = Flask(__name__)
app.config.from_object(Config)

db.init_app(app)

app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(main_bp, url_prefix='/')
app.register_blueprint(products_bp, url_prefix='/products')

if __name__ == '__main__':
app.run(debug=True)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from src.models.product import Product
from src.ext import db
def seed_products():
products = [
Product(name="python course", description="you learn how to create web app", price=10),
Product(name="marketing", description="sfgsrge3", price=20.0),
]
db.session.add_all(products)
db.session.commit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

class Config:
SECRET_KEY = "kasnfoabven123e@"
SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager


db = SQLAlchemy()
migrate = Migrate()
login_manager = LoginManager()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from src.ext import db

class BaseModel(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from src.ext import db
from src.models.base import BaseModel

class Product(BaseModel):
name = db.Column(db.String(150), nullable=False)
description = db.Column(db.Text, nullable=False)
price = db.Column(db.Float, nullable=False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from src.ext import db
from src.models.base import BaseModel

class Role(BaseModel):
name = db.Column(db.String(50), unique=True, nullable=False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from src.ext import db
from src.models.base import BaseModel

class User(BaseModel):
username = db.Column(db.String(150), unique=True, nullable=False)
email = db.Column(db.String(150), unique=True, nullable=False)
password = db.Column(db.String(150), nullable=False)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="POST">
{{ form.hidden_tag() }}
{{ form.email.label }} {{ form.email() }}
{{ form.password.label }} {{ form.password() }}
{{ form.submit() }}
</form>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form method="POST">
{{ form.hidden_tag() }}
{{ form.username.label }} {{ form.username() }}
{{ form.email.label }} {{ form.email() }}
{{ form.password.label }} {{ form.password() }}
{{ form.confirm_password.label }} {{ form.confirm_password() }}
{{ form.submit() }}
</form>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to the Home Page</h1>
<a href="/products">View Products</a>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Products</title>
</head>
<body>
<h1>Products</h1>
<ul>
{% for product in products %}
<li>{{ product.name }} - {{ product.description }} - ${{ product.price }}</li>
{% endfor %}
</ul>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Email, EqualTo

class RegisterForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Register')

class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Login')
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from flask import Blueprint, render_template, redirect, url_for, flash
from werkzeug.security import generate_password_hash, check_password_hash
from src.models.user import User
from src.ext import db
from src.views.auth.forms import RegisterForm, LoginForm

auth_bp = Blueprint('auth', __name__, template_folder='../../templates/auth')

@auth_bp.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
hashed_password = generate_password_hash(form.password.data, method='sha256')
new_user = User(username=form.username.data, email=form.email.data, password=hashed_password)
db.session.add(new_user)
db.session.commit()
flash('Registration successful!', 'success')
return redirect(url_for('auth.login'))
return render_template('register.html', form=form)

@auth_bp.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and check_password_hash(user.password.data, form.password.data):
flash('Login successful!', 'success')
return redirect(url_for('main.home'))
flash('Invalid credentials.', 'danger')
return render_template('login.html', form=form)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from flask import render_template, Blueprint

main_bp = Blueprint('main', __name__, template_folder='../../templates/main')

@main_bp.route('/')
def home():
return render_template('home.html')
Loading