1 from django.core.exceptions import ObjectDoesNotExist
 
   5 from catalogue.models import Lesson
 
   7 from .forms import PostForm
 
   8 from .models import Topic
 
  11 class PostEditMixin(pybb.views.PostEditMixin):
 
  13     def get_form_class(self):
 
  14         toret = super(PostEditMixin, self).get_form_class()
 
  15         if issubclass(toret, pybb.forms.PostForm):
 
  19     def form_valid(self, form):
 
  20         toret = super(PostEditMixin, self).form_valid(form)
 
  22         pybb_post = self.object
 
  23         pybb_topic = pybb_post.topic
 
  24         topic, topic_created = Topic.objects.get_or_create(pybb_topic = pybb_topic)
 
  26         if pybb_post == pybb_topic.head:
 
  27             topic.lesson = form.cleaned_data['lesson']
 
  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()
 
  39     def _creates_new_topic(self):
 
  40         return self.forum is not None
 
  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()
 
  49     def _edits_topics_head(self):
 
  50         return self.object == self.object.topic.head
 
  52     def get_form_kwargs(self):
 
  53         kwargs = super(EditPostView, self).get_form_kwargs()
 
  55             lesson = self.object.topic.edumed_topic.lesson
 
  56         except ObjectDoesNotExist:
 
  58         kwargs['initial']['lesson'] = lesson