def newsite(req): import base.models as M, random, string form = None auth_user = UR.getUserInfo(req) ensemble_form = None user_form = None if auth_user is not None: return HttpResponseRedirect("/") if req.method == 'POST': user = M.User(confkey="".join([choice(string.ascii_letters+string.digits) for i in xrange(0,32)])) ensemble = M.Ensemble() user_form = forms.UserForm(req.POST, instance=user) ensemble_form = forms.EnsembleForm(req.POST, instance=ensemble) if user_form.is_valid() and ensemble_form.is_valid(): user_form.save() ensemble.invitekey = "".join([ random.choice(string.ascii_letters+string.digits) for i in xrange(0,50)]) ensemble_form.save() m = M.Membership(user=user, ensemble=ensemble, admin=True) m.save() p = {"tutorial_url": settings.GUEST_TUTORIAL_URL, "conf_url": "http://%s?ckey=%s" %(settings.NB_SERVERNAME, user.confkey), "firstname": user.firstname, "email": user.email, "password": user.password } email = EmailMessage( "Welcome to NB, %s" % (user.firstname), render_to_string("email/confirm_newsite", p), settings.EMAIL_FROM, (user.email, ), (settings.EMAIL_BCC, )) email.send() return HttpResponseRedirect('/newsite_thanks') else: user_form = forms.UserForm() ensemble_form = forms.EnsembleForm() return render_to_response("web/newsite.html", {"user_form": user_form, "ensemble_form": ensemble_form})
def subscribe(req): key = req.GET.get("key", "") e = M.Ensemble.objects.get(invitekey=key) if not e.use_invitekey: return HttpResponseRedirect("/notallowed") auth_user = UR.getUserInfo(req) user = None P = {"ensemble": e, "key": key} if req.method == 'POST': if auth_user is None: user = M.User(confkey="".join([ choice(string.ascii_letters + string.digits) for i in xrange(0, 32) ])) user_form = forms.UserForm(req.POST, instance=user) if user_form.is_valid(): user_form.save() m = M.Membership(user=user, ensemble=e) m.save( ) #membership exists but user is still invalid until has confirmed their email p = { "tutorial_url": settings.GUEST_TUTORIAL_URL, "conf_url": "%s://%s/?ckey=%s" % (settings.PROTOCOL, settings.NB_SERVERNAME, user.confkey), "firstname": user.firstname, "email": user.email } email = EmailMessage( "Welcome to NB, %s" % (user.firstname, ), render_to_string("email/confirm_subscribe", p), settings.EMAIL_FROM, (user.email, ), (settings.EMAIL_BCC, )) email.send() return HttpResponseRedirect('/subscribe_thanks') else: P["form"] = forms.UserForm(req.POST, instance=user) return render_to_response("web/subscribe_newuser.html", P) else: user = auth_user m = M.Membership.objects.filter(user=user, ensemble=e) if m.count() == 0: m = M.Membership(user=user, ensemble=e) m.save() return HttpResponseRedirect('/') #user_form = forms.EnterYourNameUserForm(req.POST, instance=user) else: if auth_user is not None: P["user"] = auth_user P["form"] = forms.UserForm(instance=user) return render_to_response("web/subscribe_existinguser.html", P) else: P["form"] = forms.UserForm() return render_to_response("web/subscribe_newuser.html", P)
def user_post_save_handler(sender, **kwargs): user = kwargs["instance"] u = None if user is not None: email = user.email try: u = M.User.objects.get(email=email) if u.firstname == "" or u.lastname== "": u.firstname = user.first_name u.lastname = user.last_name u.save() except M.User.DoesNotExist: password = "".join([ random.choice(string.ascii_letters+string.digits) for i in xrange(0,6)]) confkey = "".join([ random.choice(string.ascii_letters+string.digits) for i in xrange(0,32)]) u = M.User(email=email, firstname=user.first_name, lastname=user.last_name, password=password, confkey=confkey, guest=False, valid=True ) u.save()
def newsite_form(req): import base.models as M, random, string auth_user = UR.getUserInfo(req) if auth_user is not None: return HttpResponse(UR.prepare_response({"redirect": "/"})) if req.method == 'GET': remote_user_form = RemoteForm(forms.UserForm()) remote_class_form = RemoteForm(forms.EnsembleForm()) return HttpResponse( UR.prepare_response({ "user_form": remote_user_form.as_dict(), "class_form": remote_class_form.as_dict() })) else: user = M.User(confkey="".join([ choice(string.ascii_letters + string.digits) for i in xrange(0, 32) ])) ensemble = M.Ensemble() req.POST = dict( req.POST.iteritems()) # Convert immutable object to mutable object user_form = forms.UserForm(req.POST, instance=user) ensemble_form = forms.EnsembleForm(req.POST, instance=ensemble) if user_form.is_valid() and ensemble_form.is_valid(): user_form.save() ensemble.invitekey = "".join([ random.choice(string.ascii_letters + string.digits) for i in xrange(0, 50) ]) ensemble_form.save() m = M.Membership(user=user, ensemble=ensemble, admin=True) m.save() p = { "tutorial_url": settings.GUEST_TUTORIAL_URL, "conf_url": "http://%s?ckey=%s" % (settings.NB_SERVERNAME, user.confkey), "firstname": user.firstname, "email": user.email } email = EmailMessage("Welcome to NB, %s" % (user.firstname), render_to_string("email/confirm_newsite", p), settings.EMAIL_FROM, (user.email, ), (settings.EMAIL_BCC, )) email.send() return HttpResponse( UR.prepare_response({"redirect": "/newsite_thanks"})) else: # Invalid form - return form with error messages __clean_form( user_form ) # Ensure user-generated data gets cleaned before sending back the form __clean_form( ensemble_form ) # Ensure user-generated data gets cleaned before sending back the form remote_user_form = RemoteForm(user_form) remote_class_form = RemoteForm(ensemble_form) return HttpResponse( UR.prepare_response({ "user_form": remote_user_form.as_dict(), "class_form": remote_class_form.as_dict() }))
def subscribe_with_key(req): key = req.GET.get("key", "") if not key: return HttpResponse(UR.prepare_response({}, 1, "NOT ALLOWED")) try: e = M.Ensemble.objects.get(invitekey=key) except ObjectDoesNotExist: return HttpResponse(UR.prepare_response({}, 1, "NOT ALLOWED")) if not e.use_invitekey: return HttpResponse(UR.prepare_response({}, 1, "NOT ALLOWED")) auth_user = UR.getUserInfo(req) if req.method == 'GET': if auth_user is None: # Guest retrieving the subscribe page remote_form = RemoteForm(forms.UserForm()) return HttpResponse( UR.prepare_response({ "new_user": True, "class_settings": UR.model2dict(e), "form": remote_form.as_dict() })) else: # Logged in user retrieving the subscribe page user = auth_user remote_form = RemoteForm(forms.UserForm(instance=user)) m = M.Membership.objects.filter(user=user, ensemble=e) if m.count() == 0: m = M.Membership(user=user, ensemble=e) m.save() return HttpResponse( UR.prepare_response({ "new_user": False, "user": UR.model2dict(user), "class_settings": UR.model2dict(e), "form": remote_form.as_dict() })) else: # POST requests if auth_user is None: # Guest subscribing to a class user = M.User(confkey="".join([ choice(string.ascii_letters + string.digits) for i in xrange(0, 32) ])) req.POST = dict(req.POST.iteritems() ) # Convert immutable object to mutable object user_form = forms.UserForm(req.POST, instance=user) if user_form.is_valid(): user_form.save() m = M.Membership(user=user, ensemble=e) m.save( ) # membership exists but user is still invalid until has confirmed their email p = { "tutorial_url": settings.GUEST_TUTORIAL_URL, "conf_url": "%s://%s/?ckey=%s" % (settings.PROTOCOL, settings.NB_SERVERNAME, user.confkey), "firstname": user.firstname, "email": user.email } email = EmailMessage( "Welcome to NB, %s" % (user.firstname, ), render_to_string("email/confirm_subscribe", p), settings.EMAIL_FROM, (user.email, ), (settings.EMAIL_BCC, )) email.send() return HttpResponse( UR.prepare_response({ "new_user": True, "class_settings": UR.model2dict(e), "next": "/subscribe_thanks" })) else: # Invalid form - return form with error messages __clean_form( user_form ) # Ensure user-generated data gets cleaned before sending back the form remote_form = RemoteForm(user_form) return HttpResponse( UR.prepare_response({ "new_user": True, "user": UR.model2dict(user), "class_settings": UR.model2dict(e), "form": remote_form.as_dict() })) else: # Logged in user subscribing to a class user = auth_user m = M.Membership.objects.filter(user=user, ensemble=e) if m.count() == 0: m = M.Membership(user=user, ensemble=e) m.save() return HttpResponse( UR.prepare_response({ "new_user": False, "class_settings": UR.model2dict(e), "next": "/" }))