src/ldt/ldt/user/views.py
author ymh <ymh.work@gmail.com>
Thu, 23 May 2013 00:17:02 +0200
changeset 1193 cd67b17d257d
parent 1191 b6e0b1811723
child 1195 81e5be7f10f9
permissions -rw-r--r--
- correct url in template - general cleaning - activate LdtUser in admin - remove ADMIN_MEDIA_PREFIX
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1193
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
     1
from .forms import ProfileForm, LanguageChangeForm, PictureForm
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     2
from django.contrib.auth import authenticate, login, logout
1193
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
     3
from django.contrib.auth.decorators import login_required
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
     4
from django.contrib.auth.forms import PasswordChangeForm
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     5
from django.http import HttpResponse, HttpResponseRedirect
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     6
from django.shortcuts import render_to_response
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     7
from django.template import RequestContext, loader
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     8
from django.utils import simplejson
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     9
from django.utils.translation import ugettext as _
1193
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    10
from django.views.i18n import set_language
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    11
    
1193
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    12
@login_required   
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    13
def profile(request):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    14
    msg = ''
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    15
    profile = request.user.get_profile()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    16
    user_language = profile.language
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    17
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    18
    if request.method == "POST":
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    19
        profile_form = ProfileForm(request.user, request.POST, instance=request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    20
        language_form = LanguageChangeForm(request.user, request.POST)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    21
        password_form = PasswordChangeForm(request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    22
        if profile_form.is_valid() and language_form.is_valid():
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    23
            profile_form.save()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    24
            language_form.save()  
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    25
            set_language(request)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    26
            msg = _("Your profile has been updated.")
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    27
            user_language = language_form.cleaned_data['language']      
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    28
              
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    29
    else:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    30
        language_form = LanguageChangeForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    31
        profile_form = ProfileForm(instance=request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    32
        password_form = PasswordChangeForm(request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    33
    profile_picture_form = PictureForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    34
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    35
    return render_to_response('ldt/user/change_profile.html', {'profile_form' : profile_form, 'language_form' : language_form, 'password_form' : password_form, 'user_language' : user_language, 'profile_picture_form':profile_picture_form, 'msg' : msg }, context_instance=RequestContext(request))    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    36
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    37
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    38
@login_required   
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    39
def password(request):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    40
    msg = ''
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    41
    user_language = request.user.get_profile().language
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    42
   
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    43
    if request.method == "POST":        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    44
        password_form = PasswordChangeForm(request.user, request.POST)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    45
        profile_form = ProfileForm(instance=request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    46
        language_form = LanguageChangeForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    47
        if password_form.is_valid():
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    48
            password_form.save()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    49
            msg = _("Your password has been updated.")
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    50
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    51
    else:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    52
        language_form = LanguageChangeForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    53
        profile_form = ProfileForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    54
        password_form = PasswordChangeForm(request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    55
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    56
    profile_picture_form = PictureForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    57
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    58
    return render_to_response('ldt/user/change_profile.html', {'profile_form' : profile_form, 'language_form' : language_form, 'password_form' : password_form, 'user_language' : user_language, 'profile_picture_form':profile_picture_form, 'msg' : msg }, context_instance=RequestContext(request))    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    59
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    60
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    61
@login_required   
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    62
def profile_picture(request):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    63
    msg = ''
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    64
    user_language = request.user.get_profile().language
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    65
   
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    66
    if request.method == "POST":
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    67
        profile_form = ProfileForm(instance=request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    68
        language_form = LanguageChangeForm(request.user, request.POST)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    69
        password_form = PasswordChangeForm(request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    70
        profile_picture_form = PictureForm(request.user, request.POST, request.FILES)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    71
        if profile_picture_form.is_valid():
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    72
            profile_picture_form.save()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    73
            msg = _("Your profile picture has been updated.")
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    74
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    75
    else:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    76
        language_form = LanguageChangeForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    77
        profile_form = ProfileForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    78
        password_form = PasswordChangeForm(request.user)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    79
        profile_picture_form = PictureForm()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    80
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    81
    return render_to_response('ldt/user/change_profile.html', {'profile_form' : profile_form, 'language_form' : language_form, 'password_form' : password_form, 'user_language' : user_language, 'profile_picture_form':profile_picture_form, 'msg' : msg }, context_instance=RequestContext(request))
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    82
169
64f24f8841ec Add profile to user
verrierj
parents: 167
diff changeset
    83
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    84
def logout_view(request):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    85
    logout(request)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    86
    return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    87
    #return HttpResponseRedirect(settings.LOGOUT_URL)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    88
    
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 165
diff changeset
    89
def login_ajax(request, loginstate_template_name='ldt/user/login_form.html'):
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    90
    if request.method == "POST":
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    91
        username = request.POST["username"]
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    92
        password = request.POST["password"]
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    93
        user = authenticate(username=username, password=password)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    94
        error_message = _(u"Sorry, that's not a valid username or password.")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    95
        if user is not None:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    96
            if user.is_active:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    97
                login(request, user)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    98
                context = RequestContext(request, { 'username': user.username, })
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    99
                template = loader.get_template(loginstate_template_name)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   100
                html = template.render(context)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   101
                return HttpResponse(simplejson.dumps({'message': u'successful', 'username': user.username, 'html': html, 'reload': request.POST["reload"], }))               
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   102
            else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   103
                return HttpResponse(simplejson.dumps({'message': error_message, }))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   104
        else:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   105
            return HttpResponse(simplejson.dumps({'message': error_message, }))
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   106
    return render_to_response('ldt/user/login_ajax.html', context_instance=RequestContext(request))
130
1cc949de2d1f Useless code removed. Iri group finally removed from the model. User's templates cleaned. Little css changed for home.
cavaliet
parents: 128
diff changeset
   107
165
a71757b03e89 Add javascript to profile form
verrierj
parents: 160
diff changeset
   108