def create_signup_code(self, commit=True): email = self.cleaned_data["email"] expiry = datetime.now() + timedelta(hours=1) code = sha_constructor("%s%s%s%s" % ( settings.SECRET_KEY, email, str(expiry), settings.SECRET_KEY, )).hexdigest() signup_code = SignupCode(code=code, email=email, max_uses=1, expiry=expiry) if commit: signup_code.save() return signup_code
def create_signup_code(self, commit=True): email = self.cleaned_data["email"] expiry = datetime.now() + timedelta(hours=1) bits = [ settings.SECRET_KEY, email, str(expiry), ] if self.group: bits.append("%s%s" % (self.group._meta, self.group.pk)) code = sha_constructor("".join(bits)).hexdigest() signup_code = SignupCode(code=code, email=email, max_uses=1, expiry=expiry) if commit: signup_code.save() return signup_code
def test_accept_invite(self): """ test the user's ability to accept their token """ #print help(self) # Create an invitation email = "*****@*****.**" expiry = datetime.datetime.now() + datetime.timedelta(hours=1) code = sha_constructor("%s%s%s%s" % ( settings.SECRET_KEY, email, str(expiry), settings.SECRET_KEY, )).hexdigest() signup_code = SignupCode(code=code, email=email, max_uses=1, expiry=expiry) signup_code.save() # First the invitee tries a bad signup_code data = {"code": "12345"} response = self.client.get(reverse("test_signup"), data) print response self.assertContains(response, "Incorrect Code") # Now they remember the code and try the right one data = {"code": signup_code.code} response = self.client.get(reverse("test_signup"), data, follow=True) self.assertContains(response, "id_username") self.assertContains(response, "id_email") self.assertContains(response, "id_password") # User now creates their account data = { "username": "******", "email": "*****@*****.**", "password1": "abc1234", "password2": "abc1234", "signup_code": signup_code.code, "submit": "Sign Up »" } response = self.client.post(reverse("test_signup"), data, follow=True) print User.objects.all()
def signup(request, **kwargs): form_class = kwargs.pop("form_class", SignupForm) template_name = kwargs.pop("template_name", "account/signup.html") template_name_failure = kwargs.pop("template_name_failure", "signup_codes/failure.html") success_url = kwargs.pop("success_url", None) group, bridge = group_and_bridge(request) ctx = group_context(group, bridge) if success_url is None: if hasattr(settings, "SIGNUP_REDIRECT_URLNAME"): fallback_url = reverse(settings.SIGNUP_REDIRECT_URLNAME) else: if hasattr(settings, "LOGIN_REDIRECT_URLNAME"): fallback_url = reverse(settings.LOGIN_REDIRECT_URLNAME) else: fallback_url = settings.LOGIN_REDIRECT_URL success_url = get_default_redirect(request, fallback_url) code = request.GET.get("code") if request.method == "POST": form = form_class(request.POST, group=group) if form.is_valid(): user = form.save(request=request) signup_code = form.cleaned_data["signup_code"] if signup_code: signup_code.use(user) form.login(request, user) messages.add_message( request, messages.SUCCESS, ugettext("Successfully logged in as %(username)s.") % {"username": user_display(user)}, ) return HttpResponseRedirect(success_url) else: signup_code = SignupCode.check(code) if signup_code: initial = {"signup_code": code, "email": signup_code.email} form = form_class(initial=initial, group=group) else: if not settings.ACCOUNT_OPEN_SIGNUP: ctx.update({"code": code}) ctx = RequestContext(request, ctx) # if account signup is not open we want to fail when there is # no sign up code or what was provided failed. return render_to_response(template_name_failure, ctx) else: form = form_class(group=group) ctx.update({"code": code, "form": form}) return render_to_response(template_name, RequestContext(request, ctx))
def clean_signup_code(self): code = self.cleaned_data.get("signup_code") signup_code = SignupCode.check(code) if signup_code: return signup_code else: if settings.ACCOUNT_OPEN_SIGNUP: return None else: raise forms.ValidationError("Signup code was not valid.")
def test_accept_invite(self): """ test the user's ability to accept their token """ #print help(self) # Create an invitation email = "*****@*****.**" expiry = now() + datetime.timedelta(hours=1) code = sha_constructor("%s%s%s%s" % ( settings.SECRET_KEY, email, str(expiry), settings.SECRET_KEY, )).hexdigest() signup_code = SignupCode(code=code, email=email, max_uses=1, expiry=expiry) signup_code.save() # First the invitee tries a bad signup_code data = { "code":"12345" } response = self.client.get(reverse("test_signup"), data) print response self.assertContains(response, "Incorrect Code") # Now they remember the code and try the right one data = { "code" : signup_code.code } response = self.client.get(reverse("test_signup"), data, follow=True) self.assertContains(response, "id_username") self.assertContains(response, "id_email") self.assertContains(response, "id_password") # User now creates their account data = { "username" : "joe", "email": "*****@*****.**", "password1": "abc1234", "password2": "abc1234", "signup_code":signup_code.code, "submit":"Sign Up »" } response = self.client.post(reverse("test_signup"), data, follow=True) print User.objects.all()
def create_signup_code(self, commit=True): email = self.cleaned_data["email"] signup_code = SignupCode.create(email, 24, group=self.group) if commit: signup_code.save() return signup_code