joeni

Add new models in Communication app

Author
Maarten 'Vngngdn' Vangeneugden
Date
Nov. 15, 2017, 9:12 p.m.
Hash
6fb2a99117b08c3de813c5748dbcaa841d5cd565
Parent
35d48d7399ca4e87c9820a7d256c50b2ede8d4e4
Modified file
communication/models.py

communication/models.py

155 additions and 4 deletions.

View changes Hide changes
1
1
from django.utils.translation import ugettext_lazy as _
2
2
from joeni import constants
3
3
4
4
class Account(models.Model):
5
5
    user = models.OneToOneField(
6
6
        'joeni.User',
7
7
        on_delete=models.CASCADE,
8
8
        )
+
9
        )
9
10
    alias = models.CharField(
10
-
        max_length=64,
11
-
        unique=True,
12
-
        default=username())  # TODO: Create function to extract username from foreign key
13
-
+
11
+
12
    def __str__(self):
+
13
        return self.alias
+
14
14
15
def account_user_directory(instance, filename):
15
16
    return '{0}/account/settings/{1}'.format(instace.account.alias, filename)
16
17
17
18
class AccountSettings(models.Model):
18
19
    account = models.OneToOneField(
19
20
        'Account',
20
21
        on_delete=models.CASCADE,
21
22
        )
22
23
    primary_color = models.CharField(
23
24
        max_length=6,
24
25
        help_text=_("The hexadecimal code of the primary color for this account."),
25
26
        default = constants.COLORS["UHasselt default"],
26
27
        )
27
28
    """secondary_color = models.CharField(
28
29
        max_length=6,
29
30
        help_text=_("The hexadecimal code of the secondary color for this account."),
30
31
        )"""
31
32
    # We're gonna leave the secondary color out for now.
32
33
    account_page_banner = models.ImageField(  # Requires the Pillow library!
33
34
        upload_to=account_user_directory,
34
35
        help_text=_("The banner image to be shown on this account's homepage."),
35
36
        )
36
37
    avatar = models.ImageField(
37
38
        upload_to=account_user_directory,
38
39
        help_text=_("The avatar image of this account."),
39
40
        )
40
41
+
42
    def __str__(self):
+
43
        return str(self.account)
+
44
+
45
class Post(models.Model):
+
46
    """ An abstract model that represents a post. """
+
47
    timestamp = models.DateTimeField(auto_now_add=True)
+
48
    title = models.CharField(
+
49
        max_length=64,
+
50
        blank=True,
+
51
        help_text=_("The title for this post."),
+
52
        )
+
53
    text = models.TextField(
+
54
        blank=True,
+
55
        help_text=_("A text message for this post. May be left blank."),
+
56
        )
+
57
    author = models.ForeignKey(
+
58
        "Account",
+
59
        on_delete=models.CASCADE,
+
60
        null=False,  # There must be an author
+
61
        editable=False,  # It makes no sense to change the author after creation
+
62
        help_text=_("The authoring account of this post."),
+
63
        )
+
64
    response_to = models.ForeignKey(
+
65
        "self",
+
66
        on_delete=models.CASCADE,
+
67
        null=True,  # If this is null, this post is not a response, but a beginning post
+
68
        editable=False,  # This cannot be changed after creation, wouldn't make sense
+
69
        help_text=_("The post to which this was a response, if applicable."),
+
70
        )
+
71
    placed_on = models.ForeignKey(
+
72
        "Page",
+
73
        on_delete=models.CASCADE,
+
74
        null=False,
+
75
        editable=False,
+
76
        help_text=_("The page on which this post was placed."),
+
77
        )
+
78
    # Voting fields
+
79
    allow_votes = models.BooleanField(
+
80
        default=True,
+
81
        help_text=_("Decide whether to allow voting or disable it for this post."),
+
82
        )
+
83
+
84
    def __str__(self):
+
85
        return str(self.timestamp) + " | " + str(self.author)
+
86
+
87
class VideoPost(Post):
+
88
    pass
+
89
+
90
class ImagePost(Post):
+
91
    pass
+
92
+
93
class MusicPost(Post):
+
94
    pass
+
95
+
96
class DocumentPost(Post):
+
97
    pass
+
98
+
99
class Page(models.Model):
+
100
    """ In the university, people can create pages for everything they want and
+
101
    then some. These pages are put in the database through this table. """
+
102
    name = models.CharField(
+
103
        max_length=64,
+
104
        primary_key=True,
+
105
        blank=False,
+
106
        help_text=_("The name of this page."),
+
107
        )
+
108
    created = models.DateTimeField(auto_now_add=True)
+
109
    hidden = models.BooleanField(
+
110
        default=False,
+
111
        help_text=_("Determines if this page can be found without a direct link."),
+
112
        )
+
113
+
114
    class Meta:
+
115
        abstract=True
+
116
+
117
class AccountPage(Page):
+
118
    account = models.OneToOneField(
+
119
        "Account",
+
120
        null=False,
+
121
        on_delete=models.CASCADE,
+
122
        )
+
123
+
124
class GroupPage(Page):
+
125
    group = models.ForeignKey(
+
126
        "Group",
+
127
        null=False,
+
128
        on_delete=models.CASCADE,
+
129
        )
+
130
+
131
class CoursePage(Page):
+
132
    course = models.OneToOneField(
+
133
        "courses.Course",
+
134
        null=False,
+
135
        on_delete=models.CASCADE,
+
136
        )
+
137
+
138
+
139
class AccountCollection(models.Model):
+
140
    """ Every account can make a collection in which (s)he can list accounts
+
141
    at his/her wish. This can be a collection of Friends, study collegues,
+
142
    project partners, and so on.
+
143
    Accounts that are in a certain collection are not notified of this.
+
144
    However, there is one exception:
+
145
    If both accounts have a collection named "Friends" (or the localized
+
146
    equivalent), and both feature each other in that collection, then
+
147
    this is shared between the two accounts. """
+
148
    account = models.ForeignKey(
+
149
        "Account",
+
150
        null=False,
+
151
        editable=False,
+
152
        on_delete=models.CASCADE,
+
153
        help_text=_("The account that created this collection."),
+
154
        )
+
155
    name = models.CharField(
+
156
        max_length=32,
+
157
        blank=False,
+
158
        help_text=_("The name of this collection."),
+
159
        )
+
160
    accounts = models.ManyToManyField(
+
161
        "Account",
+
162
        help_text=_("All accounts that are part of this collection."),
+
163
        )
+
164
    visible_to_public = models.BooleanField(
+
165
        default=False,
+
166
        help_text=_("Make this collection visible to everybody."),
+
167
        )
+
168
    visible_to_collection = models.BooleanField(
+
169
        default=True,
+
170
        help_text=_("Make this collection visible to the accounts in this collection. Other collections are not affected by this."),
+
171
        )
+
172
+
173
    def __str__(self):
+
174
        return str(self.account) + " | " + self.name
+
175
+
176
class Vote(models.Model):
+
177
    """ Accounts can vote on posts (using ▲, which is funny because UHasselt).
+
178
    These votes are registered in this table. """
+
179
    voter = models.ForeignKey(
+
180
        "Account",
+
181
        null=False,
+
182
        editable=False,
+
183
        on_delete=models.CASCADE,
+
184
        )
+
185
    post = models.ForeignKey(
+
186
        "Post",
+
187
        null=False,  # Duh.
+
188
        editable=False,  # Transferring votes doesn't make sense
+
189
        on_delete=models.CASCADE,
+
190
        )
+
191