1
|
|
|
from django.conf import settings |
2
|
|
|
from django.core.urlresolvers import reverse |
3
|
|
|
from django.db import models |
4
|
|
|
from django.utils import timezone |
5
|
|
|
from django.utils.text import slugify |
6
|
|
|
|
7
|
|
|
from adminsortable.models import SortableMixin |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class Message(models.Model): |
11
|
|
|
""" A message with some text """ |
12
|
|
|
|
13
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL) |
14
|
|
|
thread = models.ForeignKey('simple_forums.Thread') |
15
|
|
|
body = models.TextField() |
16
|
|
|
time_created = models.DateTimeField(default=timezone.now) |
17
|
|
|
|
18
|
|
|
def __str__(self): |
19
|
|
|
""" Return the message's body """ |
20
|
|
|
return self.body |
21
|
|
|
|
22
|
|
|
def get_absolute_url(self): |
23
|
|
|
""" Return the url of the message instance """ |
24
|
|
|
return '%s#%s' % (self.thread.get_absolute_url(), self.get_anchor()) |
25
|
|
|
|
26
|
|
|
def get_anchor(self): |
27
|
|
|
""" Get the anchor for the message """ |
28
|
|
|
return 'm-%d' % self.pk |
29
|
|
|
|
30
|
|
|
def get_search_description(self): |
31
|
|
|
""" Return description of message for search results """ |
32
|
|
|
return '%s said: %s' % (self.user, self.body) |
33
|
|
|
|
34
|
|
|
def get_title(self): |
35
|
|
|
""" Return the parent thread's title """ |
36
|
|
|
return self.thread.get_title() |
37
|
|
|
|
38
|
|
|
def save(self, *args, **kwargs): |
39
|
|
|
""" Update the parent thread's 'time_last_activity' field """ |
40
|
|
|
if self.time_created > self.thread.time_last_activity: |
41
|
|
|
self.thread.time_last_activity = self.time_created |
42
|
|
|
self.thread.save() |
43
|
|
|
|
44
|
|
|
return super(Message, self).save(*args, **kwargs) |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
class Thread(models.Model): |
48
|
|
|
""" A thread with a title """ |
49
|
|
|
|
50
|
|
|
topic = models.ForeignKey('Topic') |
51
|
|
|
title = models.CharField(max_length=200) |
52
|
|
|
sticky = models.BooleanField(default=False) |
53
|
|
|
slug = models.SlugField() |
54
|
|
|
time_created = models.DateTimeField(default=timezone.now) |
55
|
|
|
time_last_activity = models.DateTimeField(default=timezone.now) |
56
|
|
|
|
57
|
|
|
def __init__(self, *args, **kwargs): |
58
|
|
|
""" Initialize 'time_last_activity' to 'time_created' """ |
59
|
|
|
super(Thread, self).__init__(*args, **kwargs) |
60
|
|
|
|
61
|
|
|
self.time_last_activity = self.time_created |
62
|
|
|
|
63
|
|
|
def __str__(self): |
64
|
|
|
""" Return the thread's title """ |
65
|
|
|
return self.title |
66
|
|
|
|
67
|
|
|
def get_absolute_url(self): |
68
|
|
|
""" Return the url of the instance's detail view """ |
69
|
|
|
url_kwargs = { |
70
|
|
|
'topic_pk': self.topic.pk, |
71
|
|
|
'topic_slug': self.topic.slug, |
72
|
|
|
'thread_pk': self.pk, |
73
|
|
|
'thread_slug': self.slug, |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return reverse('thread-detail', kwargs=url_kwargs) |
77
|
|
|
|
78
|
|
|
def get_search_description(self): |
79
|
|
|
""" Return description of thread for search results """ |
80
|
|
|
if self.message_set.exists(): |
81
|
|
|
return self.message_set.first().body |
82
|
|
|
|
83
|
|
|
return 'There are no replies to this thread.' |
84
|
|
|
|
85
|
|
|
def get_title(self): |
86
|
|
|
""" Return the thread's title """ |
87
|
|
|
return self.title |
88
|
|
|
|
89
|
|
|
@property |
90
|
|
|
def num_replies(self): |
91
|
|
|
""" Get the number of replies to the thread """ |
92
|
|
|
count = self.message_set.count() |
93
|
|
|
|
94
|
|
|
if not count: |
95
|
|
|
return count |
96
|
|
|
|
97
|
|
|
return count - 1 |
98
|
|
|
|
99
|
|
|
def save(self, *args, **kwargs): |
100
|
|
|
""" Save the thread instance |
101
|
|
|
|
102
|
|
|
Overriden to generate a url slug. |
103
|
|
|
""" |
104
|
|
|
# Only create the slug if this is a new object. |
105
|
|
|
# Changing existing slugs would create dead links. |
106
|
|
|
if not self.id: |
107
|
|
|
# Slugify and truncate to 50 characters |
108
|
|
|
self.slug = slugify(self.title)[:50] |
109
|
|
|
|
110
|
|
|
return super(Thread, self).save(*args, **kwargs) |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
class Topic(SortableMixin): |
114
|
|
|
""" A topic model to hold threads """ |
115
|
|
|
|
116
|
|
|
title = models.CharField(max_length=50) |
117
|
|
|
description = models.CharField(max_length=200) |
118
|
|
|
slug = models.SlugField() |
119
|
|
|
|
120
|
|
|
# field used by django-admin-sortable for ordering topics |
121
|
|
|
topic_order = models.PositiveIntegerField( |
122
|
|
|
default=0, editable=False, db_index=True) |
123
|
|
|
|
124
|
|
|
class Meta: |
125
|
|
|
ordering = ('topic_order',) |
126
|
|
|
|
127
|
|
|
def __str__(self): |
128
|
|
|
""" Return the topic's title """ |
129
|
|
|
return self.title |
130
|
|
|
|
131
|
|
|
def save(self, *args, **kwargs): |
132
|
|
|
""" Save the topic instance |
133
|
|
|
|
134
|
|
|
Overriden to generate a url slug. |
135
|
|
|
""" |
136
|
|
|
# Only create the slug if this is a new object. |
137
|
|
|
# Changing existing slugs would create dead links. |
138
|
|
|
if not self.id: |
139
|
|
|
# Slugify and truncate to 50 characters |
140
|
|
|
self.slug = slugify(self.title)[:50] |
141
|
|
|
|
142
|
|
|
return super(Topic, self).save(*args, **kwargs) |
143
|
|
|
|