Skip to content

Commit

Permalink
[Fixes🛠️] removed deprecated utcnow() from models fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
[esekyi] committed Sep 13, 2024
1 parent 75d6d3f commit 24fda1c
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 21 deletions.
7 changes: 4 additions & 3 deletions app/models/category.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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'<Category {self.name}>'
7 changes: 4 additions & 3 deletions app/models/comment.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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'<Comment {self.text}>'
7 changes: 4 additions & 3 deletions app/models/ingredient.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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'<Ingredient {self.name}>'
7 changes: 4 additions & 3 deletions app/models/instruction.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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'<Instruction {self.step_number}: {self.name[:20]}>'
7 changes: 4 additions & 3 deletions app/models/recipe.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions app/models/user.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions app/services/recipe_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand Down
35 changes: 35 additions & 0 deletions migrations/versions/5246baafeb00_added_subscribe_model.py
Original file line number Diff line number Diff line change
@@ -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 ###
35 changes: 35 additions & 0 deletions migrations/versions/f6f0a537719a_added_subscribe_model.py
Original file line number Diff line number Diff line change
@@ -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 ###

0 comments on commit 24fda1c

Please sign in to comment.