-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1630 lines (1383 loc) · 59.1 KB
/
app.py
File metadata and controls
1630 lines (1383 loc) · 59.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
"""
任务管理系统 REST API
完整的项目管理 API,包含:
- 用户认证 (JWT)
- 项目管理
- 任务管理(状态、优先级、截止日期)
- 成员管理
- 评论系统
- 分页、排序、过滤
- 输入验证
- OpenAPI 文档
"""
import sys
import os
import json
import uuid
from datetime import datetime, timedelta
from typing import Optional, List, Dict, Any
from urllib.parse import parse_qs
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'src'))
from litefs import Litefs, Response
from litefs.routing import get, post, put, delete, patch
from litefs.auth import Auth, init_default_roles_and_permissions
from litefs.auth.models import User, Role
from litefs.auth.password import hash_password
from litefs.auth.middleware import login_required, role_required
from litefs.openapi import OpenAPI
# 应用目录
APP_DIR = os.path.dirname(os.path.abspath(__file__))
# ============================================================================
# 应用配置
# ============================================================================
app = Litefs(
host='0.0.0.0',
port=8082,
debug=True,
secret_key='task-manager-secret-key-change-in-production'
)
# 添加静态文件支持
app.add_static('/static', os.path.join(APP_DIR, 'static'))
# JWT 认证配置
auth = Auth(app, secret_key='your-secret-key-change-in-production')
openapi = OpenAPI(app, title='任务管理 API', version='1.0.0', description='团队任务管理系统 API')
# ============================================================================
# 初始化数据
# ============================================================================
init_default_roles_and_permissions()
admin_role = Role.get_by_name('admin')
editor_role = Role.get_by_name('editor')
user_role = Role.get_by_name('user')
# 创建默认用户
admin_user = User.create(
username='admin',
email='admin@example.com',
password_hash=hash_password('admin123'),
roles=[admin_role],
)
editor_user = User.create(
username='alice',
email='alice@example.com',
password_hash=hash_password('alice123'),
roles=[editor_role],
)
User.create(
username='bob',
email='bob@example.com',
password_hash=hash_password('bob123'),
roles=[user_role],
)
User.create(
username='charlie',
email='charlie@example.com',
password_hash=hash_password('charlie123'),
roles=[user_role],
)
# ============================================================================
# 内存数据库
# ============================================================================
class Database:
"""简单的内存数据库"""
def __init__(self):
self._users: Dict[str, Dict] = {}
self._projects: Dict[str, Dict] = {}
self._tasks: Dict[str, Dict] = {}
self._comments: Dict[str, Dict] = {}
self._project_members: Dict[str, List[str]] = {} # project_id -> [user_ids]
self._task_assignees: Dict[str, List[str]] = {} # task_id -> [user_ids]
self._init_sample_data()
def _init_sample_data(self):
"""初始化示例数据"""
now = datetime.now()
# 项目数据
projects = [
{
'id': 'proj-1',
'name': '官网重构',
'slug': 'website-redesign',
'description': '将公司官网迁移到新平台,提升用户体验和 SEO 效果',
'owner_id': 'admin',
'status': 'active',
'color': '#3498db',
'created_at': (now - timedelta(days=30)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=2)).isoformat() + 'Z',
},
{
'id': 'proj-2',
'name': '移动端 App 开发',
'slug': 'mobile-app',
'description': '开发 iOS 和 Android 原生应用',
'owner_id': 'alice',
'status': 'active',
'color': '#e74c3c',
'created_at': (now - timedelta(days=45)).isoformat() + 'Z',
'updated_at': (now - timedelta(hours=5)).isoformat() + 'Z',
},
{
'id': 'proj-3',
'name': 'API 性能优化',
'slug': 'api-optimization',
'description': '优化后端 API 响应时间,提升整体性能',
'owner_id': 'admin',
'status': 'planning',
'color': '#2ecc71',
'created_at': (now - timedelta(days=7)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=1)).isoformat() + 'Z',
},
{
'id': 'proj-4',
'name': '数据迁移项目',
'slug': 'data-migration',
'description': '将旧系统数据迁移到新数据库',
'owner_id': 'alice',
'status': 'completed',
'color': '#9b59b6',
'created_at': (now - timedelta(days=90)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=15)).isoformat() + 'Z',
},
]
for proj in projects:
self._projects[proj['id']] = proj
# 项目成员
self._project_members = {
'proj-1': ['admin', 'alice', 'bob'],
'proj-2': ['alice', 'charlie'],
'proj-3': ['admin', 'bob', 'charlie'],
'proj-4': ['alice'],
}
# 任务数据
tasks = [
# proj-1 的任务
{
'id': 'task-1',
'project_id': 'proj-1',
'title': '设计新首页布局',
'description': '根据新品牌形象设计首页,要求简洁大气',
'status': 'done',
'priority': 'high',
'assignees': ['alice'],
'due_date': (now + timedelta(days=-5)).isoformat() + 'Z',
'created_at': (now - timedelta(days=20)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=5)).isoformat() + 'Z',
'completed_at': (now - timedelta(days=5)).isoformat() + 'Z',
'tags': ['design'],
'estimated_hours': 8,
'actual_hours': 10,
},
{
'id': 'task-2',
'project_id': 'proj-1',
'title': '开发首页前端组件',
'description': '使用 React 实现首页各个模块的组件',
'status': 'in_progress',
'priority': 'high',
'assignees': ['bob'],
'due_date': (now + timedelta(days=7)).isoformat() + 'Z',
'created_at': (now - timedelta(days=15)).isoformat() + 'Z',
'updated_at': (now - timedelta(hours=2)).isoformat() + 'Z',
'completed_at': None,
'tags': ['frontend', 'react'],
'estimated_hours': 24,
'actual_hours': 12,
},
{
'id': 'task-3',
'project_id': 'proj-1',
'title': 'SEO 优化',
'description': '优化页面标题、描述和关键词,提升搜索引擎排名',
'status': 'todo',
'priority': 'medium',
'assignees': [],
'due_date': (now + timedelta(days=14)).isoformat() + 'Z',
'created_at': (now - timedelta(days=10)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=10)).isoformat() + 'Z',
'completed_at': None,
'tags': ['seo'],
'estimated_hours': 6,
'actual_hours': 0,
},
{
'id': 'task-4',
'project_id': 'proj-1',
'title': '性能测试',
'description': '使用 Lighthouse 进行性能测试,确保加载时间 < 3s',
'status': 'todo',
'priority': 'medium',
'assignees': ['admin'],
'due_date': (now + timedelta(days=20)).isoformat() + 'Z',
'created_at': (now - timedelta(days=5)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=5)).isoformat() + 'Z',
'completed_at': None,
'tags': ['testing', 'performance'],
'estimated_hours': 4,
'actual_hours': 0,
},
# proj-2 的任务
{
'id': 'task-5',
'project_id': 'proj-2',
'title': 'iOS 登录界面开发',
'description': '实现用户登录/注册界面,支持第三方登录',
'status': 'done',
'priority': 'high',
'assignees': ['charlie'],
'due_date': (now + timedelta(days=-10)).isoformat() + 'Z',
'created_at': (now - timedelta(days=30)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=10)).isoformat() + 'Z',
'completed_at': (now - timedelta(days=10)).isoformat() + 'Z',
'tags': ['ios', 'ui'],
'estimated_hours': 12,
'actual_hours': 14,
},
{
'id': 'task-6',
'project_id': 'proj-2',
'title': 'Android 登录界面开发',
'description': '实现 Android 平台的登录界面',
'status': 'in_progress',
'priority': 'high',
'assignees': ['charlie'],
'due_date': (now + timedelta(days=3)).isoformat() + 'Z',
'created_at': (now - timedelta(days=25)).isoformat() + 'Z',
'updated_at': (now - timedelta(hours=6)).isoformat() + 'Z',
'completed_at': None,
'tags': ['android', 'ui'],
'estimated_hours': 12,
'actual_hours': 8,
},
{
'id': 'task-7',
'project_id': 'proj-2',
'title': '推送通知服务集成',
'description': '集成 APNs 和 FCM 推送服务',
'status': 'todo',
'priority': 'medium',
'assignees': ['alice'],
'due_date': (now + timedelta(days=10)).isoformat() + 'Z',
'created_at': (now - timedelta(days=15)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=15)).isoformat() + 'Z',
'completed_at': None,
'tags': ['backend', 'push'],
'estimated_hours': 8,
'actual_hours': 0,
},
# proj-3 的任务
{
'id': 'task-8',
'project_id': 'proj-3',
'title': '分析 API 性能瓶颈',
'description': '使用 profiling 工具找出性能瓶颈',
'status': 'in_progress',
'priority': 'high',
'assignees': ['admin'],
'due_date': (now + timedelta(days=5)).isoformat() + 'Z',
'created_at': (now - timedelta(days=7)).isoformat() + 'Z',
'updated_at': (now - timedelta(hours=1)).isoformat() + 'Z',
'completed_at': None,
'tags': ['analysis'],
'estimated_hours': 8,
'actual_hours': 4,
},
{
'id': 'task-9',
'project_id': 'proj-3',
'title': '实现 Redis 缓存',
'description': '对高频访问的数据添加 Redis 缓存',
'status': 'todo',
'priority': 'high',
'assignees': ['bob'],
'due_date': (now + timedelta(days=12)).isoformat() + 'Z',
'created_at': (now - timedelta(days=3)).isoformat() + 'Z',
'updated_at': (now - timedelta(days=3)).isoformat() + 'Z',
'completed_at': None,
'tags': ['cache', 'redis'],
'estimated_hours': 16,
'actual_hours': 0,
},
]
for task in tasks:
self._tasks[task['id']] = task
# 任务负责人
self._task_assignees = {t['id']: t['assignees'] for t in tasks}
# 评论数据
comments = [
{
'id': 'comment-1',
'task_id': 'task-1',
'user_id': 'admin',
'content': '设计稿已经完成了,可以开始开发了',
'created_at': (now - timedelta(days=18)).isoformat() + 'Z',
},
{
'id': 'comment-2',
'task_id': 'task-1',
'user_id': 'alice',
'content': '收到,我这边开始做前端开发',
'created_at': (now - timedelta(days=17)).isoformat() + 'Z',
},
{
'id': 'comment-3',
'task_id': 'task-2',
'user_id': 'bob',
'content': '首页组件已经完成了 60%,预计后天可以提测',
'created_at': (now - timedelta(hours=5)).isoformat() + 'Z',
},
{
'id': 'comment-4',
'task_id': 'task-6',
'user_id': 'alice',
'content': '登录界面的 UI 有点调整,需要重新对接接口',
'created_at': (now - timedelta(days=2)).isoformat() + 'Z',
},
{
'id': 'comment-5',
'task_id': 'task-8',
'user_id': 'admin',
'content': '初步分析发现 /api/users 和 /api/reports 这两个接口响应较慢',
'created_at': (now - timedelta(hours=8)).isoformat() + 'Z',
},
]
for comment in comments:
self._comments[comment['id']] = comment
def generate_id(self, prefix: str) -> str:
return f"{prefix}-{uuid.uuid4().hex[:8]}"
def generate_slug(self, text: str) -> str:
import re
slug = re.sub(r'[^\w\s-]', '', text.lower())
slug = re.sub(r'[-\s]+', '-', slug)
return slug.strip('-')
# 全局数据库实例
db = Database()
# ============================================================================
# 辅助函数
# ============================================================================
def success_response(data: Any = None, message: str = None, meta: Dict = None, status_code: int = 200):
"""统一成功响应格式"""
response = {'success': True}
if message:
response['message'] = message
if meta:
response['meta'] = meta
if data is not None:
response['data'] = data
# 如果需要返回非 200 状态码,使用 (response, status_code) 格式
if status_code != 200:
return response, status_code
return response
def error_response(message: str, code: str = None, details: Any = None, status_code: int = 400):
"""统一错误响应格式"""
return {
'success': False,
'error': {
'code': code or 'ERROR',
'message': message,
'details': details,
}
}, status_code
def paginate(items: List, page: int = 1, per_page: int = 20):
"""分页处理"""
total = len(items)
start = (page - 1) * per_page
end = start + per_page
return {
'data': items[start:end],
'meta': {
'page': page,
'per_page': per_page,
'total': total,
'total_pages': (total + per_page - 1) // per_page,
'has_next': end < total,
'has_prev': page > 1,
}
}
def get_query_param(request, key: str, default=None, type_func=None):
"""获取查询参数"""
query_string = request.query_string
if not query_string:
return default
params = parse_qs(query_string)
values = params.get(key)
if values is None:
return default
value = values[0] if values else default
if type_func:
try:
return type_func(value)
except (ValueError, TypeError):
return default
return value
def validate_required(data: Dict, fields: List[str]):
"""验证必填字段"""
missing = [f for f in fields if not data.get(f)]
if missing:
raise ValidationError(f"缺少必填字段: {', '.join(missing)}")
class ValidationError(Exception):
pass
# ============================================================================
# API 路由
# ============================================================================
# ----------------------------------------------------------------------------
# 首页
# ----------------------------------------------------------------------------
@get('/', name='index')
def index(request):
"""前端页面"""
template_path = os.path.join(APP_DIR, 'templates', 'index.html')
with open(template_path, 'r', encoding='utf-8') as f:
html_content = f.read()
return Response(html_content, headers=[("Content-Type", "text/html; charset=utf-8")])
@get('/api', name='api_info')
def api_info(request):
"""API 首页"""
return success_response({
'name': '任务管理 API',
'version': '1.0.0',
'description': '团队任务管理系统 RESTful API',
'endpoints': {
'认证': [
{'method': 'POST', 'path': '/api/auth/login', 'desc': '用户登录'},
{'method': 'POST', 'path': '/api/auth/register', 'desc': '用户注册'},
{'method': 'POST', 'path': '/api/auth/refresh', 'desc': '刷新 Token'},
],
'用户': [
{'method': 'GET', 'path': '/api/users', 'desc': '用户列表'},
{'method': 'GET', 'path': '/api/users/{id}', 'desc': '用户详情'},
{'method': 'PUT', 'path': '/api/users/{id}', 'desc': '更新用户'},
],
'项目': [
{'method': 'GET', 'path': '/api/projects', 'desc': '项目列表'},
{'method': 'POST', 'path': '/api/projects', 'desc': '创建项目'},
{'method': 'GET', 'path': '/api/projects/{id}', 'desc': '项目详情'},
{'method': 'PUT', 'path': '/api/projects/{id}', 'desc': '更新项目'},
{'method': 'DELETE', 'path': '/api/projects/{id}', 'desc': '删除项目'},
{'method': 'GET', 'path': '/api/projects/{id}/members', 'desc': '项目成员'},
{'method': 'POST', 'path': '/api/projects/{id}/members', 'desc': '添加成员'},
],
'任务': [
{'method': 'GET', 'path': '/api/tasks', 'desc': '任务列表'},
{'method': 'POST', 'path': '/api/tasks', 'desc': '创建任务'},
{'method': 'GET', 'path': '/api/tasks/{id}', 'desc': '任务详情'},
{'method': 'PUT', 'path': '/api/tasks/{id}', 'desc': '更新任务'},
{'method': 'PATCH', 'path': '/api/tasks/{id}/status', 'desc': '更新状态'},
{'method': 'PATCH', 'path': '/api/tasks/{id}/assignees', 'desc': '分配负责人'},
{'method': 'DELETE', 'path': '/api/tasks/{id}', 'desc': '删除任务'},
],
'评论': [
{'method': 'GET', 'path': '/api/tasks/{id}/comments', 'desc': '任务评论'},
{'method': 'POST', 'path': '/api/tasks/{id}/comments', 'desc': '添加评论'},
{'method': 'DELETE', 'path': '/api/comments/{id}', 'desc': '删除评论'},
],
'统计': [
{'method': 'GET', 'path': '/api/stats/overview', 'desc': '总体概览'},
{'method': 'GET', 'path': '/api/stats/projects/{id}', 'desc': '项目统计'},
],
},
'documentation': {
'swagger_ui': '/docs',
'openapi_spec': '/openapi.json',
}
})
# ----------------------------------------------------------------------------
# 认证相关
# ----------------------------------------------------------------------------
@post('/api/auth/login', name='login')
def login(request):
"""用户登录"""
data = request.json if hasattr(request, 'json') else {}
try:
validate_required(data, ['username', 'password'])
except ValidationError as e:
return error_response(str(e), code='VALIDATION_ERROR')
username = data['username']
password = data['password']
# 查找用户
user = User.get_by_username(username)
if not user:
return error_response('用户名或密码错误', code='AUTH_FAILED', status_code=401)
# 验证密码
from litefs.auth.password import verify_password
if not verify_password(password, user.password_hash):
return error_response('用户名或密码错误', code='AUTH_FAILED', status_code=401)
# 生成 Token
token = auth._jwt.create_access_token({'sub': user.id, 'username': user.username})
refresh_token = auth._jwt.create_refresh_token({'sub': user.id})
return success_response({
'access_token': token,
'refresh_token': refresh_token,
'token_type': 'Bearer',
'user': {
'id': user.id,
'username': user.username,
'email': user.email,
'roles': [r.name for r in user.roles],
}
}, message='登录成功')
@post('/api/auth/register', name='register')
def register(request):
"""用户注册"""
data = request.json if hasattr(request, 'json') else {}
try:
validate_required(data, ['username', 'email', 'password'])
except ValidationError as e:
return error_response(str(e), code='VALIDATION_ERROR')
username = data['username']
email = data['email']
password = data['password']
# 检查用户名是否存在
if User.get_by_username(username):
return error_response('用户名已存在', code='USERNAME_EXISTS')
# 检查邮箱是否存在
if User.get_by_email(email):
return error_response('邮箱已被使用', code='EMAIL_EXISTS')
# 创建用户
user = User.create(
username=username,
email=email,
password_hash=hash_password(password),
roles=[user_role],
)
return success_response({
'id': user.id,
'username': user.username,
'email': user.email,
}, message='注册成功', status_code=201)
@post('/api/auth/refresh', name='refresh_token')
def refresh_token(request):
"""刷新 Token"""
data = request.json if hasattr(request, 'json') else {}
refresh_token_str = data.get('refresh_token')
if not refresh_token_str:
return error_response('缺少 refresh_token', code='MISSING_TOKEN')
# 验证并生成新 Token
payload = auth._jwt.decode_token(refresh_token_str)
if not payload or payload.get('type') != 'refresh':
return error_response('无效的 refresh_token', code='INVALID_TOKEN', status_code=401)
user_id = payload.get('sub')
user = User.get_by_id(user_id)
if not user:
return error_response('用户不存在', code='USER_NOT_FOUND', status_code=401)
new_token = auth._jwt.create_access_token({'sub': user.id, 'username': user.username})
return success_response({
'access_token': new_token,
'token_type': 'Bearer',
})
@get('/api/auth/me', name='current_user')
@login_required
def get_current_user(request):
"""获取当前用户信息"""
current_user = request.user
# 获取用户参与的项目数量
user_projects = sum(1 for members in db._project_members.values() if current_user.username in members)
# 获取用户负责的任务数量
user_tasks = sum(1 for task in db._tasks.values() if current_user.username in task['assignees'])
return success_response({
'id': current_user.id,
'username': current_user.username,
'email': current_user.email,
'roles': [r.name for r in current_user.roles],
'stats': {
'projects': user_projects,
'tasks': user_tasks,
}
})
# ----------------------------------------------------------------------------
# 用户管理
# ----------------------------------------------------------------------------
@get('/api/users', name='list_users')
@login_required
def list_users(request):
"""获取用户列表"""
page = get_query_param(request, 'page', 1, int)
per_page = get_query_param(request, 'per_page', 20, int)
search = get_query_param(request, 'search', '')
# 获取所有用户 (模拟)
users = []
for username in ['admin', 'alice', 'bob', 'charlie']:
user = User.get_by_username(username)
if user:
if not search or search.lower() in username.lower() or search.lower() in user.email.lower():
users.append({
'id': user.id,
'username': user.username,
'email': user.email,
'roles': [r.name for r in user.roles],
})
result = paginate(users, page, per_page)
return success_response(**result)
@get('/api/users/{user_id}', name='get_user')
@login_required
def get_user(request, user_id):
"""获取用户详情"""
user = User.get_by_id(user_id)
if not user:
return error_response('用户不存在', code='NOT_FOUND', status_code=404)
# 获取用户参与的项目
user_projects = []
for proj_id, members in db._project_members.items():
if user.username in members:
proj = db._projects.get(proj_id)
if proj:
user_projects.append({
'id': proj['id'],
'name': proj['name'],
'role': 'owner' if proj['owner_id'] == user.username else 'member',
})
# 获取用户负责的任务
user_tasks = [t for t in db._tasks.values() if user.username in t['assignees']]
return success_response({
'id': user.id,
'username': user.username,
'email': user.email,
'roles': [r.name for r in user.roles],
'projects': user_projects,
'tasks': {
'total': len(user_tasks),
'todo': sum(1 for t in user_tasks if t['status'] == 'todo'),
'in_progress': sum(1 for t in user_tasks if t['status'] == 'in_progress'),
'done': sum(1 for t in user_tasks if t['status'] == 'done'),
}
})
@put('/api/users/{user_id}', name='update_user')
@login_required
def update_user(request, user_id):
"""更新用户信息"""
current_user = request.user
# 只有用户自己或管理员可以更新
if current_user.id != user_id and 'admin' not in [r.name for r in current_user.roles]:
return error_response('无权限', code='FORBIDDEN', status_code=403)
user = User.get_by_id(user_id)
if not user:
return error_response('用户不存在', code='NOT_FOUND', status_code=404)
data = request.json if hasattr(request, 'json') else {}
# 更新字段
if 'email' in data:
user.email = data['email']
if 'password' in data:
user.password_hash = hash_password(data['password'])
return success_response({
'id': user.id,
'username': user.username,
'email': user.email,
}, message='更新成功')
# ----------------------------------------------------------------------------
# 项目管理
# ----------------------------------------------------------------------------
@get('/api/projects', name='list_projects')
@login_required
def list_projects(request):
"""获取项目列表"""
page = get_query_param(request, 'page', 1, int)
per_page = get_query_param(request, 'per_page', 20, int)
status = get_query_param(request, 'status')
search = get_query_param(request, 'search', '')
current_user = request.user
is_admin = 'admin' in [r.name for r in current_user.roles]
projects = []
for proj in db._projects.values():
# 非管理员只能看到自己是成员的项目
if not is_admin and current_user.username not in db._project_members.get(proj['id'], []):
continue
# 过滤
if status and proj['status'] != status:
continue
if search:
if search.lower() not in proj['name'].lower() and search.lower() not in proj['description'].lower():
continue
# 获取统计数据
project_tasks = [t for t in db._tasks.values() if t['project_id'] == proj['id']]
projects.append({
**proj,
'owner': User.get_by_username(proj['owner_id']).id if proj['owner_id'] else None,
'member_count': len(db._project_members.get(proj['id'], [])),
'task_stats': {
'total': len(project_tasks),
'todo': sum(1 for t in project_tasks if t['status'] == 'todo'),
'in_progress': sum(1 for t in project_tasks if t['status'] == 'in_progress'),
'done': sum(1 for t in project_tasks if t['status'] == 'done'),
}
})
# 按更新时间排序
projects.sort(key=lambda x: x['updated_at'], reverse=True)
result = paginate(projects, page, per_page)
return success_response(**result)
@post('/api/projects', name='create_project')
@login_required
def create_project(request):
"""创建项目"""
current_user = request.user
data = request.json if hasattr(request, 'json') else {}
try:
validate_required(data, ['name'])
except ValidationError as e:
return error_response(str(e), code='VALIDATION_ERROR')
project_id = db.generate_id('proj')
now = datetime.now().isoformat() + 'Z'
project = {
'id': project_id,
'name': data['name'],
'slug': db.generate_slug(data['name']),
'description': data.get('description', ''),
'owner_id': current_user.username,
'status': data.get('status', 'planning'),
'color': data.get('color', '#3498db'),
'created_at': now,
'updated_at': now,
}
db._projects[project_id] = project
db._project_members[project_id] = [current_user.username]
return success_response(project, message='项目创建成功', status_code=201)
@get('/api/projects/{project_id}', name='get_project')
@login_required
def get_project(request, project_id):
"""获取项目详情"""
current_user = request.user
is_admin = 'admin' in [r.name for r in current_user.roles]
project = db._projects.get(project_id)
if not project:
return error_response('项目不存在', code='NOT_FOUND', status_code=404)
# 权限检查
if not is_admin and current_user.username not in db._project_members.get(project_id, []):
return error_response('无权限访问该项目', code='FORBIDDEN', status_code=403)
# 获取成员信息
members = []
for username in db._project_members.get(project_id, []):
user = User.get_by_username(username)
if user:
members.append({
'id': user.id,
'username': user.username,
'email': user.email,
'role': 'owner' if project['owner_id'] == username else 'member',
})
# 获取任务统计
project_tasks = [t for t in db._tasks.values() if t['project_id'] == project_id]
return success_response({
**project,
'owner': {
'id': User.get_by_username(project['owner_id']).id,
'username': project['owner_id'],
} if project['owner_id'] else None,
'members': members,
'task_stats': {
'total': len(project_tasks),
'todo': sum(1 for t in project_tasks if t['status'] == 'todo'),
'in_progress': sum(1 for t in project_tasks if t['status'] == 'in_progress'),
'done': sum(1 for t in project_tasks if t['status'] == 'done'),
'overdue': sum(1 for t in project_tasks if t['status'] != 'done' and t.get('due_date') and t['due_date'] < datetime.now().isoformat() + 'Z'),
},
'recent_tasks': project_tasks[:5], # 最近的任务
})
@put('/api/projects/{project_id}', name='update_project')
@login_required
def update_project(request, project_id):
"""更新项目"""
current_user = request.user
is_admin = 'admin' in [r.name for r in current_user.roles]
project = db._projects.get(project_id)
if not project:
return error_response('项目不存在', code='NOT_FOUND', status_code=404)
# 只有所有者或管理员可以更新
if project['owner_id'] != current_user.username and not is_admin:
return error_response('无权限', code='FORBIDDEN', status_code=403)
data = request.json if hasattr(request, 'json') else {}
# 可更新字段
updatable = ['name', 'description', 'status', 'color']
for field in updatable:
if field in data:
project[field] = data[field]
if 'name' in data:
project['slug'] = db.generate_slug(data['name'])
project['updated_at'] = datetime.now().isoformat() + 'Z'
return success_response(project, message='项目更新成功')
@delete('/api/projects/{project_id}', name='delete_project')
@role_required('admin')
def delete_project(request, project_id):
"""删除项目(仅管理员)"""
if project_id not in db._projects:
return error_response('项目不存在', code='NOT_FOUND', status_code=404)
# 删除项目关联的任务和评论
task_ids = [tid for tid, t in db._tasks.items() if t['project_id'] == project_id]
for tid in task_ids:
# 删除任务的评论
for cid in list(db._comments.keys()):
if db._comments[cid]['task_id'] == tid:
del db._comments[cid]
del db._tasks[tid]
# 删除项目成员关系
if project_id in db._project_members:
del db._project_members[project_id]
# 删除项目
del db._projects[project_id]
return success_response(None, message='项目已删除')
@get('/api/projects/{project_id}/members', name='project_members')
@login_required
def get_project_members(request, project_id):
"""获取项目成员"""
current_user = request.user
project = db._projects.get(project_id)
if not project:
return error_response('项目不存在', code='NOT_FOUND', status_code=404)
members = []
for username in db._project_members.get(project_id, []):
user = User.get_by_username(username)
if user:
# 统计该成员在此项目中的任务
user_tasks = [t for t in db._tasks.values()
if t['project_id'] == project_id and username in t['assignees']]
members.append({
'id': user.id,
'username': user.username,
'email': user.email,
'role': 'owner' if project['owner_id'] == username else 'member',
'task_count': len(user_tasks),
'completed_count': sum(1 for t in user_tasks if t['status'] == 'done'),
})
return success_response({'members': members, 'total': len(members)})
@post('/api/projects/{project_id}/members', name='add_member')
@login_required
def add_member(request, project_id):
"""添加项目成员"""
current_user = request.user
project = db._projects.get(project_id)
if not project:
return error_response('项目不存在', code='NOT_FOUND', status_code=404)
# 只有所有者或管理员可以添加成员
is_admin = 'admin' in [r.name for r in current_user.roles]
if project['owner_id'] != current_user.username and not is_admin:
return error_response('无权限', code='FORBIDDEN', status_code=403)
data = request.json if hasattr(request, 'json') else {}
try:
validate_required(data, ['username'])
except ValidationError as e:
return error_response(str(e), code='VALIDATION_ERROR')
username = data['username']
# 检查用户是否存在
user = User.get_by_username(username)
if not user:
return error_response('用户不存在', code='USER_NOT_FOUND', status_code=404)
# 检查是否已是成员
if username in db._project_members.get(project_id, []):
return error_response('该用户已是项目成员', code='ALREADY_MEMBER')