|
| 1 | +from django.db import models |
| 2 | +from django.contrib.auth.models import User |
| 3 | +from django.urls import reverse |
| 4 | + |
| 5 | +# Create your models here. |
| 6 | + |
| 7 | + |
| 8 | +class Article(models.Model): |
| 9 | + |
| 10 | + title = models.CharField(max_length=100) |
| 11 | + body = models.TextField() |
| 12 | + author = models.ForeignKey(User, on_delete=models.CASCADE) |
| 13 | + create_at = models.DateTimeField(auto_now_add=True) # Add Date Automatically |
| 14 | + |
| 15 | + def __str__(self): |
| 16 | + return self.title |
| 17 | + |
| 18 | + |
| 19 | +class Comment(models.Model): |
| 20 | + |
| 21 | + article = models.ForeignKey( |
| 22 | + Article, |
| 23 | + null=True, |
| 24 | + blank=False, |
| 25 | + on_delete=models.CASCADE, |
| 26 | + related_name="comments", |
| 27 | + ) |
| 28 | + writer = models.ForeignKey(User, on_delete=models.CASCADE) |
| 29 | + content = models.CharField(max_length=100) |
| 30 | + date = models.DateTimeField(auto_now_add=True) |
| 31 | + |
| 32 | + illegal_words = ["Fuck", "Ugly", "Bad"] |
| 33 | + |
| 34 | + def filter_illegal_words(self, text): |
| 35 | + for word in self.illegal_words: |
| 36 | + if word.lower() in text.lower(): |
| 37 | + masked = "*" * len(word) |
| 38 | + text = text.replace(word.lower(), masked).replace( |
| 39 | + word.capitalize(), masked |
| 40 | + ) |
| 41 | + return text |
| 42 | + |
| 43 | + def save(self, *args, **kwargs): |
| 44 | + self.content = self.filter_illegal_words(self.content) |
| 45 | + super().save(*args, **kwargs) |
| 46 | + |
| 47 | + def __str__(self): |
| 48 | + return self.content |
| 49 | + |
| 50 | + def get_absolute_url(self): |
| 51 | + return reverse("article_details", kwargs={"pk": self.article.pk}) |
0 commit comments