from django.db import models
from folder.models import Folder
from account.models import User
from group.models import Group

# 文件表
class File(models.Model):
    # 文件id
    file_id = models.AutoField(primary_key=True)
    # 文件名
    file_name = models.CharField(max_length=50, blank=False)
    # 从属的文件夹
    folder = models.ForeignKey(Folder, on_delete=models.CASCADE, related_name='children_files')
    # 上传时间
    update_time = models.DateTimeField()
    # 文件类型
    file_type = models.CharField(max_length=32)
    # 文件大小
    file_size = models.CharField(max_length=16)

    owner = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name='files')

    group = models.ForeignKey(Group, on_delete=models.DO_NOTHING, related_name='files', null=True)

    def to_json(self):
        return {'file_id': self.file_id,
                'file_name': self.file_name,
                'folder_id': self.folder_id,
                'update_time': self.update_time,
                'file_type': self.file_type,
                'file_size': self.file_size}
