Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the models to follow DRY principle #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions article/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
29 changes: 14 additions & 15 deletions discussion/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
11 changes: 7 additions & 4 deletions feed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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']
Expand All @@ -43,7 +47,7 @@ def comments(self):



class MumbleVote(models.Model):
class MumbleVote(BaseModel):

CHOICES = (
('upvote', 'upvote'),
Expand All @@ -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) + '"'
13 changes: 8 additions & 5 deletions message/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)