Spaces:
Paused
Paused
Update routers/wordpress.py
Browse files- routers/wordpress.py +36 -8
routers/wordpress.py
CHANGED
|
@@ -21,13 +21,37 @@ class WPPost(BaseModel):
|
|
| 21 |
content: str
|
| 22 |
subhead: Optional[str] = None # 👈 campo ACF mapeado para "subtitulo"
|
| 23 |
image_url: Optional[str] = None
|
|
|
|
| 24 |
status: str = "publish"
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
@router.post("/wordpress/post")
|
| 27 |
async def create_wordpress_post(post: WPPost):
|
| 28 |
async with httpx.AsyncClient(timeout=20.0) as client:
|
| 29 |
featured_media_id = None
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Upload de imagem se fornecida
|
| 32 |
if post.image_url:
|
| 33 |
try:
|
|
@@ -48,27 +72,31 @@ async def create_wordpress_post(post: WPPost):
|
|
| 48 |
featured_media_id = upload.json().get("id")
|
| 49 |
except Exception:
|
| 50 |
pass # Ignora erro de imagem para não quebrar o post
|
| 51 |
-
|
| 52 |
# Dados do post
|
| 53 |
post_data = {
|
| 54 |
"title": post.title,
|
| 55 |
"content": post.content,
|
| 56 |
"status": post.status
|
| 57 |
}
|
| 58 |
-
|
| 59 |
if featured_media_id:
|
| 60 |
post_data["featured_media"] = featured_media_id
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Campo ACF: subhead → subtitulo
|
| 63 |
if post.subhead:
|
| 64 |
post_data["subtitulo"] = post.subhead
|
| 65 |
-
|
| 66 |
# Criar post
|
| 67 |
response = await client.post(f"{WP_URL}/posts", auth=AUTH, json=post_data)
|
| 68 |
-
|
| 69 |
if response.status_code not in [200, 201]:
|
| 70 |
raise HTTPException(status_code=response.status_code, detail=response.text)
|
| 71 |
-
|
| 72 |
result = response.json()
|
| 73 |
|
| 74 |
# 🔄 Fallback: Se ACF não foi salvo, tentar novamente
|
|
@@ -79,5 +107,5 @@ async def create_wordpress_post(post: WPPost):
|
|
| 79 |
update_resp = await client.post(f"{WP_URL}/posts/{post_id}", auth=AUTH, json=acf_data)
|
| 80 |
if update_resp.status_code in [200, 201]:
|
| 81 |
result = update_resp.json()
|
| 82 |
-
|
| 83 |
return result
|
|
|
|
| 21 |
content: str
|
| 22 |
subhead: Optional[str] = None # 👈 campo ACF mapeado para "subtitulo"
|
| 23 |
image_url: Optional[str] = None
|
| 24 |
+
author_username: Optional[str] = None # 👈 novo campo para o username do author
|
| 25 |
status: str = "publish"
|
| 26 |
|
| 27 |
+
async def get_user_id_by_username(client: httpx.AsyncClient, username: str) -> Optional[int]:
|
| 28 |
+
"""Busca o ID do usuário pelo username"""
|
| 29 |
+
try:
|
| 30 |
+
response = await client.get(f"{WP_URL}/users?search={username}", auth=AUTH)
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
users = response.json()
|
| 33 |
+
for user in users:
|
| 34 |
+
if user.get("slug") == username or user.get("name") == username:
|
| 35 |
+
return user.get("id")
|
| 36 |
+
return None
|
| 37 |
+
except Exception:
|
| 38 |
+
return None
|
| 39 |
+
|
| 40 |
@router.post("/wordpress/post")
|
| 41 |
async def create_wordpress_post(post: WPPost):
|
| 42 |
async with httpx.AsyncClient(timeout=20.0) as client:
|
| 43 |
featured_media_id = None
|
| 44 |
+
author_id = None
|
| 45 |
+
|
| 46 |
+
# 👤 Buscar ID do author se username fornecido
|
| 47 |
+
if post.author_username:
|
| 48 |
+
author_id = await get_user_id_by_username(client, post.author_username)
|
| 49 |
+
if not author_id:
|
| 50 |
+
raise HTTPException(
|
| 51 |
+
status_code=404,
|
| 52 |
+
detail=f"Usuário '{post.author_username}' não encontrado"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
# Upload de imagem se fornecida
|
| 56 |
if post.image_url:
|
| 57 |
try:
|
|
|
|
| 72 |
featured_media_id = upload.json().get("id")
|
| 73 |
except Exception:
|
| 74 |
pass # Ignora erro de imagem para não quebrar o post
|
| 75 |
+
|
| 76 |
# Dados do post
|
| 77 |
post_data = {
|
| 78 |
"title": post.title,
|
| 79 |
"content": post.content,
|
| 80 |
"status": post.status
|
| 81 |
}
|
| 82 |
+
|
| 83 |
if featured_media_id:
|
| 84 |
post_data["featured_media"] = featured_media_id
|
| 85 |
+
|
| 86 |
+
# 👤 Adicionar author se encontrado
|
| 87 |
+
if author_id:
|
| 88 |
+
post_data["author"] = author_id
|
| 89 |
+
|
| 90 |
# Campo ACF: subhead → subtitulo
|
| 91 |
if post.subhead:
|
| 92 |
post_data["subtitulo"] = post.subhead
|
| 93 |
+
|
| 94 |
# Criar post
|
| 95 |
response = await client.post(f"{WP_URL}/posts", auth=AUTH, json=post_data)
|
| 96 |
+
|
| 97 |
if response.status_code not in [200, 201]:
|
| 98 |
raise HTTPException(status_code=response.status_code, detail=response.text)
|
| 99 |
+
|
| 100 |
result = response.json()
|
| 101 |
|
| 102 |
# 🔄 Fallback: Se ACF não foi salvo, tentar novamente
|
|
|
|
| 107 |
update_resp = await client.post(f"{WP_URL}/posts/{post_id}", auth=AUTH, json=acf_data)
|
| 108 |
if update_resp.status_code in [200, 201]:
|
| 109 |
result = update_resp.json()
|
| 110 |
+
|
| 111 |
return result
|