def test_user_activation(self): user = User.objects.get( username="******") # Inactive user in fixture # Test calling accounts-activate with wrong hash, user should not be activated bad_hash = '4dad3dft' resp = self.client.get( reverse('accounts-activate', args=[user.username, bad_hash])) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.context['decode_error'], True) self.assertEqual( User.objects.get(username="******").is_active, False) # Test calling accounts-activate with good hash, user should be activated from utils.encryption import create_hash good_hash = create_hash(user.id) resp = self.client.get( reverse('accounts-activate', args=[user.username, good_hash])) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.context['all_ok'], True) self.assertEqual( User.objects.get(username="******").is_active, True) # Test calling accounts-activate for a user that does not exist resp = self.client.get( reverse('accounts-activate', args=["noone", hash])) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.context['user_does_not_exist'], True)
def send_activation(user): uid_hash = create_hash(user.id) username = user.username tvars = { 'user': user, 'username': username, 'hash': uid_hash } send_mail_template(u'activation link.', 'accounts/email_activation.txt', tvars, None, user.email)
def send_activation_email(self): tvars = { 'user': self, 'username': self.username, 'hash': create_hash(self.id) } send_mail(self.email, subject='Activate your Audio Commons user account', template='emails/account_activation.txt', context=tvars)
def activate_user(request, username, uid_hash): if request.user.is_authenticated(): return HttpResponseRedirect(reverse('accounts-home')) try: user = User.objects.get(username__iexact=username) except User.DoesNotExist: return render(request, 'accounts/activate.html', {'user_does_not_exist': True}) new_hash = create_hash(user.id) if new_hash != uid_hash: return render(request, 'accounts/activate.html', {'decode_error': True}) user.is_active = True user.save() return render(request, 'accounts/activate.html', {'all_ok': True})
def activate_user2(request, username, hash): if request.user.is_authenticated(): return HttpResponseRedirect(reverse("accounts-home")) try: user = User.objects.get(username__iexact=username) except User.DoesNotExist: #@UndefinedVariable return render_to_response('accounts/activate.html', { 'user_does_not_exist': True }, context_instance=RequestContext(request)) new_hash = create_hash(user.id) if new_hash != hash: return render_to_response('accounts/activate.html', { 'decode_error': True }, context_instance=RequestContext(request)) user.is_active = True user.save() return render_to_response('accounts/activate.html', { 'all_ok': True }, context_instance=RequestContext(request))
def get_view_description(cls, html=False): description = '' if getattr(cls, 'get_description', None): cache_key = create_hash(cls.get_view_name(), add_secret=False, limit=32) cached_description = cache.get(cache_key) if not cached_description: description = cls.get_description() description = formatting.dedent(smart_text(description)) # Cache for 1 hour (if we update description, it will take 1 hour to show) cache.set(cache_key, description, 60 * 60) else: description = cached_description if html: return formatting.markup_description(description) return description
def test_user_activation(self): user = User.objects.get(username="******") # Inactive user in fixture # Test calling accounts-activate with wrong hash, user should not be activated bad_hash = '4dad3dft' resp = self.client.get(reverse('accounts-activate', args=[user.username, bad_hash])) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.context['decode_error'], True) self.assertEqual(User.objects.get(username="******").is_active, False) # Test calling accounts-activate with good hash, user should be activated from utils.encryption import create_hash good_hash = create_hash(user.id) resp = self.client.get(reverse('accounts-activate', args=[user.username, good_hash])) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.context['all_ok'], True) self.assertEqual(User.objects.get(username="******").is_active, True) # Test calling accounts-activate for a user that does not exist resp = self.client.get(reverse('accounts-activate', args=["noone", hash])) self.assertEqual(resp.status_code, 200) self.assertEqual(resp.context['user_does_not_exist'], True)
def hash_cache_key(key): return create_hash(key, add_secret=False, limit=32)
def send_activation2(user): hash = create_hash(user.id) username = user.username send_mail_template(u'activation link.', 'accounts/email_activation2.txt', locals(), None, user.email)
def send_activation(user): uid_hash = create_hash(user.id) username = user.username tvars = {"user": user, "username": username, "hash": uid_hash} send_mail_template(u"activation link.", "accounts/email_activation.txt", tvars, None, user.email)