def setUp(self): """Creates a few basic users. Alice is registered but not in beta Bob is registered and in beta (self-signup) Charlie is in beta and has one invite """ self.alice = User.objects.create_user('alice', '*****@*****.**', 'secret') self.bob = User.objects.create_user('bob', '*****@*****.**', 'secret') right_now = now() invitation = Invitation(user=self.bob, invited=right_now, used=right_now) invitation.save() self.charlie = User.objects.create_user('charlie', '*****@*****.**', 'secret') invitation = Invitation(user=self.charlie, invited=right_now, used=right_now) invitation.save() code = InvitationCode(owner=self.charlie) code.save()
def create_code(self, private=True, email=''): code = InvitationCode(private=private) code.save() if private: invitation = Invitation(code=code, email=email, invited=now()) invitation.save() return code
def test_using_invite(self): invitation = InvitationCode(email='*****@*****.**') invitation.save() response = self.client.get(reverse('beta_verify_invite', args=[invitation.code])) self.assertRedirects(response, self.signup_url) User.objects.create_user('alice', '*****@*****.**', 'secret') self.client.login(username='******', password='******') response = self.client.get(reverse(self.signup_confirmation_view)) response = self.client.get(reverse('beta_verify_invite', args=[invitation.code])) self.assertRedirects(response, reverse('beta_used'))
def test_using_invite(self): invitation = InvitationCode(email='*****@*****.**') invitation.save() response = self.client.get( reverse('beta_verify_invite', args=[invitation.code])) self.assertRedirects(response, self.signup_url) User.objects.create_user('alice', '*****@*****.**', 'secret') self.client.login(username='******', password='******') response = self.client.get(reverse(self.signup_confirmation_view)) response = self.client.get( reverse('beta_verify_invite', args=[invitation.code])) self.assertRedirects(response, reverse('beta_used'))
def create_beta_user(backend, details, response, uid, username, user=None, *args, **kwargs): """Create user. Depends on get_username pipeline.""" if user: return {'user': user} if not username: return None if setting('BETA_ENABLE_BETA', True): request = kwargs['request'] invitation_code = request.COOKIES.get('invitation_code', False) if not invitation_code: return HttpResponseRedirect(setting('BETA_REDIRECT_URL')) valid, exists = InvitationCode.validate_code(invitation_code) if not valid: return HttpResponseRedirect(setting('BETA_REDIRECT_URL')) email = details.get('email') user = UserSocialAuth.objects.create_user(username=username, email=email) if setting('BETA_ENABLE_BETA', True): invite_used.send(sender=user, user=user, invitation_code=invitation_code) return { 'user': user, 'is_new': True }
def create_beta_user(backend, details, response, uid, username, user=None, *args, **kwargs): """Create user. Depends on get_username pipeline.""" if user: return {'user': user} if not username: return None if setting('BETA_ENABLE_BETA', True): request = kwargs['request'] invitation_code = request.COOKIES.get('invitation_code', False) if not invitation_code: return HttpResponseRedirect(setting('BETA_REDIRECT_URL')) valid, exists = InvitationCode.validate_code(invitation_code) if not valid: return HttpResponseRedirect(setting('BETA_REDIRECT_URL')) email = details.get('email') user = User.objects.create_user(username=username, email=email) if setting('BETA_ENABLE_BETA', True): invite_used.send(sender=user, user=user, invitation_code=invitation_code) return {'user': user, 'is_new': True}
class InvitationCodeAdminForm(forms.ModelForm): code = forms.CharField( initial=lambda: InvitationCode.generate_invite_code()) class Meta: model = InvitationCode exclude = ()
def process_view(self, request, view_func, view_args, view_kwargs): if request.path in self.allow_flatpages or '%s/' % request.path in self.allow_flatpages: from django.contrib.flatpages.views import flatpage return flatpage(request, request.path_info) if not self.enable_beta: #Do nothing is beta is not activated return invitation_code = request.COOKIES.get('invitation_code', '') in_beta, exists = InvitationCode.validate_code(invitation_code) whitelisted_modules = ['django.contrib.auth.views', 'django.contrib.admin.sites', 'django.views.static', 'django.contrib.staticfiles.views', 'hunger.views'] short_name = view_func.__class__.__name__ if short_name == 'function': short_name = view_func.__name__ full_view_name = '%s.%s' % (view_func.__module__, short_name) #Check modules if self.always_allow_modules: whitelisted_modules += self.always_allow_modules #if view in module then ignore - except if view is signup confirmation if '%s' % view_func.__module__ in whitelisted_modules and not full_view_name == self.signup_confirmation_view: return #Check views if full_view_name in self.never_allow_views: return HttpResponseRedirect(self.redirect_url) if full_view_name in self.always_allow_views: return if full_view_name == self.signup_confirmation_view: #signup completed - deactivate invitation code request.session['beta_complete'] = True invite_used.send(sender=self.__class__, user=request.user, invitation_code=invitation_code) return if request.user.is_authenticated() and full_view_name not in self.signup_views: # User is logged in, or beta is not active, no need to check anything else. return if full_view_name in self.signup_views and in_beta: #if beta code is valid and trying to register then let them through return else: # next_page = request.META.get('REQUEST_URI') next_page = request.path if in_beta: return HttpResponseRedirect(self.signup_url + '?next=%s' % next_page) else: return HttpResponseRedirect(self.redirect_url + '?next=%s' % next_page)
def verify_invite(request, invitation_code): valid, exists = InvitationCode.validate_code(invitation_code) if exists: if not valid: return redirect('beta_used') else: url = getattr(settings, 'BETA_SIGNUP_ACCOUNT_URL', '/register/') response = redirect(url) response.set_cookie('invitation_code', invitation_code) return response else: url = getattr(settings, 'BETA_REDIRECT_URL', '/beta/') return redirect(url)
def create_invite(self, email): code = InvitationCode(num_invites=0) code.save() invitation = Invitation(code=code, email=email, invited=now()) invitation.save() return invitation
def create_invite(self, email, code_str="foobar"): code = InvitationCode(num_invites=100, code=code_str) code.save() invitation = Invitation(code=code, email=email, invited=now()) invitation.save() return invitation
def process_view(self, request, view_func, view_args, view_kwargs): if request.path in self.allow_flatpages or '%s/' % request.path in self.allow_flatpages: from django.contrib.flatpages.views import flatpage return flatpage(request, request.path_info) if not self.enable_beta: #Do nothing is beta is not activated return invitation_code = request.COOKIES.get('invitation_code', '') in_beta, exists = InvitationCode.validate_code(invitation_code) whitelisted_modules = [ 'django.contrib.auth.views', 'django.contrib.admin.sites', 'django.views.static', 'django.contrib.staticfiles.views', 'hunger.views' ] short_name = view_func.__class__.__name__ if short_name == 'function': short_name = view_func.__name__ full_view_name = '%s.%s' % (view_func.__module__, short_name) #Check modules if self.always_allow_modules: whitelisted_modules += self.always_allow_modules #if view in module then ignore - except if view is signup confirmation if '%s' % view_func.__module__ in whitelisted_modules and not full_view_name == self.signup_confirmation_view: error_log("whitelisted or signup confirmation view") return #Check views if full_view_name in self.never_allow_views: error_log("never allow views") return HttpResponseRedirect(self.redirect_url) if full_view_name in self.always_allow_views: error_log("always allow views") return if full_view_name == self.signup_confirmation_view or request.path == self.signup_confirmation_url: #signup completed - deactivate invitation code error_log("signup complete") request.session['beta_complete'] = True # invite_used.send(sender=self.__class__, user=request.user, invitation_code=invitation_code) return if request.user.is_authenticated( ) and full_view_name not in self.signup_views: error_log("user is authed") # User is logged in, or beta is not active, no need to check anything else. return if full_view_name == 'registration.views.register' and not in_beta: error_log("not in beta, trying to register") return HttpResponseRedirect(self.signup_url) if full_view_name in self.signup_views and in_beta: #if beta code is valid and trying to register then let them through error_log("has beta code trying to register") return else: # next_page = request.META.get('REQUEST_URI') next_page = request.path if in_beta: error_log("redirect to signup") return HttpResponseRedirect( getattr(settings, 'BETA_SIGNUP_ACCOUNT_URL', '/accounts/register')) else: error_log("redirect to redirect url") return HttpResponseRedirect(self.redirect_url + '?next=%s' % next_page)