Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions auth/apps.py

This file was deleted.

3 changes: 0 additions & 3 deletions auth/tests.py

This file was deleted.

File renamed without changes.
37 changes: 37 additions & 0 deletions common/consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from enum import Enum
# Permission Levels
Everyone = 0
User = 1
VIP = 2
Admin = 3

# HTTP Status Code


class StatusCode(Enum):
# Success Code Family
SUCCESS = 200_00

# Bad Request Family
BAD_REQUEST = 400_00
INVALID_REQUEST_ARGUMENT = 400_01

# Unauthorized Family
UNAUTHORIZED = 401_00
INVALID_USERNAME_OR_PASSWORD = 401_01
INVALID_TOKEN = 401_02
TOKEN_EXPIRED = 401_03
ACCOUNT_DISABLED = 401_04

# Refuse Access Family
REFUSE_ACCESS = 403_00
PERMISSION_DENIED = 403_01

# Not Found Family
ITEM_NOT_FOUND = 404_00

# Resource Too Huge Family
Image_HUGE = 413_00

# Duplicated Family
ITEM_ALREADY_EXISTS = 409_00
53 changes: 53 additions & 0 deletions common/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import json
from enum import Enum

from django.http import HttpRequest, JsonResponse
from django.views.decorators.http import require_http_methods

from common.consts import StatusCode


def response_wrapper(func):
def inner(*args, **kwargs):
response = func(*args, **kwargs)
if isinstance(response, dict):
if response["success"]:
response = JsonResponse(response["data"])
else:
status_code = response.get("data").get("code")
response = JsonResponse(response["data"])
response.status_code = status_code
return response

return inner


def api_response(success, data) -> dict:
return {"success": success, "data": data}


def failed_api_response(code: StatusCode, error_msg=None) -> dict:
if error_msg is None:
error_msg = str(code)
else:
error_msg = str(code) + ": " + error_msg

status_code = code.value // 100
detailed_code = code.value
return api_response(
success=False,
data={
"code": status_code,
"detailed_error_code": detailed_code,
"error_msg": error_msg
})


def success_api_response(data) -> dict:
return api_response(True, data)

def parse_data(request: HttpRequest):
try:
return json.loads(request.body.decode())
except json.JSONDecodeError:
return None
69 changes: 69 additions & 0 deletions docs/api/auth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# auth 模块 API

## 总览

以下表格列出了与用户认证相关的 API:

| 方法 | 路径 | 描述 | Response |
| ------ | ------------------------ | -------- | ------------ |
| `POST` | [`/auth`](#post-auth) | 用户登录 | access token |
| `POST` | [`/auth/create`](#post-) | 用户创建 | 用户 ID |

## 详细描述

### `POST /auth`

使用用户名和密码进行登录,后端生成`access_token`。

后续请求需要带上`access_token`用于鉴权。

如果鉴权得到`access_token`过期的错误信息,将需要重新登录获取新的`access_token`。

#### Request Body

**必要字段**

| 字段 | 类型 | 描述 |
| :------: | :----: | :----: |
| username | String | 用户名 |
| password | String | 密码 |

#### 请求示例

```bash
curl -X POST \
http://127.0.0.1:8000/auth \
-F username=test \
-F password=test
```

#### Response Body

| 字段 | 类型 | 描述 |
| :----------: | :----: | :-------: |
| access_token | String | JWT Token |

```json
// Example
{
"access_token": "aaa.bbb.ccc"
}
```

### `POST /auth/create`

#### Request Body

**必要字段**

| 字段 | 类型 | 描述 |
| :------: | :----: | :------: |
| username | String | 用户名 |
| password | String | 密码 |
| email | String | 电邮地址 |

#### Response Body

| 字段 | 类型 | 描述 |
| :--: | :-----: | :-----: |
| id | Integer | 用户 ID |
164 changes: 164 additions & 0 deletions docs/api/ocr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# ocr 模块 API

## 总览

以下表格列出了与 OCR 识别相关的 API:

### project

| 方法 | 路径 | 描述 |
| -------- | ----------------------------------------------------------------- | ---------------- |
| `POST` | [`/ocr/project`](#post-ocrproject) | 创建项目 |
| `PUT` | [`/ocr/project/<int:project_id>`](#put-ocrprojectintprojectid) | 修改项目信息 |
| `GET` | [`/ocr/project`](#get-ocrproject) | 获取所有项目信息 |
| `GET` | [`/ocr/project/<int:project_id>`](#get-ocrprojectintprojectid) | 获取项目详细信息 |
| `DELETE` | [`/ocr/project/<int:project_id>`](#delete-ocrprojectintprojectid) | 删除项目 |

### recognition_result

| 方法 | 路径 | 描述 |
| -------- | -------------------------------------------------------------------------------------------- | ---------------- |
| `POST` | [`/ocr/project/<int:project_id>`](#post-ocrprojectintprojectid) | 上传待识别图片 |
| `GET` | [`/ocr/project/<int:project_id>/<int:result_id>`](#get-ocrprojectintprojectidintresultid) | 获取识别结果 |
| `PUT` | [`/ocr/project/<int:project_id>/<int:result_id>`](#put-ocrprojectintprojectidintresultid) | 修改识别结果信息 |
| `DELETE` | [`/ocr/project/<int:project_id>/<int:result_id>`](#delete-ocrprojectintprojectidintresultid) | 删除识别结果 |

## 详细描述

### `POST /ocr/project`

创建项目

#### Request Body

| 字段 | 类型 | 描述 |
| :-----: | :----: | :------------: |
| name | String | 项目名 |
| comment | String | 可选,项目描述 |

#### Response Body

| 字段 | 类型 | 描述 |
| :--------: | :------: | :----------: |
| id | Integer | 项目 ID |
| created_at | DateTime | 项目创建时间 |

### `PUT /ocr/project/<int:project_id>`

修改项目信息

#### Request Body

| 字段 | 类型 | 描述 |
| :-----: | :----: | :------------: |
| name | String | 可选,项目名 |
| comment | String | 可选,项目描述 |

### `GET /ocr/project`

获取该用户所有项目信息

#### Response Body

| 字段 | 类型 | 描述 |
| :---------: | :-----: | :----------: |
| projects | Array | 所有项目信息 |
| project_num | Integer | 项目总数 |

projects 字段元素内容

| 字段 | 类型 | 描述 |
| :--------: | :------: | :----------: |
| name | String | 项目名 |
| comment | String | 项目描述 |
| created_at | DateTime | 创建时间 |
| result_num | Integer | 识别结果总数 |

### `GET /ocr/project/<int:project_id>`

获取项目详细信息

#### Response Body

| 字段 | 类型 | 描述 |
| :--------: | :------: | :----------: |
| name | String | 项目名 |
| comment | String | 项目描述 |
| created_at | DateTime | 创建时间 |
| result_num | Integer | 识别结果总数 |
| results | Array | 所有识别结果 |

results 字段元素内容

| 字段 | 类型 | 描述 |
| :--------: | :------: | :----------: |
| name | String | 识别结果名 |
| comment | String | 识别结果描述 |
| created_at | DateTime | 创建时间 |

### `DELETE /ocr/project/<int:project_id>`

删除项目

### `POST /ocr/project/<int:project_id>`

上传待识别图片

#### Request Body(JSON part)

| 字段 | 类型 | 描述 |
| :-----: | :----: | :----------------: |
| name | String | 可选,识别结果名 |
| comment | String | 可选,识别结果描述 |

可能的发包方法

```python
import json
import requests

payload = {
"name": "test",
"comment": "test comment"
}

files = {
"json": (None, json.dumps(payload), "application/json"),
"file": (os.path.basename(file), open(file, "rb"), "image/png")
}

requests.post(url, files=files)
```

#### Response Body

| 字段 | 类型 | 描述 |
| :--------: | :------: | :---------: |
| id | Integer | 识别结果 ID |
| created_at | DateTime | 创建时间 |

### `GET /ocr/project/<int:project_id>/<int:result_id>`

获取识别结果

#### Response Body

| 字段 | 类型 | 描述 |
| :-----: | :----: | :----------: |
| name | String | 识别结果名 |
| comment | String | 识别结果描述 |
| result | JSON | 识别结果 |

### `PUT /ocr/project/<int:project_id>/<int:result_id>`

修改识别结果信息

| 字段 | 类型 | 描述 |
| :-----: | :----: | :----------------: |
| name | String | 可选,识别结果名 |
| comment | String | 可选,识别结果描述 |

### `DELETE /ocr/project/<int:project_id>/<int:result_id>`

删除识别结果

File renamed without changes.
4 changes: 3 additions & 1 deletion nocode_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
# Application definition

INSTALLED_APPS = [
'userManagement.apps.UsermanagementConfig',
'ocr.apps.OcrConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand Down Expand Up @@ -105,7 +107,7 @@

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

Expand Down
4 changes: 3 additions & 1 deletion nocode_backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('userManagement/', include('userManagement.urls')),
path('ocr/', include('ocr.urls')),
]
Empty file added ocr/api/__init__.py
Empty file.
Empty file added ocr/api/project.py
Empty file.
3 changes: 0 additions & 3 deletions ocr/models.py

This file was deleted.

Empty file added ocr/models/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions ocr/models/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models
from django.contrib.auth import get_user_model

UserModel = get_user_model()


class Project(models.Model):
name = models.CharField(max_length=50, default="")
comment = models.TextField(default="")
belong_to = models.ForeignKey(to=UserModel, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
11 changes: 11 additions & 0 deletions ocr/models/recognition_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models
from django.contrib.auth import get_user_model
from django.contrib.postgres.fields import JSONField
from ocr.models.project import Project

class RecognitionResult(models.Model):
name = models.CharField(max_length=50, default="")
comment = models.TextField(default="")
belong_to = models.ForeignKey(to=Project, on_delete=models.CASCADE)
result = JSONField()
created_at = models.DateTimeField(auto_now_add=True)
Loading