xj-study 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.
Files changed (41) hide show
  1. xj_study-0.1.0/PKG-INFO +395 -0
  2. xj_study-0.1.0/README.md +370 -0
  3. xj_study-0.1.0/setup.cfg +4 -0
  4. xj_study-0.1.0/setup.py +75 -0
  5. xj_study-0.1.0/xj_study/__init__.py +0 -0
  6. xj_study-0.1.0/xj_study/admin.py +67 -0
  7. xj_study-0.1.0/xj_study/apis/__init__.py +0 -0
  8. xj_study-0.1.0/xj_study/apis/study_stage_api.py +47 -0
  9. xj_study-0.1.0/xj_study/apis/study_subject_api.py +48 -0
  10. xj_study-0.1.0/xj_study/apis/study_task_api.py +85 -0
  11. xj_study-0.1.0/xj_study/apis/study_unit_api.py +47 -0
  12. xj_study-0.1.0/xj_study/apis/study_user_stage_api.py +65 -0
  13. xj_study-0.1.0/xj_study/apis/study_user_subject_api.py +65 -0
  14. xj_study-0.1.0/xj_study/apis/study_user_task_api.py +89 -0
  15. xj_study-0.1.0/xj_study/apis/study_user_unit_api.py +65 -0
  16. xj_study-0.1.0/xj_study/apps.py +8 -0
  17. xj_study-0.1.0/xj_study/migrations/0001_initial.py +211 -0
  18. xj_study-0.1.0/xj_study/migrations/0002_subject_stage_task_description.py +28 -0
  19. xj_study-0.1.0/xj_study/migrations/__init__.py +0 -0
  20. xj_study-0.1.0/xj_study/models.py +208 -0
  21. xj_study-0.1.0/xj_study/services/__init__.py +0 -0
  22. xj_study-0.1.0/xj_study/services/study_stage_service.py +126 -0
  23. xj_study-0.1.0/xj_study/services/study_subject_service.py +135 -0
  24. xj_study-0.1.0/xj_study/services/study_task_service.py +150 -0
  25. xj_study-0.1.0/xj_study/services/study_unit_service.py +126 -0
  26. xj_study-0.1.0/xj_study/services/study_user_stage_service.py +186 -0
  27. xj_study-0.1.0/xj_study/services/study_user_subject_service.py +164 -0
  28. xj_study-0.1.0/xj_study/services/study_user_task_service.py +287 -0
  29. xj_study-0.1.0/xj_study/services/study_user_unit_service.py +186 -0
  30. xj_study-0.1.0/xj_study/urls.py +55 -0
  31. xj_study-0.1.0/xj_study/utils/__init__.py +0 -0
  32. xj_study-0.1.0/xj_study/utils/custom_response.py +121 -0
  33. xj_study-0.1.0/xj_study/utils/custom_tool.py +891 -0
  34. xj_study-0.1.0/xj_study/utils/j_transform_type.py +111 -0
  35. xj_study-0.1.0/xj_study/utils/parse_json.py +27 -0
  36. xj_study-0.1.0/xj_study/utils/request_params_wrapper.py +76 -0
  37. xj_study-0.1.0/xj_study.egg-info/PKG-INFO +395 -0
  38. xj_study-0.1.0/xj_study.egg-info/SOURCES.txt +39 -0
  39. xj_study-0.1.0/xj_study.egg-info/dependency_links.txt +1 -0
  40. xj_study-0.1.0/xj_study.egg-info/requires.txt +1 -0
  41. xj_study-0.1.0/xj_study.egg-info/top_level.txt +1 -0
@@ -0,0 +1,395 @@
1
+ Metadata-Version: 2.1
2
+ Name: xj_study
3
+ Version: 0.1.0
4
+ Summary: 学习任务模块
5
+ Home-page:
6
+ Author: 赵向明
7
+ Author-email: sieyoo@163.com
8
+ License: apache 3.0
9
+ Keywords: django russian study dictionary vocabulary
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Framework :: Django
20
+ Classifier: Framework :: Django :: 3.2
21
+ Classifier: Framework :: Django :: 4.0
22
+ Classifier: Framework :: Django :: 4.1
23
+ Requires-Python: >=3.7
24
+ Description-Content-Type: text/markdown
25
+
26
+ # xj-study 学习中心模块
27
+
28
+ 基于 Django 的学习中心微服务模块,采用 **Model-Services-APIs** 三层架构设计,与 `xj-lexicon`、`xj-exam` 等模块保持完全一致的工程约定。本模块管理「学科 → 学习阶段 → 学习单元 → 单元任务 → 用户任务记录」五级学习层级,并支持通过外部考核接口回调记录学生测试结果。
29
+
30
+ ---
31
+
32
+ ## 一、目录结构
33
+
34
+ ```
35
+ xj-study/
36
+ ├── xj_study/
37
+ │ ├── apis/ # API 层(视图 + 路由参数解析)
38
+ │ │ ├── study_subject_api.py 学科列表 / 详情
39
+ │ │ ├── study_stage_api.py 学习阶段列表 / 详情
40
+ │ │ ├── study_unit_api.py 学习单元列表 / 详情
41
+ │ │ ├── study_task_api.py 单元任务 列表 / 详情 / 新增 / 修改
42
+ │ │ └── study_user_task_api.py 用户任务记录 列表 / 详情 / 开启 / 提交
43
+ │ ├── services/ # 业务逻辑层(查询/分页/过滤/写库)
44
+ │ │ ├── study_subject_service.py
45
+ │ │ ├── study_stage_service.py
46
+ │ │ ├── study_unit_service.py
47
+ │ │ ├── study_task_service.py
48
+ │ │ └── study_user_task_service.py
49
+ │ ├── utils/ # 工具层(与 xj-lexicon 同步)
50
+ │ │ ├── custom_response.py util_response 统一响应封装
51
+ │ │ ├── custom_tool.py format_params_handle 字段过滤
52
+ │ │ ├── j_transform_type.py JTransformType 类型强制转换
53
+ │ │ ├── request_params_wrapper.py request_params_wrapper 参数解析装饰器
54
+ │ │ └── parse_json.py JSON 字符串解析
55
+ │ ├── __init__.py
56
+ │ ├── apps.py # AppConfig:name='xj_study',sort=14
57
+ │ ├── admin.py # 后台注册(5 个模型)
58
+ │ ├── models.py # 数据模型定义
59
+ │ └── urls.py # 子路由分发
60
+ ├── __init__.py
61
+ └── README.md
62
+ ```
63
+
64
+ ### 三层职责
65
+
66
+ | 层 | 职责 | 约定 |
67
+ | ------ | ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
68
+ | Model | 仅定义数据结构与字段 | 不写业务方法 |
69
+ | Service| 全部业务逻辑:参数校验、过滤白名单、分页、时间格式化、关联嵌套查询、写库 | `@staticmethod`,统一返回 `(result, error)` 元组;不直接接触 request/response |
70
+ | API | 视图层:解析请求参数、调 Service、统一响应 | 继承 `APIView`;`@request_params_wrapper` 注入 `request_params`;`util_response()` 返回 |
71
+
72
+ ---
73
+
74
+ ## 二、数据模型层级
75
+
76
+ ```
77
+ StudySubject(学科)
78
+ └── StudyStage(学习阶段/册) FK→subject
79
+ └── StudyUnit(学习单元) FK→stage
80
+ └── StudyTask(单元任务) FK→unit,含 exam_url / thread_id 外部考核回调字段
81
+ └── StudyUserTask(用户任务记录)FK→task,user_id/user_uuid 不挂用户外键
82
+ ```
83
+
84
+ > `StudyUserTask` 与用户系统**不使用外键**,仅以 `user_id` + `user_uuid` 关联外部用户服务,便于跨服务解耦。`task_snapshot` 字段以 JSON 存储学生测试原始数据。
85
+
86
+ ---
87
+
88
+ ## 三、宿主项目注册
89
+
90
+ 本模块作为独立可安装包,挂载到宿主项目 `supreme-django-5.0`。需在宿主完成以下三处配置(已完成):
91
+
92
+ **1. `main/settings.py`** —— 把模块根目录加入 `sys.path`
93
+ ```python
94
+ sys.path.append("D:/MyGitPkuac/xj-study")
95
+ ```
96
+
97
+ **2. `config.ini` 的 `[main] app_packages`** —— 注册 Django App
98
+ ```ini
99
+ app_packages = [
100
+ ...
101
+ xj_lexicon,
102
+ xj_study,
103
+ xj_comment,
104
+ ]
105
+ ```
106
+
107
+ **3. `config.ini` 的 `[main] main_url_patterns`** —— 注册路由前缀
108
+ ```ini
109
+ main_url_patterns = [
110
+ ...
111
+ api/lexicon[/_] => xj_lexicon.urls,
112
+ api/study[/_] => xj_study.urls,
113
+ api/comment[/_] => xj_comment.urls,
114
+ ]
115
+ ```
116
+
117
+ 注册后,所有接口统一以 `/api/study/` 为前缀(`[/_]` 同时兼容 `/api/study_xxx` 写法)。
118
+
119
+ ---
120
+
121
+ ## 四、统一响应协议
122
+
123
+ 所有接口返回 `util_response()` 封装的 JSON:
124
+
125
+ ```json
126
+ {
127
+ "err": 0,
128
+ "data": { ... },
129
+ "msg": "ok"
130
+ }
131
+ ```
132
+
133
+ | 字段 | 含义 |
134
+ | ----- | --------------------------------------------------- |
135
+ | `err` | 错误码,`0` 表示成功;非 0 时 `data` 通常为空 |
136
+ | `data`| 数据体(列表接口为分页对象,详情接口为单条记录) |
137
+ | `msg` | 错误信息;成功为 `ok`,失败为具体错误描述 |
138
+
139
+ 列表接口的 `data` 结构:
140
+
141
+ ```json
142
+ {
143
+ "page": 1,
144
+ "size": 20,
145
+ "total": 35,
146
+ "list": [ ... ],
147
+ "query": "SELECT ..." // 实际执行的 SQL,便于调试
148
+ }
149
+ ```
150
+
151
+ ---
152
+
153
+ ## 五、API 路由表
154
+
155
+ | 方法 | 路径 | 说明 |
156
+ | -------- | ----------------------------- | -------------------------- |
157
+ | `GET` | `/api/study/subject_list` | 学科列表 |
158
+ | `GET` | `/api/study/subject_item` | 学科详情(含阶段列表) |
159
+ | `GET` | `/api/study/stage_list` | 学习阶段列表 |
160
+ | `GET` | `/api/study/stage_item` | 学习阶段详情(含单元列表) |
161
+ | `GET` | `/api/study/unit_list` | 学习单元列表 |
162
+ | `GET` | `/api/study/unit_item` | 学习单元详情(含任务列表) |
163
+ | `GET` | `/api/study/task_list` | 单元任务列表 |
164
+ | `GET` | `/api/study/task_item` | 单元任务详情 |
165
+ | `POST` | `/api/study/task_item` | 新增单元任务 |
166
+ | `PUT` | `/api/study/task_item` | 修改单元任务 |
167
+ | `GET` | `/api/study/user_task_list` | 用户任务记录列表 |
168
+ | `GET` | `/api/study/user_task_item` | 用户任务记录详情 |
169
+ | `POST` | `/api/study/user_task_start` | 开启一条用户任务记录 |
170
+ | `POST` | `/api/study/user_task_submit` | 提交用户任务结果(回调) |
171
+
172
+ > `@request_params_wrapper` 装饰器兼容 `GET` query、`POST` form、`application/json`、`text/plain` 多种请求格式,下列示例统一用 JSON Body 演示(GET 接口建议用 query string)。
173
+
174
+ ---
175
+
176
+ ## 六、API 调用示例
177
+
178
+ > 以下示例假设服务运行在 `http://127.0.0.1:8000`。`GET` 请求参数既可走 query string,也可走 body;`POST/PUT` 走 JSON Body。
179
+
180
+ ### 1. 学科列表
181
+
182
+ ```bash
183
+ curl -G "http://127.0.0.1:8000/api/study/subject_list" \
184
+ --data "page=1" --data "size=20" --data "sort=-sort" \
185
+ --data "search=俄" --data "enable=true"
186
+ ```
187
+
188
+ 响应:
189
+ ```json
190
+ {
191
+ "err": 0,
192
+ "data": {
193
+ "page": 1, "size": 20, "total": 2,
194
+ "list": [
195
+ { "id": 1, "name": "俄语", "enable": true, "subject_score": 0, "icon": "", "sort": 1, "create_time": "2026-08-31 10:00:00", "update_time": "2026-08-31 10:00:00" }
196
+ ],
197
+ "query": "SELECT ..."
198
+ },
199
+ "msg": "ok"
200
+ }
201
+ ```
202
+
203
+ ### 2. 学科详情(含关联阶段)
204
+
205
+ ```bash
206
+ curl -G "http://127.0.0.1:8000/api/study/subject_item" --data "id=1"
207
+ ```
208
+
209
+ 响应:
210
+ ```json
211
+ {
212
+ "err": 0,
213
+ "data": {
214
+ "id": 1, "name": "俄语", "enable": true, "subject_score": 0, "icon": "", "sort": 1,
215
+ "create_time": "2026-08-31 10:00:00", "update_time": "2026-08-31 10:00:00",
216
+ "stage_count": 2,
217
+ "stage_list": [
218
+ { "id": 1, "name": "第一册", "enable": true, "course_book": "...", "stage_score": 0, "icon": "", "sort": 1, "create_time": "...", "update_time": "..." }
219
+ ]
220
+ },
221
+ "msg": "ok"
222
+ }
223
+ ```
224
+
225
+ ### 3. 学习阶段列表
226
+
227
+ ```bash
228
+ curl -G "http://127.0.0.1:8000/api/study/stage_list" \
229
+ --data "subject_id=1" --data "enable=true" --data "page=1" --data "size=20"
230
+ ```
231
+
232
+ ### 4. 学习阶段详情(含关联单元)
233
+
234
+ ```bash
235
+ curl -G "http://127.0.0.1:8000/api/study/stage_item" --data "id=1"
236
+ ```
237
+
238
+ ### 5. 学习单元列表
239
+
240
+ ```bash
241
+ curl -G "http://127.0.0.1:8000/api/study/unit_list" \
242
+ --data "stage_id=1" --data "search=语音" --data "page=1" --data "size=20"
243
+ ```
244
+
245
+ ### 6. 学习单元详情(含关联任务)
246
+
247
+ ```bash
248
+ curl -G "http://127.0.0.1:8000/api/study/unit_item" --data "id=10"
249
+ ```
250
+
251
+ ### 7. 单元任务列表
252
+
253
+ ```bash
254
+ curl -G "http://127.0.0.1:8000/api/study/task_list" \
255
+ --data "unit_id=10" --data "enable=true" --data "sort=-sort"
256
+ ```
257
+
258
+ ### 8. 单元任务详情
259
+
260
+ ```bash
261
+ curl -G "http://127.0.0.1:8000/api/study/task_item" --data "id=100"
262
+ ```
263
+
264
+ 响应:
265
+ ```json
266
+ {
267
+ "err": 0,
268
+ "data": {
269
+ "id": 100, "unit_id": 10, "name": "A1 听力测试",
270
+ "exam_url": "https://exam.example.com/api/check", "thread_id": "th_abc123",
271
+ "enable": true, "task_score": 100, "time_limit": 1800,
272
+ "open_time": "2026-09-01 09:00:00", "close_time": "2026-09-01 11:00:00",
273
+ "publish_time": "2026-08-31 18:00:00", "icon": "", "sort": 1,
274
+ "create_time": "...", "update_time": "..."
275
+ },
276
+ "msg": "ok"
277
+ }
278
+ ```
279
+
280
+ ### 9. 新增单元任务(POST)
281
+
282
+ ```bash
283
+ curl -X POST "http://127.0.0.1:8000/api/study/task_item" \
284
+ -H "Content-Type: application/json" \
285
+ -d '{
286
+ "unit_id": 10,
287
+ "name": "A1 听力测试",
288
+ "exam_url": "https://exam.example.com/api/check",
289
+ "thread_id": "th_abc123",
290
+ "enable": true,
291
+ "task_score": 100,
292
+ "time_limit": 1800,
293
+ "sort": 1
294
+ }'
295
+ ```
296
+
297
+ 响应:
298
+ ```json
299
+ { "err": 0, "data": { "id": 101 }, "msg": "ok" }
300
+ ```
301
+
302
+ ### 10. 修改单元任务(PUT)
303
+
304
+ ```bash
305
+ curl -X PUT "http://127.0.0.1:8000/api/study/task_item" \
306
+ -H "Content-Type: application/json" \
307
+ -d '{ "id": 101, "time_limit": 2400, "task_score": 120 }'
308
+ ```
309
+
310
+ 响应:
311
+ ```json
312
+ { "err": 0, "data": { "rows": 1 }, "msg": "ok" }
313
+ ```
314
+
315
+ ### 11. 用户任务记录列表
316
+
317
+ ```bash
318
+ curl -G "http://127.0.0.1:8000/api/study/user_task_list" \
319
+ --data "user_id=10086" --data "task_id=100" --data "is_pass=false"
320
+ ```
321
+
322
+ ### 12. 用户任务记录详情
323
+
324
+ ```bash
325
+ curl -G "http://127.0.0.1:8000/api/study/user_task_item" --data "id=500"
326
+ ```
327
+
328
+ ### 13. 开启用户任务记录(POST)
329
+
330
+ 学生进入作答时调用;若存在未完成记录将直接复用,不重复创建。
331
+
332
+ ```bash
333
+ curl -X POST "http://127.0.0.1:8000/api/study/user_task_start" \
334
+ -H "Content-Type: application/json" \
335
+ -d '{
336
+ "user_id": 10086,
337
+ "user_uuid": "u-10086-xxxx",
338
+ "task_id": 100
339
+ }'
340
+ ```
341
+
342
+ 响应:
343
+ ```json
344
+ { "err": 0, "data": { "id": 500 }, "msg": "ok" }
345
+ ```
346
+
347
+ ### 14. 提交用户任务结果(POST,外部考核回调)
348
+
349
+ 外部考核系统阅卷完成后回调本接口,写入得分、是否通过及 `task_snapshot` 快照,并自动记录 `finish_time`。
350
+
351
+ ```bash
352
+ curl -X POST "http://127.0.0.1:8000/api/study/user_task_submit" \
353
+ -H "Content-Type: application/json" \
354
+ -d '{
355
+ "id": 500,
356
+ "result_score": 85,
357
+ "user_task_score": 15,
358
+ "is_pass": true,
359
+ "task_snapshot": {
360
+ "questions": [
361
+ { "qid": "Q1", "answer": "А", "correct": true },
362
+ { "qid": "Q2", "answer": "Б", "correct": false }
363
+ ],
364
+ "duration": 1620
365
+ }
366
+ }'
367
+ ```
368
+
369
+ 响应:
370
+ ```json
371
+ { "err": 0, "data": { "rows": 1 }, "msg": "ok" }
372
+ ```
373
+
374
+ ---
375
+
376
+ ## 七、典型业务流程
377
+
378
+ 1. 管理员在后台配置 `StudySubject → StudyStage → StudyUnit → StudyTask`,并为每个任务填写 `exam_url`(外部考核回调地址)与 `thread_id`。
379
+ 2. 学生打开单元,前端调 `GET /api/study/unit_item` 拿到 `task_list`。
380
+ 3. 学生点击某任务,前端调 `POST /api/study/user_task_start` 创建/复用一条 `StudyUserTask` 记录,得到 `record_id`,并据 `exam_url` 跳转外部考核系统。
381
+ 4. 外部考核系统阅卷完成,按约定回调 `POST /api/study/user_task_submit`(带 `record_id`、`result_score`、`is_pass`、`task_snapshot`)。
382
+ 5. 前端轮询或拉取 `GET /api/study/user_task_item` 展示结果与快照。
383
+
384
+ ---
385
+
386
+ ## 八、数据库迁移
387
+
388
+ 在宿主项目 `supreme-django-5.0` 根目录执行:
389
+
390
+ ```bash
391
+ python manage.py makemigrations xj_study
392
+ python manage.py migrate
393
+ ```
394
+
395
+ 完成后可在 `/administrator/` 后台看到「Ⅳ 学习中心」应用及其 5 个模型的管理界面。