from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.http import Http404

@login_required
def index(request):
    return render_to_response('members/index.html')


@login_required
def detail(request):
    member = request.user

    return render_to_response('members/detail.html', {'member': member},
                              context_instance=RequestContext(request))

@login_required
def subscriptions(request):
    member = request.user
    subscriptions = member.get_active_subscriptions()

    return render_to_response('members/subscriptions.html',
                              {'subscriptions':subscriptions},
                              context_instance=RequestContext(request))

@login_required
def invoices(request):
    member = request.user

    invoices = member.invoices.all().order_by('-date')

    return render_to_response('members/invoices.html',
                              {'invoices':invoices},
                              context_instance=RequestContext(request))


@login_required
def contact(request):
    return render_to_response('members/contact.html')

@login_required
def change_password(request):
    member = get_object_or_404(Member, pk=member_id)
    if request.POST['password']:
        member.password = ''
    else:
        return render(request, 'members/change_password.html',
                      {'member': member})
