예제 #1
0
def get_current_app(request=None):
    """
    Returns the provider ``rules.App`` as read in the active database
    for the ``rules`` and ``pages`` application.
    """
    # If we don't write the code as such, we might end-up generating
    # an extra SQL query every time ``get_current_app`` is called.
    thread_local_site = get_current_site()
    app = getattr(thread_local_site, 'app', None)
    if thread_local_site and not app:
        app_model = get_app_model()
        try:
            thread_local_site.app = app_model.objects.get(
                slug=thread_local_site.slug)
        except app_model.DoesNotExist:
            #pylint:disable=protected-access
            msg = "No %s with slug '%s' can be found." % (
                app_model._meta.object_name, thread_local_site.slug)
            if request is not None:
                LOGGER.exception("get_current_app: %s",
                                 msg,
                                 extra={'request': request})
            else:
                LOGGER.exception("get_current_app: %s", msg)
            raise Http404(msg)
        app = thread_local_site.app
    return app
예제 #2
0
class PersonalRegistrationForm(SignupForm):
    """
    Form to register a user and organization at the same time with the added
    constraint that both will behave as a single billing profile.
    """
    personal_registration = get_app_model().PERSONAL_REGISTRATION
    submit_title = _("Register")

    username = forms.SlugField(
        label=_("Username"), max_length=30,
        error_messages={'invalid': _("Username may only contain letters,"\
            " digits and -/_ characters. Spaces are not allowed.")})
    email = forms.EmailField(widget=forms.TextInput(attrs={'maxlength': 75}),
                             label=_("E-mail address"))
    email2 = forms.EmailField(widget=forms.TextInput(attrs={'maxlength': 75}),
                              label=_("E-mail confirmation"))
    new_password = forms.CharField(
        strip=False,
        label=_("Password"),
        widget=forms.PasswordInput(attrs={'placeholder': _("Password")}))
    new_password2 = forms.CharField(
        strip=False,
        label=_("Confirm password"),
        widget=forms.PasswordInput(
            attrs={'placeholder': _("Type password again")}))
    full_name = forms.RegexField(
        regex=FULL_NAME_PAT,
        max_length=60,
        widget=forms.TextInput(
            attrs={'placeholder': 'Ex: first name and last name'}),
        label=_("Full name"),
        error_messages={
            'invalid':
            _("Sorry we do not recognize some characters in your full name.")
        })
    street_address = forms.CharField(label=_("Street address"))
    locality = forms.CharField(label=_("City/Town"))
    region = forms.CharField(label=_("State/Province/County"))
    postal_code = forms.RegexField(regex=r'^[\w\s-]+$',
        label=_("Zip/Postal code"), max_length=30,
        error_messages={'invalid': _("The postal code may contain only"\
            " letters, digits, spaces and '-' characters.")})
    country = forms.RegexField(regex=r'^[a-zA-Z ]+$',
                               widget=forms.widgets.Select(choices=countries),
                               label=_("Country"))
    phone = PhoneNumberField(label=_('Phone number'))

    def clean(self):
        """
        Validates that both emails as well as both passwords respectively match.
        """
        self.cleaned_data = super(PersonalRegistrationForm, self).clean()
        if not ('email' in self._errors or 'email2' in self._errors):
            # If there are already errors reported for email or email2,
            # let's not override them with a confusing message here.
            email = self.cleaned_data.get('email', 'A')
            email2 = self.cleaned_data.get('email2', 'B')
            if email != email2:
                self._errors['email'] = self.error_class(
                    [_("This field does not match e-mail confirmation.")])
                self._errors['email2'] = self.error_class(
                    [_("This field does not match e-mail.")])
                if 'email' in self.cleaned_data:
                    del self.cleaned_data['email']
                if 'email2' in self.cleaned_data:
                    del self.cleaned_data['email2']
                raise forms.ValidationError(
                    _("E-mail and e-mail confirmation do not match."))
        return self.cleaned_data

    def clean_email2(self):
        """
        Normalizes emails in all lowercase.
        """
        if 'email2' in self.cleaned_data:
            self.cleaned_data['email2'] = self.cleaned_data['email2'].lower()
        return self.cleaned_data['email2']

    def clean_username(self):
        """
        Validate that the username is not already taken.
        """
        user = self.user_model.objects.filter(
            username__iexact=self.cleaned_data['username'])
        if user.exists():
            raise forms.ValidationError(
                _("A user with that username already exists."))
        organization = Organization.objects.filter(
            slug=self.cleaned_data['username'])
        if organization.exists():
            raise forms.ValidationError(
                # XXX use username_label? profile vs. User?
                _("A profile with that identifier already exists."))
        return self.cleaned_data['username']
예제 #3
0
class TogetherRegistrationForm(SignupForm):

    together_registration = get_app_model().TOGETHER_REGISTRATION