From 24fda1cca32b5aee27db238b9528ece864fa3a72 Mon Sep 17 00:00:00 2001 From: "[esekyi]" <[sskert10@gmail.com]> Date: Fri, 13 Sep 2024 01:21:43 +0000 Subject: [PATCH] =?UTF-8?q?[Fixes=F0=9F=9B=A0=EF=B8=8F]=20removed=20deprec?= =?UTF-8?q?ated=20utcnow()=20from=20models=20fields.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/category.py | 7 ++-- app/models/comment.py | 7 ++-- app/models/ingredient.py | 7 ++-- app/models/instruction.py | 7 ++-- app/models/recipe.py | 7 ++-- app/models/user.py | 7 ++-- app/services/recipe_service.py | 5 ++- .../5246baafeb00_added_subscribe_model.py | 35 +++++++++++++++++++ .../f6f0a537719a_added_subscribe_model.py | 35 +++++++++++++++++++ 9 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 migrations/versions/5246baafeb00_added_subscribe_model.py create mode 100644 migrations/versions/f6f0a537719a_added_subscribe_model.py diff --git a/app/models/category.py b/app/models/category.py index fa1a9d7..49e36b8 100644 --- a/app/models/category.py +++ b/app/models/category.py @@ -1,5 +1,5 @@ from app import db -from datetime import datetime +from datetime import datetime, timezone import uuid from sqlalchemy.dialects.postgresql import UUID @@ -8,9 +8,10 @@ class Category(db.Model): id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = db.Column(db.String(80), unique=True, nullable=False) description = db.Column(db.Text) - created_at = db.Column(db.DateTime, default=datetime.utcnow) + created_at = db.Column( + db.DateTime, default=datetime.now(timezone.utc)) updated_at = db.Column( - db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc)) def __repr__(self): return f'' diff --git a/app/models/comment.py b/app/models/comment.py index ea562cb..5f99ae6 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -1,5 +1,5 @@ from app import db -from datetime import datetime +from datetime import datetime, timezone import uuid from sqlalchemy.dialects.postgresql import UUID @@ -12,9 +12,10 @@ class Comment(db.Model): db.ForeignKey('user.id'), nullable=False) recipe_id = db.Column(UUID(as_uuid=True), db.ForeignKey( 'recipe.id'), nullable=False) - created_at = db.Column(db.DateTime, default=datetime.utcnow) + created_at = db.Column( + db.DateTime, default=datetime.now(timezone.utc)) updated_at = db.Column( - db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc)) def __repr__(self): return f'' diff --git a/app/models/ingredient.py b/app/models/ingredient.py index 2743edf..295dd28 100644 --- a/app/models/ingredient.py +++ b/app/models/ingredient.py @@ -1,5 +1,5 @@ from app import db -from datetime import datetime +from datetime import datetime, timezone import uuid from sqlalchemy.dialects.postgresql import UUID @@ -10,9 +10,10 @@ class Ingredient(db.Model): name = db.Column(db.String(120), nullable=False) recipe_id = db.Column(UUID(as_uuid=True), db.ForeignKey( 'recipe.id'), nullable=False) - created_at = db.Column(db.DateTime, default=datetime.utcnow) + created_at = db.Column( + db.DateTime, default=datetime.now(timezone.utc)) updated_at = db.Column( - db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc)) def __repr__(self): return f'' diff --git a/app/models/instruction.py b/app/models/instruction.py index 2444c91..e2c170d 100644 --- a/app/models/instruction.py +++ b/app/models/instruction.py @@ -1,5 +1,5 @@ from app import db -from datetime import datetime +from datetime import datetime, timezone import uuid from sqlalchemy.dialects.postgresql import UUID @@ -11,9 +11,10 @@ class Instruction(db.Model): name = db.Column(db.Text, nullable=False) recipe_id = db.Column(UUID(as_uuid=True), db.ForeignKey( 'recipe.id'), nullable=False) - created_at = db.Column(db.DateTime, default=datetime.utcnow) + created_at = db.Column( + db.DateTime, default=datetime.now(timezone.utc)) updated_at = db.Column( - db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc)) def __repr__(self) -> str: return f'' diff --git a/app/models/recipe.py b/app/models/recipe.py index 667073f..8727d43 100644 --- a/app/models/recipe.py +++ b/app/models/recipe.py @@ -1,5 +1,5 @@ from app import db -from datetime import datetime +from datetime import datetime, timezone import uuid from sqlalchemy.dialects.postgresql import UUID @@ -20,8 +20,9 @@ class Recipe(db.Model): 'category.id'), nullable=False) user_id = db.Column(UUID(as_uuid=True), db.ForeignKey( 'user.id'), nullable=False) - created_at = db.Column(db.DateTime, default=datetime.utcnow) - updated_at = db.Column(db.DateTime, default=datetime.utcnow) + created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc)) + updated_at = db.Column( + db.DateTime, default=datetime.now(timezone.utc)) category = db.relationship('Category', backref='recipes', lazy=True) comments = db.relationship('Comment', backref='recipe', cascade='all, delete-orphan', lazy=True) ingredients = db.relationship( diff --git a/app/models/user.py b/app/models/user.py index 09a10bb..2e912a6 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -1,5 +1,5 @@ from app import db -from datetime import datetime +from datetime import datetime, timezone import uuid from flask_login import UserMixin from sqlalchemy.dialects.postgresql import UUID @@ -13,9 +13,10 @@ class User(db.Model, UserMixin): username = db.Column(db.String(80), unique=True, nullable=False, index=True) email = db.Column(db.String(120), unique=True, nullable=False, index=True) password_hash = db.Column(db.String(255), nullable=False) - created_at = db.Column(db.DateTime, default=datetime.utcnow) + created_at = db.Column( + db.DateTime, default=datetime.now(timezone.utc)) updated_at = db.Column( - db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc)) recipes = db.relationship('Recipe', backref='author', lazy=True) comments = db.relationship('Comment', backref='author', lazy=True) diff --git a/app/services/recipe_service.py b/app/services/recipe_service.py index 88e34cc..482e726 100644 --- a/app/services/recipe_service.py +++ b/app/services/recipe_service.py @@ -2,11 +2,10 @@ from app.models.recipe import Recipe from app.models.recipe import Recipe from app.models.ingredient import Ingredient -from app.models.comment import Comment from app.models.instruction import Instruction from app import db import random -from datetime import datetime +from datetime import datetime, timezone from app.services.pagination_service import paginate @@ -144,7 +143,7 @@ def update_recipe(recipe, data, ingredients, instructions, image_url): db.session.delete(existing_instructions[extra_idx]) # Manually updating the `updated_at` field 'cos of view_count - recipe.updated_at = datetime.utcnow() + recipe.updated_at = datetime.now(timezone.utc) db.session.commit() diff --git a/migrations/versions/5246baafeb00_added_subscribe_model.py b/migrations/versions/5246baafeb00_added_subscribe_model.py new file mode 100644 index 0000000..257c46b --- /dev/null +++ b/migrations/versions/5246baafeb00_added_subscribe_model.py @@ -0,0 +1,35 @@ +"""Added Subscribe model + +Revision ID: 5246baafeb00 +Revises: f6f0a537719a +Create Date: 2024-09-11 16:35:24.804373 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '5246baafeb00' +down_revision = 'f6f0a537719a' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('subscriber', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('email', sa.String(length=120), nullable=False), + sa.Column('subscribed_on', sa.DateTime(), nullable=True), + sa.Column('is_active', sa.Boolean(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('subscriber') + # ### end Alembic commands ### diff --git a/migrations/versions/f6f0a537719a_added_subscribe_model.py b/migrations/versions/f6f0a537719a_added_subscribe_model.py new file mode 100644 index 0000000..56f74de --- /dev/null +++ b/migrations/versions/f6f0a537719a_added_subscribe_model.py @@ -0,0 +1,35 @@ +"""Added Subscribe model + +Revision ID: f6f0a537719a +Revises: 86043ab96515 +Create Date: 2024-09-11 14:36:40.996536 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f6f0a537719a' +down_revision = '86043ab96515' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('subscriber', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('email', sa.String(length=120), nullable=False), + sa.Column('subscribed_on', sa.DateTime(), nullable=True), + sa.Column('is_active', sa.Boolean(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('subscriber') + # ### end Alembic commands ###