Ejemplo n.º 1
0
 def create(self):
     self.topic_form = TopicForm()
     self.post_form = PostForm()
     is_topic_form_valid = self.topic_form.validate_on_submit()
     is_post_form_valid = self.post_form.validate_on_submit()
     if is_topic_form_valid and is_post_form_valid:
         del self.topic_form
         del self.post_form
         topic = Topic.create(user_id=self.current_user.id, **self._topic_params())
         Post.create(user_id=self.current_user.id, topic_id=topic.id, **self._post_params())
         return redirect(url_for('topics.show', topic_id=topic.id, forum_id=topic.forum_id))
     else:
         return self.new()
Ejemplo n.º 2
0
 def update(self):
     self.topic_form = TopicForm()
     self.post_form = PostForm()
     is_topic_form_valid = self.topic_form.validate_on_submit()
     is_post_form_valid = self.post_form.validate_on_submit()
     if is_topic_form_valid and is_post_form_valid:
         del self.topic_form
         del self.post_form
         topic = Topic.query.get_or_404(self.params['topic_id'])
         if not self.current_user.can_action_on(topic):
             return abort(401)
         post = topic.posts.first_or_404()
         topic.update(**self._topic_params())
         post.update(**self._post_params())
         return redirect(url_for('topics.show', topic_id=topic.id, forum_id=topic.forum_id))
     else:
         return self.edit()
Ejemplo n.º 3
0
class TopicsController(BaseController):
    @render
    def show(self):
        self.topic = Topic.query.get_or_404(self.params['topic_id'])
        self.topic.views_count += 1
        self.topic.save()
        self.in_topic_post_form = PostForm()

    @login_required
    @render
    def new(self):
        self.topic_form = getattr(self, 'topic_form', None) or TopicForm()
        self.post_form = getattr(self, 'post_form', None) or PostForm()

    @login_required
    @render
    def edit(self):
        self.topic = Topic.query.get_or_404(self.params['topic_id'])
        if not self.current_user.can_action_on(self.topic):
            return abort(401)
        self.topic_form = getattr(self, 'topic_form', None) or TopicForm(obj=self.topic)
        self.post_form = getattr(self, 'post_form', None) or PostForm(obj=self.topic.posts.first_or_404())

    @login_required
    def create(self):
        self.topic_form = TopicForm()
        self.post_form = PostForm()
        is_topic_form_valid = self.topic_form.validate_on_submit()
        is_post_form_valid = self.post_form.validate_on_submit()
        if is_topic_form_valid and is_post_form_valid:
            del self.topic_form
            del self.post_form
            topic = Topic.create(user_id=self.current_user.id, **self._topic_params())
            Post.create(user_id=self.current_user.id, topic_id=topic.id, **self._post_params())
            return redirect(url_for('topics.show', topic_id=topic.id, forum_id=topic.forum_id))
        else:
            return self.new()

    @login_required
    def update(self):
        self.topic_form = TopicForm()
        self.post_form = PostForm()
        is_topic_form_valid = self.topic_form.validate_on_submit()
        is_post_form_valid = self.post_form.validate_on_submit()
        if is_topic_form_valid and is_post_form_valid:
            del self.topic_form
            del self.post_form
            topic = Topic.query.get_or_404(self.params['topic_id'])
            if not self.current_user.can_action_on(topic):
                return abort(401)
            post = topic.posts.first_or_404()
            topic.update(**self._topic_params())
            post.update(**self._post_params())
            return redirect(url_for('topics.show', topic_id=topic.id, forum_id=topic.forum_id))
        else:
            return self.edit()

    @admin_required
    def destroy(self):
        topic = Topic.query.get_or_404(self.params['topic_id'])
        forum_id = topic.forum_id
        topic.delete()
        return redirect(url_for('forums.show', forum_id=forum_id))

    def _topic_params(self):
        return self.permit(self.params, 'title', 'forum_id')

    def _post_params(self):
        return self.permit(self.params, 'text')