Created
July 1, 2016 13:24
-
-
Save keimlink/15cd154153feba0e1e3ab7dbaa262bc1 to your computer and use it in GitHub Desktop.
euth/comments/models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/euth/comments/models.py b/euth/comments/models.py | |
index 32ee49e..4becef2 100644 | |
--- a/euth/comments/models.py | |
+++ b/euth/comments/models.py | |
@@ -8,6 +8,8 @@ from django.utils import timezone | |
class Comment(TimeStampedModel): | |
+ REMOVED_TEXT = 'deleted by creator' | |
+ CENSORED_TEXT = 'deleted by moderator' | |
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) | |
object_pk = models.PositiveIntegerField() | |
@@ -17,7 +19,9 @@ class Comment(TimeStampedModel): | |
settings.AUTH_USER_MODEL, on_delete=models.CASCADE) | |
comment = models.TextField(max_length=1024) | |
is_removed = models.BooleanField(default=False) | |
+ removed_date = models.DateTimeField() | |
is_censored = models.BooleanField(default=False) | |
+ censored_date = models.DateTimeField() | |
def __str__(self): | |
return str(self.created) | |
@@ -27,9 +31,21 @@ class Comment(TimeStampedModel): | |
Change the text of the comment if | |
the comment was marked removed or censored | |
""" | |
- | |
- if self.is_removed: | |
- self.comment = 'deleted by creator' | |
- if self.is_censored: | |
- self.comment = 'deleted by moderator' | |
+ self.remove(commit=False) | |
+ self.censor(commit=False) | |
return super(Comment, self).save(*args, **kwargs) | |
+ | |
+ def remove(self, commit=True): | |
+ remove = not self.removed_date and self.is_removed | |
+ force_remove = self.removed_date and self.comment != self.REMOVED_TEXT | |
+ if remove or force_remove: | |
+ self.comment = self.REMOVED_TEXT | |
+ self.user = User.objects.get_or_create(username='unknown') | |
+ if commit: | |
+ self.save() | |
+ | |
+ def censor(self, commit=True): | |
+ if not self.censored_date and self.is_censored: | |
+ self.comment = self.CENSORED_TEXT | |
+ if commit: | |
+ self.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment