Skip to content

Commit

Permalink
criação do campo published_at no banco de dados. Além de alterações n…
Browse files Browse the repository at this point in the history
…a api e templates
  • Loading branch information
thisiscleverson committed Apr 27, 2024
1 parent d34dcf1 commit 8ec7596
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 35 deletions.
34 changes: 28 additions & 6 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
13 changes: 10 additions & 3 deletions app/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down
43 changes: 23 additions & 20 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<li>
<div>
<h3><a class="post-link" href="/{{ content.slug }}">{{ content.title}}</a></h3>
<span class="post-meta">{{ content.created_at.strftime("%d/%m/%Y, %H:%M %p") }}</span>
<span class="post-meta">{{ content.published_at}}</span>
<p>{{ content.description}} <a style="font-size: 0.8em;" href="/{{ content.slug }}">continuar lendo</a></p>
</div>
</li>
Expand Down
10 changes: 5 additions & 5 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
{% endif %}

<meta property="og:site_name" content="this_is_cleverson">
<meta name="description" content="{{description}}">
<meta name="description" content="{{description if description else 'Um blog sobre tecnologia'}}">
<meta property="og:type" content="article">

<meta property="og:title" content="{{title}}">
<meta property="og:description" content="{{description}}">
<meta property="og:title" content="{{title if title else 'this_is_cleverson'}}">
<meta property="og:description" content="{{description if description else 'Um blog sobre tecnologia'}}">
<meta property="og:url" content="https://cleverson.online/{{slug}}">
<meta property="og:image" content="{{ url_for('static', filename='imgs/thisiscleverson.jpg') }}">

<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="{{title}}">
<meta name="twitter:description" content="{{description}}">
<meta name="twitter:title" content="{{title if title else 'this_is_cleverson'}}">
<meta name="twitter:description" content="{{description if description else 'Um blog sobre tecnologia'}}">
<meta property="twitter:url" content="https://cleverson.online/{{slug}}">
<meta name="twitter:image" content="{{ url_for('static', filename='imgs/thisiscleverson.jpg') }}">

Expand Down

0 comments on commit 8ec7596

Please sign in to comment.