forked from jimdevops19/FlaskSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code after the ninth episode of Flask Series
- Loading branch information
1 parent
06a47e0
commit 8e3c4f9
Showing
10 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from flask import Flask, render_template | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
app = Flask(__name__) | ||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db' | ||
app.config['SECRET_KEY'] = 'ec9439cfc6c796ae2029594d' | ||
db = SQLAlchemy(app) | ||
from market import routes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from flask_wtf import FlaskForm | ||
from wtforms import StringField, PasswordField, SubmitField | ||
from wtforms.validators import Length, EqualTo, Email, DataRequired | ||
|
||
class RegisterForm(FlaskForm): | ||
username = StringField(label='User Name:', validators=[Length(min=2, max=30), DataRequired()]) | ||
email_address = StringField(label='Email Address:', validators=[Email(), DataRequired()]) | ||
password1 = PasswordField(label='Password:', validators=[Length(min=6), DataRequired()]) | ||
password2 = PasswordField(label='Confirm Password:', validators=[EqualTo('password1'), DataRequired()]) | ||
submit = SubmitField(label='Create Account') |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from market import db | ||
|
||
class User(db.Model): | ||
id = db.Column(db.Integer(), primary_key=True) | ||
username = db.Column(db.String(length=30), nullable=False, unique=True) | ||
email_address = db.Column(db.String(length=50), nullable=False, unique=True) | ||
password_hash = db.Column(db.String(length=60), nullable=False) | ||
budget = db.Column(db.Integer(), nullable=False, default=1000) | ||
items = db.relationship('Item', backref='owned_user', lazy=True) | ||
|
||
class Item(db.Model): | ||
id = db.Column(db.Integer(), primary_key=True) | ||
name = db.Column(db.String(length=30), nullable=False, unique=True) | ||
price = db.Column(db.Integer(), nullable=False) | ||
barcode = db.Column(db.String(length=12), nullable=False, unique=True) | ||
description = db.Column(db.String(length=1024), nullable=False, unique=True) | ||
owner = db.Column(db.Integer(), db.ForeignKey('user.id')) | ||
def __repr__(self): | ||
return f'Item {self.name}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from market import app | ||
from flask import render_template, redirect, url_for | ||
from market.models import Item, User | ||
from market.forms import RegisterForm | ||
from market import db | ||
|
||
@app.route('/') | ||
@app.route('/home') | ||
def home_page(): | ||
return render_template('home.html') | ||
|
||
@app.route('/market') | ||
def market_page(): | ||
items = Item.query.all() | ||
return render_template('market.html', items=items) | ||
|
||
@app.route('/register', methods=['GET', 'POST']) | ||
def register_page(): | ||
form = RegisterForm() | ||
if form.validate_on_submit(): | ||
user_to_create = User(username=form.username.data, | ||
email_address=form.email_address.data, | ||
password_hash=form.password1.data) | ||
db.session.add(user_to_create) | ||
db.session.commit() | ||
return redirect(url_for('market_page')) | ||
if form.errors != {}: #If there are not errors from the validations | ||
for err_msg in form.errors.values(): | ||
print(f'There was an error with creating a user: {err_msg}') | ||
|
||
return render_template('register.html', form=form) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<!-- Required meta tags --> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<!-- Bootstrap CSS --> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous"> | ||
<title> | ||
{% block title %} | ||
|
||
{% endblock %} | ||
</title> | ||
</head> | ||
<body> | ||
<nav class="navbar navbar-expand-md navbar-dark bg-dark"> | ||
<a class="navbar-brand" href="#">Flask Market</a> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="collapse navbar-collapse" id="navbarNav"> | ||
<ul class="navbar-nav mr-auto"> | ||
<li class="nav-item active"> | ||
<a class="nav-link" href="{{ url_for('home_page') }}">Home <span class="sr-only">(current)</span></a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="{{ url_for('market_page') }}">Market</a> | ||
</li> | ||
</ul> | ||
<ul class="navbar-nav"> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="#">Login</a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="{{ url_for('register_page') }}">Register</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</nav> | ||
{% block content %} | ||
|
||
{% endblock %} | ||
<!-- Future Content here --> | ||
|
||
|
||
|
||
<!-- Optional JavaScript --> | ||
<!-- jQuery first, then Popper.js, then Bootstrap JS --> | ||
<script src='https://kit.fontawesome.com/a076d05399.js'></script> | ||
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script> | ||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> | ||
</body> | ||
<style> | ||
body { | ||
background-color: #212121; | ||
color: white | ||
} | ||
</style> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{% extends 'base.html' %} | ||
{% block title %} | ||
Home Page | ||
{% endblock %} | ||
|
||
{% block content %} | ||
This is our content for the Home Page | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{% extends 'base.html' %} | ||
{% block title %} | ||
Market Page | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<table class="table table-hover table-dark"> | ||
<thead> | ||
<tr> | ||
<!-- Your Columns HERE --> | ||
<th scope="col">ID</th> | ||
<th scope="col">Name</th> | ||
<th scope="col">Barcode</th> | ||
<th scope="col">Price</th> | ||
<th scope="col">Options</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<!-- Your rows inside the table HERE: --> | ||
{% for item in items %} | ||
<tr> | ||
<td>{{ item.id }}</td> | ||
<td>{{ item.name }}</td> | ||
<td>{{ item.barcode }}</td> | ||
<td>{{ item.price }}$</td> | ||
<td> | ||
<button class="btn btn-outline btn-info">More Info</button> | ||
<button class="btn btn-outline btn-success">Purchase this Item</button> | ||
</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{% extends 'base.html' %} | ||
{% block title %} | ||
Register Page | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<body class="text-center"> | ||
<div class="container"> | ||
<form method="POST" class="form-register" style="color:white"> | ||
{{ form.hidden_tag() }} | ||
<img class="mb-4" src="https://res.cloudinary.com/jimshapedcoding/image/upload/v1597332609/android-icon-192x192_ove2a7.png" alt=""> | ||
<h1 class="h3 mb-3 font-weight-normal"> | ||
Please Create your Account | ||
</h1> | ||
<br> | ||
{{ form.username.label() }} | ||
{{ form.username(class="form-control", placeholder="User Name") }} | ||
|
||
{{ form.email_address.label() }} | ||
{{ form.email_address(class="form-control", placeholder="Email Address") }} | ||
|
||
{{ form.password1.label() }} | ||
{{ form.password1(class="form-control", placeholder="Password") }} | ||
|
||
{{ form.password2.label() }} | ||
{{ form.password2(class="form-control", placeholder="Confirm Password") }} | ||
|
||
<br> | ||
|
||
{{ form.submit(class="btn btn-lg btn-block btn-primary") }} | ||
|
||
</form> | ||
</div> | ||
</body> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from market import app | ||
|
||
#Checks if the run.py file has executed directly and not imported | ||
if __name__ == '__main__': | ||
app.run(debug=True) |