def create_profile(user, **kwargs): default = { 'user': user, 'name': 'Test', 'birthday': date(1985, 8, 18), } default.update(kwargs) profile = UserProfile(**default) profile.save() return profile
def register(request): """Sign up page.""" # If you're already logged in, you shouldn't be here if request.user.is_authenticated(): messages.error(request, 'You are already logged in.') return redirect('pony.users.views.dashboard') # Handle the registration form if request.method == 'POST': register_form = forms.RegisterForm(request.POST) if register_form.is_valid(): # Create the user and log them in User.objects.create_user( register_form.cleaned_data['email'], register_form.cleaned_data['email'], register_form.cleaned_data['password'], ) user = auth.authenticate( username=register_form.cleaned_data['email'], password=register_form.cleaned_data['password'], ) auth.login(request, user) # Create the user profile user_profile = UserProfile( user=user, name=register_form.cleaned_data['name'], birthday=register_form.cleaned_data['birthday'], facebook_token=register_form.cleaned_data['facebook_token'], ) # Grab the twitter auth info from the session, if it's there session_access_token = request.session.get('twitter_access_token', None) if session_access_token: user_profile.twitter_username = session_access_token['screen_name'] user_profile.twitter_user_id = session_access_token['user_id'] user_profile.twitter_token = session_access_token['oauth_token'] user_profile.twitter_token_secret = session_access_token['oauth_token_secret'] # Save the profile user_profile.save() # If the user session has a gift on it, redirect there gift = request.session.get('gift', None) if gift: # Determine the gift date based on the current date next_birthday = user_profile.birthday.replace(year=datetime.utcnow().year) if datetime.utcnow().date() > next_birthday: next_birthday.replace(year=datetime.utcnow().year + 1) # Fill in the gift details for this user gift.user = user gift.gift_date = next_birthday gift.status = gift.ACTIVE gift.save() del request.session['gift'] return redirect(gift) return redirect('pony.users.views.dashboard') else: register_form = forms.RegisterForm() return render_to_response('users/register.html', { 'gift': request.session.get('gift', None), 'register_form': register_form, }, RequestContext(request))