Skip to content

Commit 9ee852d

Browse files
Phase 4: Implement REST API for content types
1 parent a4b8649 commit 9ee852d

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

pythoncms/modules/api/__init__.py

Whitespace-only changes.

pythoncms/modules/api/info.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"display_string": "API",
3+
"type": "show",
4+
"icons": {
5+
"fa": "fas fa-network-wired",
6+
"boxicons": "bx bx-network-chart"
7+
},
8+
"url_prefix": "/api/v1",
9+
"menu-type": "no-menu",
10+
"module_name": "api",
11+
"author": {
12+
"name": "pythoncms",
13+
"website": "https://github.com/shopyo/pythoncms",
14+
"mail": ""
15+
}
16+
}

pythoncms/modules/api/view.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from flask import Blueprint
2+
from flask import jsonify
3+
from flask import request
4+
from modules.contenttype.models import ContentType
5+
from modules.contenttype.models import ContentItem
6+
7+
api_blueprint = Blueprint(
8+
"api",
9+
__name__,
10+
url_prefix="/api/v1",
11+
)
12+
13+
@api_blueprint.route("/<string:type_name>", methods=["GET"])
14+
def get_content(type_name):
15+
content_type = ContentType.query.filter_by(name=type_name).first()
16+
if not content_type:
17+
return jsonify({"error": "Content type not found"}), 404
18+
19+
items = ContentItem.query.filter_by(content_type_id=content_type.id).all()
20+
21+
data = []
22+
for item in items:
23+
item_data = {
24+
"id": item.id,
25+
"created_at": item.created_at.isoformat(),
26+
"updated_at": item.updated_at.isoformat(),
27+
"content": item.data
28+
}
29+
data.append(item_data)
30+
31+
return jsonify({
32+
"count": len(data),
33+
"data": data
34+
})
35+
36+
@api_blueprint.route("/types", methods=["GET"])
37+
def get_types():
38+
content_types = ContentType.query.all()
39+
data = []
40+
for ct in content_types:
41+
data.append({
42+
"id": ct.id,
43+
"name": ct.name,
44+
"description": ct.description,
45+
"schema": ct.schema
46+
})
47+
return jsonify(data)

0 commit comments

Comments
 (0)