from django.conf import settings
from django.contrib.auth import login as user_login, logout as user_logout
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.utils.http import is_safe_url
from django.utils.translation import check_for_language
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.cache import never_cache
from django.views.generic import ListView, DetailView, UpdateView
from datetime import datetime, timedelta
from .forms import EmailLoginForm, SpeakerForm, ProgramForm
from .helper import sendEmailToken
from .models import (Room,
                     Program, ProgramDate, ProgramTime, ProgramCategory,
                     Speaker, Sponsor, Jobfair, Announcement,

def index(request):
    return render(request, 'index.html', {
        'recent_announcements': Announcement.objects.all()[:3],

def schedule(request):
    dates = ProgramDate.objects.all()
    times = ProgramTime.objects.all()
    rooms = Room.objects.all()

    wide = {}
    narrow = {}
    processed = set()
    for d in dates:
        wide[d] = {}
        narrow[d] = {}
        for t in times:
            wide[d][t] = {}
            narrow[d][t] = {}
            for r in rooms:
                s = Program.objects.filter(date=d, times=t, rooms=r)
                if s:
                    if s[0].times.all()[0] == t and s[0].id not in processed:
                        wide[d][t][r] = s[0]
                        narrow[d][t][r] = s[0]
                    wide[d][t][r] = None

            if len(narrow[d][t]) == 0:

    contexts = {
        'wide': wide,
        'narrow': narrow,
        'rooms': rooms,
    return render(request, 'schedule.html', contexts)

class RoomDetail(DetailView):
    model = Room

class SponsorList(ListView):
    model = Sponsor

class SponsorDetail(DetailView):
    model = Sponsor

class SpeakerList(ListView):
    model = Speaker

class SpeakerDetail(DetailView):
    model = Speaker

    def get_context_data(self, **kwargs):
        context = super(SpeakerDetail, self).get_context_data(**kwargs)

        if self.request.user.is_authenticated():
            if ==
                context['editable'] = True

        return context

class SpeakerUpdate(UpdateView):
    model = Speaker
    form_class = SpeakerForm

    def get_queryset(self):
        queryset = super(SpeakerUpdate, self).get_queryset()
        return queryset.filter(

class ProgramList(ListView):
    model = ProgramCategory
    template_name = "pyconkr/program_list.html"

class ProgramDetail(DetailView):
    model = Program

    def get_context_data(self, **kwargs):
        context = super(ProgramDetail, self).get_context_data(**kwargs)

        if self.request.user.is_authenticated():
            for speaker in self.object.speakers.all():
                if ==
                    context['editable'] = True

        return context

class ProgramUpdate(UpdateView):
    model = Program
    form_class = ProgramForm

    def get_queryset(self):
        queryset = super(ProgramUpdate, self).get_queryset()
        return queryset.filter(

class JobfairList(ListView):
    model = Jobfair

class AnnouncementList(ListView):
    model = Announcement

class AnnouncementDetail(DetailView):
    model = Announcement

def setlang(request, lang_code):
    # Copied from django.views.i18n.set_language
    next = request.REQUEST.get('next')
    if not is_safe_url(url=next, host=request.get_host()):
        next = request.META.get('HTTP_REFERER')
        if not is_safe_url(url=next, host=request.get_host()):
            next = '/'
    response = HttpResponseRedirect(next)
    if lang_code and check_for_language(lang_code):
        if hasattr(request, 'session'):
            request.session['django_language'] = lang_code
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
    return response

def robots(request):
    return render(request, 'robots.txt', content_type='text/plain')

def login(request):
    form = EmailLoginForm()

    if request.method == 'POST':
        form = EmailLoginForm(request.POST)
        if form.is_valid():
            # Remove previous tokens
            email = form.cleaned_data['email']

            # Create new
            token = EmailToken(email=email)

            sendEmailToken(request, token)
            return redirect(reverse('login_mailsent'))

    return render(request, 'login.html', {
        'form': form,
        'title': _('Login'),

def login_req(request, token):
    time_threshold = - timedelta(hours=1)

        token = EmailToken.objects.get(token=token,
    except ObjectDoesNotExist:
        return render(request, 'login_notvalidtoken.html',
                      {'title': _('Not valid token')})
    email =

    # Create user automatically by email as id, token as password
        user = User.objects.get(email=email)
    except ObjectDoesNotExist:
        user = User.objects.create_user(email, email, token)


    # Set backend manually
    user.backend = 'django.contrib.auth.backends.ModelBackend'
    user_login(request, user)

    return redirect(reverse('index'))

def login_mailsent(request):
    return render(request, 'login_mailsent.html', {
        'title': _('Mail sent'),

def logout(request):
    return redirect(reverse('index'))