1
0
forked from erp-dev/erp
Files
erpnew/api_v1/models.py
2025-11-17 14:34:09 +08:00

85 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
API v1 通用模型
"""
import os
import uuid
from django.db import models
from django.contrib.auth import get_user_model
from flower.common import ModelBase
User = get_user_model()
def upload_file_path(instance, filename):
"""
生成随机化的文件上传路径
格式: uploads/YYYY/MM/DD/uuid.ext
"""
# 获取文件扩展名
ext = os.path.splitext(filename)[1].lower()
# 生成随机文件名
random_filename = f"{uuid.uuid4().hex}{ext}"
# 返回完整路径
from datetime import datetime
now = datetime.now()
return f"uploads/{now.year}/{now.month:02d}/{now.day:02d}/{random_filename}"
class UploadedFile(ModelBase):
"""
通用文件上传记录
用于存储无法归类到具体业务的文件上传记录
"""
path = models.FileField(
upload_to=upload_file_path,
max_length=500,
verbose_name='文件路径',
help_text='文件存储路径,包含前缀、随机文件名和后缀'
)
owner = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='uploaded_files',
verbose_name='上传者'
)
is_deleted = models.BooleanField(
default=False,
verbose_name='是否已删除',
help_text='软删除标记'
)
original_filename = models.CharField(
max_length=255,
blank=True,
verbose_name='原始文件名',
help_text='用户上传时的原始文件名'
)
file_size = models.BigIntegerField(
null=True,
blank=True,
verbose_name='文件大小(字节)'
)
content_type = models.CharField(
max_length=100,
blank=True,
verbose_name='文件类型',
help_text='MIME类型如 image/jpeg'
)
class Meta:
db_table = 'api_uploaded_file'
verbose_name = '上传文件'
verbose_name_plural = '上传文件'
ordering = ['-created_at']
def __str__(self):
return f"{self.original_filename or self.path.name} (by {self.owner.username})"
@property
def file_url(self):
"""获取文件访问URL"""
if self.path:
return self.path.url
return None