Source

Lightning Talks / lightning / common / views.py

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

from common.forms import ProfileForm, TalkForm
from common.models import Talk

def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse('home'))

class CreateUpdateProfileView(TemplateView):
    template_name = 'common/profile_form.html'

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

    def get(self, request):
        form = ProfileForm(instance=request.user)
        return self.render_to_response({'form': form})

    def post(self, request):
        form = ProfileForm(request.POST, instance=request.user)

        if form.is_valid():
            form.save()

            return HttpResponseRedirect(reverse('profile'))

        return self.render_to_response({'form': form})


class TalksView(ListView):
    template_name = 'common/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 CreateTalkView(CreateView):
    template_name = 'common/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('profile'))

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