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