from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver from django.urls import reverse from django.utils import timezone class Board(models.Model): # The short URL name for the board url = models.CharField(max_length=255, null=False, blank=False, unique=True) # Human-readable name for the board name = models.CharField(max_length=255, null=False, blank=False) @property def threads(self): return Post.objects.filter(board=self, op=None) class Post(models.Model): # Board that this post was made on board = models.ForeignKey("Board", on_delete=models.CASCADE) # Thread that this is a part of op = models.ForeignKey( "self", null=True, on_delete=models.CASCADE, related_name="all_replies" ) # Post that this is replying to reply = models.ForeignKey( "self", null=True, on_delete=models.CASCADE, related_name="replies" ) # User's supplied name for this post name = models.CharField(max_length=255, null=True, blank=True) # User's supplied subject for this post subject = models.CharField(max_length=255, null=True, blank=True) # Text of this post text = models.TextField(max_length=10000, null=False, blank=True) # The IP address of the user that made this post ip = models.GenericIPAddressField() # Creation time created = models.DateTimeField(auto_now_add=True) # Last bump time last_bump = models.DateTimeField(auto_now_add=True) # TODO : images def get_absolute_url(self): if self.op is None: return reverse( "board:post_detail", kwargs={"url": self.board.url, "id": self.id} ) else: return ( reverse( "board:post_detail", kwargs={"url": self.board.url, "id": self.op.id}, ) + f"#p{self.id}" ) @receiver(post_save, sender=Post) def post_created(sender, instance, created, **kwargs): if created and instance.op: instance.op.last_bump = timezone.now() instance.op.save()