views.py 3.58 KB
Newer Older
Skia's avatar
Skia committed
1 2 3 4 5 6 7 8 9 10
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView, RedirectView
from django.views.generic.edit import UpdateView, CreateView, DeleteView
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse, reverse_lazy
from django.utils import timezone
from django.conf import settings
from django import forms
from django.core.exceptions import PermissionDenied

11
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin, TabedViewMixin
Skia's avatar
Skia committed
12 13
from forum.models import Forum, ForumMessage, ForumTopic

14
class ForumMainView(CanViewMixin, ListView):
Skia's avatar
Skia committed
15 16 17
    queryset = Forum.objects.filter(parent=None)
    template_name = "forum/main.jinja"

18
class ForumCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
19
    model = Forum
20
    fields = ['name', 'parent', 'is_category', 'edit_groups', 'view_groups']
Skia's avatar
Skia committed
21 22 23 24 25 26 27 28
    template_name = "core/create.jinja"

    def get_initial(self):
        init = super(ForumCreateView, self).get_initial()
        parent = Forum.objects.filter(id=self.request.GET['parent']).first()
        init['parent'] = parent
        return init

29
class ForumEditView(CanEditPropMixin, UpdateView):
Skia's avatar
Skia committed
30 31
    model = Forum
    pk_url_kwarg = "forum_id"
32
    fields = ['name', 'parent', 'is_category', 'edit_groups', 'view_groups']
Skia's avatar
Skia committed
33 34 35
    template_name = "core/edit.jinja"
    success_url = reverse_lazy('forum:main')

36
class ForumDetailView(CanViewMixin, DetailView):
Skia's avatar
Skia committed
37 38 39 40
    model = Forum
    template_name = "forum/forum.jinja"
    pk_url_kwarg = "forum_id"

41
class ForumTopicCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
42 43
    model = ForumMessage
    fields = ['title', 'message']
Skia's avatar
Skia committed
44 45 46 47 48 49 50 51 52
    template_name = "core/create.jinja"

    def dispatch(self, request, *args, **kwargs):
        self.forum = get_object_or_404(Forum, id=self.kwargs['forum_id'], is_category=False)
        if not request.user.can_view(self.forum):
            raise PermissionDenied
        return super(ForumTopicCreateView, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
Skia's avatar
Skia committed
53 54 55
        topic = ForumTopic(title=form.instance.title, author=self.request.user, forum=self.forum)
        topic.save()
        form.instance.topic = topic
Skia's avatar
Skia committed
56 57 58
        form.instance.author = self.request.user
        return super(ForumTopicCreateView, self).form_valid(form)

59
class ForumTopicEditView(CanEditPropMixin, UpdateView):
Skia's avatar
Skia committed
60
    model = ForumTopic
Skia's avatar
Skia committed
61
    fields = ['title', 'forum']
Skia's avatar
Skia committed
62 63 64
    pk_url_kwarg = "topic_id"
    template_name = "core/edit.jinja"

65
class ForumTopicDetailView(CanViewMixin, DetailView):
Skia's avatar
Skia committed
66 67 68 69 70
    model = ForumTopic
    pk_url_kwarg = "topic_id"
    template_name = "forum/topic.jinja"
    context_object_name = "topic"

71
class ForumMessageCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    model = ForumMessage
    fields = ['title', 'message']
    template_name = "core/create.jinja"

    def dispatch(self, request, *args, **kwargs):
        self.topic = get_object_or_404(ForumTopic, id=self.kwargs['topic_id'])
        if not request.user.can_view(self.topic):
            raise PermissionDenied
        return super(ForumMessageCreateView, self).dispatch(request, *args, **kwargs)

    def get_initial(self):
        init = super(ForumMessageCreateView, self).get_initial()
        try:
            init['message'] = "\n".join([
                " > " + line for line in ForumMessage.objects.filter(id=self.request.GET['quote_id']).first().message.split('\n')
                ])
        except: pass
        return init

    def form_valid(self, form):
        form.instance.topic = self.topic
        form.instance.author = self.request.user
        return super(ForumMessageCreateView, self).form_valid(form)