Skip to content

Commit

Permalink
official website api
Browse files Browse the repository at this point in the history
  • Loading branch information
guoshijiang committed Mar 17, 2024
1 parent e6534b9 commit dc5ff00
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 0 deletions.
12 changes: 12 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
get_questions,
get_reward_info
)
from api.website.api_v1 import (
get_blog_list,
get_event_list,
get_forum_list,
get_blog_cat_list
)

urlpatterns = [
# Hd wallet module
Expand Down Expand Up @@ -117,4 +123,10 @@
path(r'submit_invite_info', submit_invite_info, name='submit_invite_info'),
path(r'get_points_by_address', get_points_by_address, name='get_points_by_address'),
path(r'get_points_record_by_address', get_points_record_by_address, name='get_points_record_by_address'),

# official website
path(r'get_blog_cat_list', get_blog_cat_list, name='get_blog_cat_list'),
path(r'get_blog_list', get_blog_list, name='get_blog_list'),
path(r'get_event_list', get_event_list, name='get_event_list'),
path(r'get_forum_list', get_forum_list, name='get_forum_list'),
]
Empty file added api/website/__init__.py
Empty file.
82 changes: 82 additions & 0 deletions api/website/api_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# encoding=utf-8

import json
from common.helpers import ok_json
from website.models import (
Event,
Forum,
BlogCat,
Blog
)


# @check_api_token
def get_event_list(request):
params = json.loads(request.body.decode())
page = params.get('page', 1)
page_size = params.get('page_size', 20)
start = (page - 1) * page_size
end = start + page_size
events = Event.objects.all().order_by("-id")[start:end]
total = Event.objects.all().order_by("-id").count()
event_list = []
for event in events:
event_list.append(event.as_dict())
data = {
"total": total,
"event_list": event_list,
}
return ok_json(data)


# @check_api_token
def get_forum_list(request):
params = json.loads(request.body.decode())
page = params.get('page', 1)
page_size = params.get('page_size', 20)
start = (page - 1) * page_size
end = start + page_size
forums = Forum.objects.all().order_by("-id")[start:end]
total = Forum.objects.all().order_by("-id").count()
forum_list = []
for forum in forums:
forum_list.append(forum.as_dict())
data = {
"total": total,
"forum_list": forum_list,
}
return ok_json(data)


# @check_api_token
def get_blog_cat_list(request):
blog_cats = BlogCat.objects.all()
blog_cat_list = []
for blog_cat in blog_cats:
blog_cat_list.append(blog_cat.as_dict())
return ok_json(blog_cat_list)


# @check_api_token
def get_blog_list(request):
params = json.loads(request.body.decode())
cat_id = params.get('cat_id', 0)
page = params.get('page', 1)
page_size = params.get('page_size', 20)
start = (page - 1) * page_size
end = start + page_size
bcat = BlogCat.objects.filter(id=cat_id).first()
if bcat is None:
blogs = Blog.objects.all().order_by("-id")[start:end]
total = Blog.objects.all().order_by("-id").count()
else:
blogs = Blog.objects.filter(cat=bcat).order_by("-id")[start:end]
total = Blog.objects.filter(cat=bcat).order_by("-id").count()
blog_list = []
for blog in blogs:
blog_list.append(blog.as_dict())
data = {
"total": total,
"blog_list": blog_list,
}
return ok_json(data)
19 changes: 19 additions & 0 deletions website/migrations/0002_blog_cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.1.1 on 2024-03-17 10:40

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('website', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='blog',
name='cat',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='blog_cat', to='website.blogcat', verbose_name='博客分类'),
),
]
9 changes: 9 additions & 0 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ class Blog(BaseModel):
unique=False,
verbose_name='博客链接'
)
cat = models.ForeignKey(
BlogCat,
blank=True,
related_name='blog_cat',
null=True,
on_delete=models.CASCADE,
verbose_name='博客分类'
)
tags = models.CharField(
default="",
max_length=200,
Expand All @@ -109,6 +117,7 @@ def as_dict(self):
return {
'id': self.id,
'title': self.title,
'cat_name': self.cat.name,
'image': str(self.image),
'describe': self.describe,
'link_url': self.link_url,
Expand Down
127 changes: 127 additions & 0 deletions website_api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# 网站接口

## 1. 获取博客类别

请求示范
```
curl --location --request POST 'http://127.0.0.1:8000/api/get_blog_cat_list' \
--data ''
```
返回值
```
{
"ok": true,
"code": 200,
"result": [
{
"id": 1,
"name": "web3"
},
{
"id": 2,
"name": "ecosystem"
}
]
}
```

## 2. 获取博客列表

请求示范
```
curl --location 'http://127.0.0.1:8000/api/get_blog_list' \
--header 'Content-Type: application/json' \
--data '{
"cat_id": 2,
"page": 1,
"page_size": 10
}'
```
返回值
```
{
"ok": true,
"code": 200,
"result": {
"total": 1,
"blog_list": [
{
"id": 1,
"title": "unknown",
"cat_name": "unknown",
"image": "blog/2024/03/17/1280X1280.JPEG",
"describe": "10DP once, max 10000DP",
"link_url": "dapplink protobuf protobufprotobufprotobuf",
"tags": "hhhh",
"created_at": "2024-03-17 18:44:53"
}
]
}
}
```


## 3. 获取事件列表

请求示范
```
curl --location 'http://127.0.0.1:8000/api/get_forum_list' \
--header 'Content-Type: application/json' \
--data '{
"page": 1,
"page_size": 10
}'
```
返回值
```
{
"ok": true,
"code": 200,
"result": {
"total": 1,
"forum_list": [
{
"id": 1,
"name": "Execute transactions on the testnet cross-chain bridge",
"link": "dapplink protobuf protobufprotobufprotobuf",
"describe": "10DP once, max 10000DP",
"created_at": "2024-03-17 18:44:04"
}
]
}
}
}
```


## 4. 获取 forum 列表

请求示范
```
curl --location 'http://127.0.0.1:8000/api/get_forum_list' \
--header 'Content-Type: application/json' \
--data '{
"page": 1,
"page_size": 10
}'
```
返回值
```
{
"ok": true,
"code": 200,
"result": {
"total": 1,
"forum_list": [
{
"id": 1,
"title": "dapplink protobuf protobufprotobufprotobuf",
"link": "dapplink protobuf protobufprotobufprotobuf",
"describe": "dapplink protobuf protobufprotobufprotobuf",
"created_at": "2024-03-17 18:43:51"
}
]
}
}
```

0 comments on commit dc5ff00

Please sign in to comment.