Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Parser
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CMS
Parser
Commits
ebef491c
Commit
ebef491c
authored
10 months ago
by
esrsc23
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2
: UPDATE Inserting a route to 'who we are'
parent
2ecc1249
Branches
Branches containing commit
No related tags found
1 merge request
!2
Issue #2: UPDATE Inserting a route to 'who we are'
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
app.py
+62
-51
62 additions, 51 deletions
app.py
with
62 additions
and
51 deletions
app.py
+
62
−
51
View file @
ebef491c
# Importando bibliotecas
# Flask = Criação aplicação/serviço Web
# Request permite acessar as requisições
from
flask
import
Flask
,
request
import
os
app
=
Flask
(
__name__
)
# Diretório onde os arquivos Markdown serão salvos
DIR
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content/noticia
'
)
# Diretórios onde os arquivos Markdown serão salvos
DIR_O_C3SL
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content
'
)
DIR_POSTS
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content/noticia
'
)
def
create_or_update_post
(
post_id
,
data
,
event
):
def
create_or_update_post
(
post_id
,
data
,
event
,
content_type
):
if
not
data
:
return
# Armazena todos os atributos do JSON usando a própria variável Data
if
content_type
==
'
o-c3sl
'
:
attributes
=
data
.
get
(
'
entry
'
,
{})
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
# Nome fixo para single type
file_name
=
"
quem-somos.md
"
file_path
=
os
.
path
.
join
(
DIR_O_C3SL
,
file_name
)
content_markdown
=
f
"""
---
title:
"
{
title
}
"
layout:
'
legal
'
---
{
content
}
"""
else
:
attributes
=
data
.
get
(
'
entry
'
,
{})
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
published_at
=
attributes
.
get
(
'
Data
'
,
''
)
...
...
@@ -22,21 +34,17 @@ def create_or_update_post(post_id, data, event):
author
=
attributes
.
get
(
'
Autor
'
,
''
)
summary
=
attributes
.
get
(
'
Sumario
'
,
''
)
# Acessa a URL da imagem da imagem da lista
images
=
attributes
.
get
(
'
Imagem
'
,
[])
if
images
and
isinstance
(
images
,
list
):
image_url
=
images
[
0
].
get
(
'
url
'
,
''
)
else
:
image_url
=
''
# Cria o nome do arquivo conforme o ID da notícia
file_name
=
f
"
{
post_id
}
.md
"
file_path
=
os
.
path
.
join
(
DIR
,
file_name
)
file_path
=
os
.
path
.
join
(
DIR
_POSTS
,
file_name
)
# Adiciona a linha "Edited: true" caso o evento seja de atualização da notícia
edited_line
=
'
Edited: true
\n
'
if
event
==
'
entry.update
'
else
''
# Escreve os dados no formato Markdown na variável content_markdown
content_markdown
=
f
"""
---
title:
"
{
title
}
"
date:
"
{
published_at
}
"
...
...
@@ -49,41 +57,44 @@ summary: "{summary}"
{
content
}
"""
# Escreve o arquivo no caminho solicitado
try
:
with
open
(
file_path
,
'
w
'
,
encoding
=
'
utf8
'
)
as
file
:
file
.
write
(
content_markdown
)
#print(f"Conteúdo do Markdown criado/atualizado para o post ID {post_id}:\n{content_markdown}\n")
#print(f"Post '{title}' salvo como {file_path}")
except
Exception
as
e
:
print
(
f
"
Erro ao escrever o arquivo:
{
e
}
"
)
print
(
f
"
Conteúdo do Markdown criado/atualizado para o post ID
{
post_id
}
:
\n
{
content_markdown
}
\n
"
)
print
(
f
"
Post
'
{
title
}
'
salvo como
{
file_path
}
"
)
# Função responsável por excluir o post quando a requisição é do tipo delete ou unpublish
def
delete_post
(
post_id
):
def
delete_post
(
post_id
,
content_type
):
if
content_type
==
'
post
'
:
file_name
=
f
"
{
post_id
}
.md
"
file_path
=
os
.
path
.
join
(
DIR
,
file_name
)
file_path
=
os
.
path
.
join
(
DIR
_POSTS
,
file_name
)
if
os
.
path
.
exists
(
file_path
):
os
.
remove
(
file_path
)
print
(
f
"
Post com ID
{
post_id
}
deletado
"
)
#
print(f"Post com ID {post_id} deletado")
else
:
print
(
f
"
Arquivo com ID
{
post_id
}
não encontrado para exclusão
"
)
@app.route
(
'
/run-script
'
,
methods
=
[
'
POST
'
])
def
run_script
():
# Recebe o JSON enviado pelo webhooks do Strapi
data
=
request
.
json
#print("Dados recebidos:", data)
# Armazena o ID do post e também o tipo de evento (update, create ou delete...)
post_id
=
data
.
get
(
'
entry
'
,
{}).
get
(
'
id
'
)
event
=
data
.
get
(
'
event
'
)
# Extraindo tipo do evento
print
(
f
"
Evento:
{
event
}
, ID:
{
post_id
}
"
)
event
=
data
.
get
(
'
event
'
)
content_type
=
data
.
get
(
'
model
'
,
''
)
# Obtém o tipo de conteúdo do Strapi
print
(
f
"
Evento:
{
event
}
, ID:
{
post_id
}
, Tipo:
{
content_type
}
"
)
if
not
post_id
:
return
"
ID not found in JSON
"
,
400
if
event
in
[
'
entry.update
'
,
'
entry.publish
'
]:
create_or_update_post
(
post_id
,
data
,
event
)
create_or_update_post
(
post_id
,
data
,
event
,
content_type
)
elif
event
in
[
'
entry.delete
'
,
'
entry.unpublish
'
]:
delete_post
(
post_id
)
delete_post
(
post_id
,
content_type
)
else
:
return
"
Evento não reconhecido
"
,
400
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment