diff --git a/app/api.py b/app/api.py index 4463f04..a7d5798 100644 --- a/app/api.py +++ b/app/api.py @@ -4,6 +4,7 @@ from flask import Blueprint, request, redirect, session, render_template, flash, jsonify from typing import Tuple, Union from slugify import slugify as slug +from datetime import datetime from app.models import Contents, db @@ -21,8 +22,14 @@ def obtain_draft_title_and_body(self, id: str) -> Tuple[str, str,str] : def data_valid(self, title: str, content: str) -> bool: return len(title.strip()) > 0 and len(content.strip()) > 0 - def update_content(self, id:str, title: str, body: str, description:str, status:str) -> None: - Contents.query.filter_by(id=id).update({Contents.title:title, Contents.body:body, Contents.status:status, Contents.description:description}) + def update_content(self, id:str, title: str, body: str, description:str, status:str, published_at:datetime) -> None: + Contents.query.filter_by(id=id).update({ + Contents.title:title, + Contents.body:body, + Contents.status:status, + Contents.description:description, + Contents.published_at:published_at + }) db.session.commit() def extract_json_data(self, request:request, keys:list) -> dict: @@ -50,7 +57,7 @@ def extract_json_data(self, request:request, keys:list) -> dict: return extracted_data - def insert_content(self, title: str, body: str, status: str, description:str, accessType: str = 'public') -> None: + def insert_content(self, title: str, body: str, status: str, description:str, published_at:datetime, accessType: str = 'public') -> None: """ status: "published" or "draft" accessType: "public" or "private" @@ -63,7 +70,8 @@ def insert_content(self, title: str, body: str, status: str, description:str, ac slug=slug(title), status=status, accessType=accessType, - description=description + description=description, + published_at=published_at ) ) db.session.commit() @@ -102,12 +110,18 @@ def publish_post(): "message": "O titulo ou texto não está preenchido adequadamente! Por favor, verifique se você preencheu os campo corretamente!", "status_code": 400 }), 400 + + + date_published = None + if status_publication == "published": + date_published = datetime.utcnow() contentManager.insert_content( title=title, body=body, status=status_publication, - description=description + description=description, + published_at =date_published ) return jsonify({ @@ -168,12 +182,20 @@ def update_post(id): "status_code": 400 }), 400 + post = Contents.query.filter(Contents.id == id).first() + + date_published = None + if post.status == "draft" and status_publication == "published": + date_published = datetime.utcnow() + + contentManager.update_content( id=id, title=title, body=body, status=status_publication, - description=description + description=description, + published_at=date_published ) return jsonify({ diff --git a/app/blog.py b/app/blog.py index 58a084c..ef4f78e 100644 --- a/app/blog.py +++ b/app/blog.py @@ -10,13 +10,15 @@ @blog.route('/', methods=['GET']) def get_blog(): - contents = Contents.query.filter(Contents.status == "published").order_by(Contents.created_at.desc()).all() + contents = Contents.query.filter(Contents.status == "published").order_by(Contents.published_at.desc()).all() utc_timezone = pytz.timezone('UTC') user_timezone = pytz.timezone('America/Sao_Paulo') + for content in contents: - content.created_at = convert_datetime(content.created_at) + if content.published_at != None: + content.published_at = convert_datetime(content.published_at).strftime("%d/%m/%Y, %H:%M %p") return render_template('index.html', contents=contents) @@ -31,9 +33,14 @@ def render_text(slug): if data is None: abort(404) + date_published = "" + + if data.published_at != None: + date_published = convert_datetime(data.published_at).strftime("%d/%m/%Y, %H:%M %p") + title = data.title body = data.body - date = convert_datetime(data.created_at).strftime("%d/%m/%Y, %H:%M %p") + date = date_published slug = data.slug description = data.description diff --git a/app/models.py b/app/models.py index 42ef626..4ab8b6b 100644 --- a/app/models.py +++ b/app/models.py @@ -1,6 +1,7 @@ from datetime import datetime from flask_sqlalchemy import SQLAlchemy +from sqlalchemy.event import listens_for from sqlalchemy import Integer, String, Column, Text, Enum, CheckConstraint, DateTime @@ -14,26 +15,28 @@ def config_models(app) -> None: class Contents(db.Model): __tablename__ = 'contents' - id = Column(String(36), primary_key=True, nullable=False, unique=True) - title = Column(String(256), nullable=False) - body = Column(Text) - slug = Column(String(256), nullable=False, unique=True) - status = Column(String, CheckConstraint("status IN ('published', 'draft')"), default='draft') - accessType = Column(String, CheckConstraint("type IN ('public', 'private')"), default='public') - description = Column(String(1000), default='') - created_at = Column(DateTime, default=datetime.utcnow) - updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) - - - def __init__(self, id: str, title: str, body:str, slug:str, status:str, accessType:str, description:str) -> None: - self.id = id - self.title = title - self.body = body - self.slug = slug - self.status = status - self.accessType = accessType - self.description = description - + id = Column(String(36), primary_key=True, nullable=False, unique=True) + title = Column(String(256), nullable=False) + body = Column(Text) + slug = Column(String(256), nullable=False, unique=True) + status = Column(String, CheckConstraint("status IN ('published', 'draft')"), default='draft') + accessType = Column(String, CheckConstraint("type IN ('public', 'private')"), default='public') + description = Column(String(1000), default='') + published_at = Column(DateTime, nullable=True) + created_at = Column(DateTime, default=datetime.utcnow) + updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + + + def __init__(self, id: str, title: str, body:str, slug:str, status:str, accessType:str, description:str, published_at:datetime) -> None: + self.id = id + self.title = title + self.body = body + self.slug = slug + self.status = status + self.accessType = accessType + self.description = description + self.published_at = published_at + class Users(db.Model): __tablename__ = 'users' diff --git a/templates/index.html b/templates/index.html index 4e10278..ffc5a14 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,7 +6,7 @@
  • {{ content.title}}

    - +

    {{ content.description}} continuar lendo

  • diff --git a/templates/layout.html b/templates/layout.html index 8eee1ba..a0c8f01 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,17 +12,17 @@ {% endif %} - + - - + + - - + +