예제 #1
0
파일: forms.py 프로젝트: lxp20201/lxp
    def clean_email_or_username(self):
        """
        Clean email form field

        Returns:
            str: the cleaned value, converted to an email address (or an empty string)
        """
        email_or_username = self.cleaned_data[
            self.Fields.EMAIL_OR_USERNAME].strip()

        if not email_or_username:
            # The field is blank; we just return the existing blank value.
            return email_or_username

        email = email_or_username__to__email(email_or_username)
        bulk_entry = len(split_usernames_and_emails(email)) > 1
        if bulk_entry:
            for email in split_usernames_and_emails(email):
                validate_email_to_link(
                    email,
                    None,
                    ValidationMessages.INVALID_EMAIL_OR_USERNAME,
                    ignore_existing=True)
            email = email_or_username
        else:
            validate_email_to_link(
                email,
                email_or_username,
                ValidationMessages.INVALID_EMAIL_OR_USERNAME,
                ignore_existing=True)

        return email
예제 #2
0
    def _handle_singular(cls, request, enterprise_customer,
                         manage_learners_form):
        """
        Link single user by email or username.

        Arguments:
            enterprise_customer (EnterpriseCustomer): learners will be linked to this Enterprise Customer instance
            manage_learners_form (ManageLearnersForm): bound ManageLearners form instance
        """
        form_field_value = manage_learners_form.cleaned_data[
            ManageLearnersForm.Fields.EMAIL_OR_USERNAME]
        email = email_or_username__to__email(form_field_value)
        try:
            existing_record = validate_email_to_link(
                email, form_field_value,
                ValidationMessages.INVALID_EMAIL_OR_USERNAME, True)
        except ValidationError as exc:
            manage_learners_form.add_error(
                ManageLearnersForm.Fields.EMAIL_OR_USERNAME, exc)
        else:
            if isinstance(existing_record, PendingEnterpriseCustomerUser) and existing_record.enterprise_customer \
                    != enterprise_customer:
                messages.warning(
                    request,
                    ValidationMessages.PENDING_USER_ALREADY_LINKED.format(
                        user_email=email,
                        ec_name=existing_record.enterprise_customer.name))
                return None
            EnterpriseCustomerUser.objects.link_user(enterprise_customer,
                                                     email)
            return [email]
예제 #3
0
    def clean_email_or_username(self):
        """
        Verify email_or_username has associated user in our database.
        """
        email_or_username = self.cleaned_data[self.Fields.EMAIL_OR_USERNAME].strip()
        email = email_or_username__to__email(email_or_username)
        # Check whether user exists in our database.
        if not User.objects.filter(email=email).exists():
            raise ValidationError(ValidationMessages.USER_NOT_EXIST.format(email=email))

        # Check whether user in linked to the enterprise customer.
        if not self.is_user_linked(email):
            raise ValidationError(ValidationMessages.USER_NOT_LINKED)
        return email
예제 #4
0
    def _handle_singular(cls, enterprise_customer, manage_learners_form):
        """
        Link single user by email or username.

        Arguments:
            enterprise_customer (EnterpriseCustomer): learners will be linked to this Enterprise Customer instance
            manage_learners_form (ManageLearnersForm): bound ManageLearners form instance
        """
        form_field_value = manage_learners_form.cleaned_data[ManageLearnersForm.Fields.EMAIL_OR_USERNAME]
        email = email_or_username__to__email(form_field_value)
        try:
            validate_email_to_link(email, form_field_value, ValidationMessages.INVALID_EMAIL_OR_USERNAME)
        except ValidationError as exc:
            manage_learners_form.add_error(ManageLearnersForm.Fields.EMAIL_OR_USERNAME, exc.message)
        else:
            EnterpriseCustomerUser.objects.link_user(enterprise_customer, email)
            return [email]
예제 #5
0
    def clean_email_or_username(self):
        """
        Clean email form field

        Returns:
            str: the cleaned value, converted to an email address (or an empty string)
        """
        email_or_username = self.cleaned_data[
            self.Fields.EMAIL_OR_USERNAME].strip()

        if not email_or_username:
            # The field is blank; we just return the existing blank value.
            return email_or_username

        email = email_or_username__to__email(email_or_username)
        validate_email_to_link(email, email_or_username,
                               ValidationMessages.INVALID_EMAIL_OR_USERNAME)

        return email
예제 #6
0
    def test_email_or_username__to__email_something_else(self):
        email_or_username = FAKER.email()  # pylint: disable=no-member

        assert email_or_username__to__email(email_or_username) == email_or_username
예제 #7
0
    def test_email_or_username__to__email_username(self):
        user = UserFactory()
        email_or_username = user.username

        assert email_or_username__to__email(email_or_username) == user.email
예제 #8
0
    def test_email_or_username__to__email_something_else(self):
        email_or_username = FAKER.email()

        assert email_or_username__to__email(email_or_username) == email_or_username