message.py 2.57 KB
Newer Older
Bach Dániel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

18
from django.contrib.messages.views import SuccessMessageMixin
19 20
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
21 22 23 24 25 26 27 28 29 30 31 32
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.views.generic import CreateView, DeleteView, UpdateView

from braces.views import SuperuserRequiredMixin, LoginRequiredMixin
from django_tables2 import SingleTableView

from ..forms import MessageForm
from ..models import Message
from ..tables import MessageListTable


33 34 35 36 37 38 39
class InvalidateMessageCacheMixin(object):
    def post(self, *args, **kwargs):
        key = make_template_fragment_key('broadcast_messages')
        cache.delete(key)
        return super(InvalidateMessageCacheMixin, self).post(*args, **kwargs)


40 41 42 43 44 45
class MessageList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView):
    template_name = "dashboard/message-list.html"
    model = Message
    table_class = MessageListTable


46 47
class MessageDetail(InvalidateMessageCacheMixin, LoginRequiredMixin,
                    SuperuserRequiredMixin, SuccessMessageMixin, UpdateView):
48 49 50 51 52 53
    model = Message
    template_name = "dashboard/message-edit.html"
    form_class = MessageForm
    success_message = _("Broadcast message successfully updated.")


54 55
class MessageCreate(InvalidateMessageCacheMixin, LoginRequiredMixin,
                    SuperuserRequiredMixin, SuccessMessageMixin, CreateView):
56 57 58 59 60 61
    model = Message
    template_name = "dashboard/message-create.html"
    form_class = MessageForm
    success_message = _("New broadcast message successfully created.")


62 63
class MessageDelete(InvalidateMessageCacheMixin, LoginRequiredMixin,
                    SuperuserRequiredMixin, DeleteView):
64 65 66 67 68
    model = Message
    template_name = "dashboard/confirm/base-delete.html"

    def get_success_url(self):
        return reverse("dashboard.views.message-list")