from django.shortcuts import render

from django.http import HttpResponseRedirect

from django.shortcuts import render_to_response
from django.template import RequestContext

from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required

from courses.models import Course


#def handler404(request):
#    response = render_to_response('404.html', {}, context_instance=RequestContext(request))
#    response.status_code = 404
#    return response


#def handler500(request):
#    response = render_to_response('500.html', {}, context_instance=RequestContext(request))
#    response.status_code = 500
#    return response



def index(request):
    courses_list = Course.objects.order_by('title')
    data = {'courses_list': courses_list}
    return render(request, 'frontend/index.html', data)


def attribution(request):
    data = {}
    return render(request, 'frontend/attribution.html', data)

def privacy(request):
    data = {}
    return render(request, 'frontend/privacy.html', data)

def dashboard(request):
    # only registered users can see their statistics
    if not request.user.is_authenticated:
        return HttpResponseRedirect('/')
    else:
        data = {}
        return render(request, 'frontend/dashboard.html')


# Alternative approach to requiring auth using decorators (will redirect to login page)
@login_required
def settings(request):
    # only registered users have settings
    if not request.user.is_authenticated:
        return HttpResponseRedirect('/')
    else:
        data = {}
        return render(request, 'frontend/settings.html')
