pytask/taskapp/views/user.py
changeset 10 c2001db39937
parent 8 825a497d95b0
child 11 d28fcc644fbb
equal deleted inserted replaced
9:554581fa3253 10:c2001db39937
       
     1 from django.http import HttpResponse
       
     2 from django.shortcuts import redirect, render_to_response
       
     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
       
     8 
       
     9 def redirect_to_homepage(request):
       
    10     """ simply redirect to homepage """
       
    11     
       
    12     return redirect('/')
       
    13 
       
    14 def homepage(request):
       
    15     """ check for authentication and display accordingly. """
       
    16     
       
    17     user = request.user
       
    18     is_guest = False
       
    19     is_mentor = False
       
    20     can_create_task = False
       
    21     task_list = []
       
    22     
       
    23     if not user.is_authenticated():
       
    24         is_guest = True
       
    25         disp_num = 10
       
    26         tasks_count = Task.objects.count()
       
    27         if tasks_count <= disp_num:
       
    28             task_list = Task.objects.order_by('id').reverse()
       
    29         else:
       
    30             task_list = Task.objects.order_by('id').reverse()[:10]
       
    31     else:
       
    32         user_profile = user.get_profile()
       
    33         is_mentor = True if user.task_mentors.all() else False
       
    34         can_create_task = False if user_profile.rights == u"CT" else True
       
    35         
       
    36     context = {'user':user,
       
    37                'is_guest':is_guest,
       
    38                'is_mentor':is_mentor,
       
    39                'task_list':task_list,
       
    40                'can_create_task':can_create_task,
       
    41                }
       
    42                
       
    43     return render_to_response('index.html', context)
       
    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                 if data['username'].isalnum():
       
    54                     try:
       
    55                         if User.objects.get(username__exact = data['username']):
       
    56                             errors=['Choose some other username']
       
    57                             return render_to_response('user/register.html',{'form':form,'errors':errors})
       
    58                     except:
       
    59                          u = createUser(username=data['username'], email=data['email'], password=data['password'],dob = data['dob'],gender = data['gender'])
       
    60                     return redirect('/accounts/login/')
       
    61                 else:
       
    62                     errors = ['Username can contain only alphabets and numbers!']
       
    63                     return render_to_response('user/register.html',{'form':form,'errors':errors})
       
    64             else:
       
    65                 errors=['Password do not match']
       
    66                 form = RegistrationForm(request.POST)
       
    67                 return render_to_response('user/register.html',{'form':form,'errors':errors})#HttpResponse('Password did not match')
       
    68         else:
       
    69             form = RegistrationForm(request.POST)
       
    70     else:
       
    71         form = RegistrationForm()
       
    72     return render_to_response('user/register.html', {'form': form})
       
    73 
       
    74 def user_login(request):
       
    75     if request.method == 'POST':
       
    76         username = request.POST['username']
       
    77         password = request.POST['password']
       
    78         user = authenticate(username=username, password=password)
       
    79         if user is not None:
       
    80             if user.is_active:
       
    81                 login(request, user)
       
    82                 return redirect('/')# Redirect to a success page.
       
    83             else:
       
    84                 return HttpResponse('username is not active, please contact the administrator')# Return a 'disabled account' error message
       
    85         else:
       
    86             errors = ['Please check your username and password']
       
    87             form = LoginForm()
       
    88             return render_to_response('user/login.html',{'form':form,'errors':errors})# Return an 'invalid login' error message.
       
    89         return redirect('/')
       
    90     else:
       
    91         form = LoginForm()
       
    92         return render_to_response('user/login.html',{'form': form})
       
    93 
       
    94 def user_logout(request):
       
    95     logout(request)
       
    96     return HttpResponse('You have logged off successfully!!!')