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

0 downloads

dbbasic-content/examples/pages_example.py
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
pages_example.py · 5.3 KB · 155 lines
1#!/usr/bin/env python3
2"""
3Example demonstrating posts vs pages in dbbasic-content.
4
5This shows how to use dbbasic-content for a site with both:
6- Blog posts (dated, categorized)
7- Static pages (documentation, about, etc.)
8"""
9
10import tempfile
11from pathlib import Path
12from dbbasic_content import ContentDB
13
14
15def main():
16 # Create a temporary content directory for this example
17 with tempfile.TemporaryDirectory() as tmpdir:
18 content = ContentDB(tmpdir)
19
20 print("=" * 60)
21 print("dbbasic-content: Posts vs Pages Example")
22 print("=" * 60)
23 print()
24
25 # Create some blog posts
26 print("Creating blog posts...")
27 content.create_post(
28 slug='announcing-v1',
29 title='Announcing Version 1.0',
30 content_type='post',
31 date='2025-10-20',
32 author='admin',
33 categories=['Announcements', 'Releases'],
34 tags=['v1', 'release'],
35 blocks=[
36 {'type': 'heading', 'data': {'level': 1, 'content': 'Announcing Version 1.0'}},
37 {'type': 'paragraph', 'data': {'content': 'We are excited to announce version 1.0!'}},
38 ]
39 )
40
41 content.create_post(
42 slug='getting-started',
43 title='Getting Started with DBBasic',
44 content_type='post',
45 date='2025-10-21',
46 author='admin',
47 categories=['Tutorials'],
48 tags=['tutorial', 'beginner'],
49 blocks=[
50 {'type': 'heading', 'data': {'level': 1, 'content': 'Getting Started'}},
51 {'type': 'paragraph', 'data': {'content': 'This tutorial will help you get started...'}},
52 ]
53 )
54
55 # Create some static pages
56 print("Creating static pages...")
57 content.create_page(
58 slug='about',
59 title='About DBBasic',
60 blocks=[
61 {'type': 'heading', 'data': {'level': 1, 'content': 'About DBBasic'}},
62 {'type': 'paragraph', 'data': {'content': 'DBBasic is a Unix-foundation content management system.'}},
63 ]
64 )
65
66 content.create_page(
67 slug='api-docs',
68 title='API Documentation',
69 blocks=[
70 {'type': 'heading', 'data': {'level': 1, 'content': 'API Documentation'}},
71 {'type': 'paragraph', 'data': {'content': 'Complete API reference for DBBasic.'}},
72 {'type': 'code', 'data': {'code': 'from dbbasic_content import ContentDB', 'language': 'python'}},
73 ]
74 )
75
76 content.create_page(
77 slug='contact',
78 title='Contact Us',
79 blocks=[
80 {'type': 'heading', 'data': {'level': 1, 'content': 'Contact Us'}},
81 {'type': 'paragraph', 'data': {'content': 'Get in touch: hello@example.com'}},
82 ]
83 )
84
85 print()
86 print("-" * 60)
87 print("Directory Structure:")
88 print("-" * 60)
89
90 # Show directory structure
91 for subdir in ['posts', 'pages', 'articles']:
92 dir_path = Path(tmpdir) / subdir
93 if dir_path.exists():
94 files = list(dir_path.glob('*.json'))
95 print(f"{subdir}/")
96 for f in files:
97 print(f" - {f.name}")
98
99 print()
100 print("-" * 60)
101 print("Blog Posts (chronological):")
102 print("-" * 60)
103
104 # Get all posts
105 posts = content.get_posts(content_type='post', order_by='date', order='desc')
106 for post in posts:
107 print(f" [{post['date']}] {post['title']}")
108 print(f" Categories: {', '.join(post.get('categories', []))}")
109 print(f" Tags: {', '.join(post.get('tags', []))}")
110 print()
111
112 print("-" * 60)
113 print("Static Pages (alphabetical):")
114 print("-" * 60)
115
116 # Get all pages
117 pages = content.get_pages(order_by='title', order='asc')
118 for page in pages:
119 print(f" {page['title']}")
120 print(f" Slug: {page['slug']}")
121 print(f" Type: {page['content_type']}")
122 print()
123
124 print("-" * 60)
125 print("Example Queries:")
126 print("-" * 60)
127
128 # Query examples
129 print("\n1. Get posts in 'Tutorials' category:")
130 tutorial_posts = content.get_posts(content_type='post', categories=['Tutorials'])
131 for post in tutorial_posts:
132 print(f" - {post['title']}")
133
134 print("\n2. Get specific page:")
135 about = content.get_page('about')
136 print(f" Title: {about['title']}")
137 print(f" Type: {about['content_type']}")
138
139 print("\n3. Check if content exists:")
140 print(f" Post 'announcing-v1' exists: {content.post_exists('announcing-v1', content_type='post')}")
141 print(f" Page 'about' exists: {content.page_exists('about')}")
142 print(f" Page 'nonexistent' exists: {content.page_exists('nonexistent')}")
143
144 print()
145 print("=" * 60)
146 print("Key Differences:")
147 print("=" * 60)
148 print("Posts: Have dates, categories, tags - for blog content")
149 print("Pages: No dates/categories/tags - for static content")
150 print("Both: Use the same block-based format")
151 print("=" * 60)
152
153
154if __name__ == '__main__':
155 main()