<s> from __future__ import unicode_literals <EOL> import io <EOL> import logging <EOL> import sys <EOL> import traceback <EOL> from django . conf import settings <EOL> from django . core . mail import get_connection <EOL> from django . core . paginator import Paginator <EOL> from django . utils import timezone <EOL> StringIO = io . BytesIO if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> else io . StringIO <EOL> from . import models <EOL> logger = logging . getLogger ( __name__ ) <EOL> def _chunked_iterate_queryset ( queryset , chunk_size = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> paginator = Paginator ( queryset , chunk_size ) <EOL> for page_index in paginator . page_range : <EOL> page = paginator . page ( page_index ) <EOL> for item in page . object_list : <EOL> yield item <EOL> def _safe_send_message ( message_model , connection ) : <EOL> """<STR_LIT>""" <EOL> email = message_model . get_email_message ( ) <EOL> sended = <NUM_LIT:0> <EOL> with StringIO ( ) as f : <EOL> try : <EOL> sended = connection . send_messages ( [ email ] ) <EOL> except Exception : <EOL> traceback . print_exc ( file = f ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> message_model . exception = f . read ( ) <EOL> logger . error ( message_model . exception ) <EOL> else : <EOL> if sended == <NUM_LIT:1> : <EOL> message_model . status = models . STATUS_SENT <EOL> message_model . sent_at = timezone . now ( ) <EOL> else : <EOL> message_model . status = models . STATUS_FAILED <EOL> message_model . retry_count += <NUM_LIT:1> <EOL> message_model . save ( ) <EOL> return sended <EOL> def _get_real_backend ( ) : <EOL> real_backend_path = getattr ( <EOL> settings , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> return get_connection ( backend = real_backend_path , fail_silently = False ) <EOL> def _send_messages ( email_messages ) : <EOL> connection = _get_real_backend ( ) <EOL> email_models = [ models . Message . from_email_message ( email , save = True ) <EOL> for email in email_messages ] <EOL> connection . open ( ) <EOL> try : <EOL> sended_counter = <NUM_LIT:0> <EOL> for email , model_instance in zip ( email_messages , email_models ) : <EOL> if hasattr ( email , "<STR_LIT>" ) : <EOL> if email . priority <= models . PRIORITY_LOW : <EOL> model_instance . priority = email . priority <EOL> model_instance . status = models . STATUS_PENDING <EOL> model_instance . save ( ) <EOL> sended_counter += <NUM_LIT:1> <EOL> continue <EOL> sended_counter += _safe_send_message ( model_instance , connection ) <EOL> finally : <EOL> connection . close ( ) <EOL> return sended_counter <EOL> def _send_pending_messages ( ) : <EOL> """<STR_LIT>""" <EOL> queryset = models . Message . objects . filter ( status = models . STATUS_PENDING ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> connection = _get_real_backend ( ) <EOL> connection . open ( ) <EOL> try : <EOL> sended_counter = <NUM_LIT:0> <EOL> for message_model in _chunked_iterate_queryset ( queryset , <NUM_LIT:100> ) : <EOL> sended_counter += _safe_send_message ( message_model , connection ) <EOL> finally : <EOL> connection . close ( ) <EOL> return sended_counter <EOL> def _retry_send_messages ( ) : <EOL> """<STR_LIT>""" <EOL> max_retry_value = getattr ( settings , '<STR_LIT>' , <NUM_LIT:3> ) <EOL> queryset = models . Message . objects . filter ( status = models . STATUS_FAILED ) . filter ( retry_count__lte = max_retry_value ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> connection = _get_real_backend ( ) <EOL> connection . open ( ) <EOL> try : <EOL> sended_counter = <NUM_LIT:0> <EOL> for message_model in _chunked_iterate_queryset ( queryset , <NUM_LIT:100> ) : <EOL> sended_counter += _safe_send_message ( message_model , connection ) <EOL> finally : <EOL> connection . close ( ) <EOL> return sended_counter <EOL> def _mark_discarded_messages ( ) : <EOL> """<STR_LIT>""" <EOL> max_retry_value = getattr ( settings , '<STR_LIT>' , <NUM_LIT:3> ) <EOL> queryset = models . Message . objects . filter ( status = models . STATUS_FAILED , <EOL> retry_count__gt = max_retry_value ) <EOL> return queryset . update ( status = models . STATUS_DISCARDED ) </s>
