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