posts will be published immediately
This commit is contained in:
45
src/apply.py
45
src/apply.py
@@ -1,7 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import Dict, List, Set
|
||||
from datetime import datetime
|
||||
from typing import Dict, List, Optional, Set
|
||||
|
||||
from .errors import WordPressError
|
||||
from .models import EvaluationResult, PostPlan
|
||||
@@ -87,6 +88,8 @@ def _apply_post(post: PostPlan, wp: WordPressCLI, category_map: Dict[tuple[int,
|
||||
parent = category_map[map_key]
|
||||
category_ids.append(parent)
|
||||
|
||||
created_on, last_modified = _normalize_post_dates(post.created_on, post.last_modified)
|
||||
|
||||
post_id = wp.find_post_id(post.identity)
|
||||
if post_id is None:
|
||||
wp.create_post(
|
||||
@@ -95,8 +98,8 @@ def _apply_post(post: PostPlan, wp: WordPressCLI, category_map: Dict[tuple[int,
|
||||
categories=category_ids,
|
||||
tags=post.tags,
|
||||
source_identity=post.identity,
|
||||
created_on=post.created_on,
|
||||
last_modified=post.last_modified,
|
||||
created_on=created_on,
|
||||
last_modified=last_modified,
|
||||
author=post.author,
|
||||
)
|
||||
return
|
||||
@@ -107,7 +110,39 @@ def _apply_post(post: PostPlan, wp: WordPressCLI, category_map: Dict[tuple[int,
|
||||
content=post.html,
|
||||
categories=category_ids,
|
||||
tags=post.tags,
|
||||
created_on=post.created_on,
|
||||
last_modified=post.last_modified,
|
||||
created_on=created_on,
|
||||
last_modified=last_modified,
|
||||
author=post.author,
|
||||
)
|
||||
|
||||
|
||||
def _normalize_post_dates(
|
||||
created_on: Optional[str],
|
||||
last_modified: Optional[str],
|
||||
) -> tuple[Optional[str], Optional[str]]:
|
||||
if not created_on and not last_modified:
|
||||
return created_on, last_modified
|
||||
|
||||
now = datetime.now()
|
||||
created_dt = _parse_post_date(created_on)
|
||||
modified_dt = _parse_post_date(last_modified)
|
||||
|
||||
if created_dt and created_dt > now:
|
||||
created_dt = now
|
||||
if modified_dt and modified_dt > now:
|
||||
modified_dt = now
|
||||
if created_dt and modified_dt and modified_dt < created_dt:
|
||||
modified_dt = created_dt
|
||||
|
||||
created_str = created_dt.strftime("%Y-%m-%d %H:%M:%S") if created_dt else None
|
||||
modified_str = modified_dt.strftime("%Y-%m-%d %H:%M:%S") if modified_dt else None
|
||||
return created_str, modified_str
|
||||
|
||||
|
||||
def _parse_post_date(value: Optional[str]) -> Optional[datetime]:
|
||||
if not value:
|
||||
return None
|
||||
try:
|
||||
return datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user