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
d499bc8c
Commit
d499bc8c
authored
8 months ago
by
Richard Fernando Heise Ferreira
Browse files
Options
Downloads
Plain Diff
Merge branch 'issue-6/update-parser-to-strapi5' into 'main'
Issue
#6
: UPDATE Parser modifications for strapi V5.0 See merge request
!6
parents
1ac0f1ba
db9bbdb4
No related branches found
No related tags found
1 merge request
!6
Issue #6: UPDATE Parser modifications for strapi V5.0
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/parser.py
+44
-34
44 additions, 34 deletions
app/parser.py
with
44 additions
and
34 deletions
app/parser.py
+
44
−
34
View file @
d499bc8c
...
@@ -2,6 +2,7 @@ from flask import Flask, request
...
@@ -2,6 +2,7 @@ from flask import Flask, request
import
os
import
os
import
logging
import
logging
import
sys
import
sys
import
requests
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
...
@@ -13,11 +14,11 @@ logging.basicConfig(stream=sys.stdout,
...
@@ -13,11 +14,11 @@ logging.basicConfig(stream=sys.stdout,
# Diretórios onde os arquivos Markdown serão salvos dentro da pasta do hugo
# Diretórios onde os arquivos Markdown serão salvos dentro da pasta do hugo
DIR_O_C3SL
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content
'
)
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
'
)
DIR_POSTS
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../
../
cms-c3sl/themes/c3sl/exampleSite/content/noticia
'
)
DIR_PROJECTS
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../cms-c3sl/themes/c3sl/exampleSite/content/projetos
'
)
DIR_PROJECTS
=
os
.
path
.
join
(
os
.
getcwd
(),
'
../
../
cms-c3sl/themes/c3sl/exampleSite/content/projetos
'
)
def
create_or_update_
post
(
post
_id
,
data
,
event
,
content_type
):
def
create_or_update_
noticia
(
noticia
_id
,
data
,
event
,
content_type
):
if
not
data
:
if
not
data
:
logging
.
error
(
f
"
Code: 400
"
logging
.
error
(
f
"
Code: 400
"
f
"
Status: JSON file not found
\n
"
)
f
"
Status: JSON file not found
\n
"
)
...
@@ -25,7 +26,7 @@ def create_or_update_post(post_id, data, event, content_type):
...
@@ -25,7 +26,7 @@ def create_or_update_post(post_id, data, event, content_type):
return
"
JSON not found
"
,
400
return
"
JSON not found
"
,
400
#Caso a requisição seja para criar/alterar o conteudo do "Quem Somos"
#Caso a requisição seja para criar/alterar o conteudo do "Quem Somos"
if
content_type
==
'
o-c3sl
'
:
if
content_type
==
'
sobre
'
:
attributes
=
data
.
get
(
'
entry
'
,
{})
attributes
=
data
.
get
(
'
entry
'
,
{})
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
...
@@ -49,7 +50,7 @@ layout: 'legal'
...
@@ -49,7 +50,7 @@ layout: 'legal'
partner
=
attributes
.
get
(
'
Parceiro
'
,
''
)
partner
=
attributes
.
get
(
'
Parceiro
'
,
''
)
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
file_name
=
f
"
{
post
_id
}
.md
"
file_name
=
f
"
{
noticia
_id
}
.md
"
file_path
=
os
.
path
.
join
(
DIR_PROJECTS
,
file_name
)
file_path
=
os
.
path
.
join
(
DIR_PROJECTS
,
file_name
)
edited_line
=
'
Edited: true
\n
'
if
event
==
'
entry.update
'
else
''
edited_line
=
'
Edited: true
\n
'
if
event
==
'
entry.update
'
else
''
...
@@ -65,22 +66,31 @@ status: "{status}"
...
@@ -65,22 +66,31 @@ status: "{status}"
{
content
}
{
content
}
"""
"""
##Caso a requisição seja para criar/alterar uma notícia
##Caso a requisição seja para criar/alterar uma notícia
elif
content_type
==
'
post
'
:
elif
content_type
==
'
noticia
'
:
#Coleta os dados necessários para estar no arquivo JSON
#Coleta os dados necessários para estar no arquivo JSON
attributes
=
data
.
get
(
'
entry
'
,
{})
attributes
=
data
.
get
(
'
entry
'
,
{})
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
content
=
attributes
.
get
(
'
Descricao
'
,
''
)
published_at
=
attributes
.
get
(
'
Data
'
,
''
)
published_at
=
attributes
.
get
(
'
Data
'
,
''
)
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
title
=
attributes
.
get
(
'
Titulo
'
,
''
)
author
=
attributes
.
get
(
'
Autor
'
,
''
)
author
=
attributes
.
get
(
'
Autor
'
,
{})
summary
=
attributes
.
get
(
'
Sumario
'
,
''
)
author_name
=
author
.
get
(
'
name
'
,
''
)
summary
=
attributes
.
get
(
'
Gravata
'
,
''
)
images
=
attributes
.
get
(
'
Imagem
'
,
[])
#Recebo o atributo da imagem enviado no JSON
if
images
and
isinstance
(
images
,
list
):
image
=
attributes
.
get
(
'
Imagem
'
,
{})
image_url
=
images
[
0
].
get
(
'
url
'
,
''
)
else
:
#Recebo o ID da imagem para solicitar a requisição
image_url
=
''
image_id
=
image
.
get
(
'
id
'
,
''
)
image_url
=
f
"
http://localhost:1337/api/upload/files/
{
image_id
}
"
#Recebo o JSON requisitado pela URL com o ID da imagem
response
=
requests
.
get
(
image_url
)
image_json
=
response
.
json
()
#Adquiro a URL para inserir markdown
image_url
=
image_json
.
get
(
'
url
'
,
''
)
file_name
=
f
"
{
post
_id
}
.md
"
file_name
=
f
"
{
noticia
_id
}
.md
"
file_path
=
os
.
path
.
join
(
DIR_POSTS
,
file_name
)
file_path
=
os
.
path
.
join
(
DIR_POSTS
,
file_name
)
edited_line
=
'
Edited: true
\n
'
if
event
==
'
entry.update
'
else
''
edited_line
=
'
Edited: true
\n
'
if
event
==
'
entry.update
'
else
''
...
@@ -88,7 +98,7 @@ status: "{status}"
...
@@ -88,7 +98,7 @@ status: "{status}"
content_markdown
=
f
"""
---
content_markdown
=
f
"""
---
title:
"
{
title
}
"
title:
"
{
title
}
"
date:
"
{
published_at
}
"
date:
"
{
published_at
}
"
author:
"
{
author
}
"
author:
"
{
author
_name
}
"
image:
"
http://localhost:1337
{
image_url
}
"
image:
"
http://localhost:1337
{
image_url
}
"
summary:
"
{
summary
}
"
summary:
"
{
summary
}
"
{
edited_line
}
ShowReadingTime: true
{
edited_line
}
ShowReadingTime: true
...
@@ -109,28 +119,28 @@ summary: "{summary}"
...
@@ -109,28 +119,28 @@ summary: "{summary}"
return
f
"
Error writing file
{
e
}
"
,
400
return
f
"
Error writing file
{
e
}
"
,
400
#Deleta algum tipo de conteudo enviado por requisição
#Deleta algum tipo de conteudo enviado por requisição
def
delete_
post
(
post
_id
,
content_type
):
def
delete_
noticia
(
noticia
_id
,
content_type
):
#Deletando uma notícia
#Deletando uma notícia
if
content_type
==
'
post
'
:
if
content_type
==
'
noticia
'
:
file_name
=
f
"
{
post
_id
}
.md
"
file_name
=
f
"
{
noticia
_id
}
.md
"
file_path
=
os
.
path
.
join
(
DIR_POSTS
,
file_name
)
file_path
=
os
.
path
.
join
(
DIR_POSTS
,
file_name
)
if
os
.
path
.
exists
(
file_path
):
if
os
.
path
.
exists
(
file_path
):
os
.
remove
(
file_path
)
os
.
remove
(
file_path
)
logging
.
info
(
f
"
200 - DELETED:
Post
with ID number
{
post
_id
}
"
)
logging
.
info
(
f
"
200 - DELETED:
noticia
with ID number
{
noticia
_id
}
"
)
else
:
else
:
logging
.
error
(
f
"
400 - ERROR:
Post
with with ID number
{
post
_id
}
not found for exclusion
"
)
logging
.
error
(
f
"
400 - ERROR:
noticia
with with ID number
{
noticia
_id
}
not found for exclusion
"
)
#Deletando um projeto
#Deletando um projeto
elif
content_type
==
'
projeto
'
:
elif
content_type
==
'
projeto
'
:
file_name
=
f
"
{
post
_id
}
.md
"
file_name
=
f
"
{
noticia
_id
}
.md
"
file_path
=
os
.
path
.
join
(
DIR_PROJECTS
,
file_name
)
file_path
=
os
.
path
.
join
(
DIR_PROJECTS
,
file_name
)
if
os
.
path
.
exists
(
file_path
):
if
os
.
path
.
exists
(
file_path
):
os
.
remove
(
file_path
)
os
.
remove
(
file_path
)
logging
.
info
(
f
"
200 - DELETED: Project with ID number
{
post
_id
}
"
)
logging
.
info
(
f
"
200 - DELETED: Project with ID number
{
noticia
_id
}
"
)
else
:
else
:
logging
.
error
(
f
"
200 - ERROR: File of project with ID number
{
post
_id
}
not found for exclusion
"
)
logging
.
error
(
f
"
200 - ERROR: File of project with ID number
{
noticia
_id
}
not found for exclusion
"
)
else
:
else
:
logging
.
error
(
f
"
400 - ERROR: Content type:
'
{
content_type
}
'
not recognized for deletion
"
)
logging
.
error
(
f
"
400 - ERROR: Content type:
'
{
content_type
}
'
not recognized for deletion
"
)
...
@@ -143,34 +153,34 @@ def run_script():
...
@@ -143,34 +153,34 @@ def run_script():
logging
.
info
(
"
Data (JSON) received
"
)
logging
.
info
(
"
Data (JSON) received
"
)
#Do arquivo JSON, separa ID do conteudo, tipo do evento e o conteúdo
#Do arquivo JSON, separa ID do conteudo, tipo do evento e o conteúdo
post
_id
=
data
.
get
(
'
entry
'
,
{}).
get
(
'
id
'
)
noticia
_id
=
data
.
get
(
'
entry
'
,
{}).
get
(
'
id
'
)
event
=
data
.
get
(
'
event
'
)
event
=
data
.
get
(
'
event
'
)
content_type
=
data
.
get
(
'
model
'
,
''
)
# Obtém o tipo de conteúdo do Strapi
content_type
=
data
.
get
(
'
model
'
,
''
)
# Obtém o tipo de conteúdo do Strapi
print
(
f
"
Event:
{
event
}
, ID:
{
post
_id
}
, Type:
{
content_type
}
"
)
print
(
f
"
Event:
{
event
}
, ID:
{
noticia
_id
}
, Type:
{
content_type
}
"
)
if
not
post
_id
:
if
not
noticia
_id
:
logging
.
error
(
f
"
Code: 400
"
logging
.
error
(
f
"
Code: 400
"
f
"
Status: ID not found in JSON:
{
data
}
"
)
f
"
Status: ID not found in JSON:
{
data
}
"
)
logging
.
exception
(
"
error occurred while processing the event
"
)
logging
.
exception
(
"
error occurred while processing the event
"
)
return
"
ID not found in JSON
"
,
400
return
"
ID not found in JSON
"
,
400
if
event
in
[
'
entry.create
'
]:
if
event
in
[
'
entry.create
'
]:
logging
.
info
(
f
"
File created in Strapi- ID:
{
post
_id
}
- Type:
{
content_type
}
"
logging
.
info
(
f
"
File created in Strapi- ID:
{
noticia
_id
}
- Type:
{
content_type
}
"
f
"
\n
Data:
{
data
}
"
)
f
"
\n
Data:
{
data
}
"
)
return
f
"
File created in Strapi
{
post
_id
}
- Type:
{
content_type
}
"
,
200
return
f
"
File created in Strapi
{
noticia
_id
}
- Type:
{
content_type
}
"
,
200
elif
event
in
[
'
entry.update
'
,
'
entry.publish
'
]:
elif
event
in
[
'
entry.update
'
,
'
entry.publish
'
]:
create_or_update_
post
(
post
_id
,
data
,
event
,
content_type
)
create_or_update_
noticia
(
noticia
_id
,
data
,
event
,
content_type
)
logging
.
info
(
f
"
\n
File created in Hugo- ID:
{
post
_id
}
- Type:
{
content_type
}
"
logging
.
info
(
f
"
\n
File created in Hugo- ID:
{
noticia
_id
}
- Type:
{
content_type
}
"
f
"
\n
Data:
{
data
}
\n
Operation completed successfully
\n
"
)
f
"
\n
Data:
{
data
}
\n
Operation completed successfully
\n
"
)
return
f
"
Operation completed successfully
\n
Data:
{
data
}
"
,
200
return
f
"
Operation completed successfully
\n
Data:
{
data
}
"
,
200
elif
event
in
[
'
entry.delete
'
,
'
entry.unpublish
'
]:
elif
event
in
[
'
entry.delete
'
,
'
entry.unpublish
'
]:
delete_
post
(
post
_id
,
content_type
)
delete_
noticia
(
noticia
_id
,
content_type
)
logging
.
info
(
f
"
File removed - ID:
{
post
_id
}
- Type:
{
content_type
}
"
)
logging
.
info
(
f
"
File removed - ID:
{
noticia
_id
}
- Type:
{
content_type
}
"
)
return
f
"
File removed with ID
{
post
_id
}
"
,
200
return
f
"
File removed with ID
{
noticia
_id
}
"
,
200
else
:
else
:
logging
.
error
(
f
"
Code: 400
\n
"
logging
.
error
(
f
"
Code: 400
\n
"
...
...
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