from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from rest_framework import viewsets
from django.views.generic.detail import DetailView

from .serializers import EventSerializer
from datetimewidget.widgets import DateTimeWidget

from .models import Event
from .forms import EventForm

from datetime import datetime


def events_home(request):
    context = {
        'future': Event.objects.filter(stop__gt=datetime.now()).order_by('start')[:10],
        'past': Event.objects.exclude(stop__gt=datetime.now()).order_by('-start')[:10],
    }

    return render(request, "events_home.html", context)


def add_event(request):
    if request.method == 'POST':
        form = EventForm(request.POST)
        if form.is_valid():
            event = form.save(commit=False)
            event.organizer = request.user
            event.save()

            return HttpResponseRedirect(reverse('view_event', args=[event.id]))

    else:
        form = EventForm()

    form.fields['start'].widget = DateTimeWidget(attrs=form.fields['start'].widget.attrs,
                                                            usel10n=True, bootstrap_version=3)
    form.fields['stop'].widget = DateTimeWidget(attrs=form.fields['stop'].widget.attrs,
                                                            usel10n=True, bootstrap_version=3)

    return render(request, 'add_event.html', {'form': form})


class EventDetailView(DetailView):

    model = Event
    template_name = 'event_detail.html'
    context_object_name = 'event'


class EventViewSet(viewsets.ModelViewSet):
    queryset = Event.objects.all()
    serializer_class = EventSerializer

@@ -365,23 +365,27 @@
 om d
-atetime
+jango.utils
  import 
 date
@@ -380,23 +380,23 @@
  import 
-datetim
+timezon
 e%0A%0A%0Adef 
@@ -473,39 +473,39 @@
 filter(stop__gt=
-datetim
+timezon
 e.now()).order_b
@@ -568,23 +568,23 @@
 top__gt=
-datetim
+timezon
 e.now())
@@ -1071,32 +1071,41 @@
  DateTimeWidget(
+%0A        
 attrs=form.field
@@ -1133,60 +1133,8 @@
 rs,%0A
-                                                    
     
@@ -1142,32 +1142,40 @@
    usel10n=True,
+%0A       
  bootstrap_versi
@@ -1178,16 +1178,21 @@
 ersion=3
+%0A    
 )%0A    fo
@@ -1233,16 +1233,25 @@
 eWidget(
+%0A        
 attrs=fo
@@ -1286,60 +1286,8 @@
 rs,%0A
-                                                    
     
@@ -1303,16 +1303,24 @@
 0n=True,
+%0A       
  bootstr
@@ -1331,16 +1331,21 @@
 ersion=3
+%0A    
 )%0A%0A    r

Fix naive timezone filtering in project list