updated to selfdelete inheritance

This commit is contained in:
2026-04-19 01:36:03 +02:00
parent 043e866ac9
commit 5280a87e8b
7 changed files with 151 additions and 58 deletions

View File

@@ -1,12 +1,14 @@
from django.db import models
from django.conf import settings
from vontor_cz.models import SoftDeleteModel
import magic
mime = magic.Magic(mime=True)
class Post(models.Model):
class Post(SoftDeleteModel):
AUTHOR_FIELD = 'author'
content = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
@@ -23,11 +25,19 @@ class Post(models.Model):
null=True,
blank=True
)
reply_to = models.ForeignKey(
'self',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='replies'
)
def __str__(self):
return f"Post {self.id}"
class PostContent(models.Model):
class PostContent(SoftDeleteModel):
post = models.ForeignKey(Post, related_name='contents', on_delete=models.CASCADE)
mime_type = models.CharField(max_length=100)
file = models.FileField(upload_to='post_contents/', null=True, blank=True) # For images/videos
@@ -39,4 +49,22 @@ class PostContent(models.Model):
def save(self, *args, **kwargs):
if self.file and not self.file._committed:
self.mime_type = mime.from_buffer(self.file.read(1024))
return super().save(*args, **kwargs)
return super().save(*args, **kwargs)
class PostVote(SoftDeleteModel):
class VoteChoice(models.IntegerChoices):
UP = 1, 'Upvote'
DOWN = -1, 'Downvote'
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='votes')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
vote = models.SmallIntegerField(choices=VoteChoice.choices)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('post', 'user')
def __str__(self):
label = 'up' if self.vote == self.VoteChoice.UP else 'down'
return f"{self.user} voted {label} on Post {self.post_id}"