askrobots / dbbasic-content
Public
WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.
basic_usage.py · 1.9 KB · 83 lines
| 1 | """ |
| 2 | Basic usage examples for dbbasic-content. |
| 3 | """ |
| 4 | |
| 5 | from dbbasic_content import ContentDB |
| 6 | |
| 7 | # Initialize content database |
| 8 | content = ContentDB('/tmp/example_content') |
| 9 | |
| 10 | # Create a post |
| 11 | post = content.create_post( |
| 12 | slug='hello-world', |
| 13 | title='Hello World', |
| 14 | author='john', |
| 15 | categories=['Technology', 'Python'], |
| 16 | tags=['tutorial', 'beginner'], |
| 17 | blocks=[ |
| 18 | { |
| 19 | 'type': 'paragraph', |
| 20 | 'data': { |
| 21 | 'content': 'Welcome to my blog! This is my first post.' |
| 22 | } |
| 23 | }, |
| 24 | { |
| 25 | 'type': 'heading', |
| 26 | 'data': { |
| 27 | 'level': 2, |
| 28 | 'content': 'Getting Started' |
| 29 | } |
| 30 | }, |
| 31 | { |
| 32 | 'type': 'paragraph', |
| 33 | 'data': { |
| 34 | 'content': 'Here are some things you should know...' |
| 35 | } |
| 36 | }, |
| 37 | { |
| 38 | 'type': 'list', |
| 39 | 'data': { |
| 40 | 'listType': 'unordered', |
| 41 | 'items': [ |
| 42 | 'Item one', |
| 43 | 'Item two', |
| 44 | 'Item three' |
| 45 | ] |
| 46 | } |
| 47 | } |
| 48 | ] |
| 49 | ) |
| 50 | |
| 51 | print(f"Created post: {post['slug']}") |
| 52 | |
| 53 | # Get a post |
| 54 | retrieved_post = content.get_post('hello-world') |
| 55 | print(f"Retrieved: {retrieved_post['title']}") |
| 56 | |
| 57 | # Get all published posts |
| 58 | posts = content.get_posts(status='published', limit=10) |
| 59 | print(f"Found {len(posts)} published posts") |
| 60 | |
| 61 | # Get posts by category |
| 62 | tech_posts = content.get_posts(categories=['Technology']) |
| 63 | print(f"Found {len(tech_posts)} tech posts") |
| 64 | |
| 65 | # Search posts |
| 66 | results = content.search('Python') |
| 67 | print(f"Search found {len(results)} results") |
| 68 | |
| 69 | # Update a post |
| 70 | updated = content.update_post( |
| 71 | 'hello-world', |
| 72 | title='Hello World - Updated!', |
| 73 | status='draft' |
| 74 | ) |
| 75 | print(f"Updated post: {updated['title']}") |
| 76 | |
| 77 | # Get all categories |
| 78 | categories = content.get_categories() |
| 79 | print(f"Categories: {', '.join(categories)}") |
| 80 | |
| 81 | # Get all tags |
| 82 | tags = content.get_tags() |
| 83 | print(f"Tags: {', '.join(tags)}") |