init. 初始化项目,并初步完成前端模板样式

This commit is contained in:
2026-03-26 11:08:42 +08:00
commit af9d7a676f
116 changed files with 5266 additions and 0 deletions
View File
+48
View File
@@ -0,0 +1,48 @@
from typing import Optional, TypeVar, Generic
from ninja import Schema
T = TypeVar('T')
class CommonResponse(Schema, Generic[T]):
code: int
msg: Optional[str]
data: Optional[T] = None
succeed: bool = True
"""
统一的json返回格式
"""
def __init__(self, code: int = 0, data: object = None, msg: str = '', succeed: bool = True):
super().__init__(code=code, data=data, msg=msg, succeed=succeed)
@classmethod
def success(cls, code=0, data=None, msg=''):
return cls(code, data, msg, succeed=True)
@classmethod
def error(cls, code=-1, data=None, msg=''):
return cls(code, data, msg, succeed=False)
def to_dict(self):
return {
"code": self.code,
"msg": self.msg,
"data": self.data,
"succeed": self.succeed,
}
class DotDict(dict):
"""实现支持 "." 表示法的字典类"""
def __getattr__(self, attr):
try:
return self[attr]
except Exception:
return None
def __setattr__(self, attr, value):
self[attr] = value
+23
View File
@@ -0,0 +1,23 @@
from datetime import datetime
def get_now_strftime_format():
"""
返回当前时间的格式化字符串:'2026年3月25日 星期三'
特点:
1. 自动去除月、日的前导零 (如 3月 而非 03月)
2. 强制输出 '星期X' 格式 (避免系统差异导致输出 '周X')
"""
now = datetime.now()
# 星期映射列表 (weekday(): 0=周一, ..., 6=周日)
weekdays = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
# 使用 strftime 获取年份,直接属性获取月日(自动无前导零),列表获取星期
# %Y: 四位年份
year_str = now.strftime("%Y")
month_val = now.month # 整数,自动无前置0
day_val = now.day # 整数,自动无前置0
weekday_str = weekdays[now.weekday()]
return f"{year_str}{month_val}{day_val}{weekday_str}"
+15
View File
@@ -0,0 +1,15 @@
# utils/notice.py
from article.models import Notice
def get_user_notices(user):
"""获取用户有权查看且需签收的通知"""
if user.dept:
notices = Notice.objects.filter(
target_departments=user.dept,
require_sign=True,
is_published=True
).distinct()
else:
notices = Notice.objects.none()
return notices
+9
View File
@@ -0,0 +1,9 @@
def user_has_permission(user, resource, action):
if user.is_superuser:
return True
if not user.is_active:
return False
return user.roles.filter(
permissions__resource=resource,
permissions__action=action
).exists()