Ejemplo n.º 1
0
    def share_with_user(self, email, create_user=True):
        """
        Attempts to add a user with the supplied email address to the list of
        shared users for this query. If create_user is set to True, users will
        be created for emails that are not already associated with an
        existing user.

        Returns True if the email was added to the list of shared users and
        False if the email wasn't added because it already exists or wasn't
        created.
        """
        # If the query is already shared then there is no need to share it
        # again.
        if self.shared_users.filter(email__iexact=email).exists():
            return False

        try:
            user = User.objects.get(email__iexact=email)
        except User.DoesNotExist:
            if not create_user:
                return False
            else:
                user = utils.create_email_based_user(email)

        self.shared_users.add(user)
        self.save()
Ejemplo n.º 2
0
    def share_with_user(self, email, create_user=True):
        """
        Attempts to add a user with the supplied email address to the list of
        shared users for this query. If create_user is set to True, users will
        be created for emails that are not already associated with an
        existing user.

        Returns True if the email was added to the list of shared users and
        False if the email wasn't added because it already exists or wasn't
        created.
        """
        # If the query is already shared then there is no need to share it
        # again.
        if self.shared_users.filter(email__iexact=email).exists():
            return False

        try:
            user = User.objects.get(email__iexact=email)
        except User.DoesNotExist:
            if not create_user:
                return False
            else:
                user = utils.create_email_based_user(email)

        self.shared_users.add(user)
        self.save()
Ejemplo n.º 3
0
    def test_create_user(self):
        # Make sure we are starting with the anticipated number of users.
        self.assertEqual(User.objects.count(), 1)

        user = utils.create_email_based_user(self.email)

        self.assertEqual(User.objects.count(), 2)

        # Make sure the user we got back has the correct email set and that
        # they are not active.
        self.assertEqual(user.email, self.email)
        self.assertFalse(user.is_active)
Ejemplo n.º 4
0
    def test_create_user(self):
        # Make sure we are starting with the anticipated number of users.
        self.assertEqual(User.objects.count(), 1)

        user = utils.create_email_based_user(self.email)

        self.assertEqual(User.objects.count(), 2)

        # Make sure the user we got back has the correct email set and that
        # they are not active.
        self.assertEqual(user.email, self.email)
        self.assertFalse(user.is_active)
Ejemplo n.º 5
0
    def share_with_user(self, username_or_email, create_user=True):
        """
        Attempts to add a user with the supplied email address or username
        to the list of shared users for this query. If create_user is set to
        True, users will be created for emails that are not already associated
        with an existing user. New users are not created for a provided
        username if it cannot be found.

        Returns True if the email/username was added to the list of shared
        users and False if the email/username wasn't added because it already
        exists or wasn't created.
        """

        # If both share setttings are set to false, nothing can be done
        if not settings.SHARE_BY_USERNAME and not settings.SHARE_BY_EMAIL:
            log.warning('Cannot share with any user because SHARE_BY_USERNAME'
                        ' and SHARE_BY_EMAIL are both set to False.')
            return False

        # If the query is already shared then there is no need to share it
        # again.
        if self.shared_users.filter(
                Q(email__iexact=username_or_email) |
                Q(username__iexact=username_or_email)).exists():
            return False

        user = None

        # Create a Q() object to build our query
        q = Q()

        if settings.SHARE_BY_USERNAME:
            if settings.SHARE_BY_USERNAME_CASE_SENSITIVE:
                q |= Q(username=username_or_email)
            else:
                q |= Q(username__iexact=username_or_email)

        if settings.SHARE_BY_EMAIL:
            q |= Q(email__iexact=username_or_email)

        # Try to retrive a user. If this fails, create a new user with the
        # email address
        try:
            user = User.objects.get(q)
        except User.DoesNotExist:
            log.warning('Cannot find user "{0}".'.format(username_or_email))

        if not user and create_user:
            try:
                user = utils.create_email_based_user(username_or_email)
            except ValidationError:
                log.warning('Could not create user with email. "{0}" is not a '
                            'valid email.'.format(username_or_email))

        # If a user was found/created, add that user to shared users
        if user:
            self.shared_users.add(user)
            self.save()
            return True

        return False