-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
here are all my files for the pizza project.
- Loading branch information
0 parents
commit aa6daad
Showing
25 changed files
with
394 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,3 @@ | ||
SECRET_KEY= "7d5ef2ba9a0ac56fceb7992786baaa48cb11ae4e" | ||
DEBUG=1 | ||
JWT_SECRET_KEY='0a964ece05798db1386de96f4a0bcf282c32f86d' |
Empty file.
Binary file not shown.
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,127 @@ | ||
from flask_restx import Resource, Namespace, fields, abort | ||
from flask_jwt_extended import jwt_required, get_jwt_identity | ||
from http import HTTPStatus | ||
from ..models.orders import Order | ||
from ..models.users import User | ||
from enum import Enum | ||
from ..utils import db | ||
|
||
|
||
order_namespace = Namespace("orders", description="a namespace for placing order") | ||
|
||
order_model = order_namespace.model( | ||
"Order", | ||
{ | ||
"id": fields.Integer(description="An ID"), | ||
"Size": fields.String( | ||
description="Size of order", | ||
required=True, | ||
enum=["SMALL", "MEDIUM", "LARGE", "EXTRA_LARGE"], | ||
), | ||
"order_status": fields.String( | ||
description="The status of the Order", | ||
required=True, | ||
enum=["PENDING", "IN_TRANSIT", "DELIVERED"], | ||
), | ||
"quantity": fields.Integer(required=True, description='The quantity'), | ||
}, | ||
) | ||
|
||
|
||
@order_namespace.route("/orders") | ||
class OrderGetCreate(Resource): | ||
@order_namespace.marshal_with(order_model) | ||
@jwt_required() | ||
def get(self): | ||
""" | ||
Get all orders | ||
""" | ||
orders = Order.query.all() | ||
|
||
return orders, 200 | ||
|
||
@order_namespace.expect(order_model) | ||
@order_namespace.marshal_with(order_model) | ||
@jwt_required() | ||
def post(self): | ||
""" | ||
place an order | ||
""" | ||
|
||
username = get_jwt_identity() | ||
print(f'Here is the username {username}') | ||
|
||
current_user = User.query.filter_by(username=username).first() | ||
|
||
data = order_namespace.payload | ||
print(data['quantity']) | ||
|
||
new_order = Order( | ||
Size = data["size"], | ||
quantity = data["quantity"], | ||
flavour = data["flavour"] | ||
) | ||
|
||
new_order.user = current_user.id | ||
|
||
new_order.save() | ||
|
||
return new_order, 201 | ||
|
||
|
||
@order_namespace.route("/order/<int:order_id>") | ||
class GetUpdateDelete(Resource): | ||
@order_namespace.marshal_with(order_model) | ||
@jwt_required() | ||
def get(self, order_id): | ||
""" | ||
Retrieve an order by id | ||
""" | ||
|
||
order = Order.query.get(order_id) | ||
if not order: | ||
abort(404, message='Not found') | ||
|
||
return order, HTTPStatus.OK | ||
|
||
def put(self, order_id): | ||
""" | ||
Retrieve an order by id | ||
""" | ||
pass | ||
|
||
def put(self, order_id): | ||
""" | ||
Update an order with id | ||
""" | ||
pass | ||
|
||
def delete(self, order_id): | ||
""" | ||
Delete an order with id | ||
""" | ||
pass | ||
|
||
|
||
@order_namespace.route("/user/<int:user_id>/order/<int:order>") | ||
class UserOrders(Resource): | ||
def get(self, user_id): | ||
""" | ||
Get all orders by a specific. | ||
""" | ||
|
||
user = User.get_by_id(user_id) | ||
|
||
Orders = user.Orders | ||
|
||
|
||
@order_namespace.route("/order/status/<int:order_id>") | ||
class UpdateOrdersStatus(Resource): | ||
def get(self, user_id): | ||
""" | ||
update an order's status | ||
""" | ||
pass |
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,41 @@ | ||
from flask import Flask | ||
from flask_restx import Api | ||
from .Order.views import order_namespace | ||
from .auth.views import auth_namespace | ||
from .config.config import config_dict | ||
from .utils import db | ||
from .models.orders import Order | ||
from .models.users import User | ||
from flask_migrate import Migrate | ||
from flask_jwt_extended import JWTManager | ||
|
||
|
||
def create_app(config=config_dict ['dev']): | ||
app=Flask(__name__) | ||
|
||
|
||
app.config.from_object(config) | ||
|
||
db.init_app(app) | ||
|
||
|
||
jwt=JWTManager(app) | ||
|
||
migrate=Migrate(app,db) | ||
|
||
api=Api(app) | ||
|
||
|
||
api.add_namespace(order_namespace) | ||
api.add_namespace(auth_namespace, path='/auth') | ||
|
||
|
||
@app.shell_context_processor | ||
def make_shell_context(): | ||
return{ | ||
'db':db, | ||
'User':User, | ||
'Order':Order | ||
} | ||
|
||
return app |
Empty file.
Binary file not shown.
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,113 @@ | ||
from flask_restx import Namespace, Resource, fields | ||
from flask import request | ||
from ..models.users import User | ||
from werkzeug.security import generate_password_hash,check_password_hash | ||
from http import HTTPStatus | ||
from flask_jwt_extended import create_access_token,create_refresh_token,jwt_required,get_jwt_identity | ||
|
||
|
||
auth_namespace = Namespace('auth', description="a namespace for authentication") | ||
|
||
|
||
signup_model = auth_namespace.model( | ||
'SignUp',{ | ||
'id':fields.Integer(), | ||
'username':fields.String(reqired=True,description="A username"), | ||
'email':fields.String(require=True,description="An email"), | ||
'password': fields.String(required=True,description="A password"), | ||
} | ||
) | ||
|
||
user_model = auth_namespace.model( | ||
'User' ,{ | ||
'id':fields.Integer(), | ||
'username':fields.String(reqired=True,description="A username"), | ||
'email':fields.String(require=True,description="An email"), | ||
'password_hash': fields.String(required=True,description="A password"), | ||
'is_active':fields.Boolean(description="This shows that User is active"), | ||
'is_staff':fields.Boolean(description="This shows User is Staff") | ||
} | ||
) | ||
|
||
|
||
|
||
login_model=auth_namespace.model( | ||
'Login' ,{ | ||
'email':fields.String(required=True,description="An email"), | ||
"password":fields.String(required=True,description="A password") | ||
} | ||
) | ||
|
||
|
||
@auth_namespace.route('/signup') | ||
class SignUp(Resource): | ||
|
||
|
||
@auth_namespace.expect(signup_model) | ||
@auth_namespace.marshal_with(user_model) | ||
def post(self): | ||
""" | ||
create a new user account | ||
""" | ||
|
||
data = request.get_json() | ||
# data = {"username": "Emmanuel", "email": "[email protected]", "password": "passwordd4757"} | ||
|
||
|
||
new_user=User( | ||
username=data.get('username'), | ||
email=data.get('email'), | ||
password_hash=generate_password_hash(data.get('password')) | ||
|
||
) | ||
|
||
new_user.save() | ||
|
||
return new_user , HTTPStatus.CREATED | ||
|
||
|
||
|
||
@auth_namespace.route('/login') | ||
class Login(Resource): | ||
|
||
@auth_namespace.expect(login_model) | ||
def post(self): | ||
""" | ||
Generate a JWT pair | ||
""" | ||
|
||
|
||
data=request.get_json() | ||
|
||
email=data.get('email') | ||
password=data.get('password') | ||
|
||
user=User.query.filter_by(email=email).first() | ||
|
||
|
||
|
||
if (user is not None) and check_password_hash(user.password_hash,password): | ||
access_token=create_access_token(identity=user.username) | ||
refresh_token=create_refresh_token(identity=user.username) | ||
|
||
response={ | ||
'access_token': access_token, | ||
'refresh_token': refresh_token | ||
} | ||
|
||
|
||
return response, HTTPStatus.CREATED | ||
|
||
@auth_namespace.route('/refresh') | ||
class Refresh(Resource): | ||
|
||
@jwt_required(refresh=True) | ||
def post(self): | ||
username=get_jwt_identity() | ||
|
||
|
||
access_token=create_access_token(identity=username) | ||
|
||
|
||
|
||
return{"access_token":access_token}, HTTPStatus.OK |
Empty file.
Binary file not shown.
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,34 @@ | ||
import os | ||
from decouple import config | ||
from datetime import timedelta | ||
|
||
|
||
BASE_DIR=os.path.dirname(os.path.realpath(__file__)) | ||
|
||
|
||
class config: | ||
SECRET_KEY=config('SECRET_KEY', 'secret') | ||
JWT_ACCESS_TOKEN_EXPIRES=timedelta(minutes=30) | ||
JWT_REFRESH_TOKEN_EXPIRES=timedelta(minutes=30) | ||
JWT_SECRET_KEY=config('JWT_SECRET_KEY') | ||
|
||
|
||
class DevConfig(config): | ||
DEBUG=config() | ||
SQLALCHEMY_ECHO=True | ||
SQLALCHEMY_DATABASE_URI='sqlite:///'+os.path.join(BASE_DIR,'db.sqlite3') | ||
SQLALCHEMY_TRACK_MODIFICATIONS=False | ||
|
||
|
||
|
||
class TestConfig(config): | ||
pass | ||
|
||
class ProdConfig(config): | ||
pass | ||
|
||
config_dict={ | ||
'dev':DevConfig, | ||
'prod':ProdConfig, | ||
'test':TestConfig | ||
} |
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
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,44 @@ | ||
from ..utils import db | ||
from enum import Enum | ||
from datetime import datetime | ||
|
||
|
||
|
||
class Sizes(Enum): | ||
SMALL='small' | ||
MEDIUM="medium" | ||
LARGE='large' | ||
EXTRA_LARGE='extra_large' | ||
|
||
|
||
class OrderStatus(Enum): | ||
PENDING='pending' | ||
IN_TRANSIT='in_transit' | ||
DELIVERED='Delivered' | ||
|
||
|
||
class Order(db.Model): | ||
__tablename__='orders' | ||
id = db.Column(db.Integer(),primary_key=True) | ||
Size = db.Column(db.Enum(Sizes),default=Sizes.SMALL) | ||
order_status = db.Column(db.Enum(OrderStatus),default=OrderStatus.PENDING) | ||
flavour = db.Column(db.String(),nullable=False) | ||
quantity = db.Column(db.Integer(), default=1) | ||
date_created = db.Column(db.DateTime(),default=datetime.utcnow) | ||
user = db.Column(db.Integer(),db.ForeignKey('users.id')) | ||
|
||
|
||
|
||
|
||
def __repr__(self): | ||
return f"<Order {self.id}>" | ||
|
||
|
||
|
||
def save(self): | ||
db.session.add(self) | ||
db.session.commit() | ||
|
||
# @classmethod | ||
# def get_by_id(cls, id): | ||
# return cls.query.get_or_404(id) |
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 ..utils import db | ||
|
||
|
||
class User(db.Model): | ||
__tablename__ = "users" | ||
id = db.Column(db.Integer(), primary_key=True) | ||
username = db.Column(db.String(45), nullable=False) | ||
email = db.Column(db.String(50), nullable=False) | ||
password_hash = db.Column(db.Text(), nullable=False) | ||
is_staff = db.Column(db.Boolean(), default=False) | ||
is_active = db.Column(db.Boolean(), default=False) | ||
orders = db.relationship("Order", backref="customer", lazy=True) | ||
|
||
def __repr__(self): | ||
return f"<User {self.username}>" | ||
|
||
def save(self): | ||
db.session.add(self) | ||
db.session.commit() |
Binary file not shown.
Oops, something went wrong.