Files
file_message_transfer/main/models.py
2025-01-05 10:45:32 +08:00

64 lines
2.5 KiB
Python
Raw Permalink 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.
from django.db import models
from django.contrib.auth.models import User
class File(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='files')
file = models.FileField(upload_to='uploads/')
shared_with = models.ManyToManyField(User, related_name='shared_files', blank=True)
created_at = models.DateTimeField(auto_now_add=True)
description = models.TextField(blank=True)
def __str__(self):
return f"{self.file.name}{self.owner.username}"
class Message(models.Model):
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sent_messages')
recipients = models.ManyToManyField(User, related_name='received_messages')
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
file = models.ForeignKey(File, on_delete=models.SET_NULL, null=True, blank=True)
def __str__(self):
return f"来自 {self.sender.username} 的消息"
class Meta:
ordering = ['-created_at']
class PublicMessage(models.Model):
name = models.CharField(max_length=100, default="公共", editable=False)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
ip_address = models.GenericIPAddressField(null=True, blank=True)
def __str__(self):
return f"来自 {self.name or '匿名用户'} 的公开留言"
class PublicFile(models.Model):
name = models.CharField(max_length=100, default="公共", editable=False)
file = models.FileField(upload_to='public_uploads/')
description = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
ip_address = models.GenericIPAddressField(null=True, blank=True)
def __str__(self):
return f"来自 {self.name or '匿名用户'} 的公开文件"
class Friendship(models.Model):
STATUS_CHOICES = [
('pending', '等待中'),
('accepted', '已接受'),
('rejected', '已拒绝'),
]
from_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='friendship_requests_sent')
to_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='friendship_requests_received')
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='pending')
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('from_user', 'to_user')
ordering = ['-created_at']
def __str__(self):
return f"{self.from_user.username} -> {self.to_user.username} ({self.status})"