Beispiel #1
0
    def test_asynchronous(self):
        from serrano.utils import send_mail
        user1 = User(username='******',
                     first_name='Shared',
                     last_name='User',
                     email='*****@*****.**')
        user2 = User(username='******',
                     first_name='Shared',
                     last_name='User',
                     email='')
        user3 = User(username='******',
                     first_name='Shared',
                     last_name='User',
                     email='*****@*****.**')

        send_mail([user1.email, user2.email, user3.email], self.subject,
                  self.message)

        # Make sure the mail was sent(after a slight pause to account for the
        # "asynchronousness".
        time.sleep(5)
        self.assertEqual(len(mail.outbox), 1)
        # Make sure the subject is correct
        self.assertEqual(mail.outbox[0].subject, self.subject)
        self.assertEqual(mail.outbox[0].body, self.message)
        # Make sure the recipient list is correct
        self.assertSequenceEqual(
            mail.outbox[0].to, ['*****@*****.**', '', '*****@*****.**'])
Beispiel #2
0
    def delete(self, request, **kwargs):
        instance = self.get_object(request, **kwargs)

        if instance.session:
            data = {
                'message': 'Cannot delete session query',
            }
            return self.render(request, data, status=codes.bad_request)

        utils.send_mail(instance.shared_users.values_list('email', flat=True),
                        DELETE_QUERY_EMAIL_TITLE.format(instance.name),
                        DELETE_QUERY_EMAIL_BODY.format(instance.name))

        instance.delete()
        usage.log('delete', instance=instance, request=request)
Beispiel #3
0
    def delete(self, request, **kwargs):
        instance = self.get_object(request, **kwargs)

        if instance.session:
            data = {
                'message': 'Cannot delete session query',
            }
            return self.render(request, data, status=codes.bad_request)

        utils.send_mail(instance.shared_users.values_list('email', flat=True),
                        DELETE_QUERY_EMAIL_TITLE.format(instance.name),
                        DELETE_QUERY_EMAIL_BODY.format(instance.name))

        instance.delete()
        usage.log('delete', instance=instance, request=request)
Beispiel #4
0
    def test_synchronous(self):
        from serrano.utils import send_mail
        user1 = User(username='******', first_name='Shared', last_name='User',
                     email='*****@*****.**')
        user2 = User(username='******', first_name='Shared', last_name='User',
                     email='')
        user3 = User(username='******', first_name='Shared', last_name='User',
                     email='*****@*****.**')

        send_mail([user1.email, user2.email, user3.email], self.subject,
                  self.message, async=False)

        # Make sure the mail was sent
        self.assertEqual(len(mail.outbox), 1)
        # Make sure the subject is correct
        self.assertEqual(mail.outbox[0].subject, self.subject)
        self.assertEqual(mail.outbox[0].body, self.message)
        # Make sure the recipient list is correct
        self.assertSequenceEqual(
            mail.outbox[0].to, ['*****@*****.**', '', '*****@*****.**'])
Beispiel #5
0
    def save(self, commit=True):
        instance = super(QueryForm, self).save(commit=False)
        request = self.request

        if getattr(request, 'user', None) and request.user.is_authenticated():
            instance.user = request.user
        else:
            instance.session_key = request.session.session_key

        # Only recalculated count if conditions exist. This is to
        # prevent re-counting the entire dataset. An alternative
        # solution may be desirable such as pre-computing and
        # caching the count ahead of time.
        if self.count_needs_update_context:
            instance.distinct_count = instance.apply().distinct().count()
            self.count_needs_update_context = False
        else:
            instance.distinct_count = None

        if self.count_needs_update_view:
            instance.record_count = instance.apply().count()
            self.count_needs_update_view = False
        else:
            instance.record_count = None

        if commit:
            instance.save()

            script_name = getattr(django_settings, 'SCRIPT_NAME', '')

            # The code to update the shared_users field on the Query model
            # included inside this if statement because the shared_users
            # field in inaccessible until the instance is saved which is only
            # done in the case of commit being True. Using commit=False when
            # saving the super class was not enough. That is the reason for
            # this being embedded within the commit if and for the explicit
            # save_m2m call below.
            all_emails = self.cleaned_data.get('usernames_or_emails')

            # Get the list of existing email addresses for users this query is
            # already shared with. We only want to email users the first time
            # a query is shared with them so we get the existing list of email
            # addresses to avoid repeated emails to users about the same query.
            existing_emails = set(instance.shared_users.all().values_list(
                'email', flat=True))
            new_emails = all_emails - existing_emails

            site = get_current_site(request)

            try:
                site_url = request.build_absolute_uri(script_name + '/')
            except KeyError:
                site_url = site.domain + script_name

            # Use the site url as the default query url in case there are
            # issues generating the query url.
            query_url = site_url

            reverse_name = settings.QUERY_REVERSE_NAME

            if reverse_name:
                try:
                    query_url = reverse(reverse_name,
                                        kwargs={'pk': instance.pk})

                    # Since reverse will just return the path to the query
                    # we need to prepend the site url to make it a valid
                    # link that people can follow.
                    try:
                        query_url = request.build_absolute_uri(query_url)
                    except KeyError:
                        query_url = site.domain + script_name + query_url
                except NoReverseMatch:
                    log.warn("Could not reverse '{0}'. ".format(reverse_name))
            else:
                log.warn('SERRANO_QUERY_REVERSE_NAME not found in settings.')

            title = SHARED_QUERY_EMAIL_TITLE.format(query_name=instance.name,
                                                    site_name=site.name)

            body = SHARED_QUERY_EMAIL_BODY.format(query_name=instance.name,
                                                  site_name=site.name,
                                                  site_url=site_url,
                                                  query_url=query_url)

            # Email and register all the new email addresses
            utils.send_mail(new_emails, title, body)

            for email in new_emails:
                instance.share_with_user(email)

            # Find and remove users who have had their query share revoked
            removed_emails = existing_emails - all_emails

            for user in User.objects.filter(email__in=removed_emails):
                instance.shared_users.remove(user)

            self.save_m2m()

        return instance
Beispiel #6
0
    def save(self, commit=True):
        instance = super(QueryForm, self).save(commit=False)
        request = self.request

        if getattr(request, 'user', None) and request.user.is_authenticated():
            instance.user = request.user
        else:
            instance.session_key = request.session.session_key

        if commit:
            instance.save()

            script_name = getattr(django_settings, 'SCRIPT_NAME', '')

            # The code to update the shared_users field on the Query model
            # included inside this if statement because the shared_users
            # field in inaccessible until the instance is saved which is only
            # done in the case of commit being True. Using commit=False when
            # saving the super class was not enough. That is the reason for
            # this being embedded within the commit if and for the explicit
            # save_m2m call below.
            all_emails = self.cleaned_data.get('usernames_or_emails')

            # Get the list of existing email addresses for users this query is
            # already shared with. We only want to email users the first time
            # a query is shared with them so we get the existing list of email
            # addresses to avoid repeated emails to users about the same query.
            existing_emails = set(instance.shared_users.all().values_list(
                'email', flat=True))
            new_emails = all_emails - existing_emails

            site = get_current_site(request)

            try:
                site_url = request.build_absolute_uri(script_name + '/')
            except KeyError:
                site_url = site.domain + script_name

            # Use the site url as the default query url in case there are
            # issues generating the query url.
            query_url = site_url

            reverse_name = settings.QUERY_REVERSE_NAME

            if reverse_name:
                try:
                    query_url = reverse(reverse_name,
                                        kwargs={'pk': instance.pk})

                    # Since reverse will just return the path to the query
                    # we need to prepend the site url to make it a valid
                    # link that people can follow.
                    try:
                        query_url = request.build_absolute_uri(query_url)
                    except KeyError:
                        query_url = site.domain + script_name + query_url
                except NoReverseMatch:
                    log.warn("Could not reverse '{0}'. ".format(reverse_name))
            else:
                log.warn('SERRANO_QUERY_REVERSE_NAME not found in settings.')

            title = SHARED_QUERY_EMAIL_TITLE.format(query_name=instance.name,
                                                    site_name=site.name)

            body = SHARED_QUERY_EMAIL_BODY.format(query_url=query_url)

            if self.cleaned_data.get('message'):
                body = '{0}\n\n--\n{1}'.format(
                    self.cleaned_data.get('message'), body)

            # Email and register all the new email addresses
            utils.send_mail(new_emails, title, body)

            for email in new_emails:
                instance.share_with_user(email)

            # Find and remove users who have had their query share revoked
            removed_emails = existing_emails - all_emails

            for user in User.objects.filter(email__in=removed_emails):
                instance.shared_users.remove(user)

            self.save_m2m()

        return instance