1
|
|
|
from django.core.exceptions import PermissionDenied |
2
|
|
|
from django.http import HttpResponseRedirect |
3
|
|
|
from django.shortcuts import get_object_or_404, render |
4
|
|
|
from django.views import generic |
5
|
|
|
|
6
|
|
|
from simple_forums import forms, models |
7
|
|
|
from simple_forums.backends.search import simple_search |
8
|
|
|
from simple_forums.utils import thread_detail_url |
9
|
|
|
|
10
|
|
|
try: |
11
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin |
12
|
|
|
except ImportError: |
13
|
|
|
from simple_forums.compatability.mixins import LoginRequiredMixin |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class SearchView(generic.View): |
17
|
|
|
""" View for searching """ |
18
|
|
|
|
19
|
|
|
template_name = 'simple_forums/search.html' |
20
|
|
|
query_kwarg = 'q' |
21
|
|
|
|
22
|
|
|
def get(self, request, *args, **kwargs): |
23
|
|
|
""" Show the search form and results if applicable """ |
24
|
|
|
self.args = args |
25
|
|
|
self.kwargs = kwargs |
26
|
|
|
self.request = request |
27
|
|
|
|
28
|
|
|
return render(request, self.template_name, self.get_context_data()) |
29
|
|
|
|
30
|
|
|
def get_context_data(self, **kwargs): |
31
|
|
|
context = dict() |
32
|
|
|
|
33
|
|
|
query = self.get_query() |
34
|
|
|
if query is not None: |
35
|
|
|
context['results'] = self.get_queryset() |
36
|
|
|
context['query'] = query |
37
|
|
|
|
38
|
|
|
return context |
39
|
|
|
|
40
|
|
|
def get_query(self): |
41
|
|
|
""" Return the query passed as a GET parameter """ |
42
|
|
|
return self.request.GET.get(self.query_kwarg, None) |
43
|
|
|
|
44
|
|
|
def get_queryset(self): |
45
|
|
|
""" Return the list of threads that match the query """ |
46
|
|
|
backend = simple_search.SimpleSearch() |
47
|
|
|
|
48
|
|
|
return backend.search(self.get_query()) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
class ThreadCreateView(LoginRequiredMixin, generic.edit.FormView): |
52
|
|
|
""" View for creating new threads """ |
53
|
|
|
|
54
|
|
|
template_name = 'simple_forums/thread_create.html' |
55
|
|
|
form_class = forms.ThreadCreationForm |
56
|
|
|
|
57
|
|
|
def form_valid(self, form): |
58
|
|
|
""" Save form if it is valid """ |
59
|
|
|
thread = form.save(self.request.user) |
60
|
|
|
|
61
|
|
|
return HttpResponseRedirect(thread_detail_url(thread=thread)) |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
class ThreadDetailView(generic.DetailView): |
65
|
|
|
""" View for getting a thread's details """ |
66
|
|
|
|
67
|
|
|
model = models.Thread |
68
|
|
|
pk_url_kwarg = 'thread_pk' |
69
|
|
|
|
70
|
|
|
def get_context_data(self, **kwargs): |
71
|
|
|
context = super(ThreadDetailView, self).get_context_data(**kwargs) |
72
|
|
|
|
73
|
|
|
if self.request.user.is_authenticated(): |
74
|
|
|
context['reply_form'] = forms.ThreadReplyForm() |
75
|
|
|
|
76
|
|
|
return context |
77
|
|
|
|
78
|
|
|
def post(self, request, *args, **kwargs): |
79
|
|
|
""" Create a new reply to the current thread """ |
80
|
|
|
if not request.user.is_authenticated(): |
81
|
|
|
raise PermissionDenied() |
82
|
|
|
|
83
|
|
|
self.object = self.get_object() |
84
|
|
|
|
85
|
|
|
form = forms.ThreadReplyForm(request.POST) |
86
|
|
|
|
87
|
|
|
if form.is_valid(): |
88
|
|
|
form.save(request.user, self.object) |
89
|
|
|
|
90
|
|
|
return HttpResponseRedirect(thread_detail_url(thread=self.object)) |
91
|
|
|
|
92
|
|
|
context = self.get_context_data() |
93
|
|
|
context['reply_form'] = form |
94
|
|
|
|
95
|
|
|
return render(request, self.get_template_names(), context) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
class ThreadListView(generic.ListView): |
99
|
|
|
""" View for listing threads """ |
100
|
|
|
|
101
|
|
|
model = models.Thread |
102
|
|
|
|
103
|
|
|
def get_context_data(self, **kwargs): |
104
|
|
|
context = super(ThreadListView, self).get_context_data(**kwargs) |
105
|
|
|
|
106
|
|
|
sticky_threads = self._get_base_queryset().filter(sticky=True) |
107
|
|
|
context['sticky_thread_list'] = sticky_threads |
108
|
|
|
|
109
|
|
|
topic = get_object_or_404( |
110
|
|
|
models.Topic, |
111
|
|
|
pk=self.kwargs.get('topic_pk')) |
112
|
|
|
context['topic'] = topic |
113
|
|
|
|
114
|
|
|
return context |
115
|
|
|
|
116
|
|
|
def _get_base_queryset(self): |
117
|
|
|
""" Retrieve all threads associated with the given topic """ |
118
|
|
|
topic = get_object_or_404( |
119
|
|
|
models.Topic, |
120
|
|
|
pk=self.kwargs.get('topic_pk')) |
121
|
|
|
|
122
|
|
|
return self.model.objects.filter(topic=topic) |
123
|
|
|
|
124
|
|
|
def get_queryset(self): |
125
|
|
|
""" Return all non-sticky threads """ |
126
|
|
|
return self._get_base_queryset() \ |
127
|
|
|
.exclude(sticky=True) \ |
128
|
|
|
.order_by('-time_last_activity') |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
class TopicListView(generic.ListView): |
132
|
|
|
""" View for listing topics """ |
133
|
|
|
|
134
|
|
|
model = models.Topic |
135
|
|
|
|