Exemple #1
0
def user_update(request, user_id):
    
    # security check
    if str(request.user.id) != user_id:
        raise Exception("User not authorized to change profile data")
    
    # get user
    user = get_object_or_404(User, pk=user_id)
    
    if (request.method=='GET'):
        
        # pre-populate form, including value of extra field 'confirm_password'
        form = UserForm(instance=user, initial={ 'confirm_password':user.password })
                        
        return render_to_response(USER_FORM_PAGE, {'form': form }, context_instance=RequestContext(request))

    else:
        form = UserForm(request.POST, instance=user) # form with bounded data
        
        if form.is_valid():
            
            # update user
            user = form.save()
                                                
            # redirect to user profile page
            return HttpResponseRedirect(reverse('user_detail', kwargs={ 'user_id':user.id }))
             
        else: 
            print "Form is invalid: %s" % form.errors
            return render_to_response(USER_FORM_PAGE, {'form': form }, context_instance=RequestContext(request))
Exemple #2
0
def user_add(request):

    # GET
    if (request.method=='GET'):
        
        # FIXME
        #form = UserForm( initial={ 'first_name':'Test', 
        #                           'last_name':'User',
        #                           'email':'*****@*****.**',
        #                           'username':'******',
        #                           'password':'',
        #                           'confirm_password:'******'' }) 
        form = UserForm() # empty form with no data
        return render_to_response(USER_FORM_PAGE, {'form': form }, context_instance=RequestContext(request))
        
    # POST
    else:
        
        form = UserForm(request.POST) # form with bounded data
        
        if form.is_valid():
            
            # create a user from the form but don't save it to the database yet because the password is not encoded
            user = form.save(commit=False)
            # must reset the password through the special method that encodes it correctly
            user.set_password(form.cleaned_data['password'])
            # save user to database
            user.save()
            
            # subscription to mailing list ?
            subscribed = form.cleaned_data['subscribed']
            
            # notify site administrators
            notifyAdminsOfUserRegistration(user, subscribed)
            
            
            # redirect to login page with special message
            message = 'Thank you for creating an account. You can now login.'
            return HttpResponseRedirect(reverse('login')+"?message=%s" % message)
        
        else:
            print "Form is invalid: %s" % form.errors
            return render_to_response(USER_FORM_PAGE, {'form': form }, context_instance=RequestContext(request))