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, UserProfileEditForm |
4 from pytask.taskapp.forms.user import UserProfileEditForm |
5 from pytask.taskapp.events.user import createUser, updateProfile |
5 from pytask.taskapp.events.user import createUser, updateProfile |
6 from django.contrib.auth import login, logout, authenticate |
|
7 from django.contrib.auth.models import User |
6 from django.contrib.auth.models import User |
8 from pytask.taskapp.models import Profile |
7 from pytask.taskapp.models import Profile |
9 from django.contrib.auth.decorators import login_required |
8 from django.contrib.auth.decorators import login_required |
10 |
9 |
11 |
10 |
46 'can_create_task':can_create_task, |
45 'can_create_task':can_create_task, |
47 } |
46 } |
48 |
47 |
49 return render_to_response('index.html', context) |
48 return render_to_response('index.html', context) |
50 |
49 |
51 |
|
52 def register(request): |
|
53 """ user registration: gets the user details and create the user and userprofile if data entered is valid""" |
|
54 if request.method == 'POST': |
|
55 form = RegistrationForm(request.POST) |
|
56 if form.is_valid(): |
|
57 data = form.cleaned_data |
|
58 if data['password'] == data['repeat_password']: |
|
59 if data['username'].isalnum(): |
|
60 try: |
|
61 if User.objects.get(username__exact = data['username']): |
|
62 errors=['Choose some other username'] |
|
63 return render_to_response('user/register.html',{'form':form,'errors':errors}) |
|
64 except: |
|
65 u = createUser(username=data['username'], email=data['email'], password=data['password'],dob = data['dob'],gender = data['gender']) |
|
66 return redirect('/accounts/login/') |
|
67 else: |
|
68 errors = ['Username can contain only alphabets and numbers!'] |
|
69 return render_to_response('user/register.html',{'form':form,'errors':errors}) |
|
70 else: |
|
71 errors=['Password do not match'] |
|
72 form = RegistrationForm(request.POST) |
|
73 return render_to_response('user/register.html',{'form':form,'errors':errors})#HttpResponse('Password did not match') |
|
74 else: |
|
75 form = RegistrationForm(request.POST) |
|
76 else: |
|
77 form = RegistrationForm() |
|
78 return render_to_response('user/register.html', {'form': form}) |
|
79 |
|
80 def user_login(request): |
|
81 if request.method == 'POST': |
|
82 username = request.POST['username'] |
|
83 password = request.POST['password'] |
|
84 user = authenticate(username=username, password=password) |
|
85 if user is not None: |
|
86 if user.is_active: |
|
87 login(request, user) |
|
88 return redirect('/')# Redirect to a success page. |
|
89 else: |
|
90 return show_msg('username is not active, please contact the administrator')# Return a 'disabled account' error message |
|
91 else: |
|
92 errors = ['Please check your username and password'] |
|
93 form = LoginForm() |
|
94 return render_to_response('user/login.html',{'form':form,'errors':errors})# Return an 'invalid login' error message. |
|
95 return redirect('/') |
|
96 else: |
|
97 form = LoginForm() |
|
98 return render_to_response('user/login.html',{'form': form}) |
|
99 |
|
100 def user_logout(request): |
|
101 logout(request) |
|
102 return show_msg('You have logged off successfully!!!') |
|
103 |
|
104 @login_required |
50 @login_required |
105 def view_my_profile(request,uid): |
51 def view_my_profile(request,uid): |
106 """ allows the user to view the profiles of users """ |
52 """ allows the user to view the profiles of users """ |
107 edit_profile = True if request.user == User.objects.get(pk=uid) else False |
53 edit_profile = True if request.user == User.objects.get(pk=uid) else False |
108 try: |
54 try: |