# # User registrations, logins, and profiles. # from django.shortcuts import render_to_response, get_object_or_404 from django import forms from django.core import validators from django.contrib.auth.models import User from django.contrib.auth import authenticate from django.http import HttpResponseRedirect # from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME from django.template import RequestContext def show_login(request): """ Display and handle the login form. """ from django.contrib.auth.forms import AuthenticationForm messages = {} manipulator = AuthenticationForm(request) redirect_to = request.GET.get('next','') if request.POST: errors = manipulator.get_validation_errors(request.POST) if not errors: if not redirect_to: redirect_to = '/' from django.contrib.auth import login login(request, manipulator.get_user()) request.session.delete_test_cookie() return HttpResponseRedirect(redirect_to) else: errors = {} request.session.set_test_cookie() pagevars = { "page_title": "Login", "form": forms.FormWrapper(manipulator, request.POST, errors), } context_instance = RequestContext(request) return render_to_response('registration/login.html', pagevars, context_instance) class UserRegManip(forms.Manipulator): """ User registration manipulator. Validate our incoming data and handle saving it. """ def __init__(self): self.fields = ( forms.TextField(field_name="username", is_required=True, validator_list=[self.isValidUsername]), forms.TextField(field_name="email", is_required=True, validator_list=[validators.isValidEmail]), forms.PasswordField(field_name="pass1", is_required=True, validator_list=[self.isValidPass]), forms.PasswordField(field_name="pass2", is_required=True), ) def isValidPass(self, field_data, all_data): """ Validate form passwords. """ if field_data != all_data['pass2']: raise validators.ValidationError("Please enter the same password twice.") if len(field_data) < 4: raise validators.ValidationError("Your password must be at least 4 characters long.") def isValidUsername(self, field_data, all_data): """ Validate form usernames. """ if len(field_data) < 3: raise validators.ValidationError("Usernames must be at least 3 characters long.") name_collisions = User.objects.filter(username__iexact=field_data).count() if name_collisions != 0: raise validators.ValidationError("There is already a user with that name.") def save_newuser(self, udata): """ Carry out the saving of the new user. """ newuser = User.objects.create_user(udata['username'], udata['email'], udata['pass1']) newuser.save() # --- END CLASS UserRegManip --- def mail_reginfo(uemail, uname, upass): """ Sends new users their registration information. """ from django.core.mail import send_mail import guildbuilder.settings subject = guildbuilder.settings.SITE_NAME + " Registration" replyto = guildbuilder.settings.SITE_EMAIL_FROM message = """ Your registration has been processed! You may now login with the following: Username: """ + uname + """ Pass: """ + upass + """ Sent from: """ + guildbuilder.settings.ROOT_URL send_mail(subject, message, replyto, [uemail]) # This really should be using a generic view but I couldn't figure out # how to get the generic login review to redirect correctly after # POSTing. def register_user(request): """ Handle the user's registration info. """ manipulator = UserRegManip() context_instance = RequestContext(request) if request.POST: # If data was POSTed, we're trying to create a new Place. new_user = request.POST.copy() # Check for errors. errors = manipulator.get_validation_errors(new_user) if not errors: # No errors. This means we can save the data! manipulator.save_newuser(new_user) mail_reginfo(new_user['email'], new_user['username'], new_user['pass1']) pagevars = { "page_title": "Registration Completed", "pdat": new_user, } # Redirect to the object's "edit" page. Always use a redirect # after POST data, so that reloads don't accidently create # duplicate entires, and so users don't see the confusing # "Repost POST data?" alert box in their browsers. return render_to_response("registration/finish_reg.html", pagevars, context_instance) else: # No POST, so we want a brand new form without any data or errors. errors = new_user = {} # Create the FormWrapper, template, context, response. form = forms.FormWrapper(manipulator, new_user, errors) pagevars = { "page_title": "User Registration", "form": form, "errors": errors, } return render_to_response('registration/register.html', pagevars, context_instance)