Initialize Fonrey Django multi-tenant project skeleton
Set up the required directory layout, app scaffolding, core settings, templates, static assets, and Docker/Tailwind tooling to establish a standardized development baseline.
This commit is contained in:
0
core/__init__.py
Normal file
0
core/__init__.py
Normal file
13
core/cache.py
Normal file
13
core/cache.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from decouple import config as env
|
||||
|
||||
|
||||
class RedisCache:
|
||||
"""Redis 工具骨架。"""
|
||||
|
||||
@staticmethod
|
||||
def get_client_url() -> str:
|
||||
return env("REDIS_URL", default="redis://127.0.0.1:6379/0")
|
||||
|
||||
@staticmethod
|
||||
def make_key(*parts: str) -> str:
|
||||
return ":".join(["fonrey", *parts])
|
||||
35
core/encryption.py
Normal file
35
core/encryption.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import base64
|
||||
import hashlib
|
||||
|
||||
from cryptography.fernet import Fernet
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class PhoneEncryption:
|
||||
"""
|
||||
手机号 AES-256-GCM 加密存储 + SHA-256 哈希索引
|
||||
存储字段:phone_encrypted(加密密文)+ phone_hash(哈希,用于精确查询)
|
||||
显示:脱敏格式 138****1234
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def encrypt(phone: str) -> str:
|
||||
"""加密手机号,返回 base64 密文"""
|
||||
...
|
||||
|
||||
@staticmethod
|
||||
def decrypt(ciphertext: str) -> str:
|
||||
"""解密返回明文"""
|
||||
...
|
||||
|
||||
@staticmethod
|
||||
def hash(phone: str) -> str:
|
||||
"""返回 SHA-256 哈希(用于 DB 索引查询)"""
|
||||
...
|
||||
|
||||
@staticmethod
|
||||
def mask(phone: str) -> str:
|
||||
"""返回脱敏格式:138****1234"""
|
||||
if not phone or len(phone) < 7:
|
||||
return "***"
|
||||
return phone[:3] + "****" + phone[-4:]
|
||||
15
core/htmx.py
Normal file
15
core/htmx.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from django.http import HttpResponse
|
||||
|
||||
|
||||
def htmx_response(content="", status=200, toast=None, redirect=None):
|
||||
"""
|
||||
toast: {"type": "success|error|warning|info", "message": "..."}
|
||||
"""
|
||||
response = HttpResponse(content, status=status)
|
||||
if toast:
|
||||
import json
|
||||
|
||||
response["HX-Trigger"] = json.dumps({"fonrey:toast": toast})
|
||||
if redirect:
|
||||
response["HX-Redirect"] = redirect
|
||||
return response
|
||||
0
core/middleware/__init__.py
Normal file
0
core/middleware/__init__.py
Normal file
9
core/middleware/audit.py
Normal file
9
core/middleware/audit.py
Normal file
@@ -0,0 +1,9 @@
|
||||
class AuditMiddleware:
|
||||
"""审计日志中间件骨架。"""
|
||||
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
response = self.get_response(request)
|
||||
return response
|
||||
0
core/models/__init__.py
Normal file
0
core/models/__init__.py
Normal file
80
core/models/base.py
Normal file
80
core/models/base.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import uuid
|
||||
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
class UUIDPrimaryKeyModel(models.Model):
|
||||
"""所有业务模型的根基类:UUID v4 主键"""
|
||||
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class TimeStampedModel(UUIDPrimaryKeyModel):
|
||||
"""追加创建/更新时间(TIMESTAMPTZ)"""
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
ordering = ["-created_at"]
|
||||
|
||||
|
||||
class ActiveManager(models.Manager):
|
||||
"""默认只返回未软删除的记录"""
|
||||
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(deleted_at__isnull=True)
|
||||
|
||||
|
||||
class SoftDeleteModel(TimeStampedModel):
|
||||
"""软删除:deleted_at=NULL 表示未删除"""
|
||||
|
||||
deleted_at = models.DateTimeField(null=True, blank=True, db_index=True)
|
||||
objects = ActiveManager()
|
||||
all_objects = models.Manager()
|
||||
|
||||
def delete(self, using=None, keep_parents=False):
|
||||
self.deleted_at = timezone.now()
|
||||
self.save(update_fields=["deleted_at"])
|
||||
|
||||
def hard_delete(self):
|
||||
super().delete()
|
||||
|
||||
def restore(self):
|
||||
self.deleted_at = None
|
||||
self.save(update_fields=["deleted_at"])
|
||||
|
||||
@property
|
||||
def is_deleted(self):
|
||||
return self.deleted_at is not None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class AuditedModel(SoftDeleteModel):
|
||||
"""审计字段:操作人(FK to Staff,允许 NULL 表示系统操作)"""
|
||||
|
||||
created_by = models.ForeignKey(
|
||||
"org.Staff",
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name="%(app_label)s_%(class)s_created",
|
||||
db_index=True,
|
||||
)
|
||||
updated_by = models.ForeignKey(
|
||||
"org.Staff",
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name="%(app_label)s_%(class)s_updated",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
0
core/templatetags/__init__.py
Normal file
0
core/templatetags/__init__.py
Normal file
21
core/templatetags/heroicons.py
Normal file
21
core/templatetags/heroicons.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import os
|
||||
|
||||
from django import template
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
register = template.Library()
|
||||
ICONS_PATH = os.path.join(os.path.dirname(__file__), "..", "static", "icons")
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def heroicon(name: str, size: str = "24", style: str = "outline", css_class: str = "") -> str:
|
||||
"""
|
||||
用法: {% heroicon 'plus' %}
|
||||
{% heroicon 'trash' size='20' style='solid' css_class='text-danger-600' %}
|
||||
"""
|
||||
css = (
|
||||
f'class="w-{size//4 if isinstance(size, int) else size} '
|
||||
f'h-{size//4 if isinstance(size, int) else size} {css_class}"'
|
||||
)
|
||||
_ = css
|
||||
return mark_safe(f"<!-- heroicon:{style}/{name} -->")
|
||||
Reference in New Issue
Block a user