Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added changes for the Recipe model with pg db
- Loading branch information
Showing
4 changed files
with
71 additions
and
38 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 |
---|---|---|
@@ -1,14 +1,45 @@ | ||
from flask import Flask | ||
from flask_restful import Api | ||
from resources.recipe import RecipeListResource, RecipeResource, RecipePublishResource | ||
from config import Config | ||
from extensions import db | ||
from flask_migrate import Migrate | ||
|
||
app = Flask(__name__) | ||
api = Api(app) | ||
|
||
api.add_resource(RecipeListResource, '/recipes') | ||
api.add_resource(RecipeResource, '/recipes/<int:recipe_id>') | ||
api.add_resource(RecipePublishResource, '/recipes/<int:recipe_id>/publish') | ||
def create_app() -> Flask: | ||
""" | ||
Create the Flask Application Object | ||
:return: | ||
""" | ||
app = Flask(__name__) | ||
app.config.from_object(Config) | ||
register_extensions(app) | ||
register_resources(app) | ||
return app | ||
|
||
|
||
def register_extensions(app): | ||
""" | ||
Perform database initialization and migrate the database to create tables | ||
:param app: flask application object | ||
:return: Nothing | ||
""" | ||
db.init_app(app) | ||
migrate = Migrate(app, db) | ||
|
||
|
||
def register_resources(app): | ||
""" | ||
This method registers api resources | ||
:param app: flask application object | ||
:return: Nothing | ||
""" | ||
api = Api(app) | ||
api.add_resource(RecipeListResource, '/recipes') | ||
api.add_resource(RecipeResource, '/recipes/<int:recipe_id>') | ||
api.add_resource(RecipePublishResource, '/recipes/<int:recipe_id>/publish') | ||
|
||
|
||
if __name__ == '__main__': | ||
app = create_app() | ||
app.run(port='5000', host='0.0.0.0', debug=True) |
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,16 @@ | ||
import os | ||
|
||
|
||
class Config: | ||
""" | ||
This class represents the db configuration | ||
""" | ||
__username = os.getenv('DB_USER', 'sqlalchemy') | ||
__password = os.getenv('DB_PASSWORD', 'sqlalchemy') | ||
__host = os.getenv('DB_HOST', 'localhost') | ||
__port = os.getenv('DB_PORT', '5432') | ||
__db_name = os.getenv('DB_NAME', 'instacook') | ||
|
||
DEBUG = True | ||
SQLALCHEMY_DATABASE_URI = f'postgresql+psycopg2://{__username}:{__password}@{__host}:{__port}/{__db_name}' | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False |
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 @@ | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
db: SQLAlchemy = SQLAlchemy() |
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 |
---|---|---|
@@ -1,51 +1,34 @@ | ||
from extensions import db | ||
|
||
recipe_list = [] | ||
|
||
|
||
def get_last_id(): | ||
""" | ||
This method will return the recipe id | ||
This method will return the last id | ||
:return: id to be generated | ||
""" | ||
if recipe_list: | ||
last_recipe = recipe_list[-1] | ||
else: | ||
return 1 | ||
|
||
return last_recipe.id + 1 | ||
|
||
|
||
class Recipe: | ||
class Recipe(db.Model): | ||
""" | ||
This class represents the Recipe Model | ||
""" | ||
__tablename__ = 'recipe' | ||
|
||
def __init__(self, name, description, num_of_servings, cook_time, directions): | ||
""" | ||
The initializer for the recipe | ||
:param name: name of the recipe | ||
:param description: description | ||
:param num_of_servings: number of servings | ||
:param cook_time: cook_time in seconds | ||
:param directions: directions to follow | ||
""" | ||
self.id = get_last_id() | ||
self.name = name | ||
self.description = description | ||
self.num_of_servings = num_of_servings | ||
self.cook_time = cook_time | ||
self.directions = directions | ||
self.is_publish = False | ||
|
||
@property | ||
def data(self): | ||
""" | ||
property to return the Recipe as dictionary | ||
:return: dictionary representation of current Recipe object | ||
""" | ||
return { | ||
'id': self.id, | ||
'name': self.name, | ||
'description': self.description, | ||
'num_of_servings': self.num_of_servings, | ||
'cook_time': self.cook_time, | ||
'directions': self.directions, | ||
} | ||
id = db.Column(db.Integer, primary_key=True) | ||
name = db.Column(db.String, nullable=False) | ||
description = db.Column(db.String(256)) | ||
num_of_servings = db.Column(db.Integer) | ||
cook_time = db.Column(db.Integer) | ||
directions = db.Column(db.String(1000)) | ||
is_publish = db.Column(db.Boolean(), default=False) | ||
created_at = db.Column(db.DateTime(), nullable=False, server_default=db.func.now()) | ||
updated_at = db.Column(db.DateTime(), nullable=False, server_default=db.func.now(), onupdate=db.func.now()) | ||
|