Skip to content
Snippets Groups Projects
Commit 88be5a3b authored by Richard Fernando Heise Ferreira's avatar Richard Fernando Heise Ferreira
Browse files

Merge branch 'issue-7/markdown-com-titulo-noticia' into 'main'

Issue #7: ADD Altering id title to news title

See merge request !7
parents d499bc8c 2b8427b9
No related branches found
No related tags found
1 merge request!7Issue #7: ADD Altering id title to news title
...@@ -18,7 +18,7 @@ DIR_O_C3SL = os.path.join(os.getcwd(), '../../cms-c3sl/themes/c3sl/exampleSite/c ...@@ -18,7 +18,7 @@ DIR_O_C3SL = os.path.join(os.getcwd(), '../../cms-c3sl/themes/c3sl/exampleSite/c
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_noticia(noticia_id, data, event, content_type): def create_or_update_noticia(identifier, projeto_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")
...@@ -50,7 +50,7 @@ layout: 'legal' ...@@ -50,7 +50,7 @@ layout: 'legal'
partner = attributes.get('Parceiro', '') partner = attributes.get('Parceiro', '')
content = attributes.get('Descricao', '') content = attributes.get('Descricao', '')
file_name = f"{noticia_id}.md" file_name = f"{projeto_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 ''
...@@ -90,7 +90,7 @@ status: "{status}" ...@@ -90,7 +90,7 @@ status: "{status}"
#Adquiro a URL para inserir markdown #Adquiro a URL para inserir markdown
image_url = image_json.get('url', '') image_url = image_json.get('url', '')
file_name = f"{noticia_id}.md" file_name = f"{identifier}.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 ''
...@@ -119,28 +119,28 @@ summary: "{summary}" ...@@ -119,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_noticia(noticia_id, content_type): def delete_noticia(identifier, projeto_id, content_type):
#Deletando uma notícia #Deletando uma notícia
if content_type == 'noticia': if content_type == 'noticia':
file_name = f"{noticia_id}.md" file_name = f"{identifier}.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: noticia with ID number {noticia_id}") logging.info(f"200 - DELETED: noticia with ID title {identifier}")
else: else:
logging.error(f"400 - ERROR: noticia with with ID number{noticia_id} not found for exclusion") logging.error(f"400 - ERROR: noticia with with ID title {identifier} not found for exclusion")
#Deletando um projeto #Deletando um projeto
elif content_type == 'projeto': elif content_type == 'projeto':
file_name = f"{noticia_id}.md" file_name = f"{projeto_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 {noticia_id}") logging.info(f"200 - DELETED: Project with ID number {projeto_id}")
else: else:
logging.error(f"200 - ERROR: File of project with ID number {noticia_id} not found for exclusion") logging.error(f"200 - ERROR: File of project with ID number {projeto_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")
...@@ -152,35 +152,47 @@ def run_script(): ...@@ -152,35 +152,47 @@ def run_script():
print("JSON received: ", data) print("JSON received: ", data)
logging.info ("Data (JSON) received") logging.info ("Data (JSON) received")
attributes = data.get('entry', {})
identifier = attributes.get('Identificador', '')
#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
noticia_id = data.get('entry', {}).get('id') projeto_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: {noticia_id}, Type: {content_type}") if not projeto_id:
logging.error(f"Code: 400"
f"Status: ID not found in JSON: {data}")
logging.exception("error occurred while processing the event")
return "ID not found in JSON", 400
else:
print(f"Event: {event}, ID: {projeto_id}, Type: {content_type}")
if not noticia_id: if not identifier:
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
else:
print(f"Event: {event}, ID: {identifier}, Type: {content_type}")
if event in ['entry.create']: if event in ['entry.create']:
logging.info (f"File created in Strapi- ID: {noticia_id} - Type: {content_type}" logging.info (f"File created in Strapi- ID: {identifier} - Type: {content_type}"
f"\nData: {data}") f"\nData: {data}")
return f"File created in Strapi {noticia_id} - Type: {content_type}", 200 return f"File created in Strapi {identifier} - Type: {content_type}", 200
elif event in ['entry.update', 'entry.publish']: elif event in ['entry.update', 'entry.publish']:
create_or_update_noticia(noticia_id, data, event, content_type) create_or_update_noticia(identifier, projeto_id, data, event, content_type)
logging.info(f"\nFile created in Hugo- ID: {noticia_id} - Type: {content_type}" logging.info(f"\nFile created in Hugo- ID: {identifier} - Type: {content_type}"
f"\nData: {data}\nOperation completed successfully\n") f"\nData: {data}\nOperation 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_noticia(noticia_id, content_type) delete_noticia(identifier, projeto_id, content_type)
logging.info(f"File removed - ID: {noticia_id} - Type: {content_type}") logging.info(f"File removed - ID: {identifier} - Type: {content_type}")
return f"File removed with ID {noticia_id}", 200 return f"File removed with ID {identifier}", 200
else: else:
logging.error(f"Code: 400\n" logging.error(f"Code: 400\n"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment