Forum: styling topic view
[edumed.git] / forum / views.py
1 import pybb.views
2 import pybb.forms
3
4 from .forms import PostForm
5 from .models import Topic
6
7
8 class PostEditMixin(pybb.views.PostEditMixin):
9
10     def get_form_class(self):
11         toret = super(PostEditMixin, self).get_form_class()
12         if issubclass(toret, pybb.forms.PostForm):
13             toret = PostForm
14         return toret
15
16     def form_valid(self, form):
17         toret = super(PostEditMixin, self).form_valid(form)
18
19         pybb_post = self.object
20         pybb_topic = pybb_post.topic
21         topic, topic_created = Topic.objects.get_or_create(pybb_topic = pybb_topic)
22
23         if pybb_post == pybb_topic.head:
24             topic.lesson = form.cleaned_data['lesson']
25             topic.save()
26
27         return toret
28
29
30 class AddPostView(PostEditMixin, pybb.views.AddPostView):
31     def get_context_data(self, **kwargs):
32         ctx = super(AddPostView, self).get_context_data(**kwargs)
33         ctx['lesson_editable'] = self._creates_new_topic()
34         return ctx
35
36     def _creates_new_topic(self):
37         return self.forum is not None
38
39
40 class EditPostView(PostEditMixin, pybb.views.EditPostView):
41     def get_context_data(self, **kwargs):
42         ctx = super(EditPostView, self).get_context_data(**kwargs)
43         ctx['lesson_editable'] = self._edits_topics_head()
44         return ctx
45
46     def _edits_topics_head(self):
47         return self.object == self.object.topic.head
48
49     def get_form_kwargs(self):
50         kwargs = super(EditPostView, self).get_form_kwargs()
51         kwargs['initial']['lesson'] = self.object.topic.edumed_topic.lesson
52         return kwargs