1
|
|
|
import importlib |
2
|
|
|
|
3
|
|
|
from django.conf import settings |
4
|
|
|
from django.core.urlresolvers import reverse |
5
|
|
|
|
6
|
|
|
from simple_forums import models |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def get_setting(setting_name, default=None): |
10
|
|
|
""" Get the specified setting. |
11
|
|
|
|
12
|
|
|
If the setting exists, it will be returned. Otherwise, the default |
13
|
|
|
value will be returned. |
14
|
|
|
""" |
15
|
|
|
settings_dict = getattr(settings, 'SIMPLE_FORUMS', None) |
16
|
|
|
|
17
|
|
|
if settings_dict: |
18
|
|
|
return settings_dict.get(setting_name, default) |
19
|
|
|
|
20
|
|
|
return default |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def string_to_class(string): |
24
|
|
|
""" Return the class represented by the given string """ |
25
|
|
|
module, class_name = string.rsplit('.', 1) |
26
|
|
|
|
27
|
|
|
return getattr(importlib.import_module(module), class_name) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def thread_detail_url(pk=None, thread=None): |
31
|
|
|
""" Get the url of a thread's detail view. |
32
|
|
|
|
33
|
|
|
Uses either the thread's pk or the thread instance itself to |
34
|
|
|
determine the url of the thread's detail view. |
35
|
|
|
""" |
36
|
|
|
if pk is None and thread is None: |
37
|
|
|
raise ValueError("Either 'pk' or 'thread' must not be None") |
38
|
|
|
|
39
|
|
|
if pk: |
40
|
|
|
thread = models.Thread.objects.get(pk=pk) |
41
|
|
|
|
42
|
|
|
kwargs = { |
43
|
|
|
'topic_pk': thread.topic.pk, |
44
|
|
|
'topic_slug': thread.topic.slug, |
45
|
|
|
'thread_pk': thread.pk, |
46
|
|
|
'thread_slug': thread.slug, |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return reverse('thread-detail', kwargs=kwargs) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def thread_list_url(topic_pk=None, topic=None, sort=None, rev=False): |
53
|
|
|
""" Get the url of the thread list view for a topic. |
54
|
|
|
|
55
|
|
|
Uses either the topic's pk or the topic instance itself to |
56
|
|
|
determine the url of the thread list view. |
57
|
|
|
""" |
58
|
|
|
if topic_pk is None and topic is None: |
59
|
|
|
raise ValueError("Either 'topic_pk' or 'topic' must not be None") |
60
|
|
|
|
61
|
|
|
if topic_pk: |
62
|
|
|
topic = models.Topic.objects.get(pk=topic_pk) |
63
|
|
|
|
64
|
|
|
kwargs = { |
65
|
|
|
'topic_pk': topic.pk, |
66
|
|
|
'topic_slug': topic.slug, |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
url = reverse('thread-list', kwargs=kwargs) |
70
|
|
|
|
71
|
|
|
args = [] |
72
|
|
|
|
73
|
|
|
if sort: |
74
|
|
|
args.append('sort=%s' % sort) |
75
|
|
|
|
76
|
|
|
if rev is True: |
77
|
|
|
args.append('rev=true') |
78
|
|
|
|
79
|
|
|
if args: |
80
|
|
|
return '%s?%s' % (url, '&'.join(args)) |
81
|
|
|
|
82
|
|
|
return url |
83
|
|
|
|