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]
                        processed.add(s[0].id)
                else:
                    wide[d][t][r] = None

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

    contexts = {
        'wide': wide,
        'narrow': narrow,
        'rooms': rooms,
    }
    return render(request, 'schedule.html', contexts) 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
        else:
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
    return response def robots(request):
    return render(request, 'robots.txt', content_type='text/plain') class RoomDetail(DetailView):
    model = Room class JobfairList(ListView):
    model = Jobfair class AnnouncementList(ListView):
    model = Announcement class AnnouncementDetail(DetailView):
    model = Announcement 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 self.request.user.email == self.object.email:
                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(email=self.request.user.email) class SponsorList(ListView):
    model = Sponsor class SponsorDetail(DetailView):
    model = Sponsor 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 self.request.user.email == speaker.email:
                    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(speakers__email=self.request.user.email) 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']
            EmailToken.objects.filter(email=email).delete()

            # Create new
            token = EmailToken(email=email)
            token.save()

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

    return render(request, 'login.html', {
        'form': form,
        'title': _('Login'),
    }) @never_cache
def login_req(request, token):
    time_threshold = datetime.now() - timedelta(hours=1)

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

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

    token.delete()

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

    return redirect(reverse('index')) @never_cache
def login_mailsent(request):
    return render(request, 'login_mailsent.html', {
        'title': _('Mail sent'),
    }) def logout(request):
    user_logout(request)
    return redirect(reverse('index'))
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib.flatpages import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from .views import index, schedule, setlang, robots
from .views import RoomDetail, JobfairList
from .views import AnnouncementList, AnnouncementDetail
from .views import SpeakerList, SpeakerDetail, SpeakerUpdate
from .views import SponsorList, SponsorDetail
from .views import ProgramList, ProgramDetail, ProgramUpdate
from .views import login, login_req, login_mailsent, logout
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns(
    '',
    url(r'^$', index, name='index'),

    url(r'^room/(?P<pk>\d+)$',
        RoomDetail.as_view(), name='room'),
    url(r'^about/announcements/$',
        AnnouncementList.as_view(), name='announcements'),
    url(r'^about/announcement/(?P<pk>\d+)$',
        AnnouncementDetail.as_view(), name='announcement'),
    url(r'^about/sponsors/$',
        SponsorList.as_view(), name='sponsors'),
    url(r'^about/sponsor/(?P<slug>\w+)$',
        SponsorDetail.as_view(), name='sponsor'),
    url(r'^programs/list/$',
        ProgramList.as_view(), name='programs'),
    url(r'^program/(?P<pk>\d+)$',
        ProgramDetail.as_view(), name='program'),
    url(r'^program/(?P<pk>\d+)/edit$',
        ProgramUpdate.as_view(), name='program_edit'),
    url(r'^programs/speakers/$',
        SpeakerList.as_view(), name='speakers'),
    url(r'^programs/speaker/(?P<slug>\w+)$',
        SpeakerDetail.as_view(), name='speaker'),
    url(r'^programs/speaker/(?P<slug>\w+)/edit$',
        SpeakerUpdate.as_view(), name='speaker_edit'),
    url(r'^programs/schedule/$',
        schedule, name='schedule'),
    url(r'^programs/jobfair/$',

        JobfairList.as_view(), name='jobfair'),