def register_public_key_server(request): if request.method == 'POST': form = RegisteredDevicePublicKeyForm(request.user, data=request.POST) if form.is_valid(): form.save() zone_id = form.data["zone"] org_id = Zone.objects.get(id=zone_id).get_org().id callback_url = form.cleaned_data.get("callback_url", None) if callback_url: # New style: go directly to the origin page, which will force a sync to occur (no reason to ping refresh) # This is better for the current force_job return HttpResponseRedirect(callback_url) else: # Old style, for clients that don't send a callback url messages.success( request, _("The device's public key has been successfully registered. You may now close this window." )) return HttpResponseRedirect( reverse("zone_management", kwargs={ 'org_id': org_id, 'zone_id': zone_id })) else: # This is hackish--we now create default organizations and zones for users, based on their # registration information. For previous users, however, we don't. And we don't # give any links / instructions for creating zones when they get here. # So, rather than block them, let's create an org and zone for them, so that # at least they can proceed directly. if request.user.organization_set.count() == 0: # Localizing central-only import from central.models import Organization org = Organization(name="Your organization", owner=request.user) org.save() org.add_member(request.user) org.save() if not sum( [org.zones.count() for org in request.user.organization_set.all()]): org = request.user.organization_set.all()[0] zone = Zone(name="Default zone") zone.save() org.add_zone(zone) # callback_url: 0.10.3 and higher (distributed server) # prev: 0.10.3 and higher (central server) # # Note: can't use referer, because this breaks if the user is redirected # to the central server login page--gets confusing. form = RegisteredDevicePublicKeyForm( request.user, callback_url=request.REQUEST.get("callback_url") or request.REQUEST.get("prev"), ) return { "form": form, }
def setUp(self): super(OrganizationManagementTestCase, self).setUp() self.user = User(username=self.USER_EMAIL, email=self.USER_EMAIL) self.user.set_password(self.USER_PASSWORD) self.user.save() self.org = Organization(name=self.ORG_NAME, owner=self.user) self.org.save() self.org.add_member(self.user) self.org.save()
def contact_wizard(request, type=""): """Contact form consists of a contact main portion, and three possible contact types (deployment, support, info). Here, we handle all the forms and save them into their parts.""" # handle a submitted contact form if request.method == "POST": # Note that each form has a "prefix", which makes forms with entries # of the same name avoid colliding with each other # # Note that these prefixes have to match those below in the "GET" section. contact_form = ContactForm(prefix="contact_form", data=request.POST) deployment_form = DeploymentForm(prefix="deployment_form", data=request.POST) support_form = SupportForm(prefix="support_form", data=request.POST) contribute_form = ContributeForm(prefix="contribute_form", data=request.POST) info_form = InfoForm(prefix="info_form", data=request.POST) if contact_form.is_valid(): # Point to authenticated user if request.user.is_authenticated(): contact_form.instance.user = request.user # Map over the field at the bottom of the form to the hidden form element. # I couldn't find a better way to get this set up in the form, without # making a giant HTML mess, other than this way. contact_form.instance.cc_email = request.POST["hack_cc_email"] # Deployment if contact_form.cleaned_data["type"] == CONTACT_TYPE_DEPLOYMENT: if deployment_form.is_valid(): return handle_contact(request, contact_form, deployment_form, settings.CENTRAL_DEPLOYMENT_EMAIL, "deployment") # Support elif contact_form.cleaned_data["type"] == CONTACT_TYPE_SUPPORT: if support_form.is_valid(): return handle_contact(request, contact_form, support_form, settings.CENTRAL_SUPPORT_EMAIL, "support") # Info elif contact_form.cleaned_data["type"] == CONTACT_TYPE_INFO: if info_form.is_valid(): return handle_contact(request, contact_form, info_form, settings.CENTRAL_INFO_EMAIL, "info") # Contribute elif contact_form.cleaned_data["type"] == CONTACT_TYPE_CONTRIBUTE: if contribute_form.is_valid(): # Send development inquiries to the development list if contribute_form.cleaned_data[ "type"] == CONTRIBUTE_TYPE_DEVELOPMENT: return handle_contact(request, contact_form, contribute_form, settings.CENTRAL_DEV_EMAIL, "contribute") # Everything else must go to the info list else: return handle_contact(request, contact_form, contribute_form, settings.CENTRAL_INFO_EMAIL, "contribute") else: raise Exception("Unknown contact type: %s" % (contact_form.cleaned_data["type"])) # A GET request. Create empty forms, fill in user details if available # Auto-select the type, if relevant else: deployment_form = DeploymentForm(prefix="deployment_form") support_form = SupportForm(prefix="support_form") info_form = InfoForm(prefix="info_form") contribute_form = ContributeForm(prefix="contribute_form") # Use the user's information, if available if request.user.is_authenticated(): if request.user.owned_organizations.count() > 0: org = request.user.owned_organizations.all()[0] elif request.user.organization_set.count() > 0: org = request.user.organization_set[0] else: org = Organization() contact_form = ContactForm( prefix="contact_form", instance=Contact( type=type, user=request.user, name="%s %s" % (request.user.first_name, request.user.last_name), email=request.user.email, org_name=org.name, org_url=org.url)) else: contact_form = ContactForm(prefix="contact_form", instance=Contact(type=type)) return { "central_contact_email": settings.CENTRAL_CONTACT_EMAIL, "wiki_url": settings.CENTRAL_WIKI_URL, 'deployment_form': deployment_form, 'support_form': support_form, 'contribute_form': contribute_form, 'info_form': info_form, 'contact_form': contact_form, }
def register(request, backend, success_url=None, form_class=None, disallowed_url='registration_disallowed', template_name='registration/registration_form.html', extra_context=None): """ Allow a new user to register an account. The actual registration of the account will be delegated to the backend specified by the ``backend`` keyword argument (see below); it will be used as follows: 1. The backend's ``registration_allowed()`` method will be called, passing the ``HttpRequest``, to determine whether registration of an account is to be allowed; if not, a redirect is issued to the view corresponding to the named URL pattern ``registration_disallowed``. To override this, see the list of optional arguments for this view (below). 2. The form to use for account registration will be obtained by calling the backend's ``get_form_class()`` method, passing the ``HttpRequest``. To override this, see the list of optional arguments for this view (below). 3. If valid, the form's ``cleaned_data`` will be passed (as keyword arguments, and along with the ``HttpRequest``) to the backend's ``register()`` method, which should return the new ``User`` object. 4. Upon successful registration, the backend's ``post_registration_redirect()`` method will be called, passing the ``HttpRequest`` and the new ``User``, to determine the URL to redirect the user to. To override this, see the list of optional arguments for this view (below). **Required arguments** None. **Optional arguments** ``backend`` The dotted Python import path to the backend class to use. ``disallowed_url`` URL to redirect to if registration is not permitted for the current ``HttpRequest``. Must be a value which can legally be passed to ``django.shortcuts.redirect``. If not supplied, this will be whatever URL corresponds to the named URL pattern ``registration_disallowed``. ``form_class`` The form class to use for registration. If not supplied, this will be retrieved from the registration backend. ``extra_context`` A dictionary of variables to add to the template context. Any callable object in this dictionary will be called to produce the end result which appears in the context. ``success_url`` URL to redirect to after successful registration. Must be a value which can legally be passed to ``django.shortcuts.redirect``. If not supplied, this will be retrieved from the registration backend. ``template_name`` A custom template to use. If not supplied, this will default to ``registration/registration_form.html``. **Context:** ``form`` The registration form. Any extra variables supplied in the ``extra_context`` argument (see above). **Template:** registration/registration_form.html or ``template_name`` keyword argument. """ backend = get_backend(backend) if not backend.registration_allowed(request): return redirect(disallowed_url) if form_class is None: form_class = backend.get_form_class(request) do_subscribe = request.REQUEST.get("email_subscribe") == "on" if request.method == 'POST': form = form_class(data=request.POST, files=request.FILES) org_form = OrganizationForm(data=request.POST, instance=Organization()) # Could register if form.is_valid() and org_form.is_valid(): assert form.cleaned_data.get("username") == form.cleaned_data.get("email"), "Should be set equal in the call to clean()" try: # Create the user new_user = backend.register(request, **form.cleaned_data) # Add an org. Must create org before adding user. org_form.instance.owner = new_user org_form.save() org = org_form.instance org.add_member(new_user) # Now add a zone, and link to the org zone = Zone(name=org_form.instance.name + " Default Zone") zone.save() org.add_zone(zone) # Finally, try and subscribe the user to the mailing list # (silently; don't return anything to the user) if do_subscribe: contact_subscribe(request, form.cleaned_data['email']) # no "return" org.save() if success_url is None: to, args, kwargs = backend.post_registration_redirect(request, new_user) return redirect(to, *args, **kwargs) else: return redirect(success_url) except IntegrityError as e: if e.message=='column username is not unique': form._errors['__all__'] = _("An account with this email address has already been created. Please login at the link above.") else: raise e # GET, not POST else: form = form_class() org_form = OrganizationForm() if extra_context is None: extra_context = {} context = RequestContext(request) for key, value in extra_context.items(): context[key] = callable(value) and value() or value return render_to_response( template_name, { 'form': form, "org_form" : org_form, "subscribe": do_subscribe, }, context_instance=context, )