├── .github └── FUNDING.yml ├── 01 - Introduction └── market.py ├── 02 - Styling and Templates ├── market.py └── templates │ └── home.html ├── 03 - Sending Data to Templates ├── market.py └── templates │ ├── home.html │ └── market.html ├── 04 - Template Inheritance ├── market.py └── templates │ ├── base.html │ ├── home.html │ └── market.html ├── 05 - Models and Databases ├── market.db ├── market.py └── templates │ ├── base.html │ ├── home.html │ └── market.html ├── 06 - Project Restructure ├── market │ ├── __init__.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ └── market.html └── run.py ├── 07 - Model Relationships ├── market │ ├── __init__.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ └── market.html └── run.py ├── 08 - Flask Forms ├── market │ ├── __init__.py │ ├── forms.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ ├── market.html │ │ └── register.html └── run.py ├── 09 - Flask Validations ├── market │ ├── __init__.py │ ├── forms.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ ├── market.html │ │ └── register.html └── run.py ├── 10 - Flash Messages & Advanced Validations ├── market │ ├── __init__.py │ ├── forms.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ ├── market.html │ │ └── register.html └── run.py ├── 11 - User Authentication Part 1 ├── market │ ├── __init__.py │ ├── forms.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ ├── login.html │ │ ├── market.html │ │ └── register.html └── run.py ├── 12 - User Authentication Part 2 ├── market │ ├── __init__.py │ ├── forms.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ ├── login.html │ │ ├── market.html │ │ └── register.html └── run.py ├── 13 - Logout & Customizations ├── market │ ├── __init__.py │ ├── forms.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ ├── login.html │ │ ├── market.html │ │ └── register.html └── run.py ├── 14 - Item Purchasing Part 1 ├── market │ ├── __init__.py │ ├── forms.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ ├── includes │ │ └── items_modals.html │ │ ├── login.html │ │ ├── market.html │ │ └── register.html └── run.py ├── 15 - Item Purchasing Part 2 ├── market │ ├── __init__.py │ ├── forms.py │ ├── market.db │ ├── models.py │ ├── routes.py │ └── templates │ │ ├── base.html │ │ ├── home.html │ │ ├── includes │ │ └── items_modals.html │ │ ├── login.html │ │ ├── market.html │ │ └── register.html └── run.py └── 16 - Item Selling ├── market ├── __init__.py ├── forms.py ├── market.db ├── models.py ├── routes.py └── templates │ ├── base.html │ ├── home.html │ ├── includes │ ├── items_modals.html │ └── owned_items_modals.html │ ├── login.html │ ├── market.html │ └── register.html └── run.py /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [jimdevops19] 2 | patreon: jimshapedcoding 3 | custom: ["https://www.buymeacoffee.com/jimsc"] 4 | -------------------------------------------------------------------------------- /01 - Introduction/market.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | app = Flask(__name__) 3 | 4 | @app.route('/') 5 | def hello_world(): 6 | return '

Hello World

' 7 | 8 | @app.route('/about/') 9 | def about_page(username): 10 | return f'

This is the about page of {username}' -------------------------------------------------------------------------------- /02 - Styling and Templates/market.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | app = Flask(__name__) 3 | 4 | @app.route('/') 5 | @app.route('/home') 6 | def home_page(): 7 | return render_template('home.html') -------------------------------------------------------------------------------- /02 - Styling and Templates/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Home Page 12 | 13 | 14 | 38 | 39 |

Home Page

40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 52 | 53 | 59 | -------------------------------------------------------------------------------- /03 - Sending Data to Templates/market.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | app = Flask(__name__) 3 | 4 | @app.route('/') 5 | @app.route('/home') 6 | def home_page(): 7 | return render_template('home.html') 8 | 9 | @app.route('/market') 10 | def market_page(): 11 | items = [ 12 | {'id': 1, 'name': 'Phone', 'barcode': '893212299897', 'price': 500}, 13 | {'id': 2, 'name': 'Laptop', 'barcode': '123985473165', 'price': 900}, 14 | {'id': 3, 'name': 'Keyboard', 'barcode': '231985128446', 'price': 150} 15 | ] 16 | return render_template('market.html', items=items) -------------------------------------------------------------------------------- /03 - Sending Data to Templates/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Home Page 12 | 13 | 14 | 38 | 39 |

Home Page

40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 52 | 53 | 59 | -------------------------------------------------------------------------------- /03 - Sending Data to Templates/templates/market.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Market Page 12 | 13 | 14 | 38 | 39 |

Market Page

40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | {% for item in items %} 54 | 55 | 56 | 57 | 58 | 59 | 63 | 64 | {% endfor %} 65 | 66 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 60 | 61 | 62 |
67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 79 | 80 | 86 | -------------------------------------------------------------------------------- /04 - Template Inheritance/market.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | app = Flask(__name__) 3 | 4 | @app.route('/') 5 | @app.route('/home') 6 | def home_page(): 7 | return render_template('home.html') 8 | 9 | @app.route('/market') 10 | def market_page(): 11 | items = [ 12 | {'id': 1, 'name': 'Phone', 'barcode': '893212299897', 'price': 500}, 13 | {'id': 2, 'name': 'Laptop', 'barcode': '123985473165', 'price': 900}, 14 | {'id': 3, 'name': 'Keyboard', 'barcode': '231985128446', 'price': 150} 15 | ] 16 | return render_template('market.html', items=items) -------------------------------------------------------------------------------- /04 - Template Inheritance/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 40 | {% block content %} 41 | 42 | {% endblock %} 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 60 | -------------------------------------------------------------------------------- /04 - Template Inheritance/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /04 - Template Inheritance/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /05 - Models and Databases/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/05 - Models and Databases/market.db -------------------------------------------------------------------------------- /05 - Models and Databases/market.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | from flask_sqlalchemy import SQLAlchemy 3 | app = Flask(__name__) 4 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 5 | db = SQLAlchemy(app) 6 | 7 | class Item(db.Model): 8 | id = db.Column(db.Integer(), primary_key=True) 9 | name = db.Column(db.String(length=30), nullable=False, unique=True) 10 | price = db.Column(db.Integer(), nullable=False) 11 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 12 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 13 | 14 | def __repr__(self): 15 | return f'Item {self.name}' 16 | 17 | 18 | @app.route('/') 19 | @app.route('/home') 20 | def home_page(): 21 | return render_template('home.html') 22 | 23 | @app.route('/market') 24 | def market_page(): 25 | items = Item.query.all() 26 | return render_template('market.html', items=items) -------------------------------------------------------------------------------- /05 - Models and Databases/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 40 | {% block content %} 41 | 42 | {% endblock %} 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 60 | -------------------------------------------------------------------------------- /05 - Models and Databases/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /05 - Models and Databases/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /06 - Project Restructure/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | from flask_sqlalchemy import SQLAlchemy 3 | app = Flask(__name__) 4 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 5 | db = SQLAlchemy(app) 6 | 7 | from market import routes -------------------------------------------------------------------------------- /06 - Project Restructure/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/06 - Project Restructure/market/market.db -------------------------------------------------------------------------------- /06 - Project Restructure/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db 2 | 3 | 4 | class Item(db.Model): 5 | id = db.Column(db.Integer(), primary_key=True) 6 | name = db.Column(db.String(length=30), nullable=False, unique=True) 7 | price = db.Column(db.Integer(), nullable=False) 8 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 9 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 10 | 11 | def __repr__(self): 12 | return f'Item {self.name}' -------------------------------------------------------------------------------- /06 - Project Restructure/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template 3 | from market.models import Item 4 | 5 | @app.route('/') 6 | @app.route('/home') 7 | def home_page(): 8 | return render_template('home.html') 9 | 10 | @app.route('/market') 11 | def market_page(): 12 | items = Item.query.all() 13 | return render_template('market.html', items=items) -------------------------------------------------------------------------------- /06 - Project Restructure/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 40 | {% block content %} 41 | 42 | {% endblock %} 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 60 | -------------------------------------------------------------------------------- /06 - Project Restructure/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /06 - Project Restructure/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /06 - Project Restructure/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /07 - Model Relationships/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | from flask_sqlalchemy import SQLAlchemy 3 | app = Flask(__name__) 4 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 5 | db = SQLAlchemy(app) 6 | 7 | from market import routes -------------------------------------------------------------------------------- /07 - Model Relationships/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/07 - Model Relationships/market/market.db -------------------------------------------------------------------------------- /07 - Model Relationships/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db 2 | 3 | class User(db.Model): 4 | id = db.Column(db.Integer(), primary_key=True) 5 | username = db.Column(db.String(length=30), nullable=False, unique=True) 6 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 7 | password_hash = db.Column(db.String(length=60), nullable=False) 8 | budget = db.Column(db.Integer(), nullable=False, default=1000) 9 | items = db.relationship('Item', backref='owned_user', lazy=True) 10 | 11 | class Item(db.Model): 12 | id = db.Column(db.Integer(), primary_key=True) 13 | name = db.Column(db.String(length=30), nullable=False, unique=True) 14 | price = db.Column(db.Integer(), nullable=False) 15 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 16 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 17 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 18 | def __repr__(self): 19 | return f'Item {self.name}' -------------------------------------------------------------------------------- /07 - Model Relationships/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template 3 | from market.models import Item 4 | 5 | @app.route('/') 6 | @app.route('/home') 7 | def home_page(): 8 | return render_template('home.html') 9 | 10 | @app.route('/market') 11 | def market_page(): 12 | items = Item.query.all() 13 | return render_template('market.html', items=items) -------------------------------------------------------------------------------- /07 - Model Relationships/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 40 | {% block content %} 41 | 42 | {% endblock %} 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 60 | -------------------------------------------------------------------------------- /07 - Model Relationships/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /07 - Model Relationships/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /07 - Model Relationships/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /08 - Flask Forms/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | from flask_sqlalchemy import SQLAlchemy 3 | 4 | app = Flask(__name__) 5 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 6 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 7 | db = SQLAlchemy(app) 8 | from market import routes -------------------------------------------------------------------------------- /08 - Flask Forms/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField 3 | 4 | 5 | class RegisterForm(FlaskForm): 6 | username = StringField(label='User Name:') 7 | email_address = StringField(label='Email Address:') 8 | password1 = PasswordField(label='Password:') 9 | password2 = PasswordField(label='Confirm Password:') 10 | submit = SubmitField(label='Create Account') -------------------------------------------------------------------------------- /08 - Flask Forms/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/08 - Flask Forms/market/market.db -------------------------------------------------------------------------------- /08 - Flask Forms/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db 2 | 3 | class User(db.Model): 4 | id = db.Column(db.Integer(), primary_key=True) 5 | username = db.Column(db.String(length=30), nullable=False, unique=True) 6 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 7 | password_hash = db.Column(db.String(length=60), nullable=False) 8 | budget = db.Column(db.Integer(), nullable=False, default=1000) 9 | items = db.relationship('Item', backref='owned_user', lazy=True) 10 | 11 | class Item(db.Model): 12 | id = db.Column(db.Integer(), primary_key=True) 13 | name = db.Column(db.String(length=30), nullable=False, unique=True) 14 | price = db.Column(db.Integer(), nullable=False) 15 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 16 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 17 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 18 | def __repr__(self): 19 | return f'Item {self.name}' -------------------------------------------------------------------------------- /08 - Flask Forms/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template 3 | from market.models import Item 4 | from market.forms import RegisterForm 5 | 6 | @app.route('/') 7 | @app.route('/home') 8 | def home_page(): 9 | return render_template('home.html') 10 | 11 | @app.route('/market') 12 | def market_page(): 13 | items = Item.query.all() 14 | return render_template('market.html', items=items) 15 | 16 | @app.route('/register') 17 | def register_page(): 18 | form = RegisterForm() 19 | return render_template('register.html', form=form) 20 | -------------------------------------------------------------------------------- /08 - Flask Forms/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 40 | {% block content %} 41 | 42 | {% endblock %} 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 60 | -------------------------------------------------------------------------------- /08 - Flask Forms/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /08 - Flask Forms/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /08 - Flask Forms/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | 11 |

12 | Please Create your Account 13 |

14 |
15 | {{ form.username.label() }} 16 | {{ form.username(class="form-control", placeholder="User Name") }} 17 | 18 | {{ form.email_address.label() }} 19 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 20 | 21 | {{ form.password1.label() }} 22 | {{ form.password1(class="form-control", placeholder="Password") }} 23 | 24 | {{ form.password2.label() }} 25 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 26 | 27 |
28 | 29 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 30 | 31 |
32 |
33 | 34 | {% endblock %} -------------------------------------------------------------------------------- /08 - Flask Forms/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /09 - Flask Validations/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | from flask_sqlalchemy import SQLAlchemy 3 | 4 | app = Flask(__name__) 5 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 6 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 7 | db = SQLAlchemy(app) 8 | from market import routes -------------------------------------------------------------------------------- /09 - Flask Validations/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField 3 | from wtforms.validators import Length, EqualTo, Email, DataRequired 4 | 5 | class RegisterForm(FlaskForm): 6 | username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) 7 | email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) 8 | password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) 9 | password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) 10 | submit = SubmitField(label='Create Account') -------------------------------------------------------------------------------- /09 - Flask Validations/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/09 - Flask Validations/market/market.db -------------------------------------------------------------------------------- /09 - Flask Validations/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db 2 | 3 | class User(db.Model): 4 | id = db.Column(db.Integer(), primary_key=True) 5 | username = db.Column(db.String(length=30), nullable=False, unique=True) 6 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 7 | password_hash = db.Column(db.String(length=60), nullable=False) 8 | budget = db.Column(db.Integer(), nullable=False, default=1000) 9 | items = db.relationship('Item', backref='owned_user', lazy=True) 10 | 11 | class Item(db.Model): 12 | id = db.Column(db.Integer(), primary_key=True) 13 | name = db.Column(db.String(length=30), nullable=False, unique=True) 14 | price = db.Column(db.Integer(), nullable=False) 15 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 16 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 17 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 18 | def __repr__(self): 19 | return f'Item {self.name}' -------------------------------------------------------------------------------- /09 - Flask Validations/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template, redirect, url_for 3 | from market.models import Item, User 4 | from market.forms import RegisterForm 5 | from market import db 6 | 7 | @app.route('/') 8 | @app.route('/home') 9 | def home_page(): 10 | return render_template('home.html') 11 | 12 | @app.route('/market') 13 | def market_page(): 14 | items = Item.query.all() 15 | return render_template('market.html', items=items) 16 | 17 | @app.route('/register', methods=['GET', 'POST']) 18 | def register_page(): 19 | form = RegisterForm() 20 | if form.validate_on_submit(): 21 | user_to_create = User(username=form.username.data, 22 | email_address=form.email_address.data, 23 | password_hash=form.password1.data) 24 | db.session.add(user_to_create) 25 | db.session.commit() 26 | return redirect(url_for('market_page')) 27 | if form.errors != {}: #If there are not errors from the validations 28 | for err_msg in form.errors.values(): 29 | print(f'There was an error with creating a user: {err_msg}') 30 | 31 | return render_template('register.html', form=form) 32 | -------------------------------------------------------------------------------- /09 - Flask Validations/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 40 | {% block content %} 41 | 42 | {% endblock %} 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 60 | -------------------------------------------------------------------------------- /09 - Flask Validations/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /09 - Flask Validations/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /09 - Flask Validations/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | {{ form.hidden_tag() }} 11 | 12 |

13 | Please Create your Account 14 |

15 |
16 | {{ form.username.label() }} 17 | {{ form.username(class="form-control", placeholder="User Name") }} 18 | 19 | {{ form.email_address.label() }} 20 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 21 | 22 | {{ form.password1.label() }} 23 | {{ form.password1(class="form-control", placeholder="Password") }} 24 | 25 | {{ form.password2.label() }} 26 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 27 | 28 |
29 | 30 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 31 | 32 |
33 |
34 | 35 | {% endblock %} -------------------------------------------------------------------------------- /09 - Flask Validations/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template 2 | from flask_sqlalchemy import SQLAlchemy 3 | 4 | app = Flask(__name__) 5 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 6 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 7 | db = SQLAlchemy(app) 8 | from market import routes -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField 3 | from wtforms.validators import Length, EqualTo, Email, DataRequired, ValidationError 4 | from market.models import User 5 | 6 | 7 | class RegisterForm(FlaskForm): 8 | def validate_username(self, username_to_check): 9 | user = User.query.filter_by(username=username_to_check.data).first() 10 | if user: 11 | raise ValidationError('Username already exists! Please try a different username') 12 | 13 | def validate_email_address(self, email_address_to_check): 14 | email_address = User.query.filter_by(email_address=email_address_to_check.data).first() 15 | if email_address: 16 | raise ValidationError('Email Address already exists! Please try a different email address') 17 | 18 | username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) 19 | email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) 20 | password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) 21 | password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) 22 | submit = SubmitField(label='Create Account') -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/10 - Flash Messages & Advanced Validations/market/market.db -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db 2 | 3 | class User(db.Model): 4 | id = db.Column(db.Integer(), primary_key=True) 5 | username = db.Column(db.String(length=30), nullable=False, unique=True) 6 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 7 | password_hash = db.Column(db.String(length=60), nullable=False) 8 | budget = db.Column(db.Integer(), nullable=False, default=1000) 9 | items = db.relationship('Item', backref='owned_user', lazy=True) 10 | 11 | class Item(db.Model): 12 | id = db.Column(db.Integer(), primary_key=True) 13 | name = db.Column(db.String(length=30), nullable=False, unique=True) 14 | price = db.Column(db.Integer(), nullable=False) 15 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 16 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 17 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 18 | def __repr__(self): 19 | return f'Item {self.name}' -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template, redirect, url_for, flash 3 | from market.models import Item, User 4 | from market.forms import RegisterForm 5 | from market import db 6 | 7 | @app.route('/') 8 | @app.route('/home') 9 | def home_page(): 10 | return render_template('home.html') 11 | 12 | @app.route('/market') 13 | def market_page(): 14 | items = Item.query.all() 15 | return render_template('market.html', items=items) 16 | 17 | @app.route('/register', methods=['GET', 'POST']) 18 | def register_page(): 19 | form = RegisterForm() 20 | if form.validate_on_submit(): 21 | user_to_create = User(username=form.username.data, 22 | email_address=form.email_address.data, 23 | password_hash=form.password1.data) 24 | db.session.add(user_to_create) 25 | db.session.commit() 26 | return redirect(url_for('market_page')) 27 | if form.errors != {}: #If there are not errors from the validations 28 | for err_msg in form.errors.values(): 29 | flash(f'There was an error with creating a user: {err_msg}', category='danger') 30 | 31 | return render_template('register.html', form=form) 32 | -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 40 | {% with messages = get_flashed_messages(with_categories=true) %} 41 | {% if messages %} 42 | {% for category, message in messages %} 43 |
44 | 47 | {{ message }} 48 |
49 | {% endfor %} 50 | {% endif %} 51 | {% endwith %} 52 | {% block content %} 53 | 54 | {% endblock %} 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 72 | -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | {{ form.hidden_tag() }} 11 | 12 |

13 | Please Create your Account 14 |

15 |
16 | {{ form.username.label() }} 17 | {{ form.username(class="form-control", placeholder="User Name") }} 18 | 19 | {{ form.email_address.label() }} 20 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 21 | 22 | {{ form.password1.label() }} 23 | {{ form.password1(class="form-control", placeholder="Password") }} 24 | 25 | {{ form.password2.label() }} 26 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 27 | 28 |
29 | 30 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 31 | 32 |
33 |
34 | 35 | {% endblock %} -------------------------------------------------------------------------------- /10 - Flash Messages & Advanced Validations/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_sqlalchemy import SQLAlchemy 3 | from flask_bcrypt import Bcrypt 4 | 5 | app = Flask(__name__) 6 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 7 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 8 | db = SQLAlchemy(app) 9 | bcrypt = Bcrypt(app) 10 | from market import routes -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField 3 | from wtforms.validators import Length, EqualTo, Email, DataRequired, ValidationError 4 | from market.models import User 5 | 6 | 7 | class RegisterForm(FlaskForm): 8 | def validate_username(self, username_to_check): 9 | user = User.query.filter_by(username=username_to_check.data).first() 10 | if user: 11 | raise ValidationError('Username already exists! Please try a different username') 12 | 13 | def validate_email_address(self, email_address_to_check): 14 | email_address = User.query.filter_by(email_address=email_address_to_check.data).first() 15 | if email_address: 16 | raise ValidationError('Email Address already exists! Please try a different email address') 17 | 18 | username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) 19 | email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) 20 | password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) 21 | password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) 22 | submit = SubmitField(label='Create Account') 23 | 24 | 25 | class LoginForm(FlaskForm): 26 | username = StringField(label='User Name:', validators=[DataRequired()]) 27 | password = PasswordField(label='Password:', validators=[DataRequired()]) 28 | submit = SubmitField(label='Sign in') -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/11 - User Authentication Part 1/market/market.db -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db 2 | from market import bcrypt 3 | 4 | class User(db.Model): 5 | id = db.Column(db.Integer(), primary_key=True) 6 | username = db.Column(db.String(length=30), nullable=False, unique=True) 7 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 8 | password_hash = db.Column(db.String(length=60), nullable=False) 9 | budget = db.Column(db.Integer(), nullable=False, default=1000) 10 | items = db.relationship('Item', backref='owned_user', lazy=True) 11 | 12 | @property 13 | def password(self): 14 | return self.password 15 | 16 | @password.setter 17 | def password(self, plain_text_password): 18 | self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8') 19 | 20 | class Item(db.Model): 21 | id = db.Column(db.Integer(), primary_key=True) 22 | name = db.Column(db.String(length=30), nullable=False, unique=True) 23 | price = db.Column(db.Integer(), nullable=False) 24 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 25 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 26 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 27 | def __repr__(self): 28 | return f'Item {self.name}' -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template, redirect, url_for, flash 3 | from market.models import Item, User 4 | from market.forms import RegisterForm, LoginForm 5 | from market import db 6 | 7 | @app.route('/') 8 | @app.route('/home') 9 | def home_page(): 10 | return render_template('home.html') 11 | 12 | @app.route('/market') 13 | def market_page(): 14 | items = Item.query.all() 15 | return render_template('market.html', items=items) 16 | 17 | @app.route('/register', methods=['GET', 'POST']) 18 | def register_page(): 19 | form = RegisterForm() 20 | if form.validate_on_submit(): 21 | user_to_create = User(username=form.username.data, 22 | email_address=form.email_address.data, 23 | password=form.password1.data) 24 | db.session.add(user_to_create) 25 | db.session.commit() 26 | return redirect(url_for('market_page')) 27 | if form.errors != {}: #If there are not errors from the validations 28 | for err_msg in form.errors.values(): 29 | flash(f'There was an error with creating a user: {err_msg}', category='danger') 30 | 31 | return render_template('register.html', form=form) 32 | 33 | @app.route('/login', methods=['GET', 'POST']) 34 | def login_page(): 35 | form = LoginForm() 36 | return render_template('login.html', form=form) 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 40 | {% with messages = get_flashed_messages(with_categories=true) %} 41 | {% if messages %} 42 | {% for category, message in messages %} 43 |
44 | 47 | {{ message }} 48 |
49 | {% endfor %} 50 | {% endif %} 51 | {% endwith %} 52 | {% block content %} 53 | 54 | {% endblock %} 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 72 | -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Login Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 | 33 |
34 | 35 | {% endblock %} -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /11 - User Authentication Part 1/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | {{ form.hidden_tag() }} 11 | 12 |

13 | Please Create your Account 14 |

15 |
16 | {{ form.username.label() }} 17 | {{ form.username(class="form-control", placeholder="User Name") }} 18 | 19 | {{ form.email_address.label() }} 20 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 21 | 22 | {{ form.password1.label() }} 23 | {{ form.password1(class="form-control", placeholder="Password") }} 24 | 25 | {{ form.password2.label() }} 26 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 27 | 28 |
29 | 30 |
31 |
Already have an account?
32 | Login 33 |
34 | 35 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 36 | 37 |
38 |
39 | 40 | {% endblock %} -------------------------------------------------------------------------------- /11 - User Authentication Part 1/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_sqlalchemy import SQLAlchemy 3 | from flask_bcrypt import Bcrypt 4 | from flask_login import LoginManager 5 | 6 | app = Flask(__name__) 7 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 8 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 9 | db = SQLAlchemy(app) 10 | bcrypt = Bcrypt(app) 11 | login_manager = LoginManager(app) 12 | from market import routes -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField 3 | from wtforms.validators import Length, EqualTo, Email, DataRequired, ValidationError 4 | from market.models import User 5 | 6 | 7 | class RegisterForm(FlaskForm): 8 | def validate_username(self, username_to_check): 9 | user = User.query.filter_by(username=username_to_check.data).first() 10 | if user: 11 | raise ValidationError('Username already exists! Please try a different username') 12 | 13 | def validate_email_address(self, email_address_to_check): 14 | email_address = User.query.filter_by(email_address=email_address_to_check.data).first() 15 | if email_address: 16 | raise ValidationError('Email Address already exists! Please try a different email address') 17 | 18 | username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) 19 | email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) 20 | password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) 21 | password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) 22 | submit = SubmitField(label='Create Account') 23 | 24 | 25 | class LoginForm(FlaskForm): 26 | username = StringField(label='User Name:', validators=[DataRequired()]) 27 | password = PasswordField(label='Password:', validators=[DataRequired()]) 28 | submit = SubmitField(label='Sign in') -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/12 - User Authentication Part 2/market/market.db -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db, login_manager 2 | from market import bcrypt 3 | from flask_login import UserMixin 4 | 5 | @login_manager.user_loader 6 | def load_user(user_id): 7 | return User.query.get(int(user_id)) 8 | 9 | class User(db.Model, UserMixin): 10 | id = db.Column(db.Integer(), primary_key=True) 11 | username = db.Column(db.String(length=30), nullable=False, unique=True) 12 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 13 | password_hash = db.Column(db.String(length=60), nullable=False) 14 | budget = db.Column(db.Integer(), nullable=False, default=1000) 15 | items = db.relationship('Item', backref='owned_user', lazy=True) 16 | 17 | @property 18 | def prettier_budget(self): 19 | if len(str(self.budget)) >= 4: 20 | return f'{str(self.budget)[:-3]},{str(self.budget)[-3:]}$' 21 | else: 22 | return f"{self.budget}$" 23 | 24 | @property 25 | def password(self): 26 | return self.password 27 | 28 | @password.setter 29 | def password(self, plain_text_password): 30 | self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8') 31 | 32 | def check_password_correction(self, attempted_password): 33 | return bcrypt.check_password_hash(self.password_hash, attempted_password) 34 | 35 | class Item(db.Model): 36 | id = db.Column(db.Integer(), primary_key=True) 37 | name = db.Column(db.String(length=30), nullable=False, unique=True) 38 | price = db.Column(db.Integer(), nullable=False) 39 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 40 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 41 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 42 | def __repr__(self): 43 | return f'Item {self.name}' -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template, redirect, url_for, flash 3 | from market.models import Item, User 4 | from market.forms import RegisterForm, LoginForm 5 | from market import db 6 | from flask_login import login_user 7 | 8 | @app.route('/') 9 | @app.route('/home') 10 | def home_page(): 11 | return render_template('home.html') 12 | 13 | @app.route('/market') 14 | def market_page(): 15 | items = Item.query.all() 16 | return render_template('market.html', items=items) 17 | 18 | @app.route('/register', methods=['GET', 'POST']) 19 | def register_page(): 20 | form = RegisterForm() 21 | if form.validate_on_submit(): 22 | user_to_create = User(username=form.username.data, 23 | email_address=form.email_address.data, 24 | password=form.password1.data) 25 | db.session.add(user_to_create) 26 | db.session.commit() 27 | return redirect(url_for('market_page')) 28 | if form.errors != {}: #If there are not errors from the validations 29 | for err_msg in form.errors.values(): 30 | flash(f'There was an error with creating a user: {err_msg}', category='danger') 31 | 32 | return render_template('register.html', form=form) 33 | 34 | @app.route('/login', methods=['GET', 'POST']) 35 | def login_page(): 36 | form = LoginForm() 37 | if form.validate_on_submit(): 38 | attempted_user = User.query.filter_by(username=form.username.data).first() 39 | if attempted_user and attempted_user.check_password_correction( 40 | attempted_password=form.password.data 41 | ): 42 | login_user(attempted_user) 43 | flash(f'Success! You are logged in as: {attempted_user.username}', category='success') 44 | return redirect(url_for('market_page')) 45 | else: 46 | flash('Username and password are not match! Please try again', category='danger') 47 | 48 | return render_template('login.html', form=form) 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 57 | {% with messages = get_flashed_messages(with_categories=true) %} 58 | {% if messages %} 59 | {% for category, message in messages %} 60 |
61 | 64 | {{ message }} 65 |
66 | {% endfor %} 67 | {% endif %} 68 | {% endwith %} 69 | {% block content %} 70 | 71 | {% endblock %} 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 89 | -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Home Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | This is our content for the Home Page 8 | {% endblock %} -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Login Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 | 33 |
34 | 35 | {% endblock %} -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /12 - User Authentication Part 2/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | {{ form.hidden_tag() }} 11 | 12 |

13 | Please Create your Account 14 |

15 |
16 | {{ form.username.label() }} 17 | {{ form.username(class="form-control", placeholder="User Name") }} 18 | 19 | {{ form.email_address.label() }} 20 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 21 | 22 | {{ form.password1.label() }} 23 | {{ form.password1(class="form-control", placeholder="Password") }} 24 | 25 | {{ form.password2.label() }} 26 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 27 | 28 |
29 | 30 |
31 |
Already have an account?
32 | Login 33 |
34 | 35 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 36 | 37 |
38 |
39 | 40 | {% endblock %} -------------------------------------------------------------------------------- /12 - User Authentication Part 2/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_sqlalchemy import SQLAlchemy 3 | from flask_bcrypt import Bcrypt 4 | from flask_login import LoginManager 5 | 6 | app = Flask(__name__) 7 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 8 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 9 | db = SQLAlchemy(app) 10 | bcrypt = Bcrypt(app) 11 | login_manager = LoginManager(app) 12 | login_manager.login_view = "login_page" 13 | login_manager.login_message_category = "info" 14 | from market import routes -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField 3 | from wtforms.validators import Length, EqualTo, Email, DataRequired, ValidationError 4 | from market.models import User 5 | 6 | 7 | class RegisterForm(FlaskForm): 8 | def validate_username(self, username_to_check): 9 | user = User.query.filter_by(username=username_to_check.data).first() 10 | if user: 11 | raise ValidationError('Username already exists! Please try a different username') 12 | 13 | def validate_email_address(self, email_address_to_check): 14 | email_address = User.query.filter_by(email_address=email_address_to_check.data).first() 15 | if email_address: 16 | raise ValidationError('Email Address already exists! Please try a different email address') 17 | 18 | username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) 19 | email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) 20 | password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) 21 | password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) 22 | submit = SubmitField(label='Create Account') 23 | 24 | 25 | class LoginForm(FlaskForm): 26 | username = StringField(label='User Name:', validators=[DataRequired()]) 27 | password = PasswordField(label='Password:', validators=[DataRequired()]) 28 | submit = SubmitField(label='Sign in') -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/13 - Logout & Customizations/market/market.db -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db, login_manager 2 | from market import bcrypt 3 | from flask_login import UserMixin 4 | 5 | @login_manager.user_loader 6 | def load_user(user_id): 7 | return User.query.get(int(user_id)) 8 | 9 | class User(db.Model, UserMixin): 10 | id = db.Column(db.Integer(), primary_key=True) 11 | username = db.Column(db.String(length=30), nullable=False, unique=True) 12 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 13 | password_hash = db.Column(db.String(length=60), nullable=False) 14 | budget = db.Column(db.Integer(), nullable=False, default=1000) 15 | items = db.relationship('Item', backref='owned_user', lazy=True) 16 | 17 | @property 18 | def prettier_budget(self): 19 | if len(str(self.budget)) >= 4: 20 | return f'{str(self.budget)[:-3]},{str(self.budget)[-3:]}$' 21 | else: 22 | return f"{self.budget}$" 23 | 24 | @property 25 | def password(self): 26 | return self.password 27 | 28 | @password.setter 29 | def password(self, plain_text_password): 30 | self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8') 31 | 32 | def check_password_correction(self, attempted_password): 33 | return bcrypt.check_password_hash(self.password_hash, attempted_password) 34 | 35 | class Item(db.Model): 36 | id = db.Column(db.Integer(), primary_key=True) 37 | name = db.Column(db.String(length=30), nullable=False, unique=True) 38 | price = db.Column(db.Integer(), nullable=False) 39 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 40 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 41 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 42 | def __repr__(self): 43 | return f'Item {self.name}' -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template, redirect, url_for, flash 3 | from market.models import Item, User 4 | from market.forms import RegisterForm, LoginForm 5 | from market import db 6 | from flask_login import login_user, logout_user, login_required 7 | 8 | @app.route('/') 9 | @app.route('/home') 10 | def home_page(): 11 | return render_template('home.html') 12 | 13 | @app.route('/market') 14 | @login_required 15 | def market_page(): 16 | items = Item.query.all() 17 | return render_template('market.html', items=items) 18 | 19 | @app.route('/register', methods=['GET', 'POST']) 20 | def register_page(): 21 | form = RegisterForm() 22 | if form.validate_on_submit(): 23 | user_to_create = User(username=form.username.data, 24 | email_address=form.email_address.data, 25 | password=form.password1.data) 26 | db.session.add(user_to_create) 27 | db.session.commit() 28 | login_user(user_to_create) 29 | flash(f"Account created successfully! You are now logged in as {user_to_create.username}", category='success') 30 | return redirect(url_for('market_page')) 31 | if form.errors != {}: #If there are not errors from the validations 32 | for err_msg in form.errors.values(): 33 | flash(f'There was an error with creating a user: {err_msg}', category='danger') 34 | 35 | return render_template('register.html', form=form) 36 | 37 | @app.route('/login', methods=['GET', 'POST']) 38 | def login_page(): 39 | form = LoginForm() 40 | if form.validate_on_submit(): 41 | attempted_user = User.query.filter_by(username=form.username.data).first() 42 | if attempted_user and attempted_user.check_password_correction( 43 | attempted_password=form.password.data 44 | ): 45 | login_user(attempted_user) 46 | flash(f'Success! You are logged in as: {attempted_user.username}', category='success') 47 | return redirect(url_for('market_page')) 48 | else: 49 | flash('Username and password are not match! Please try again', category='danger') 50 | 51 | return render_template('login.html', form=form) 52 | 53 | @app.route('/logout') 54 | def logout_page(): 55 | logout_user() 56 | flash("You have been logged out!", category='info') 57 | return redirect(url_for("home_page")) 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 57 | {% with messages = get_flashed_messages(with_categories=true) %} 58 | {% if messages %} 59 | {% for category, message in messages %} 60 |
61 | 64 | {{ message }} 65 |
66 | {% endfor %} 67 | {% endif %} 68 | {% endwith %} 69 | {% block content %} 70 | 71 | {% endblock %} 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 89 | -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Welcome to Jim Shaped Coding Market 4 | {% endblock %} 5 | {% block content %} 6 |
7 |
8 |

Jim Shaped Coding Market

9 |

Start purchasing products by clicking the link below

10 | Get Started 11 |
12 |
13 |
14 |
15 | {% endblock %} -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Login Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 | 33 |
34 | 35 | {% endblock %} -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | {% for item in items %} 21 | 22 | 23 | 24 | 25 | 26 | 30 | 31 | {% endfor %} 32 | 33 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 27 | 28 | 29 |
34 | {% endblock %} -------------------------------------------------------------------------------- /13 - Logout & Customizations/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | {{ form.hidden_tag() }} 11 | 12 |

13 | Please Create your Account 14 |

15 |
16 | {{ form.username.label() }} 17 | {{ form.username(class="form-control", placeholder="User Name") }} 18 | 19 | {{ form.email_address.label() }} 20 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 21 | 22 | {{ form.password1.label() }} 23 | {{ form.password1(class="form-control", placeholder="Password") }} 24 | 25 | {{ form.password2.label() }} 26 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 27 | 28 |
29 | 30 |
31 |
Already have an account?
32 | Login 33 |
34 | 35 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 36 | 37 |
38 |
39 | 40 | {% endblock %} -------------------------------------------------------------------------------- /13 - Logout & Customizations/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_sqlalchemy import SQLAlchemy 3 | from flask_bcrypt import Bcrypt 4 | from flask_login import LoginManager 5 | 6 | app = Flask(__name__) 7 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 8 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 9 | db = SQLAlchemy(app) 10 | bcrypt = Bcrypt(app) 11 | login_manager = LoginManager(app) 12 | login_manager.login_view = "login_page" 13 | login_manager.login_message_category = "info" 14 | from market import routes -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField 3 | from wtforms.validators import Length, EqualTo, Email, DataRequired, ValidationError 4 | from market.models import User 5 | 6 | 7 | class RegisterForm(FlaskForm): 8 | def validate_username(self, username_to_check): 9 | user = User.query.filter_by(username=username_to_check.data).first() 10 | if user: 11 | raise ValidationError('Username already exists! Please try a different username') 12 | 13 | def validate_email_address(self, email_address_to_check): 14 | email_address = User.query.filter_by(email_address=email_address_to_check.data).first() 15 | if email_address: 16 | raise ValidationError('Email Address already exists! Please try a different email address') 17 | 18 | username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) 19 | email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) 20 | password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) 21 | password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) 22 | submit = SubmitField(label='Create Account') 23 | 24 | 25 | class LoginForm(FlaskForm): 26 | username = StringField(label='User Name:', validators=[DataRequired()]) 27 | password = PasswordField(label='Password:', validators=[DataRequired()]) 28 | submit = SubmitField(label='Sign in') -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/14 - Item Purchasing Part 1/market/market.db -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db, login_manager 2 | from market import bcrypt 3 | from flask_login import UserMixin 4 | 5 | @login_manager.user_loader 6 | def load_user(user_id): 7 | return User.query.get(int(user_id)) 8 | 9 | class User(db.Model, UserMixin): 10 | id = db.Column(db.Integer(), primary_key=True) 11 | username = db.Column(db.String(length=30), nullable=False, unique=True) 12 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 13 | password_hash = db.Column(db.String(length=60), nullable=False) 14 | budget = db.Column(db.Integer(), nullable=False, default=1000) 15 | items = db.relationship('Item', backref='owned_user', lazy=True) 16 | 17 | @property 18 | def prettier_budget(self): 19 | if len(str(self.budget)) >= 4: 20 | return f'{str(self.budget)[:-3]},{str(self.budget)[-3:]}$' 21 | else: 22 | return f"{self.budget}$" 23 | 24 | @property 25 | def password(self): 26 | return self.password 27 | 28 | @password.setter 29 | def password(self, plain_text_password): 30 | self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8') 31 | 32 | def check_password_correction(self, attempted_password): 33 | return bcrypt.check_password_hash(self.password_hash, attempted_password) 34 | 35 | class Item(db.Model): 36 | id = db.Column(db.Integer(), primary_key=True) 37 | name = db.Column(db.String(length=30), nullable=False, unique=True) 38 | price = db.Column(db.Integer(), nullable=False) 39 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 40 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 41 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 42 | def __repr__(self): 43 | return f'Item {self.name}' -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template, redirect, url_for, flash 3 | from market.models import Item, User 4 | from market.forms import RegisterForm, LoginForm 5 | from market import db 6 | from flask_login import login_user, logout_user, login_required 7 | 8 | @app.route('/') 9 | @app.route('/home') 10 | def home_page(): 11 | return render_template('home.html') 12 | 13 | @app.route('/market') 14 | @login_required 15 | def market_page(): 16 | items = Item.query.all() 17 | return render_template('market.html', items=items) 18 | 19 | @app.route('/register', methods=['GET', 'POST']) 20 | def register_page(): 21 | form = RegisterForm() 22 | if form.validate_on_submit(): 23 | user_to_create = User(username=form.username.data, 24 | email_address=form.email_address.data, 25 | password=form.password1.data) 26 | db.session.add(user_to_create) 27 | db.session.commit() 28 | login_user(user_to_create) 29 | flash(f"Account created successfully! You are now logged in as {user_to_create.username}", category='success') 30 | return redirect(url_for('market_page')) 31 | if form.errors != {}: #If there are not errors from the validations 32 | for err_msg in form.errors.values(): 33 | flash(f'There was an error with creating a user: {err_msg}', category='danger') 34 | 35 | return render_template('register.html', form=form) 36 | 37 | @app.route('/login', methods=['GET', 'POST']) 38 | def login_page(): 39 | form = LoginForm() 40 | if form.validate_on_submit(): 41 | attempted_user = User.query.filter_by(username=form.username.data).first() 42 | if attempted_user and attempted_user.check_password_correction( 43 | attempted_password=form.password.data 44 | ): 45 | login_user(attempted_user) 46 | flash(f'Success! You are logged in as: {attempted_user.username}', category='success') 47 | return redirect(url_for('market_page')) 48 | else: 49 | flash('Username and password are not match! Please try again', category='danger') 50 | 51 | return render_template('login.html', form=form) 52 | 53 | @app.route('/logout') 54 | def logout_page(): 55 | logout_user() 56 | flash("You have been logged out!", category='info') 57 | return redirect(url_for("home_page")) 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 57 | {% with messages = get_flashed_messages(with_categories=true) %} 58 | {% if messages %} 59 | {% for category, message in messages %} 60 |
61 | 64 | {{ message }} 65 |
66 | {% endfor %} 67 | {% endif %} 68 | {% endwith %} 69 | {% block content %} 70 | 71 | {% endblock %} 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 89 | -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Welcome to Jim Shaped Coding Market 4 | {% endblock %} 5 | {% block content %} 6 |
7 |
8 |

Jim Shaped Coding Market

9 |

Start purchasing products by clicking the link below

10 | Get Started 11 |
12 |
13 |
14 |
15 | {% endblock %} -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/templates/includes/items_modals.html: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 34 | 35 | 36 | 62 | -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Login Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 | 33 |
34 | 35 | {% endblock %} -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | {% block content %} 6 | 7 |
8 |
9 |

Available items on the Market

10 |

Click on one of the items to start buying

11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | {% for item in items %} 26 | {% include 'includes/items_modals.html' %} 27 | 28 | 29 | 30 | 31 | 32 | 36 | 37 | {% endfor %} 38 | 39 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 33 | 34 | 35 |
40 |
41 |
42 |

Owned Items

43 |

Click on sell item to put an item back on the Market

44 |
45 |
46 |
47 | {% endblock %} -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | {{ form.hidden_tag() }} 11 | 12 |

13 | Please Create your Account 14 |

15 |
16 | {{ form.username.label() }} 17 | {{ form.username(class="form-control", placeholder="User Name") }} 18 | 19 | {{ form.email_address.label() }} 20 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 21 | 22 | {{ form.password1.label() }} 23 | {{ form.password1(class="form-control", placeholder="Password") }} 24 | 25 | {{ form.password2.label() }} 26 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 27 | 28 |
29 | 30 |
31 |
Already have an account?
32 | Login 33 |
34 | 35 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 36 | 37 |
38 |
39 | 40 | {% endblock %} -------------------------------------------------------------------------------- /14 - Item Purchasing Part 1/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_sqlalchemy import SQLAlchemy 3 | from flask_bcrypt import Bcrypt 4 | from flask_login import LoginManager 5 | 6 | app = Flask(__name__) 7 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 8 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 9 | db = SQLAlchemy(app) 10 | bcrypt = Bcrypt(app) 11 | login_manager = LoginManager(app) 12 | login_manager.login_view = "login_page" 13 | login_manager.login_message_category = "info" 14 | from market import routes -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField, HiddenField 3 | from wtforms.validators import Length, EqualTo, Email, DataRequired, ValidationError 4 | from market.models import User 5 | 6 | 7 | class RegisterForm(FlaskForm): 8 | def validate_username(self, username_to_check): 9 | user = User.query.filter_by(username=username_to_check.data).first() 10 | if user: 11 | raise ValidationError('Username already exists! Please try a different username') 12 | 13 | def validate_email_address(self, email_address_to_check): 14 | email_address = User.query.filter_by(email_address=email_address_to_check.data).first() 15 | if email_address: 16 | raise ValidationError('Email Address already exists! Please try a different email address') 17 | 18 | username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) 19 | email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) 20 | password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) 21 | password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) 22 | submit = SubmitField(label='Create Account') 23 | 24 | 25 | class LoginForm(FlaskForm): 26 | username = StringField(label='User Name:', validators=[DataRequired()]) 27 | password = PasswordField(label='Password:', validators=[DataRequired()]) 28 | submit = SubmitField(label='Sign in') 29 | 30 | class PurchaseItemForm(FlaskForm): 31 | submit = SubmitField(label='Purchase Item!') 32 | 33 | class SellItemForm(FlaskForm): 34 | submit = SubmitField(label='Sell Item!') -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/15 - Item Purchasing Part 2/market/market.db -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db, login_manager 2 | from market import bcrypt 3 | from flask_login import UserMixin 4 | 5 | @login_manager.user_loader 6 | def load_user(user_id): 7 | return User.query.get(int(user_id)) 8 | 9 | class User(db.Model, UserMixin): 10 | id = db.Column(db.Integer(), primary_key=True) 11 | username = db.Column(db.String(length=30), nullable=False, unique=True) 12 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 13 | password_hash = db.Column(db.String(length=60), nullable=False) 14 | budget = db.Column(db.Integer(), nullable=False, default=1000) 15 | items = db.relationship('Item', backref='owned_user', lazy=True) 16 | 17 | @property 18 | def prettier_budget(self): 19 | if len(str(self.budget)) >= 4: 20 | return f'{str(self.budget)[:-3]},{str(self.budget)[-3:]}$' 21 | else: 22 | return f"{self.budget}$" 23 | 24 | @property 25 | def password(self): 26 | return self.password 27 | 28 | @password.setter 29 | def password(self, plain_text_password): 30 | self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8') 31 | 32 | def check_password_correction(self, attempted_password): 33 | return bcrypt.check_password_hash(self.password_hash, attempted_password) 34 | 35 | def can_purchase(self, item_obj): 36 | return self.budget >= item_obj.price 37 | 38 | class Item(db.Model): 39 | id = db.Column(db.Integer(), primary_key=True) 40 | name = db.Column(db.String(length=30), nullable=False, unique=True) 41 | price = db.Column(db.Integer(), nullable=False) 42 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 43 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 44 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 45 | def __repr__(self): 46 | return f'Item {self.name}' 47 | 48 | def buy(self, user): 49 | self.owner = user.id 50 | user.budget -= self.price 51 | db.session.commit() -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template, redirect, url_for, flash, request 3 | from market.models import Item, User 4 | from market.forms import RegisterForm, LoginForm, PurchaseItemForm 5 | from market import db 6 | from flask_login import login_user, logout_user, login_required, current_user 7 | 8 | @app.route('/') 9 | @app.route('/home') 10 | def home_page(): 11 | return render_template('home.html') 12 | 13 | @app.route('/market', methods=['GET', 'POST']) 14 | @login_required 15 | def market_page(): 16 | purchase_form = PurchaseItemForm() 17 | if request.method == "POST": 18 | purchased_item = request.form.get('purchased_item') 19 | p_item_object = Item.query.filter_by(name=purchased_item).first() 20 | if p_item_object: 21 | if current_user.can_purchase(p_item_object): 22 | p_item_object.buy(current_user) 23 | flash(f"Congratulations! You purchased {p_item_object.name} for {p_item_object.price}$", category='success') 24 | else: 25 | flash(f"Unfortunately, you don't have enough money to purchase {p_item_object.name}!", category='danger') 26 | 27 | return redirect(url_for('market_page')) 28 | 29 | if request.method == "GET": 30 | items = Item.query.filter_by(owner=None) 31 | return render_template('market.html', items=items, purchase_form=purchase_form) 32 | 33 | @app.route('/register', methods=['GET', 'POST']) 34 | def register_page(): 35 | form = RegisterForm() 36 | if form.validate_on_submit(): 37 | user_to_create = User(username=form.username.data, 38 | email_address=form.email_address.data, 39 | password=form.password1.data) 40 | db.session.add(user_to_create) 41 | db.session.commit() 42 | login_user(user_to_create) 43 | flash(f"Account created successfully! You are now logged in as {user_to_create.username}", category='success') 44 | return redirect(url_for('market_page')) 45 | if form.errors != {}: #If there are not errors from the validations 46 | for err_msg in form.errors.values(): 47 | flash(f'There was an error with creating a user: {err_msg}', category='danger') 48 | 49 | return render_template('register.html', form=form) 50 | 51 | @app.route('/login', methods=['GET', 'POST']) 52 | def login_page(): 53 | form = LoginForm() 54 | if form.validate_on_submit(): 55 | attempted_user = User.query.filter_by(username=form.username.data).first() 56 | if attempted_user and attempted_user.check_password_correction( 57 | attempted_password=form.password.data 58 | ): 59 | login_user(attempted_user) 60 | flash(f'Success! You are logged in as: {attempted_user.username}', category='success') 61 | return redirect(url_for('market_page')) 62 | else: 63 | flash('Username and password are not match! Please try again', category='danger') 64 | 65 | return render_template('login.html', form=form) 66 | 67 | @app.route('/logout') 68 | def logout_page(): 69 | logout_user() 70 | flash("You have been logged out!", category='info') 71 | return redirect(url_for("home_page")) 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 57 | {% with messages = get_flashed_messages(with_categories=true) %} 58 | {% if messages %} 59 | {% for category, message in messages %} 60 |
61 | 64 | {{ message }} 65 |
66 | {% endfor %} 67 | {% endif %} 68 | {% endwith %} 69 | {% block content %} 70 | 71 | {% endblock %} 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 89 | -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Welcome to Jim Shaped Coding Market 4 | {% endblock %} 5 | {% block content %} 6 |
7 |
8 |

Jim Shaped Coding Market

9 |

Start purchasing products by clicking the link below

10 | Get Started 11 |
12 |
13 |
14 |
15 | {% endblock %} -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/templates/includes/items_modals.html: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 34 | 35 | 36 | 74 | -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 | 33 |
34 | 35 | {% endblock %} -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | {% block content %} 6 | 7 |
8 |
9 |

Available items on the Market

10 |

Click on one of the items to start buying

11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | {% for item in items %} 26 | {% include 'includes/items_modals.html' %} 27 | 28 | 29 | 30 | 31 | 32 | 36 | 37 | {% endfor %} 38 | 39 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 33 | 34 | 35 |
40 |
41 |
42 |

Owned Items

43 |

Click on sell item to put an item back on the Market

44 |
45 |
46 |
47 | {% endblock %} -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | {{ form.hidden_tag() }} 11 | 12 |

13 | Please Create your Account 14 |

15 |
16 | {{ form.username.label() }} 17 | {{ form.username(class="form-control", placeholder="User Name") }} 18 | 19 | {{ form.email_address.label() }} 20 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 21 | 22 | {{ form.password1.label() }} 23 | {{ form.password1(class="form-control", placeholder="Password") }} 24 | 25 | {{ form.password2.label() }} 26 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 27 | 28 |
29 | 30 |
31 |
Already have an account?
32 | Login 33 |
34 | 35 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 36 | 37 |
38 |
39 | 40 | {% endblock %} -------------------------------------------------------------------------------- /15 - Item Purchasing Part 2/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) -------------------------------------------------------------------------------- /16 - Item Selling/market/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_sqlalchemy import SQLAlchemy 3 | from flask_bcrypt import Bcrypt 4 | from flask_login import LoginManager 5 | 6 | app = Flask(__name__) 7 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' 8 | app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' 9 | db = SQLAlchemy(app) 10 | bcrypt = Bcrypt(app) 11 | login_manager = LoginManager(app) 12 | login_manager.login_view = "login_page" 13 | login_manager.login_message_category = "info" 14 | from market import routes -------------------------------------------------------------------------------- /16 - Item Selling/market/forms.py: -------------------------------------------------------------------------------- 1 | from flask_wtf import FlaskForm 2 | from wtforms import StringField, PasswordField, SubmitField, HiddenField 3 | from wtforms.validators import Length, EqualTo, Email, DataRequired, ValidationError 4 | from market.models import User 5 | 6 | 7 | class RegisterForm(FlaskForm): 8 | def validate_username(self, username_to_check): 9 | user = User.query.filter_by(username=username_to_check.data).first() 10 | if user: 11 | raise ValidationError('Username already exists! Please try a different username') 12 | 13 | def validate_email_address(self, email_address_to_check): 14 | email_address = User.query.filter_by(email_address=email_address_to_check.data).first() 15 | if email_address: 16 | raise ValidationError('Email Address already exists! Please try a different email address') 17 | 18 | username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) 19 | email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) 20 | password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) 21 | password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) 22 | submit = SubmitField(label='Create Account') 23 | 24 | 25 | class LoginForm(FlaskForm): 26 | username = StringField(label='User Name:', validators=[DataRequired()]) 27 | password = PasswordField(label='Password:', validators=[DataRequired()]) 28 | submit = SubmitField(label='Sign in') 29 | 30 | class PurchaseItemForm(FlaskForm): 31 | submit = SubmitField(label='Purchase Item!') 32 | 33 | class SellItemForm(FlaskForm): 34 | submit = SubmitField(label='Sell Item!') -------------------------------------------------------------------------------- /16 - Item Selling/market/market.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimdevops19/FlaskSeries/ccf586f9394303a309986e9fc83c660d6ed20498/16 - Item Selling/market/market.db -------------------------------------------------------------------------------- /16 - Item Selling/market/models.py: -------------------------------------------------------------------------------- 1 | from market import db, login_manager 2 | from market import bcrypt 3 | from flask_login import UserMixin 4 | 5 | @login_manager.user_loader 6 | def load_user(user_id): 7 | return User.query.get(int(user_id)) 8 | 9 | class User(db.Model, UserMixin): 10 | id = db.Column(db.Integer(), primary_key=True) 11 | username = db.Column(db.String(length=30), nullable=False, unique=True) 12 | email_address = db.Column(db.String(length=50), nullable=False, unique=True) 13 | password_hash = db.Column(db.String(length=60), nullable=False) 14 | budget = db.Column(db.Integer(), nullable=False, default=1000) 15 | items = db.relationship('Item', backref='owned_user', lazy=True) 16 | 17 | @property 18 | def prettier_budget(self): 19 | if len(str(self.budget)) >= 4: 20 | return f'{str(self.budget)[:-3]},{str(self.budget)[-3:]}$' 21 | else: 22 | return f"{self.budget}$" 23 | 24 | @property 25 | def password(self): 26 | return self.password 27 | 28 | @password.setter 29 | def password(self, plain_text_password): 30 | self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8') 31 | 32 | def check_password_correction(self, attempted_password): 33 | return bcrypt.check_password_hash(self.password_hash, attempted_password) 34 | 35 | def can_purchase(self, item_obj): 36 | return self.budget >= item_obj.price 37 | 38 | def can_sell(self, item_obj): 39 | return item_obj in self.items 40 | 41 | class Item(db.Model): 42 | id = db.Column(db.Integer(), primary_key=True) 43 | name = db.Column(db.String(length=30), nullable=False, unique=True) 44 | price = db.Column(db.Integer(), nullable=False) 45 | barcode = db.Column(db.String(length=12), nullable=False, unique=True) 46 | description = db.Column(db.String(length=1024), nullable=False, unique=True) 47 | owner = db.Column(db.Integer(), db.ForeignKey('user.id')) 48 | def __repr__(self): 49 | return f'Item {self.name}' 50 | 51 | def buy(self, user): 52 | self.owner = user.id 53 | user.budget -= self.price 54 | db.session.commit() 55 | 56 | def sell(self, user): 57 | self.owner = None 58 | user.budget += self.price 59 | db.session.commit() -------------------------------------------------------------------------------- /16 - Item Selling/market/routes.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | from flask import render_template, redirect, url_for, flash, request 3 | from market.models import Item, User 4 | from market.forms import RegisterForm, LoginForm, PurchaseItemForm, SellItemForm 5 | from market import db 6 | from flask_login import login_user, logout_user, login_required, current_user 7 | 8 | @app.route('/') 9 | @app.route('/home') 10 | def home_page(): 11 | return render_template('home.html') 12 | 13 | @app.route('/market', methods=['GET', 'POST']) 14 | @login_required 15 | def market_page(): 16 | purchase_form = PurchaseItemForm() 17 | selling_form = SellItemForm() 18 | if request.method == "POST": 19 | #Purchase Item Logic 20 | purchased_item = request.form.get('purchased_item') 21 | p_item_object = Item.query.filter_by(name=purchased_item).first() 22 | if p_item_object: 23 | if current_user.can_purchase(p_item_object): 24 | p_item_object.buy(current_user) 25 | flash(f"Congratulations! You purchased {p_item_object.name} for {p_item_object.price}$", category='success') 26 | else: 27 | flash(f"Unfortunately, you don't have enough money to purchase {p_item_object.name}!", category='danger') 28 | #Sell Item Logic 29 | sold_item = request.form.get('sold_item') 30 | s_item_object = Item.query.filter_by(name=sold_item).first() 31 | if s_item_object: 32 | if current_user.can_sell(s_item_object): 33 | s_item_object.sell(current_user) 34 | flash(f"Congratulations! You sold {s_item_object.name} back to market!", category='success') 35 | else: 36 | flash(f"Something went wrong with selling {s_item_object.name}", category='danger') 37 | 38 | 39 | return redirect(url_for('market_page')) 40 | 41 | if request.method == "GET": 42 | items = Item.query.filter_by(owner=None) 43 | owned_items = Item.query.filter_by(owner=current_user.id) 44 | return render_template('market.html', items=items, purchase_form=purchase_form, owned_items=owned_items, selling_form=selling_form) 45 | 46 | @app.route('/register', methods=['GET', 'POST']) 47 | def register_page(): 48 | form = RegisterForm() 49 | if form.validate_on_submit(): 50 | user_to_create = User(username=form.username.data, 51 | email_address=form.email_address.data, 52 | password=form.password1.data) 53 | db.session.add(user_to_create) 54 | db.session.commit() 55 | login_user(user_to_create) 56 | flash(f"Account created successfully! You are now logged in as {user_to_create.username}", category='success') 57 | return redirect(url_for('market_page')) 58 | if form.errors != {}: #If there are not errors from the validations 59 | for err_msg in form.errors.values(): 60 | flash(f'There was an error with creating a user: {err_msg}', category='danger') 61 | 62 | return render_template('register.html', form=form) 63 | 64 | @app.route('/login', methods=['GET', 'POST']) 65 | def login_page(): 66 | form = LoginForm() 67 | if form.validate_on_submit(): 68 | attempted_user = User.query.filter_by(username=form.username.data).first() 69 | if attempted_user and attempted_user.check_password_correction( 70 | attempted_password=form.password.data 71 | ): 72 | login_user(attempted_user) 73 | flash(f'Success! You are logged in as: {attempted_user.username}', category='success') 74 | return redirect(url_for('market_page')) 75 | else: 76 | flash('Username and password are not match! Please try again', category='danger') 77 | 78 | return render_template('login.html', form=form) 79 | 80 | @app.route('/logout') 81 | def logout_page(): 82 | logout_user() 83 | flash("You have been logged out!", category='info') 84 | return redirect(url_for("home_page")) 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /16 - Item Selling/market/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | {% block title %} 11 | 12 | {% endblock %} 13 | 14 | 15 | 16 | 57 | {% with messages = get_flashed_messages(with_categories=true) %} 58 | {% if messages %} 59 | {% for category, message in messages %} 60 |
61 | 64 | {{ message }} 65 |
66 | {% endfor %} 67 | {% endif %} 68 | {% endwith %} 69 | {% block content %} 70 | 71 | {% endblock %} 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 89 | -------------------------------------------------------------------------------- /16 - Item Selling/market/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Welcome to Jim Shaped Coding Market 4 | {% endblock %} 5 | {% block content %} 6 |
7 |
8 |

Jim Shaped Coding Market

9 |

Start purchasing products by clicking the link below

10 | Get Started 11 |
12 |
13 |
14 |
15 | {% endblock %} -------------------------------------------------------------------------------- /16 - Item Selling/market/templates/includes/items_modals.html: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 34 | 35 | 36 | 74 | -------------------------------------------------------------------------------- /16 - Item Selling/market/templates/includes/owned_items_modals.html: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /16 - Item Selling/market/templates/login.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 | 33 |
34 | 35 | {% endblock %} -------------------------------------------------------------------------------- /16 - Item Selling/market/templates/market.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Market Page 4 | {% endblock %} 5 | {% block content %} 6 | 7 |
8 |
9 |

Available items on the Market

10 |

Click on one of the items to start buying

11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | {% for item in items %} 26 | {% include 'includes/items_modals.html' %} 27 | 28 | 29 | 30 | 31 | 32 | 36 | 37 | {% endfor %} 38 | 39 |
IDNameBarcodePriceOptions
{{ item.id }}{{ item.name }}{{ item.barcode }}{{ item.price }}$ 33 | 34 | 35 |
40 |
41 |
42 |

Owned Items

43 |

Click on sell item to put an item back on the Market

44 |
45 |
46 | {% for owned_item in owned_items %} 47 | {% include 'includes/owned_items_modals.html' %} 48 |
49 |
50 |
51 |
{{ owned_item.name }}
52 | 56 |

57 | This item costs {{ owned_item.price }}$ 58 |

59 |
60 |
61 |
62 | {% endfor %} 63 |
64 |
65 |
66 | {% endblock %} -------------------------------------------------------------------------------- /16 - Item Selling/market/templates/register.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %} 3 | Register Page 4 | {% endblock %} 5 | 6 | {% block content %} 7 | 8 |
9 |
10 | {{ form.hidden_tag() }} 11 | 12 |

13 | Please Create your Account 14 |

15 |
16 | {{ form.username.label() }} 17 | {{ form.username(class="form-control", placeholder="User Name") }} 18 | 19 | {{ form.email_address.label() }} 20 | {{ form.email_address(class="form-control", placeholder="Email Address") }} 21 | 22 | {{ form.password1.label() }} 23 | {{ form.password1(class="form-control", placeholder="Password") }} 24 | 25 | {{ form.password2.label() }} 26 | {{ form.password2(class="form-control", placeholder="Confirm Password") }} 27 | 28 |
29 | 30 |
31 |
Already have an account?
32 | Login 33 |
34 | 35 | {{ form.submit(class="btn btn-lg btn-block btn-primary") }} 36 | 37 |
38 |
39 | 40 | {% endblock %} -------------------------------------------------------------------------------- /16 - Item Selling/run.py: -------------------------------------------------------------------------------- 1 | from market import app 2 | 3 | #Checks if the run.py file has executed directly and not imported 4 | if __name__ == '__main__': 5 | app.run(debug=True) --------------------------------------------------------------------------------