Exemple #1
0
 def display_addresses(self, mailbox):
     # Get from forwards
     cache = caches.get_request_cache()
     cached_forwards = cache.get('forwards')
     if cached_forwards is None:
         cached_forwards = {}
         qs = Address.objects.filter(
             forward__regex=r'(^|.*\s)[^@]+(\s.*|$)')
         qs = qs.annotate(email=Concat('name', V('@'), 'domain__name'))
         qs = qs.values_list('id', 'email', 'forward')
         for addr_id, email, mbox in qs:
             url = reverse('admin:mailboxes_address_change',
                           args=(addr_id, ))
             link = '<a href="%s">%s</a>' % (url, email)
             try:
                 cached_forwards[mbox].append(link)
             except KeyError:
                 cached_forwards[mbox] = [link]
         cache.set('forwards', cached_forwards)
     try:
         forwards = cached_forwards[mailbox.name]
     except KeyError:
         forwards = []
     # Get from mailboxes
     addresses = []
     for addr in mailbox.addresses.all():
         url = change_url(addr)
         addresses.append('<a href="%s">%s</a>' % (url, addr.email))
     return '<br>'.join(addresses + forwards)
Exemple #2
0
 def display_addresses(self, mailbox):
     # Get from forwards
     cache = caches.get_request_cache()
     cached_forwards = cache.get('forwards')
     if cached_forwards is None:
         cached_forwards = {}
         qs = Address.objects.filter(forward__regex=r'(^|.*\s)[^@]+(\s.*|$)')
         qs = qs.annotate(email=Concat('name', V('@'), 'domain__name'))
         qs = qs.values_list('id', 'email', 'forward')
         for addr_id, email, mbox in qs:
             url = reverse('admin:mailboxes_address_change', args=(addr_id,))
             link = '<a href="%s">%s</a>' % (url, email)
             try:
                 cached_forwards[mbox].append(link)
             except KeyError:
                 cached_forwards[mbox] = [link]
         cache.set('forwards', cached_forwards)
     try:
         forwards = cached_forwards[mailbox.name]
     except KeyError:
         forwards = []
     # Get from mailboxes
     addresses = []
     for addr in mailbox.addresses.all():
         url = change_url(addr)
         addresses.append('<a href="%s">%s</a>' % (url, addr.email))
     return '<br>'.join(addresses+forwards)
Exemple #3
0
 def get_services(cls, instance):
     cache = caches.get_request_cache()
     ct = ContentType.objects.get_for_model(instance)
     services = cache.get(ct)
     if services is None:
         services = cls.objects.filter(content_type=ct, is_active=True)
         cache.set(ct, services)
     return services
Exemple #4
0
 def filter_by_instance(self, instance):
     cache = caches.get_request_cache()
     ct = ContentType.objects.get_for_model(instance)
     key = 'services.Service-%i' % ct.pk
     services = cache.get(key)
     if services is None:
         services = self.filter(content_type=ct, is_active=True)
         cache.set(key, services)
     return services
Exemple #5
0
 def filter_by_instance(self, instance):
     cache = caches.get_request_cache()
     ct = ContentType.objects.get_for_model(instance)
     key = 'services.Service-%i' % ct.pk
     services = cache.get(key)
     if services is None:
         services = self.filter(content_type=ct, is_active=True)
         cache.set(key, services)
     return services
Exemple #6
0
    def send_messages(self, email_messages):
        if not email_messages:
            return
        # Count messages per request
        cache = get_request_cache()
        key = 'mailer.sent_messages'
        sent_messages = cache.get(key) or 0
        sent_messages += 1
        cache.set(key, sent_messages)

        is_bulk = len(email_messages) > 1
        if sent_messages > settings.MAILER_NON_QUEUED_PER_REQUEST_THRESHOLD:
            is_bulk = True
        default_priority = Message.NORMAL if is_bulk else Message.CRITICAL
        num_sent = 0
        connection = None
        for message in email_messages:
            priority = message.extra_headers.get('X-Mail-Priority',
                                                 default_priority)
            content = message.message().as_string()
            for to_email in message.recipients():
                message = Message(
                    priority=priority,
                    to_address=to_email,
                    from_address=getattr(message, 'from_email',
                                         djsettings.DEFAULT_FROM_EMAIL),
                    subject=message.subject,
                    content=content,
                )
                if priority == Message.CRITICAL:
                    # send immidiately
                    if connection is None:
                        connection = get_connection(
                            backend=
                            'django.core.mail.backends.smtp.EmailBackend')
                    send_message.apply_async(message, connection=connection)
                else:
                    message.save()
            num_sent += 1
        if connection is not None:
            connection.close()
        return num_sent
Exemple #7
0
 def send_messages(self, email_messages):
     if not email_messages:
         return
     # Count messages per request
     cache = get_request_cache()
     key = 'mailer.sent_messages'
     sent_messages = cache.get(key) or 0
     sent_messages += 1
     cache.set(key, sent_messages)
     
     is_bulk = len(email_messages) > 1
     if sent_messages > settings.MAILER_NON_QUEUED_PER_REQUEST_THRESHOLD:
         is_bulk = True
     default_priority = Message.NORMAL if is_bulk else Message.CRITICAL
     num_sent = 0
     connection = None
     for message in email_messages:
         priority = message.extra_headers.get('X-Mail-Priority', default_priority)
         content = message.message().as_string()
         for to_email in message.recipients():
             message = Message(
                 priority=priority,
                 to_address=to_email,
                 from_address=getattr(message, 'from_email', djsettings.DEFAULT_FROM_EMAIL),
                 subject=message.subject,
                 content=content,
             )
             if priority == Message.CRITICAL:
                 # send immidiately
                 if connection is None:
                     connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend')
                 send_message.apply_async(message, connection=connection)
             else:
                 message.save()
         num_sent += 1
     if connection is not None:
         connection.close()
     return num_sent