|
| 1 | +from django import forms |
| 2 | +from django.http import HttpResponse, HttpResponseRedirect |
| 3 | +from django.shortcuts import render_to_response |
| 4 | +from django.contrib.auth.models import User |
| 5 | +from django.contrib.auth.views import password_reset |
| 6 | +from django.core.context_processors import csrf |
| 7 | +from django.contrib.auth import authenticate, login as login_user, logout as logout_user |
| 8 | +from django.template import RequestContext |
| 9 | + |
| 10 | +INVALID_PASSWORD = "1" |
| 11 | +NO_ERROR = "0" |
| 12 | + |
| 13 | + |
| 14 | +#Template defnitions |
| 15 | +REGISTRATION_TEMPLATE = 'auth/register.html' |
| 16 | +LOGIN_TEMPLATE = 'auth/login.html' |
| 17 | + |
| 18 | +#Defining forms. Django will take this definition and produce a form which can |
| 19 | +#then be picked up by one of the django templates and customised. |
| 20 | +class RegistrationForm(forms.Form): |
| 21 | + firstname = forms.CharField(max_length=100) |
| 22 | + surname = forms.CharField() |
| 23 | + password = forms.CharField(label=(u'Password'),widget=forms.PasswordInput(render_value=False)) |
| 24 | + email = forms.EmailField() |
| 25 | +#Fields left out of the form until we know what is wanted for a signup. |
| 26 | +#Address Details |
| 27 | +#house = forms.CharField() |
| 28 | +#addrline1 = forms.CharField() |
| 29 | +#addrline2 = forms.CharField() |
| 30 | +#country = forms.CharField() |
| 31 | +#postcode = forms.CharField() |
| 32 | + |
| 33 | +class ResetPasswordForm(forms.Form): |
| 34 | + email = forms.EmailField() |
| 35 | + |
| 36 | +class LoginForm(forms.Form): |
| 37 | + username = forms.CharField(max_length=100) |
| 38 | + password = forms.CharField(label=(u'Password'),widget=forms.PasswordInput(render_value=False)) |
| 39 | + |
| 40 | + |
| 41 | +######## Helper Functions ###################################################### |
| 42 | + |
| 43 | +def generate_username(fname, sname): |
| 44 | + return "%s.%s" % (fname, sname) |
| 45 | + |
| 46 | + |
| 47 | +######## Request Functions #################################################### |
| 48 | + |
| 49 | +def register(request): |
| 50 | + c = {} |
| 51 | + c.update(csrf(request)) |
| 52 | + context_inst = RequestContext(request) |
| 53 | + if request.method == 'POST': |
| 54 | + #Handle form submission |
| 55 | + form = RegistrationForm(request.POST) |
| 56 | + if form.is_valid(): |
| 57 | + firstname = form.cleaned_data['firstname'] |
| 58 | + surname = form.cleaned_data['surname'] |
| 59 | + password = form.cleaned_data['password'] |
| 60 | + email = form.cleaned_data['email'] |
| 61 | + try: |
| 62 | + #Check if someone has already used that email |
| 63 | + #address. The email is the username as well, |
| 64 | + #and so has to be unique. |
| 65 | + if User.objects.get(email__exact=email): |
| 66 | + c['error'] = 'ALREADY_SIGNED_UP' |
| 67 | + c['error_value'] = email |
| 68 | + c['form'] = RegistrationForm() |
| 69 | + return render_to_response(REGISTRATION_TEMPLATE, c, context_inst) |
| 70 | + except: |
| 71 | + pass |
| 72 | + print form.cleaned_data |
| 73 | + user = User.objects.create_user(email, email, password) |
| 74 | + user.first_name = firstname |
| 75 | + user.last_name = surname |
| 76 | + user.save() |
| 77 | + return render_to_response('thanks.html', c, context_inst) |
| 78 | + |
| 79 | + else: |
| 80 | + #First time visiting, return an empty form for filling in |
| 81 | + form = RegistrationForm() |
| 82 | + c['form'] = form |
| 83 | + return render_to_response(REGISTRATION_TEMPLATE, c) |
| 84 | + |
| 85 | + |
| 86 | +def login(request, code=NO_ERROR): |
| 87 | + context_inst = RequestContext(request) |
| 88 | + redirect = request.GET.get('next', '') |
| 89 | + c = {} |
| 90 | + c.update(csrf(request)) |
| 91 | + #Check for the invalid password error code |
| 92 | + if code == INVALID_PASSWORD: |
| 93 | + #Reload the login form pass the invalid_password variable to |
| 94 | + #indicate to the user that the login has failed. |
| 95 | + form = LoginForm() |
| 96 | + c['form'] = form |
| 97 | + c['invalid_password'] = True |
| 98 | + return render_to_response(LOGIN_TEMPLATE, c, context_instance=context_inst) |
| 99 | + |
| 100 | + if request.user.is_authenticated(): |
| 101 | + return HttpResponseRedirect('/main/') |
| 102 | + |
| 103 | + #Handle submitting username & password |
| 104 | + if request.method == 'POST': |
| 105 | + form = LoginForm(request.POST) |
| 106 | + if form.is_valid(): |
| 107 | + user_text = form.cleaned_data['username'] |
| 108 | + pass_text = form.cleaned_data['password'] |
| 109 | + user = authenticate(username=user_text, password=pass_text) |
| 110 | + if not user: |
| 111 | + return HttpResponseRedirect('/login/%s/' % INVALID_PASSWORD) |
| 112 | + #Login the user |
| 113 | + login_user(request, user) |
| 114 | + |
| 115 | + #Get Redirect - the user may have navigated to a page that required |
| 116 | + #authentication, we want to log them in, and then pass them on their way. |
| 117 | + redirect = request.POST.get('next') |
| 118 | + |
| 119 | + if redirect != '': |
| 120 | + return HttpResponseRedirect(redirect) |
| 121 | + else: |
| 122 | + #Default location to send the user is to the main page. |
| 123 | + return HttpResponseRedirect('/main/') |
| 124 | + else: |
| 125 | + form = LoginForm() |
| 126 | + c['form'] = form |
| 127 | + c['next'] = redirect |
| 128 | + return render_to_response(LOGIN_TEMPLATE, c, context_instance=context_inst) |
| 129 | + |
| 130 | +def reset_password(request): |
| 131 | + return password_reset(request, template_name='account_reset.html') |
| 132 | + |
| 133 | +def logout(request): |
| 134 | + logout_user(request) |
| 135 | + return HttpResponseRedirect('/main/') |
| 136 | + |
| 137 | +def main(request): |
| 138 | + #For the moment, make sure the user is authenticated, |
| 139 | + #and just send them to the static dashboard page. |
| 140 | + c = RequestContext(request, {'user': request.user}) |
| 141 | + if not request.user.is_authenticated(): |
| 142 | + return HttpResponseRedirect('/login/') |
| 143 | + return render_to_response('dashboard.html', c) |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments