taskapp/views/users.py
changeset 13 0fb64b24a1c9
parent 12 a93eebabfeb1
child 14 54694601c5e3
equal deleted inserted replaced
12:a93eebabfeb1 13:0fb64b24a1c9
     1 from django.http import HttpResponse
     1 from django.http import HttpResponse
     2 from django.shortcuts import redirect, render_to_response
     2 from django.shortcuts import redirect, render_to_response
     3 from pytask.taskapp.models import Task
     3 from pytask.taskapp.models import Task
       
     4 from pytask.taskapp.forms.user import RegistrationForm, LoginForm
       
     5 from pytask.taskapp.events.user import createUser
       
     6 from django.contrib.auth import login, logout, authenticate
       
     7 from django.contrib.auth.models import User
     4 
     8 
     5 def redirect_to_homepage(request):
     9 def redirect_to_homepage(request):
     6     """ simply redirect to homepage """
    10     """ simply redirect to homepage """
     7     
    11     
     8     return redirect('/')
    12     return redirect('/')
    35                'task_list':task_list,
    39                'task_list':task_list,
    36                'can_create_task':can_create_task,
    40                'can_create_task':can_create_task,
    37                }
    41                }
    38                
    42                
    39     return render_to_response('index.html', context)
    43     return render_to_response('index.html', context)
    40                
       
    41 
    44 
       
    45 
       
    46 def register(request):
       
    47     """ user registration: gets the user details and create the user and userprofile if data entered is valid"""
       
    48     if request.method == 'POST':
       
    49         form = RegistrationForm(request.POST)
       
    50         if form.is_valid():
       
    51             data = form.cleaned_data
       
    52             if data['password'] == data['repeat_password']:
       
    53                 try:
       
    54                     if User.objects.get(username__exact = data['username']):
       
    55                         errors=['Choose some other username']
       
    56                         return render_to_response('user/register.html',{'form':form,'errors':errors})
       
    57                 except:
       
    58                      u = createUser(username=data['username'], email=data['email'], password=data['password'],dob = data['dob'],gender = data['gender'])
       
    59                 return redirect('/accounts/login/')
       
    60             else:
       
    61                 errors=['Password do not match']
       
    62                 form = RegistrationForm(request.POST)
       
    63                 return render_to_response('user/register.html',{'form':form,'errors':errors})#HttpResponse('Password did not match')
       
    64         else:
       
    65             form = RegistrationForm()
       
    66     else:
       
    67         form = RegistrationForm()
       
    68     return render_to_response('user/register.html', {'form': form})
       
    69 
       
    70 def user_login(request):
       
    71     if request.method == 'POST':
       
    72         username = request.POST['username']
       
    73         password = request.POST['password']
       
    74         user = authenticate(username=username, password=password)
       
    75         if user is not None:
       
    76             if user.is_active:
       
    77                 login(request, user)
       
    78                 return redirect('/')# Redirect to a success page.
       
    79             else:
       
    80                 return HttpResponse('username is not active, please contact the administrator')# Return a 'disabled account' error message
       
    81         else:
       
    82             errors = ['Please check your username and password']
       
    83             form = LoginForm()
       
    84             return render_to_response('user/login.html',{'form':form,'errors':errors})# Return an 'invalid login' error message.
       
    85         return redirect('/')
       
    86     else:
       
    87         form = LoginForm()
       
    88         return render_to_response('user/login.html',{'form': form})
       
    89 
       
    90 def user_logout(request):
       
    91     logout(request)
       
    92     return HttpResponse('You have logged off successfully!!!')