Пример #1
0
def registration(request):
    """
    Register new user and send email with activation link
    """
    
    if request.POST:
        user_form = UserRegistrationForm(request.POST, prefix='user_form')
        profile_form = ProfileRegistrationForm(request.POST, prefix='profile_form')
        if user_form.is_valid() and profile_form.is_valid():
            # Create new inactive user
            user = user_form.save()
            user.set_password(user_form.cleaned_data['password'])
            user.is_active = False
            user.save()
            
            # Fill user profile with data from registration form
            profile = user.get_profile()
            profile_form = ProfileRegistrationForm(request.POST, instance=profile, prefix='profile_form')
            profile = profile_form.save()
            
            # Save IP address
            profile.ip_address = request.META.get('REMOTE_ADDR', None)
            profile.save()
            
            # Send email with activation key to the user
            current_site = get_current_site(request)
            site_name = current_site.name
            domain = current_site.domain
            registration_token_generator = TokenGenerator(settings.REGISTRATION_TIMEOUT_DAYS)
            
            context = {
                'domain': domain,
                'site_name': site_name,
                'uid': int_to_base36(user.id),
                'user': user,
                'token': registration_token_generator.make_token(user),
                'protocol': 'http',
                'expire': settings.REGISTRATION_TIMEOUT_DAYS,
            }
            
            subject = u'Registration confirmation on %s' % site_name
            message = render_to_string('registration/registration_email.txt', context)
            user.email_user(subject, message)
            
            return HttpResponseRedirect(reverse('registration_done'))
    else:
        user_form = UserRegistrationForm(prefix='user_form')
        profile_form = ProfileRegistrationForm(prefix='profile_form')
    
    return {
        'user_form': user_form,
        'profile_form': profile_form,
    }
Пример #2
0
def registration_confirm(request, uidb36, token):
    """
    Check the hash in a link the user clicked and activate account
    """
    
    try:
        uid_int = base36_to_int(uidb36)
        user = User.objects.get(id=uid_int)
    except (ValueError, User.DoesNotExist):
        user = None
    
    registration_token_generator = TokenGenerator(settings.REGISTRATION_TIMEOUT_DAYS)
    if (user is not None) and registration_token_generator.check_token(user, token):
        user.is_active = True
        user.save()
        return HttpResponseRedirect(reverse('registration_complete'))
    else:
        return HttpResponseRedirect(reverse('registration_failed'))
Пример #3
0
def email_change_confirm(request, uidb36, token):
    """
    Change the primary email in the case if the hash
    in the link the user clicked was correct
    """
    
    email_change_token_generator = TokenGenerator(settings.EMAIL_CHANGE_TIMEOUT_DAYS)
    
    try:
        uid_int = base36_to_int(uidb36)
        user = User.objects.get(id=uid_int)
    except (ValueError, User.DoesNotExist):
        user = None
    
    if (user is not None) and email_change_token_generator.check_token(user, token):
        profile = user.get_profile()
        user.email = profile.temporary_email
        user.save()
        
        return HttpResponseRedirect(reverse('email_change_complete'))
    else:
        return HttpResponseRedirect(reverse('email_change_failed'))
Пример #4
0
 def test__make_hash_value(self):
     token = TokenGenerator()
     result = token._make_hash_value(self.user_obj, self.user_obj.date_joined)
     string_add = str(self.user_obj.pk) + str(self.user_obj.date_joined) + str(self.user_obj.is_active)
     self.assertEqual(result, string_add)
Пример #5
0
 def test_make_hash_value(self):
     generator = TokenGenerator()
     token = generator._make_hash_value(self.user_pk,
                                        self.cleaned_timestamp)
     expected_token = str(self.user_pk) + str(self.cleaned_timestamp)
     assert token == expected_token
Пример #6
0
def profile_edit(request, profile_id):
    """
    Profile edit
    """
    
    profile = get_object_or_404(UserProfile, id=profile_id)
    old_email = profile.user.email
    
    # Allow user edit only his profile
    if profile.user != request.user:
        raise Http404
    
    if request.POST:
        user_form = UserEditForm(request.POST, instance=profile.user, prefix='user_form')
        profile_form = ProfileEditForm(request.POST, instance=profile, prefix='profile_form')
        
        if user_form.is_valid() and profile_form.is_valid():
            if 'email' in user_form.changed_data:
                # If the user has changed his primary e-mail address,
                # save the new one in the temporary field and send an e-mail
                # with the confirmation link to the new address
                new_email = user_form.cleaned_data['email']
                
                user = user_form.save(commit=False)
                user.email = old_email
                user.save()
                
                # Save new email address in temporary field
                profile = profile_form.save()
                profile.temporary_email = new_email
                profile.ip_address = request.META.get('REMOTE_ADDR', None)
                profile.save()
                
                # Send the email with the confirmation link to the user
                current_site = get_current_site(request)
                site_name = current_site.name
                domain = current_site.domain
                email_change_token_generator = TokenGenerator(settings.EMAIL_CHANGE_TIMEOUT_DAYS)
                
                context = {
                    'domain': domain,
                    'site_name': site_name,
                    'uid': int_to_base36(user.id),
                    'user': user,
                    'token': email_change_token_generator.make_token(user),
                    'protocol': 'http',
                    'expire': settings.EMAIL_CHANGE_TIMEOUT_DAYS,
                }
                
                subject = u'Primary e-mail address change confirmation on %s' % site_name
                message = render_to_string('email_change/email_change_email.txt', context)
                user.email_user(subject, message)
                
                return HttpResponseRedirect(reverse('email_change_done'))
            else:
                user_form.save()
                profile = profile_form.save()
                profile.ip_address = request.META.get('REMOTE_ADDR', None)
                profile.save()
                
                return HttpResponseRedirect(reverse('profile', kwargs={'profile_id': profile.id}))
    else:
        user_form = UserEditForm(instance=profile.user, prefix='user_form')
        profile_form = ProfileEditForm(instance=profile, prefix='profile_form')
    
    return {
        'profile': profile,
        'user_form': user_form,
        'profile_form': profile_form,
    }