Source

Lightning Talks / lightning / talks / views.py

Full commit
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import CreateView, DeleteView
from django.views.generic import ListView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

from talks.forms import TalkForm
from talks.models import Talk


class TalksView(ListView):
    template_name = 'talks/your_talks.html'
    model = Talk

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(TalksView, self).dispatch(*args, **kwargs)

    def get_queryset(self):
        queryset = super(TalksView, self).get_queryset()
        queryset = queryset.filter(user=self.request.user)
        return queryset


class DeleteTalkView(DeleteView):
    model = Talk
    slug_field = 'pk'

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(DeleteTalkView, self).dispatch(*args, **kwargs)

    def get_queryset(self):
        queryset = super(DeleteTalkView, self).get_queryset()
        queryset = queryset.filter(user=self.request.user)
        return queryset

    def get_success_url(self):
        return reverse('talks')


class CreateTalkView(CreateView):
    template_name = 'talks/create_talk.html'
    form_class = TalkForm

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(CreateTalkView, self).dispatch(*args, **kwargs)

    def post(self, request):
        form = TalkForm(request.POST)

        if form.is_valid():
            talk = form.save(commit=False)
            talk.user = request.user
            talk.save()

            return HttpResponseRedirect(reverse('talks'))

        return super(CreateTalkView, self).post(request)