init. 初始化项目,并初步完成前端模板样式
This commit is contained in:
Executable
+48
@@ -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
|
||||
@@ -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}"
|
||||
@@ -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
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user