from django.views import generic
from django.core.urlresolvers import reverse
from django.http import HttpResponseNotFound
from django.utils.translation import ugettext as _
from django.conf import settings
from django.contrib import messages

from .models import Note
from .forms import NoteForm


class NoteListView(generic.ListView):
    model = Note
    paginate_by = 25
    context_object_name = 'notes'
    template_name = 'shoutbox/message/list.html'

    def get_context_data(self, **kwargs):
        context = super(NoteListView, self).get_context_data(**kwargs)
        context.update({
            'form': NoteForm(),
        })
        return context

    def get_queryset(self):
        return super(NoteListView, self).get_queryset().select_related(
            'author'
        )


class NoteCreateView(generic.CreateView):
    model = Note
    fields = ('message', )
    message_create = _(
        """Your note has been added to the board, """
        """it will be displayed in a moment"""
    )
    template_name = 'shoutbox/message/form.html'

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

    def get_success_url(self):
        messages.success(
            self.request,
            self.message_create,
        )
        success_url = self.request.POST.get('next', None)
        if success_url is not None:
            return success_url
        else:
            return reverse(settings.REDIRECT_URL)


class NoteEditView(generic.UpdateView):
    model = Note
    fields = ('message', )
    template_name = 'shoutbox/message/form.html'

    def dispatch(self, request, *args, **kwargs):
        """ Making sure that only authors can update notes """
        obj = self.get_object()
        if obj.author != self.request.user:
            return HttpResponseNotFound(
                _('Rezo is not hacked. You don\'t have the permission xD')
            )
        return super(NoteEditView, self).dispatch(request, *args, **kwargs)

    def get_success_url(self):
        messages.success(
            self.request,
            _('Your note has been updated, it will be refreshed in a moment'),
        )
        success_url = self.request.POST.get('next', None)
        if success_url is not None:
            return success_url
        else:
            return reverse(settings.REDIRECT_URL)


class NoteDeleteView(generic.DeleteView):
    model = Note
    template_name = 'shoutbox/note_confirm_delete.html'

    def dispatch(self, request, *args, **kwargs):
        """ Making sure that only authors can update notes """
        obj = self.get_object()
        if obj.author != self.request.user:
            return HttpResponseNotFound(
                _('Rezo is not hacked. You don\'t have the permission xD')
            )
        return super(NoteDeleteView, self).dispatch(
            request, *args, **kwargs
        )

    def get_success_url(self):
        messages.success(
            self.request,
            _('Your note has been removed, it will be refreshed in a moment'),
        )
        success_url = self.request.POST.get('next')
        return success_url if success_url is not None else reverse('index')

@@ -1508,32 +1508,56 @@
 REDIRECT_URL)%0A%0A%0A
+# TODO: factorize check%0A
 class NoteEditVi
@@ -3191,16 +3191,22 @@
 t('next'
+, None
 )%0A      
@@ -3199,38 +3199,34 @@
 , None)%0A        
-return
+if
  success_url if 
@@ -3227,52 +3227,111 @@
 rl i
-f success_url is not None else reverse('index'
+s not None:%0A            return success_url%0A        else:%0A            return reverse(settings.REDIRECT_URL
 )%0A

Test compliance for delete view