views.py 4.76 KB
Newer Older
Skia's avatar
Skia committed
1 2 3 4 5 6 7 8
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
Skia's avatar
Skia committed
9
from django.db import models
Skia's avatar
Skia committed
10 11
from django.core.exceptions import PermissionDenied

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

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

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

    def get_initial(self):
        init = super(ForumCreateView, self).get_initial()
Skia's avatar
Skia committed
26 27 28
        try:
            parent = Forum.objects.filter(id=self.request.GET['parent']).first()
            init['parent'] = parent
29
            init['owner_club'] = parent.owner_club
Skia's avatar
Skia committed
30
        except: pass
Skia's avatar
Skia committed
31 32
        return init

33 34 35 36 37 38
class ForumEditForm(forms.ModelForm):
    class Meta:
        model = Forum
        fields = ['name', 'parent', 'owner_club', 'is_category', 'edit_groups', 'view_groups']
    recursive = forms.BooleanField(label=_("Apply rights and club owner recursively"), required=False)

39
class ForumEditView(CanEditPropMixin, UpdateView):
Skia's avatar
Skia committed
40 41
    model = Forum
    pk_url_kwarg = "forum_id"
42
    form_class = ForumEditForm
Skia's avatar
Skia committed
43 44 45
    template_name = "core/edit.jinja"
    success_url = reverse_lazy('forum:main')

46 47 48 49 50 51 52 53 54 55 56 57
    def form_valid(self, form):
        ret = super(ForumEditView, self).form_valid(form)
        if form.cleaned_data['recursive']:
            self.object.apply_rights_recursively()
        return ret

class ForumDeleteView(CanEditPropMixin, DeleteView):
    model = Forum
    pk_url_kwarg = "forum_id"
    template_name = "core/delete_confirm.jinja"
    success_url = reverse_lazy('forum:main')

58
class ForumDetailView(CanViewMixin, DetailView):
Skia's avatar
Skia committed
59 60 61 62
    model = Forum
    template_name = "forum/forum.jinja"
    pk_url_kwarg = "forum_id"

Skia's avatar
Skia committed
63 64 65 66 67
    def get_context_data(self, **kwargs):
        kwargs = super(ForumDetailView, self).get_context_data(**kwargs)
        kwargs['topics'] = self.object.topics.annotate(models.Max('messages__date')).order_by('-messages__date__max')
        return kwargs

68
class ForumTopicCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
69 70
    model = ForumMessage
    fields = ['title', 'message']
Skia's avatar
Skia committed
71 72 73 74 75 76 77 78 79
    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
80 81 82
        topic = ForumTopic(title=form.instance.title, author=self.request.user, forum=self.forum)
        topic.save()
        form.instance.topic = topic
Skia's avatar
Skia committed
83 84 85
        form.instance.author = self.request.user
        return super(ForumTopicCreateView, self).form_valid(form)

Skia's avatar
Skia committed
86
class ForumTopicEditView(CanEditMixin, UpdateView):
Skia's avatar
Skia committed
87
    model = ForumTopic
Skia's avatar
Skia committed
88
    fields = ['title', 'forum']
Skia's avatar
Skia committed
89 90 91
    pk_url_kwarg = "topic_id"
    template_name = "core/edit.jinja"

92
class ForumTopicDetailView(CanViewMixin, DetailView):
Skia's avatar
Skia committed
93 94 95 96 97
    model = ForumTopic
    pk_url_kwarg = "topic_id"
    template_name = "forum/topic.jinja"
    context_object_name = "topic"

Skia's avatar
Skia committed
98 99 100 101 102 103
class ForumMessageEditView(CanEditMixin, UpdateView):
    model = ForumMessage
    fields = ['title', 'message']
    template_name = "core/edit.jinja"
    pk_url_kwarg = "message_id"

104
class ForumMessageCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    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)