web/ldt/user/views.py
author wakimd
Sun, 14 Nov 2010 20:25:22 +0100
changeset 1 3a30d255c235
permissions -rw-r--r--
First version of API with tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     1
from django.conf import settings
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     2
from django.http import HttpResponse, HttpResponseRedirect
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     3
from django.shortcuts import render_to_response
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     4
from django.contrib.auth import authenticate, login, logout
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     5
from django.contrib.auth.decorators import login_required
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     6
# from django.contrib.sites.models import Site, RequestSite
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     7
from django.template import RequestContext, Context, loader
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     8
from django.utils.translation import ugettext as _
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
     9
from django.core.urlresolvers import reverse
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    10
from forms import EmailChangeForm
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    11
from django.utils import simplejson
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    12
from ldt.management import test_cms, test_ldt
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    13
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    14
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    15
def home(request):
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    16
    return render_to_response('ldt/user/home.html',context_instance=RequestContext(request))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    17
    
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    18
@login_required   
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    19
def profile(request):
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    20
    return render_to_response('ldt/user/profile.html', context_instance=RequestContext(request))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    21
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    22
@login_required     
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    23
def space(request, page_id=None, slug=None):
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    24
    cms = test_cms()
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    25
    ldt = test_ldt()
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    26
    context={
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    27
        'cms': cms,
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    28
        'ldt': ldt
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    29
    }
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    30
    return render_to_response('ldt/user/space.html', context, context_instance=RequestContext(request))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    31
    
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    32
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    33
    
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    34
def logout_view(request):
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    35
    logout(request)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    36
    return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    37
    #return HttpResponseRedirect(settings.LOGOUT_URL)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    38
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    39
    
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    40
def loginAjax(request, loginstate_template_name='ldt/user/login_form.html'):
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    41
    if request.method == "POST":
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    42
        username=request.POST["username"]
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    43
        password=request.POST["password"]
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    44
        user = authenticate(username=username, password=password)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    45
        error_message = _(u"Sorry, that's not a valid username or password.")
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    46
        if user is not None:
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    47
            if user.is_active:
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    48
                login(request, user)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    49
                context = RequestContext(request, { 'username': user.username,})
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    50
                template = loader.get_template(loginstate_template_name)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    51
                html = template.render(context)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    52
                return HttpResponse(simplejson.dumps({'message': u'successful', 'username': user.username, 'html': html, 'reload': request.POST["reload"],}))               
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    53
            else:
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    54
                return HttpResponse(simplejson.dumps({'message': error_message,}))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    55
        else:
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    56
            return HttpResponse(simplejson.dumps({'message': error_message,}))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    57
    return render_to_response('ldt/user/login_ajax.html', context_instance=RequestContext(request))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    58
            
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    59
@login_required              
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    60
def change_email(request, post_change_redirect=None):
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    61
    if post_change_redirect is None:
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    62
        post_change_redirect = reverse('ldt.user.views.change_email_done')
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    63
    if request.method == "POST":
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    64
        form = EmailChangeForm(request.user, request.POST)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    65
        if form.is_valid():
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    66
            form.save()
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    67
            return HttpResponseRedirect(post_change_redirect)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    68
    else:
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    69
        form = EmailChangeForm(request.user)
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    70
    return render_to_response('ldt/user/change_email.html', {'form': form,}, context_instance=RequestContext(request))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    71
    
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    72
@login_required    
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    73
def change_email_done(request, template_name='ldt/user/change_email_done.html'):
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    74
    return render_to_response(template_name, context_instance=RequestContext(request))
3a30d255c235 First version of API with tests
wakimd
parents:
diff changeset
    75