WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-content.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-content.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-content.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Release v0.2.0 - Pages support 25b7e5e 1 year, 1 month ago History
content.py · 13.5 KB · 478 lines
1"""
2ContentDB: WordPress-like API for block-based JSON content
3
4Provides familiar WordPress methods (get_post, get_posts) but backed by
5filesystem JSON files instead of MySQL.
6"""
7
8import json
9import os
10from pathlib import Path
11from typing import Dict, List, Optional, Any
12from datetime import datetime
13
14
15class ContentDB:
16 """
17 Content database backed by JSON files on disk.
18
19 Unix Gateway Pattern: Thin layer over filesystem primitives.
20
21 Storage structure:
22 /content/
23 articles/
24 hello-world.json
25 about.json
26 metadata.tsv
27 taxonomy.tsv
28 """
29
30 def __init__(self, content_dir: str):
31 """
32 Initialize ContentDB.
33
34 Args:
35 content_dir: Path to content directory
36 """
37 self.content_dir = Path(content_dir)
38 self.articles_dir = self.content_dir / "articles"
39 self.posts_dir = self.content_dir / "posts"
40 self.pages_dir = self.content_dir / "pages"
41
42 # Create directories if they don't exist
43 # Keep articles_dir for backward compatibility
44 self.articles_dir.mkdir(parents=True, exist_ok=True)
45 self.posts_dir.mkdir(parents=True, exist_ok=True)
46 self.pages_dir.mkdir(parents=True, exist_ok=True)
47
48 def _get_content_dir(self, content_type: str) -> Path:
49 """
50 Get directory for content type.
51
52 Args:
53 content_type: "post", "page", or "article"
54
55 Returns:
56 Path to content directory
57 """
58 if content_type == "post":
59 return self.posts_dir
60 elif content_type == "page":
61 return self.pages_dir
62 else:
63 # Default to articles_dir for backward compatibility
64 return self.articles_dir
65
66 def get_post(self, slug: str, content_type: str = "article") -> Optional[Dict[str, Any]]:
67 """
68 Get a single post by slug (like WordPress get_post).
69
70 Args:
71 slug: Post slug
72 content_type: "post", "page", or "article" (default: "article" for backward compat)
73
74 Returns:
75 Post data dict or None if not found
76 """
77 content_dir = self._get_content_dir(content_type)
78 article_path = content_dir / f"{slug}.json"
79
80 if not article_path.exists():
81 return None
82
83 with open(article_path, 'r', encoding='utf-8') as f:
84 return json.load(f)
85
86 def get_posts(
87 self,
88 content_type: str = "article",
89 status: Optional[str] = None,
90 categories: Optional[List[str]] = None,
91 tags: Optional[List[str]] = None,
92 author: Optional[str] = None,
93 limit: Optional[int] = None,
94 offset: int = 0,
95 order_by: str = 'date',
96 order: str = 'desc'
97 ) -> List[Dict[str, Any]]:
98 """
99 Get multiple posts with filtering (like WordPress WP_Query).
100
101 Args:
102 content_type: "post", "page", or "article" (default: "article" for backward compat)
103 status: Filter by status (published, draft, etc.)
104 categories: Filter by categories
105 tags: Filter by tags
106 author: Filter by author
107 limit: Maximum number of posts to return
108 offset: Number of posts to skip
109 order_by: Field to order by (date, title, etc.)
110 order: Order direction (asc or desc)
111
112 Returns:
113 List of post dicts
114 """
115 posts = []
116 content_dir = self._get_content_dir(content_type)
117
118 # Read all JSON files from content directory
119 for article_path in content_dir.glob("*.json"):
120 try:
121 with open(article_path, 'r', encoding='utf-8') as f:
122 post = json.load(f)
123 posts.append(post)
124 except (json.JSONDecodeError, IOError):
125 # Skip invalid files
126 continue
127
128 # Filter by status
129 if status:
130 posts = [p for p in posts if p.get('status') == status]
131
132 # Filter by categories
133 if categories:
134 posts = [
135 p for p in posts
136 if any(cat in p.get('categories', []) for cat in categories)
137 ]
138
139 # Filter by tags
140 if tags:
141 posts = [
142 p for p in posts
143 if any(tag in p.get('tags', []) for tag in tags)
144 ]
145
146 # Filter by author
147 if author:
148 posts = [p for p in posts if p.get('author') == author]
149
150 # Sort posts
151 reverse = (order == 'desc')
152 try:
153 posts.sort(key=lambda p: p.get(order_by, ''), reverse=reverse)
154 except TypeError:
155 # If comparison fails, skip sorting
156 pass
157
158 # Apply offset and limit
159 if offset:
160 posts = posts[offset:]
161 if limit:
162 posts = posts[:limit]
163
164 return posts
165
166 def create_post(
167 self,
168 slug: str,
169 title: str,
170 blocks: List[Dict[str, Any]],
171 content_type: str = "article",
172 author: str = "admin",
173 date: Optional[str] = None,
174 status: str = "published",
175 categories: Optional[List[str]] = None,
176 tags: Optional[List[str]] = None,
177 description: Optional[str] = None,
178 **kwargs
179 ) -> Dict[str, Any]:
180 """
181 Create a new post (like WordPress wp_insert_post).
182
183 Args:
184 slug: URL slug
185 title: Post title
186 blocks: List of content blocks
187 content_type: "post", "page", or "article" (default: "article" for backward compat)
188 author: Post author
189 date: Publication date (ISO format, defaults to now)
190 status: Post status (published, draft, etc.)
191 categories: List of categories
192 tags: List of tags
193 description: Meta description
194 **kwargs: Additional metadata
195
196 Returns:
197 Created post data
198 """
199 if date is None:
200 date = datetime.now().strftime("%Y-%m-%d")
201
202 post = {
203 "slug": slug,
204 "title": title,
205 "date": date,
206 "author": author,
207 "status": status,
208 "content_type": content_type,
209 "blocks": blocks,
210 }
211
212 if categories:
213 post["categories"] = categories
214 if tags:
215 post["tags"] = tags
216 if description:
217 post["description"] = description
218
219 # Add any additional metadata
220 post.update(kwargs)
221
222 # Write to file
223 content_dir = self._get_content_dir(content_type)
224 article_path = content_dir / f"{slug}.json"
225 with open(article_path, 'w', encoding='utf-8') as f:
226 json.dump(post, f, indent=2, ensure_ascii=False)
227
228 return post
229
230 def update_post(self, slug: str, content_type: str = "article", **updates) -> Optional[Dict[str, Any]]:
231 """
232 Update an existing post (like WordPress wp_update_post).
233
234 Args:
235 slug: Post slug
236 content_type: "post", "page", or "article" (default: "article" for backward compat)
237 **updates: Fields to update
238
239 Returns:
240 Updated post data or None if not found
241 """
242 post = self.get_post(slug, content_type=content_type)
243 if not post:
244 return None
245
246 # Update fields
247 post.update(updates)
248
249 # Write back to file
250 content_dir = self._get_content_dir(content_type)
251 article_path = content_dir / f"{slug}.json"
252 with open(article_path, 'w', encoding='utf-8') as f:
253 json.dump(post, f, indent=2, ensure_ascii=False)
254
255 return post
256
257 def delete_post(self, slug: str, content_type: str = "article") -> bool:
258 """
259 Delete a post (like WordPress wp_delete_post).
260
261 Args:
262 slug: Post slug
263 content_type: "post", "page", or "article" (default: "article" for backward compat)
264
265 Returns:
266 True if deleted, False if not found
267 """
268 content_dir = self._get_content_dir(content_type)
269 article_path = content_dir / f"{slug}.json"
270
271 if not article_path.exists():
272 return False
273
274 article_path.unlink()
275 return True
276
277 def post_exists(self, slug: str, content_type: str = "article") -> bool:
278 """
279 Check if a post exists.
280
281 Args:
282 slug: Post slug
283 content_type: "post", "page", or "article" (default: "article" for backward compat)
284
285 Returns:
286 True if exists, False otherwise
287 """
288 content_dir = self._get_content_dir(content_type)
289 article_path = content_dir / f"{slug}.json"
290 return article_path.exists()
291
292 def get_categories(self) -> List[str]:
293 """
294 Get all unique categories across all posts.
295
296 Returns:
297 List of category names
298 """
299 categories = set()
300 for post in self.get_posts():
301 categories.update(post.get('categories', []))
302 return sorted(list(categories))
303
304 def get_tags(self) -> List[str]:
305 """
306 Get all unique tags across all posts.
307
308 Returns:
309 List of tag names
310 """
311 tags = set()
312 for post in self.get_posts():
313 tags.update(post.get('tags', []))
314 return sorted(list(tags))
315
316 def get_authors(self) -> List[str]:
317 """
318 Get all unique authors across all posts.
319
320 Returns:
321 List of author names
322 """
323 authors = set()
324 for post in self.get_posts():
325 if 'author' in post:
326 authors.add(post['author'])
327 return sorted(list(authors))
328
329 def search(self, query: str) -> List[Dict[str, Any]]:
330 """
331 Simple text search across posts (title, description, block content).
332
333 Args:
334 query: Search query string
335
336 Returns:
337 List of matching posts
338 """
339 query_lower = query.lower()
340 results = []
341
342 for post in self.get_posts():
343 # Check title
344 if query_lower in post.get('title', '').lower():
345 results.append(post)
346 continue
347
348 # Check description
349 if query_lower in post.get('description', '').lower():
350 results.append(post)
351 continue
352
353 # Check block content
354 for block in post.get('blocks', []):
355 block_data = json.dumps(block.get('data', {})).lower()
356 if query_lower in block_data:
357 results.append(post)
358 break
359
360 return results
361
362 # Convenience methods for pages
363
364 def create_page(
365 self,
366 slug: str,
367 title: str,
368 blocks: List[Dict[str, Any]],
369 author: str = "admin",
370 status: str = "published",
371 description: Optional[str] = None,
372 **kwargs
373 ) -> Dict[str, Any]:
374 """
375 Create a new page (static content, no date/categories).
376
377 Args:
378 slug: URL slug
379 title: Page title
380 blocks: List of content blocks
381 author: Page author
382 status: Page status (published, draft, etc.)
383 description: Meta description
384 **kwargs: Additional metadata
385
386 Returns:
387 Created page data
388 """
389 return self.create_post(
390 slug=slug,
391 title=title,
392 blocks=blocks,
393 content_type="page",
394 author=author,
395 date=None, # Pages don't have dates
396 status=status,
397 description=description,
398 **kwargs
399 )
400
401 def get_page(self, slug: str) -> Optional[Dict[str, Any]]:
402 """
403 Get a single page by slug.
404
405 Args:
406 slug: Page slug
407
408 Returns:
409 Page data dict or None if not found
410 """
411 return self.get_post(slug, content_type="page")
412
413 def get_pages(
414 self,
415 status: Optional[str] = None,
416 limit: Optional[int] = None,
417 offset: int = 0,
418 order_by: str = 'title',
419 order: str = 'asc'
420 ) -> List[Dict[str, Any]]:
421 """
422 Get multiple pages with filtering.
423
424 Args:
425 status: Filter by status (published, draft, etc.)
426 limit: Maximum number of pages to return
427 offset: Number of pages to skip
428 order_by: Field to order by (title by default for pages)
429 order: Order direction (asc or desc)
430
431 Returns:
432 List of page dicts
433 """
434 return self.get_posts(
435 content_type="page",
436 status=status,
437 limit=limit,
438 offset=offset,
439 order_by=order_by,
440 order=order
441 )
442
443 def update_page(self, slug: str, **updates) -> Optional[Dict[str, Any]]:
444 """
445 Update an existing page.
446
447 Args:
448 slug: Page slug
449 **updates: Fields to update
450
451 Returns:
452 Updated page data or None if not found
453 """
454 return self.update_post(slug, content_type="page", **updates)
455
456 def delete_page(self, slug: str) -> bool:
457 """
458 Delete a page.
459
460 Args:
461 slug: Page slug
462
463 Returns:
464 True if deleted, False if not found
465 """
466 return self.delete_post(slug, content_type="page")
467
468 def page_exists(self, slug: str) -> bool:
469 """
470 Check if a page exists.
471
472 Args:
473 slug: Page slug
474
475 Returns:
476 True if exists, False otherwise
477 """
478 return self.post_exists(slug, content_type="page")