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