aboutsummaryrefslogtreecommitdiff
path: root/apps/forum/models.py
blob: 3a54514b72b5d2f5c3ef2ccd8544382820cf9d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from django.db import models
from django.contrib import auth
from django.urls import reverse
from django.conf import settings


class CategoryManager(models.Manager):

    # create category
    def create_category(self, title, slug=''):

        if slug == '':
            slug = title.lower().replace(' ', '-')

        category = self.create(title=title, slug=slug)

        return category

    # get or create
    def get_or_create(self, title):
        try:
            category = Category.objects.get(title=title)
        except Category.DoesNotExist:
            return (self.create_category(title), True)

        return (category, False)


class Category(models.Model):

    name = models.CharField(max_length=120)
    slug = models.SlugField()
    description = models.CharField(max_length=350)
    color_rgb = models.CharField(max_length=20, blank=True)

    date_created = models.DateTimeField(auto_now=False, auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    topic_count = models.PositiveIntegerField(default=0)
    topics_year = models.PositiveIntegerField(default=0)
    topics_month = models.PositiveIntegerField(default=0)
    topics_week = models.PositiveIntegerField(default=0)

    class Meta:
        verbose_name_plural = "Categories"

    objects = CategoryManager()

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("forum:category-detail", kwargs={'slug': self.slug})


class TopicManager(models.Manager):

    # create category
    def create_topic(self, title, category, user, slug=''):

        if slug == '':
            slug = title.lower().replace(' ', '-')

        topic = self.create(title=title, slug=slug, category=category, user=user)

        return topic


class Topic(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='topic_user')
    last_post_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='topic_last_post_user', blank=True, null=True)
    highest_post = models.ForeignKey('Post', on_delete=models.CASCADE, related_name='topic_highest_post', blank=True, null=True)

    title = models.CharField(max_length=255)
    slug = models.SlugField()

    date_last_posted = models.DateTimeField(auto_now=True, auto_now_add=False, blank=True)
    date_created = models.DateTimeField(auto_now=False, auto_now_add=True, blank=True)
    date_updated = models.DateTimeField(auto_now=True, auto_now_add=False, blank=True)
    date_deleted = models.DateTimeField(blank=True, null=True)
    date_bumped = models.DateTimeField(blank=True, null=True)

    views = models.PositiveIntegerField(default=0)
    posts_count = models.PositiveIntegerField(default=0)
    reply_count = models.PositiveIntegerField(default=0)
    like_count = models.PositiveIntegerField(default=0)
    bookmark_count = models.PositiveIntegerField(default=0)
    star_count = models.PositiveIntegerField(default=0)
    moderator_posts_count = models.PositiveIntegerField(default=0)
    vote_count = models.PositiveIntegerField(default=0)
    spam_count = models.PositiveIntegerField(default=0)
    illegal_count = models.PositiveIntegerField(default=0)
    inappropriate_count = models.PositiveIntegerField(default=0)

    visible = models.BooleanField(default=True)
    closed = models.BooleanField(default=False)
    pinned = models.BooleanField(default=False)
    archived = models.BooleanField(default=False)
    has_best_of = models.BooleanField(default=False)

    objects = TopicManager()

    class Meta:
        verbose_name_plural = "Topics"

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('forum:topic-detail', kwargs={"slug": self.slug, "pk": self.pk})


class Post(models.Model):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='post_user')
    reply_to_post = models.ForeignKey('self', on_delete=models.CASCADE, related_name='post_reply_to', blank=True, null=True)
    reply_to_user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='post_reply_to_user', blank=True, null=True)
    reply_below_post = models.ForeignKey('self', on_delete=models.CASCADE, related_name='post_reply_below', blank=True, null=True)
    last_editor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='post_last_editor', blank=True, null=True)
    POST_TYPE_CHOICES = (
        ('post', 'post'),
        ('reply', 'reply'),
    )
    post_type = models.CharField(max_length=60, choices=POST_TYPE_CHOICES)
    body_text = models.TextField(blank=True, null=False)
    body_html = models.TextField(blank=True, null=True)
    body_json = models.TextField(blank=True, null=True)

    date_created = models.DateTimeField(auto_now=False, auto_now_add=True, blank=True)
    date_updated = models.DateTimeField(auto_now=True, auto_now_add=False, blank=True)
    date_delete = models.DateTimeField(blank=True, null=True)

    reply_count = models.PositiveIntegerField(default=0)
    quote_count = models.PositiveIntegerField(default=0)
    like_count = models.PositiveIntegerField(default=0)
    bookmark_count = models.PositiveIntegerField(default=0)
    spam_count = models.PositiveIntegerField(default=0)
    reads = models.PositiveIntegerField(default=0)
    inappropriate_count = models.PositiveIntegerField(default=0)

    score = models.IntegerField(default=0)
    vote_count = models.IntegerField(default=0)

    class Meta:
        verbose_name_plural = "Posts"

    def __str__(self):
        return '%s - %s' % (self.topic.title, self.id)