From ba38bd1ba63e7ae652b73551a30c9aee384b0e45 Mon Sep 17 00:00:00 2001 From: azeezdot123 Date: Mon, 26 Jul 2021 04:16:50 +0100 Subject: [PATCH] Refactored the models to follow DRY principle --- article/models.py | 21 ++++++++++----------- discussion/models.py | 29 ++++++++++++++--------------- feed/models.py | 11 +++++++---- message/models.py | 13 ++++++++----- 4 files changed, 39 insertions(+), 35 deletions(-) diff --git a/article/models.py b/article/models.py index 537a96a..093cff4 100644 --- a/article/models.py +++ b/article/models.py @@ -5,37 +5,36 @@ import uuid -class Article(models.Model): +class BaseModel(models.Model): id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) - user = models.ForeignKey(User,on_delete=models.SET_NULL, null=True, blank=True) + created = models.DateTimeField(auto_now_add=True) + user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) + + class Meta: + abstract = True + +class Article(BaseModel): title = models.CharField(max_length=500, default="untitled") content = RichTextField(max_length=10000) # discussion tags from user model tags = models.ManyToManyField(TopicTag, related_name='article_tags', blank=True) - created = models.DateTimeField(auto_now_add=True) def __str__(self): return str(self.title) -class ArticleComment(models.Model): - id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) +class ArticleComment(BaseModel): article = models.ForeignKey(Article,on_delete=models.CASCADE) - user = models.ForeignKey(User,on_delete=models.SET_NULL, null=True, blank=True) content = models.TextField(max_length=1000) - created = models.DateTimeField(auto_now_add=True) def __str__(self): return str(self.user.username) -class ArticleVote(models.Model): - id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) - user = models.ForeignKey(User,on_delete=models.SET_NULL, null=True, blank=True) +class ArticleVote(BaseModel): article = models.ForeignKey(Article, on_delete=models.CASCADE) comment = models.ForeignKey(ArticleComment, on_delete=models.SET_NULL,null=True, blank=True) value = models.IntegerField(blank=True, null=True, default=0) - created = models.DateTimeField(auto_now_add=True) def __str__(self): return f"{self.article} - count - {self.value}" diff --git a/discussion/models.py b/discussion/models.py index 0b15104..9cca079 100644 --- a/discussion/models.py +++ b/discussion/models.py @@ -5,37 +5,36 @@ import uuid -class Discussion(models.Model): +class BaseModel(models.Model): id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) - user = models.ForeignKey(User,on_delete=models.SET_NULL, null=True, blank=True) + created = models.DateTimeField(auto_now_add=True) + user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) + class Meta: + abstract = True + + +class Discussion(BaseModel): headline = models.CharField(max_length=500, default="no headline") content = RichTextField(max_length=10000) # discussion tags from user model - tags = models.ManyToManyField(TopicTag, related_name='discussion_tags', blank=True) - created = models.DateTimeField(auto_now_add=True) + tags = models.ManyToManyField(TopicTag, related_name='discussion_tags', blank=True) def __str__(self): return str(self.headline) -class DiscussionComment(models.Model): - id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) - discussion = models.ForeignKey(Discussion,on_delete=models.CASCADE) - user = models.ForeignKey(User,on_delete=models.SET_NULL, null=True, blank=True) +class DiscussionComment(BaseModel): + discussion = models.ForeignKey(Discussion, on_delete=models.CASCADE) content = models.TextField(max_length=1000) - created = models.DateTimeField(auto_now_add=True) def __str__(self): return str(self.user.username) -class DiscussionVote(models.Model): - id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) - user = models.ForeignKey(User,on_delete=models.SET_NULL, null=True, blank=True) +class DiscussionVote(BaseModel): discussion = models.ForeignKey(Discussion, on_delete=models.CASCADE) - comment = models.ForeignKey(DiscussionComment, on_delete=models.SET_NULL,null=True, blank=True) + comment = models.ForeignKey(DiscussionComment, on_delete=models.SET_NULL, null=True, blank=True) value = models.IntegerField(blank=True, null=True, default=0) - created = models.DateTimeField(auto_now_add=True) def __str__(self): - return f"{self.discussion} - count - {self.value}" + return f"{self.discussion} - count - {self.value}" \ No newline at end of file diff --git a/feed/models.py b/feed/models.py index 0574dab..2115bb6 100644 --- a/feed/models.py +++ b/feed/models.py @@ -4,8 +4,13 @@ import uuid +class BaseModel(models.Model): + id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) + class Meta: + abstract = True + #This needs to be shareable -class Mumble(models.Model): +class Mumble(BaseModel): parent =models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True) #For re-mumble (Share) functionality remumble = models.ForeignKey("self", on_delete=models.CASCADE, related_name='remumbles', null=True, blank=True) @@ -18,7 +23,6 @@ class Mumble(models.Model): share_count = models.IntegerField(blank=True, null=True, default=0) created = models.DateTimeField(auto_now_add=True) votes = models.ManyToManyField(User, related_name='mumble_user', blank=True, through='MumbleVote') - id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) class Meta: ordering = ['-created'] @@ -43,7 +47,7 @@ def comments(self): -class MumbleVote(models.Model): +class MumbleVote(BaseModel): CHOICES = ( ('upvote', 'upvote'), @@ -53,7 +57,6 @@ class MumbleVote(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True) mumble = models.ForeignKey(Mumble, on_delete=models.CASCADE, null=True, blank=True) value = models.CharField(max_length=20, choices=CHOICES) - id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) def __str__(self): return str(self.user) + ' ' + str(self.value) + '"' + str(self.mumble) + '"' diff --git a/message/models.py b/message/models.py index 5b3abd1..47ec65d 100644 --- a/message/models.py +++ b/message/models.py @@ -2,25 +2,28 @@ from users.models import UserProfile import uuid -class Thread(models.Model): + +class BaseModel(models.Model): id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) + timestamp = models.DateTimeField(auto_now_add=True) + + class Meta: + abstract = True +class Thread(BaseModel): sender = models.ForeignKey(UserProfile,on_delete=models.SET_NULL,null=True,related_name="sender") reciever = models.ForeignKey(UserProfile,on_delete=models.SET_NULL,null=True,related_name="reciever") updated = models.DateTimeField(auto_now=True) - timestamp = models.DateTimeField(auto_now_add=True) def __str__(self): return str(self.sender.username + " and " + self.reciever.username) -class UserMessage(models.Model): - id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False) +class UserMessage(BaseModel): thread = models.ForeignKey( Thread, on_delete=models.CASCADE,related_name="messages") sender = models.ForeignKey(UserProfile, on_delete=models.CASCADE) body = models.TextField(null=True,blank=True) is_read = models.BooleanField(default=False) - timestamp = models.DateTimeField(auto_now_add=True) def __str__(self): return str(self.body) \ No newline at end of file