blog-package 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- blog_package-0.1.0/LICENSE +21 -0
- blog_package-0.1.0/PKG-INFO +153 -0
- blog_package-0.1.0/README.md +128 -0
- blog_package-0.1.0/blog/__init__.py +1 -0
- blog_package-0.1.0/blog/admin.py +47 -0
- blog_package-0.1.0/blog/api.py +130 -0
- blog_package-0.1.0/blog/apps.py +8 -0
- blog_package-0.1.0/blog/migrations/0001_initial.py +113 -0
- blog_package-0.1.0/blog/migrations/__init__.py +0 -0
- blog_package-0.1.0/blog/models.py +97 -0
- blog_package-0.1.0/blog/schemas.py +102 -0
- blog_package-0.1.0/blog/services.py +211 -0
- blog_package-0.1.0/blog/tests/__init__.py +0 -0
- blog_package-0.1.0/blog/tests/test_api.py +198 -0
- blog_package-0.1.0/blog_package.egg-info/PKG-INFO +153 -0
- blog_package-0.1.0/blog_package.egg-info/SOURCES.txt +19 -0
- blog_package-0.1.0/blog_package.egg-info/dependency_links.txt +1 -0
- blog_package-0.1.0/blog_package.egg-info/requires.txt +9 -0
- blog_package-0.1.0/blog_package.egg-info/top_level.txt +1 -0
- blog_package-0.1.0/pyproject.toml +59 -0
- blog_package-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 puzzllium
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: blog-package
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Blog site package for the Core boilerplate (posts with a gallery, comments, likes, saves, share tracking). Depends on core-package for auth, RBAC, and the shared User model — it does not define its own.
|
|
5
|
+
Author-email: puzzllium <puzzllium@gmail.com>
|
|
6
|
+
Maintainer-email: puzzllium <puzzllium@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/puzzelchannel/package-blog
|
|
9
|
+
Project-URL: Issues, https://github.com/puzzelchannel/package-blog/issues
|
|
10
|
+
Classifier: Framework :: Django
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: core-package
|
|
17
|
+
Requires-Dist: django-ninja>=1.7.0
|
|
18
|
+
Requires-Dist: django-unfold>=0.81.0
|
|
19
|
+
Requires-Dist: Pillow>=10.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest-django>=4.14; extra == "dev"
|
|
23
|
+
Requires-Dist: pytest-asyncio>=1.4; extra == "dev"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# blog-package
|
|
27
|
+
|
|
28
|
+
**Author:** puzzllium · [puzzllium@gmail.com](mailto:puzzllium@gmail.com) · [LinkedIn](https://www.linkedin.com/in/puzzllium)
|
|
29
|
+
|
|
30
|
+
Blog site package built on [core-package](https://pypi.org/project/core-package/):
|
|
31
|
+
posts with a cover picture and image gallery, comments, likes, saves (bookmarks)
|
|
32
|
+
and share-count tracking. It never defines its own user model: every post,
|
|
33
|
+
comment, like and save is owned by a core-package `User` via
|
|
34
|
+
`settings.AUTH_USER_MODEL`.
|
|
35
|
+
|
|
36
|
+
`Post.subject`/`description`/`text` are translatable through a single
|
|
37
|
+
`translations` JSON field (`{"fa": {"subject": "..."}, "de": {...}}`), so adding a
|
|
38
|
+
language needs no migration: `PUT /posts/{id}/translations/{language}`.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install blog-package
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Requires Python 3.10+ and Django 5.2 (via core-package).
|
|
47
|
+
|
|
48
|
+
## What's inside
|
|
49
|
+
|
|
50
|
+
The package ships the reusable `blog` Django app:
|
|
51
|
+
|
|
52
|
+
| Module | Purpose |
|
|
53
|
+
| --- | --- |
|
|
54
|
+
| `blog.models` | Post, gallery image, comment, like, save |
|
|
55
|
+
| `blog.api` | Django Ninja router (public reads, logged-in interactions, admin-only writes) |
|
|
56
|
+
| `blog.schemas` / `blog.services` | Request/response schemas and business logic |
|
|
57
|
+
| `blog.admin` | Django admin (Unfold) incl. cover and gallery image management |
|
|
58
|
+
|
|
59
|
+
`testproject/` is dev-only scaffolding for this repo's own tests and is **not** part of the installable package.
|
|
60
|
+
|
|
61
|
+
## Dependencies
|
|
62
|
+
|
|
63
|
+
**Required**
|
|
64
|
+
|
|
65
|
+
| Package | Used for |
|
|
66
|
+
| --- | --- |
|
|
67
|
+
| core-package | Auth, RBAC, the shared `User` model, base/translatable models, pagination, throttling |
|
|
68
|
+
| django-ninja `>=1.7.0` | REST API layer |
|
|
69
|
+
| django-unfold `>=0.81.0` | Admin theme |
|
|
70
|
+
| Pillow `>=10.0` | Post cover and gallery images |
|
|
71
|
+
|
|
72
|
+
core-package in turn brings in Django 5.2 and the rest of the stack; see its README.
|
|
73
|
+
|
|
74
|
+
**Optional extras**
|
|
75
|
+
|
|
76
|
+
| Extra | Installs |
|
|
77
|
+
| --- | --- |
|
|
78
|
+
| `dev` | `pytest>=8`, `pytest-django>=4.14`, `pytest-asyncio>=1.4` |
|
|
79
|
+
|
|
80
|
+
## Local development
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
python -m venv .venv
|
|
84
|
+
source .venv/bin/activate
|
|
85
|
+
pip install -e /path/to/Core/Core # core-package, editable
|
|
86
|
+
pip install -e ".[dev]"
|
|
87
|
+
pytest
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Installing into a Core-based project
|
|
91
|
+
|
|
92
|
+
1. Install it editable into the host repo's venv:
|
|
93
|
+
```bash
|
|
94
|
+
source .venv/bin/activate
|
|
95
|
+
pip install -e ../Packages/blog
|
|
96
|
+
```
|
|
97
|
+
2. Add it to `INSTALLED_APPS` in the host's `config/settings/base.py`, under
|
|
98
|
+
the `# site packages` comment:
|
|
99
|
+
```python
|
|
100
|
+
# site packages
|
|
101
|
+
"blog",
|
|
102
|
+
```
|
|
103
|
+
3. Mount its router in the host's `config/api.py`:
|
|
104
|
+
```python
|
|
105
|
+
from blog.api import router as blog_router
|
|
106
|
+
...
|
|
107
|
+
api.add_router("/blog", blog_router)
|
|
108
|
+
```
|
|
109
|
+
4. Migrate:
|
|
110
|
+
```bash
|
|
111
|
+
python manage.py migrate
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Installed automatically alongside `portfolio`**: `portfolio` declares
|
|
115
|
+
`blog-package` as a pip dependency (its site includes a blog section), so
|
|
116
|
+
`pip install -e ../Packages/portfolio` pulls this package in for you too —
|
|
117
|
+
but per Core's "no auto-discovery, explicit wiring" convention (§10), step 2
|
|
118
|
+
(`INSTALLED_APPS`) and step 3 (router mount) above still need doing by hand
|
|
119
|
+
for `blog`, exactly like for `portfolio` itself. The pip dependency only
|
|
120
|
+
saves you the `pip install` step, not the Django-level wiring.
|
|
121
|
+
|
|
122
|
+
## Endpoints
|
|
123
|
+
|
|
124
|
+
Mounted at whatever prefix the host chooses (`/blog` in the example above).
|
|
125
|
+
Reads are public; interactions require login; content writes are admin-only.
|
|
126
|
+
|
|
127
|
+
- `GET /posts` — paginated list of published posts (public), optional
|
|
128
|
+
`?user_id=` and `?lang=` (returns the post's translated `subject`/
|
|
129
|
+
`description`/`text` for that language if a translation exists, else the
|
|
130
|
+
original)
|
|
131
|
+
- `GET /posts/by-slug/{slug}` — published post detail (public), optional
|
|
132
|
+
`?lang=`
|
|
133
|
+
- `GET /posts/{post_id}/comments` — paginated comments on a post (public)
|
|
134
|
+
- `POST /posts/{post_id}/comments` — add a comment (logged in)
|
|
135
|
+
- `DELETE /comments/{comment_id}` — delete a comment (its author, or admin)
|
|
136
|
+
- `POST /posts/{post_id}/like` — toggle like on/off (logged in), returns the
|
|
137
|
+
new state + like count
|
|
138
|
+
- `POST /posts/{post_id}/save` — toggle save/bookmark on/off (logged in)
|
|
139
|
+
- `POST /posts/{post_id}/share` — public, rate-limited by IP; increments and
|
|
140
|
+
returns the post's share count (no account needed to share)
|
|
141
|
+
- `POST /posts`, `GET /posts/{post_id}`, `PATCH /posts/{post_id}`,
|
|
142
|
+
`DELETE /posts/{post_id}` — admin-only (`require_role("admin")`, from Core)
|
|
143
|
+
- `PUT /posts/{post_id}/translations/{language}` — admin-only; sets/updates
|
|
144
|
+
the translated `subject`/`description`/`text` for that language
|
|
145
|
+
|
|
146
|
+
Post pictures and gallery images are managed through the Django admin (same
|
|
147
|
+
convention as the portfolio package's `Project.cover_image`/`ProjectImage` —
|
|
148
|
+
no write API for images yet, deliberately, to avoid building endpoints
|
|
149
|
+
nothing needs yet).
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# blog-package
|
|
2
|
+
|
|
3
|
+
**Author:** puzzllium · [puzzllium@gmail.com](mailto:puzzllium@gmail.com) · [LinkedIn](https://www.linkedin.com/in/puzzllium)
|
|
4
|
+
|
|
5
|
+
Blog site package built on [core-package](https://pypi.org/project/core-package/):
|
|
6
|
+
posts with a cover picture and image gallery, comments, likes, saves (bookmarks)
|
|
7
|
+
and share-count tracking. It never defines its own user model: every post,
|
|
8
|
+
comment, like and save is owned by a core-package `User` via
|
|
9
|
+
`settings.AUTH_USER_MODEL`.
|
|
10
|
+
|
|
11
|
+
`Post.subject`/`description`/`text` are translatable through a single
|
|
12
|
+
`translations` JSON field (`{"fa": {"subject": "..."}, "de": {...}}`), so adding a
|
|
13
|
+
language needs no migration: `PUT /posts/{id}/translations/{language}`.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install blog-package
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires Python 3.10+ and Django 5.2 (via core-package).
|
|
22
|
+
|
|
23
|
+
## What's inside
|
|
24
|
+
|
|
25
|
+
The package ships the reusable `blog` Django app:
|
|
26
|
+
|
|
27
|
+
| Module | Purpose |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| `blog.models` | Post, gallery image, comment, like, save |
|
|
30
|
+
| `blog.api` | Django Ninja router (public reads, logged-in interactions, admin-only writes) |
|
|
31
|
+
| `blog.schemas` / `blog.services` | Request/response schemas and business logic |
|
|
32
|
+
| `blog.admin` | Django admin (Unfold) incl. cover and gallery image management |
|
|
33
|
+
|
|
34
|
+
`testproject/` is dev-only scaffolding for this repo's own tests and is **not** part of the installable package.
|
|
35
|
+
|
|
36
|
+
## Dependencies
|
|
37
|
+
|
|
38
|
+
**Required**
|
|
39
|
+
|
|
40
|
+
| Package | Used for |
|
|
41
|
+
| --- | --- |
|
|
42
|
+
| core-package | Auth, RBAC, the shared `User` model, base/translatable models, pagination, throttling |
|
|
43
|
+
| django-ninja `>=1.7.0` | REST API layer |
|
|
44
|
+
| django-unfold `>=0.81.0` | Admin theme |
|
|
45
|
+
| Pillow `>=10.0` | Post cover and gallery images |
|
|
46
|
+
|
|
47
|
+
core-package in turn brings in Django 5.2 and the rest of the stack; see its README.
|
|
48
|
+
|
|
49
|
+
**Optional extras**
|
|
50
|
+
|
|
51
|
+
| Extra | Installs |
|
|
52
|
+
| --- | --- |
|
|
53
|
+
| `dev` | `pytest>=8`, `pytest-django>=4.14`, `pytest-asyncio>=1.4` |
|
|
54
|
+
|
|
55
|
+
## Local development
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
python -m venv .venv
|
|
59
|
+
source .venv/bin/activate
|
|
60
|
+
pip install -e /path/to/Core/Core # core-package, editable
|
|
61
|
+
pip install -e ".[dev]"
|
|
62
|
+
pytest
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Installing into a Core-based project
|
|
66
|
+
|
|
67
|
+
1. Install it editable into the host repo's venv:
|
|
68
|
+
```bash
|
|
69
|
+
source .venv/bin/activate
|
|
70
|
+
pip install -e ../Packages/blog
|
|
71
|
+
```
|
|
72
|
+
2. Add it to `INSTALLED_APPS` in the host's `config/settings/base.py`, under
|
|
73
|
+
the `# site packages` comment:
|
|
74
|
+
```python
|
|
75
|
+
# site packages
|
|
76
|
+
"blog",
|
|
77
|
+
```
|
|
78
|
+
3. Mount its router in the host's `config/api.py`:
|
|
79
|
+
```python
|
|
80
|
+
from blog.api import router as blog_router
|
|
81
|
+
...
|
|
82
|
+
api.add_router("/blog", blog_router)
|
|
83
|
+
```
|
|
84
|
+
4. Migrate:
|
|
85
|
+
```bash
|
|
86
|
+
python manage.py migrate
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Installed automatically alongside `portfolio`**: `portfolio` declares
|
|
90
|
+
`blog-package` as a pip dependency (its site includes a blog section), so
|
|
91
|
+
`pip install -e ../Packages/portfolio` pulls this package in for you too —
|
|
92
|
+
but per Core's "no auto-discovery, explicit wiring" convention (§10), step 2
|
|
93
|
+
(`INSTALLED_APPS`) and step 3 (router mount) above still need doing by hand
|
|
94
|
+
for `blog`, exactly like for `portfolio` itself. The pip dependency only
|
|
95
|
+
saves you the `pip install` step, not the Django-level wiring.
|
|
96
|
+
|
|
97
|
+
## Endpoints
|
|
98
|
+
|
|
99
|
+
Mounted at whatever prefix the host chooses (`/blog` in the example above).
|
|
100
|
+
Reads are public; interactions require login; content writes are admin-only.
|
|
101
|
+
|
|
102
|
+
- `GET /posts` — paginated list of published posts (public), optional
|
|
103
|
+
`?user_id=` and `?lang=` (returns the post's translated `subject`/
|
|
104
|
+
`description`/`text` for that language if a translation exists, else the
|
|
105
|
+
original)
|
|
106
|
+
- `GET /posts/by-slug/{slug}` — published post detail (public), optional
|
|
107
|
+
`?lang=`
|
|
108
|
+
- `GET /posts/{post_id}/comments` — paginated comments on a post (public)
|
|
109
|
+
- `POST /posts/{post_id}/comments` — add a comment (logged in)
|
|
110
|
+
- `DELETE /comments/{comment_id}` — delete a comment (its author, or admin)
|
|
111
|
+
- `POST /posts/{post_id}/like` — toggle like on/off (logged in), returns the
|
|
112
|
+
new state + like count
|
|
113
|
+
- `POST /posts/{post_id}/save` — toggle save/bookmark on/off (logged in)
|
|
114
|
+
- `POST /posts/{post_id}/share` — public, rate-limited by IP; increments and
|
|
115
|
+
returns the post's share count (no account needed to share)
|
|
116
|
+
- `POST /posts`, `GET /posts/{post_id}`, `PATCH /posts/{post_id}`,
|
|
117
|
+
`DELETE /posts/{post_id}` — admin-only (`require_role("admin")`, from Core)
|
|
118
|
+
- `PUT /posts/{post_id}/translations/{language}` — admin-only; sets/updates
|
|
119
|
+
the translated `subject`/`description`/`text` for that language
|
|
120
|
+
|
|
121
|
+
Post pictures and gallery images are managed through the Django admin (same
|
|
122
|
+
convention as the portfolio package's `Project.cover_image`/`ProjectImage` —
|
|
123
|
+
no write API for images yet, deliberately, to avoid building endpoints
|
|
124
|
+
nothing needs yet).
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from django.contrib import admin
|
|
2
|
+
from unfold.admin import ModelAdmin, TabularInline
|
|
3
|
+
|
|
4
|
+
from blog.models import Comment, Like, Post, PostImage, SavedPost
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class PostImageInline(TabularInline):
|
|
8
|
+
model = PostImage
|
|
9
|
+
extra = 1
|
|
10
|
+
fields = ("image", "caption", "order")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@admin.register(Post)
|
|
14
|
+
class PostAdmin(ModelAdmin):
|
|
15
|
+
list_display = ("subject", "user", "is_published", "like_count", "comment_count", "share_count", "created_at")
|
|
16
|
+
list_filter = ("is_published",)
|
|
17
|
+
search_fields = ("subject", "slug", "description")
|
|
18
|
+
prepopulated_fields = {"slug": ("subject",)}
|
|
19
|
+
readonly_fields = ("id", "share_count", "created_at", "updated_at")
|
|
20
|
+
inlines = [PostImageInline]
|
|
21
|
+
|
|
22
|
+
@admin.display(description="Likes")
|
|
23
|
+
def like_count(self, obj: Post) -> int:
|
|
24
|
+
return obj.likes.count()
|
|
25
|
+
|
|
26
|
+
@admin.display(description="Comments")
|
|
27
|
+
def comment_count(self, obj: Post) -> int:
|
|
28
|
+
return obj.comments.count()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@admin.register(Comment)
|
|
32
|
+
class CommentAdmin(ModelAdmin):
|
|
33
|
+
list_display = ("post", "user", "created_at")
|
|
34
|
+
search_fields = ("text",)
|
|
35
|
+
readonly_fields = ("id", "created_at", "updated_at")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@admin.register(Like)
|
|
39
|
+
class LikeAdmin(ModelAdmin):
|
|
40
|
+
list_display = ("post", "user", "created_at")
|
|
41
|
+
readonly_fields = ("id", "created_at", "updated_at")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@admin.register(SavedPost)
|
|
45
|
+
class SavedPostAdmin(ModelAdmin):
|
|
46
|
+
list_display = ("post", "user", "created_at")
|
|
47
|
+
readonly_fields = ("id", "created_at", "updated_at")
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
|
|
3
|
+
from apps.core.pagination import PageParams, paginate_queryset
|
|
4
|
+
from apps.core.permissions import require_role
|
|
5
|
+
from apps.core.schemas import PagedResponse
|
|
6
|
+
from apps.core.throttling import check_rate_limit
|
|
7
|
+
from apps.core.utils import get_client_ip
|
|
8
|
+
from ninja import Router, Status
|
|
9
|
+
|
|
10
|
+
from blog import services
|
|
11
|
+
from blog.schemas import (
|
|
12
|
+
CommentIn,
|
|
13
|
+
CommentOut,
|
|
14
|
+
LikeToggleOut,
|
|
15
|
+
PostIn,
|
|
16
|
+
PostOut,
|
|
17
|
+
PostTranslationIn,
|
|
18
|
+
PostUpdateIn,
|
|
19
|
+
SaveToggleOut,
|
|
20
|
+
ShareOut,
|
|
21
|
+
ViewerStateOut,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
router = Router(tags=["blog"])
|
|
25
|
+
|
|
26
|
+
# NOTE: literal-path routes ("/posts/by-slug/{slug}", "/posts/{post_id}/..."
|
|
27
|
+
# sub-resources) are registered ahead of the bare "/posts/{post_id}" wildcard
|
|
28
|
+
# below. They don't actually collide by URL shape (different segment counts/
|
|
29
|
+
# literal positions — see blog's README), but keeping literal-before-wildcard
|
|
30
|
+
# is Core's house style for every router (see Core's AGENT.md §9 point 5), so
|
|
31
|
+
# this follows it even where the collision itself isn't possible.
|
|
32
|
+
|
|
33
|
+
# --- Public read endpoints ----------------------------------------------------
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@router.get("/posts", response=PagedResponse[PostOut], auth=None)
|
|
37
|
+
async def list_posts(request, page: PageParams = PageParams(), user_id: uuid.UUID | None = None, lang: str | None = None, search: str | None = None): # noqa: B008 (ninja query-schema idiom)
|
|
38
|
+
items, meta = await paginate_queryset(services.published_post_queryset(user_id, search), page)
|
|
39
|
+
return {"items": await services.serialize_posts(items, lang, None), "meta": meta}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@router.get("/posts/by-slug/{slug}", response=PostOut, auth=None)
|
|
43
|
+
async def get_post_by_slug(request, slug: str, lang: str | None = None):
|
|
44
|
+
post = await services.get_published_post_or_404(slug)
|
|
45
|
+
return (await services.serialize_posts([post], lang, None))[0]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@router.get("/posts/{post_id}/comments", response=PagedResponse[CommentOut], auth=None)
|
|
49
|
+
async def list_comments(request, post_id: uuid.UUID, page: PageParams = PageParams()): # noqa: B008
|
|
50
|
+
items, meta = await paginate_queryset(services.comment_queryset(post_id), page)
|
|
51
|
+
return {"items": items, "meta": meta}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# --- Reader interactions: like / save / comment / share -----------------------
|
|
55
|
+
# Inherit the API's default `auth=session_token_auth` (no `auth=` kwarg here)
|
|
56
|
+
# except "/share", which stays public — sharing shouldn't require an account,
|
|
57
|
+
# it's rate-limited by IP instead (same pattern as Core's contact-form POST).
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@router.get("/posts/{post_id}/viewer-state", response=ViewerStateOut)
|
|
61
|
+
async def post_viewer_state(request, post_id: uuid.UUID):
|
|
62
|
+
return await services.viewer_state(request.auth, post_id)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@router.post("/posts/{post_id}/like", response=LikeToggleOut)
|
|
66
|
+
async def like_post(request, post_id: uuid.UUID):
|
|
67
|
+
liked, like_count = await services.toggle_like(request.auth, post_id)
|
|
68
|
+
return {"liked": liked, "like_count": like_count}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@router.post("/posts/{post_id}/save", response=SaveToggleOut)
|
|
72
|
+
async def save_post(request, post_id: uuid.UUID):
|
|
73
|
+
saved, save_count = await services.toggle_save(request.auth, post_id)
|
|
74
|
+
return {"saved": saved, "save_count": save_count}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@router.post("/posts/{post_id}/comments", response={201: CommentOut})
|
|
78
|
+
async def create_comment(request, post_id: uuid.UUID, payload: CommentIn):
|
|
79
|
+
comment = await services.add_comment(request.auth, post_id, payload.text)
|
|
80
|
+
return Status(201, comment)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@router.post("/posts/{post_id}/share", response=ShareOut, auth=None)
|
|
84
|
+
async def share_post(request, post_id: uuid.UUID):
|
|
85
|
+
await check_rate_limit(f"blog_share:{get_client_ip(request)}", limit=30, window_seconds=60)
|
|
86
|
+
return {"share_count": await services.record_share(post_id)}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@router.delete("/comments/{comment_id}", response={204: None})
|
|
90
|
+
async def delete_comment(request, comment_id: uuid.UUID):
|
|
91
|
+
await services.delete_comment(request.auth, comment_id)
|
|
92
|
+
return Status(204, None)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# --- Admin-only writes (Post) --------------------------------------------------
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@router.post("/posts", response={201: PostOut})
|
|
99
|
+
@require_role("admin")
|
|
100
|
+
async def create_post(request, payload: PostIn):
|
|
101
|
+
post = await services.create_post(request.auth, request.auth.id, **payload.model_dump())
|
|
102
|
+
return Status(201, (await services.serialize_posts([post], None, None))[0])
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@router.get("/posts/{post_id}", response=PostOut)
|
|
106
|
+
@require_role("admin")
|
|
107
|
+
async def get_post(request, post_id: uuid.UUID):
|
|
108
|
+
post = await services.get_post_or_404(post_id)
|
|
109
|
+
return (await services.serialize_posts([post], None, None))[0]
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@router.patch("/posts/{post_id}", response=PostOut)
|
|
113
|
+
@require_role("admin")
|
|
114
|
+
async def update_post(request, post_id: uuid.UUID, payload: PostUpdateIn):
|
|
115
|
+
post = await services.update_post(request.auth, post_id, **payload.model_dump())
|
|
116
|
+
return (await services.serialize_posts([post], None, None))[0]
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@router.delete("/posts/{post_id}", response={204: None})
|
|
120
|
+
@require_role("admin")
|
|
121
|
+
async def delete_post(request, post_id: uuid.UUID):
|
|
122
|
+
await services.delete_post(request.auth, post_id)
|
|
123
|
+
return Status(204, None)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@router.put("/posts/{post_id}/translations/{language}", response=PostOut)
|
|
127
|
+
@require_role("admin")
|
|
128
|
+
async def set_post_translation(request, post_id: uuid.UUID, language: str, payload: PostTranslationIn):
|
|
129
|
+
post = await services.set_post_translation(request.auth, post_id, language, **payload.model_dump())
|
|
130
|
+
return (await services.serialize_posts([post], language, None))[0]
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Generated by Django 5.2.17 on 2026-09-16 11:56
|
|
2
|
+
|
|
3
|
+
import django.db.models.deletion
|
|
4
|
+
import uuid
|
|
5
|
+
from django.conf import settings
|
|
6
|
+
from django.db import migrations, models
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Migration(migrations.Migration):
|
|
10
|
+
|
|
11
|
+
initial = True
|
|
12
|
+
|
|
13
|
+
dependencies = [
|
|
14
|
+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
operations = [
|
|
18
|
+
migrations.CreateModel(
|
|
19
|
+
name='Post',
|
|
20
|
+
fields=[
|
|
21
|
+
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
|
22
|
+
('updated_at', models.DateTimeField(auto_now=True)),
|
|
23
|
+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
|
24
|
+
('translations', models.JSONField(blank=True, default=dict)),
|
|
25
|
+
('subject', models.CharField(max_length=200)),
|
|
26
|
+
('slug', models.SlugField(max_length=220, unique=True)),
|
|
27
|
+
('picture', models.ImageField(blank=True, null=True, upload_to='blog/posts/pictures/')),
|
|
28
|
+
('description', models.CharField(blank=True, default='', max_length=300)),
|
|
29
|
+
('text', models.TextField(blank=True, default='')),
|
|
30
|
+
('is_published', models.BooleanField(default=True)),
|
|
31
|
+
('share_count', models.PositiveIntegerField(default=0)),
|
|
32
|
+
('order', models.PositiveIntegerField(default=0)),
|
|
33
|
+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_posts', to=settings.AUTH_USER_MODEL)),
|
|
34
|
+
],
|
|
35
|
+
options={
|
|
36
|
+
'db_table': 'blog_posts',
|
|
37
|
+
'ordering': ['order', '-created_at'],
|
|
38
|
+
},
|
|
39
|
+
),
|
|
40
|
+
migrations.CreateModel(
|
|
41
|
+
name='Like',
|
|
42
|
+
fields=[
|
|
43
|
+
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
|
44
|
+
('updated_at', models.DateTimeField(auto_now=True)),
|
|
45
|
+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
|
46
|
+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_likes', to=settings.AUTH_USER_MODEL)),
|
|
47
|
+
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='likes', to='blog.post')),
|
|
48
|
+
],
|
|
49
|
+
options={
|
|
50
|
+
'db_table': 'blog_likes',
|
|
51
|
+
},
|
|
52
|
+
),
|
|
53
|
+
migrations.CreateModel(
|
|
54
|
+
name='Comment',
|
|
55
|
+
fields=[
|
|
56
|
+
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
|
57
|
+
('updated_at', models.DateTimeField(auto_now=True)),
|
|
58
|
+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
|
59
|
+
('text', models.TextField()),
|
|
60
|
+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_comments', to=settings.AUTH_USER_MODEL)),
|
|
61
|
+
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.post')),
|
|
62
|
+
],
|
|
63
|
+
options={
|
|
64
|
+
'db_table': 'blog_comments',
|
|
65
|
+
'ordering': ['-created_at'],
|
|
66
|
+
},
|
|
67
|
+
),
|
|
68
|
+
migrations.CreateModel(
|
|
69
|
+
name='PostImage',
|
|
70
|
+
fields=[
|
|
71
|
+
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
|
72
|
+
('updated_at', models.DateTimeField(auto_now=True)),
|
|
73
|
+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
|
74
|
+
('image', models.ImageField(upload_to='blog/posts/gallery/')),
|
|
75
|
+
('caption', models.CharField(blank=True, default='', max_length=200)),
|
|
76
|
+
('order', models.PositiveIntegerField(default=0)),
|
|
77
|
+
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='images', to='blog.post')),
|
|
78
|
+
],
|
|
79
|
+
options={
|
|
80
|
+
'db_table': 'blog_post_images',
|
|
81
|
+
'ordering': ['order'],
|
|
82
|
+
},
|
|
83
|
+
),
|
|
84
|
+
migrations.CreateModel(
|
|
85
|
+
name='SavedPost',
|
|
86
|
+
fields=[
|
|
87
|
+
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
|
88
|
+
('updated_at', models.DateTimeField(auto_now=True)),
|
|
89
|
+
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
|
90
|
+
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='saves', to='blog.post')),
|
|
91
|
+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_saved_posts', to=settings.AUTH_USER_MODEL)),
|
|
92
|
+
],
|
|
93
|
+
options={
|
|
94
|
+
'db_table': 'blog_saved_posts',
|
|
95
|
+
},
|
|
96
|
+
),
|
|
97
|
+
migrations.AddIndex(
|
|
98
|
+
model_name='post',
|
|
99
|
+
index=models.Index(fields=['slug'], name='blog_posts_slug_b4ca25_idx'),
|
|
100
|
+
),
|
|
101
|
+
migrations.AddIndex(
|
|
102
|
+
model_name='post',
|
|
103
|
+
index=models.Index(fields=['is_published'], name='blog_posts_is_publ_b91838_idx'),
|
|
104
|
+
),
|
|
105
|
+
migrations.AddConstraint(
|
|
106
|
+
model_name='like',
|
|
107
|
+
constraint=models.UniqueConstraint(fields=('post', 'user'), name='blog_unique_post_like'),
|
|
108
|
+
),
|
|
109
|
+
migrations.AddConstraint(
|
|
110
|
+
model_name='savedpost',
|
|
111
|
+
constraint=models.UniqueConstraint(fields=('post', 'user'), name='blog_unique_post_save'),
|
|
112
|
+
),
|
|
113
|
+
]
|
|
File without changes
|