views.py 6.1 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"

Skia's avatar
Skia committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
class ForumMarkAllAsRead(RedirectView):
    permanent = False
    url = reverse_lazy('forum:last_unread')

    def get(self, request, *args, **kwargs):
        try:
            fi = request.user.forum_infos
            fi.last_read_date = timezone.now()
            fi.save()
        except: pass
        return super(ForumMarkAllAsRead, self).get(request, *args, **kwargs)

class ForumLastUnread(ListView):
    model = ForumTopic
    template_name = "forum/last_unread.jinja"

    def get_queryset(self):
        l = ForumMessage.objects.exclude(readers=self.request.user).filter(
                date__gt=self.request.user.forum_infos.last_read_date).values_list('topic') # TODO try to do better
        return self.model.objects.filter(id__in=l).annotate(models.Max('messages__date')).order_by('-messages__date__max')
        # return self.model.objects.exclude(messages__readers=self.request.user).distinct().annotate(
                # models.Max('messages__date')).order_by('-messages__date__max')

42
class ForumCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
43
    model = Forum
44
    fields = ['name', 'parent', 'owner_club', 'is_category', 'edit_groups', 'view_groups']
Skia's avatar
Skia committed
45 46 47 48
    template_name = "core/create.jinja"

    def get_initial(self):
        init = super(ForumCreateView, self).get_initial()
Skia's avatar
Skia committed
49 50 51
        try:
            parent = Forum.objects.filter(id=self.request.GET['parent']).first()
            init['parent'] = parent
52
            init['owner_club'] = parent.owner_club
Skia's avatar
Skia committed
53
        except: pass
Skia's avatar
Skia committed
54 55
        return init

56 57 58 59 60 61
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)

62
class ForumEditView(CanEditPropMixin, UpdateView):
Skia's avatar
Skia committed
63 64
    model = Forum
    pk_url_kwarg = "forum_id"
65
    form_class = ForumEditForm
Skia's avatar
Skia committed
66 67 68
    template_name = "core/edit.jinja"
    success_url = reverse_lazy('forum:main')

69 70 71 72 73 74 75 76 77 78 79 80
    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')

81
class ForumDetailView(CanViewMixin, DetailView):
Skia's avatar
Skia committed
82 83 84 85
    model = Forum
    template_name = "forum/forum.jinja"
    pk_url_kwarg = "forum_id"

Skia's avatar
Skia committed
86 87 88 89 90
    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

91
class ForumTopicCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
92 93
    model = ForumMessage
    fields = ['title', 'message']
Skia's avatar
Skia committed
94 95 96 97 98 99 100 101 102
    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
103 104 105
        topic = ForumTopic(title=form.instance.title, author=self.request.user, forum=self.forum)
        topic.save()
        form.instance.topic = topic
Skia's avatar
Skia committed
106 107 108
        form.instance.author = self.request.user
        return super(ForumTopicCreateView, self).form_valid(form)

Skia's avatar
Skia committed
109
class ForumTopicEditView(CanEditMixin, UpdateView):
Skia's avatar
Skia committed
110
    model = ForumTopic
Skia's avatar
Skia committed
111
    fields = ['title', 'forum']
Skia's avatar
Skia committed
112 113 114
    pk_url_kwarg = "topic_id"
    template_name = "core/edit.jinja"

115
class ForumTopicDetailView(CanViewMixin, DetailView):
Skia's avatar
Skia committed
116 117 118 119 120
    model = ForumTopic
    pk_url_kwarg = "topic_id"
    template_name = "forum/topic.jinja"
    context_object_name = "topic"

Skia's avatar
Skia committed
121 122 123 124 125 126 127 128 129
    def get_context_data(self, **kwargs):
        kwargs = super(ForumTopicDetailView, self).get_context_data(**kwargs)
        msg = self.object.messages.exclude(readers=self.request.user).order_by('id').first()
        try:
            kwargs['first_unread_message_id'] = msg.id
        except:
            kwargs['first_unread_message_id'] = -1
        return kwargs

Skia's avatar
Skia committed
130 131 132 133 134 135
class ForumMessageEditView(CanEditMixin, UpdateView):
    model = ForumMessage
    fields = ['title', 'message']
    template_name = "core/edit.jinja"
    pk_url_kwarg = "message_id"

136
class ForumMessageCreateView(CanCreateMixin, CreateView):
Skia's avatar
Skia committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150
    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([
151
                "> " + line for line in ForumMessage.objects.filter(id=self.request.GET['quote_id']).first().message.split('\n')
Skia's avatar
Skia committed
152 153 154 155 156 157 158 159 160
                ])
        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)