1 # -*- coding: utf-8 -*-
 
   2 from django.core.exceptions import ObjectDoesNotExist
 
   6 from .forms import PostForm
 
   7 from .models import Topic
 
  10 class PostEditMixin(pybb.views.PostEditMixin):
 
  12     def get_form_class(self):
 
  13         toret = super(PostEditMixin, self).get_form_class()
 
  14         if issubclass(toret, pybb.forms.PostForm):
 
  18     def form_valid(self, form):
 
  19         toret = super(PostEditMixin, self).form_valid(form)
 
  21         pybb_post = self.object
 
  22         pybb_topic = pybb_post.topic
 
  23         topic, topic_created = Topic.objects.get_or_create(pybb_topic=pybb_topic)
 
  25         if pybb_post == pybb_topic.head:
 
  26             topic.lesson = form.cleaned_data['lesson']
 
  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()
 
  38     def _creates_new_topic(self):
 
  39         return self.forum is not None
 
  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()
 
  48     def _edits_topics_head(self):
 
  49         return self.object == self.object.topic.head
 
  51     def get_form_kwargs(self):
 
  52         kwargs = super(EditPostView, self).get_form_kwargs()
 
  54             lesson = self.object.topic.edumed_topic.lesson
 
  55         except ObjectDoesNotExist:
 
  57         kwargs['initial']['lesson'] = lesson