<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import os <EOL> import sys <EOL> import time <EOL> import smtplib <EOL> import logging <EOL> from datetime import datetime <EOL> from optparse import make_option <EOL> from django . contrib . sites . models import Site <EOL> from django . core import mail <EOL> from django . core . management . base import BaseCommand <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext as _ , activate , deactivate <EOL> from django . conf import settings <EOL> from django_nyt import models <EOL> from django_nyt import settings as nyt_settings <EOL> SLEEP_TIME = <NUM_LIT> <EOL> class Command ( BaseCommand ) : <EOL> can_import_settings = True <EOL> help = '<STR_LIT>' <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT:-c>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = SLEEP_TIME , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def _send_user_notifications ( self , context , connection ) : <EOL> subject = _ ( nyt_settings . EMAIL_SUBJECT ) <EOL> message = render_to_string ( <EOL> '<STR_LIT>' , <EOL> context <EOL> ) <EOL> email = mail . EmailMessage ( <EOL> subject , message , nyt_settings . EMAIL_SENDER , <EOL> [ context [ '<STR_LIT:user>' ] . email ] , connection = connection <EOL> ) <EOL> self . logger . info ( "<STR_LIT>" % context [ '<STR_LIT:user>' ] . email ) <EOL> email . send ( fail_silently = False ) <EOL> def handle ( self , * args , ** options ) : <EOL> activate ( settings . LANGUAGE_CODE ) <EOL> daemon = options [ '<STR_LIT>' ] <EOL> cron = options [ '<STR_LIT>' ] <EOL> assert not ( daemon and cron ) , "<STR_LIT>" <EOL> self . logger = logging . getLogger ( '<STR_LIT>' ) <EOL> if not self . logger . handlers : <EOL> if daemon : <EOL> handler = logging . FileHandler ( filename = options [ '<STR_LIT>' ] ) <EOL> else : <EOL> handler = logging . StreamHandler ( self . stdout ) <EOL> self . logger . addHandler ( handler ) <EOL> self . logger . setLevel ( logging . INFO ) <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> if not nyt_settings . SEND_EMAILS : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> if daemon : <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> try : <EOL> fpid = os . fork ( ) <EOL> if fpid > <NUM_LIT:0> : <EOL> self . logger . info ( "<STR_LIT>" % str ( fpid ) ) <EOL> pid_file = open ( options [ '<STR_LIT>' ] , "<STR_LIT:w>" ) <EOL> pid_file . write ( str ( fpid ) ) <EOL> pid_file . close ( ) <EOL> if not options [ '<STR_LIT>' ] : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> except OSError as e : <EOL> sys . stderr . write ( <EOL> "<STR_LIT>" % <EOL> ( e . errno , e . strerror ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> connection = mail . get_connection ( ) <EOL> except : <EOL> self . logger . error ( "<STR_LIT>" ) <EOL> raise <EOL> if cron : <EOL> self . send_mails ( connection ) <EOL> else : <EOL> if not daemon : <EOL> print ( "<STR_LIT>" ) <EOL> try : <EOL> self . send_loop ( connection , int ( options [ '<STR_LIT>' ] ) ) <EOL> except KeyboardInterrupt : <EOL> print ( "<STR_LIT>" ) <EOL> deactivate ( ) <EOL> def send_loop ( self , connection , sleep_time ) : <EOL> last_sent = None <EOL> while True : <EOL> started_sending_at = datetime . now ( ) <EOL> self . logger . info ( <EOL> "<STR_LIT>" % <EOL> str ( started_sending_at ) ) <EOL> if last_sent : <EOL> user_settings = models . Settings . objects . filter ( <EOL> interval__lte = ( <EOL> ( started_sending_at - <EOL> last_sent ) . seconds // <EOL> <NUM_LIT> ) // <EOL> <NUM_LIT> ) . order_by ( '<STR_LIT:user>' ) <EOL> else : <EOL> user_settings = None <EOL> self . send_mails ( connection , last_sent = last_sent , user_settings = user_settings ) <EOL> connection . close ( ) <EOL> last_sent = datetime . now ( ) <EOL> elapsed_seconds = ( last_sent - started_sending_at ) . seconds <EOL> time . sleep ( <EOL> max ( <EOL> ( min ( nyt_settings . INTERVALS ) [ <NUM_LIT:0> ] - elapsed_seconds ) * <NUM_LIT> , <EOL> sleep_time , <EOL> <NUM_LIT:0> <EOL> ) <EOL> ) <EOL> def send_mails ( self , connection , last_sent = None , user_settings = None ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> try : <EOL> connection . open ( ) <EOL> except : <EOL> self . logger . error ( "<STR_LIT>" ) <EOL> raise <EOL> if not user_settings : <EOL> user_settings = models . Settings . objects . all ( ) . order_by ( '<STR_LIT:user>' ) <EOL> context = { '<STR_LIT:user>' : None , <EOL> '<STR_LIT:username>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : Site . objects . get_current ( ) } <EOL> for setting in user_settings : <EOL> context [ '<STR_LIT:user>' ] = setting . user <EOL> context [ '<STR_LIT:username>' ] = getattr ( setting . user , setting . user . USERNAME_FIELD ) <EOL> context [ '<STR_LIT>' ] = [ ] <EOL> context [ <EOL> '<STR_LIT>' ] = nyt_settings . INTERVALS [ <EOL> [ y [ <NUM_LIT:0> ] for y in nyt_settings . INTERVALS ] . index ( setting . <EOL> interval ) ] [ <EOL> <NUM_LIT:1> ] <EOL> for subscription in setting . subscription_set . filter ( <EOL> send_emails = True , <EOL> latest__is_emailed = False <EOL> ) : <EOL> context [ '<STR_LIT>' ] . append ( subscription . latest ) <EOL> if len ( context [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> while True : <EOL> try : <EOL> self . _send_user_notifications ( context , connection ) <EOL> for n in context [ '<STR_LIT>' ] : <EOL> n . is_emailed = True <EOL> n . save ( ) <EOL> break <EOL> except smtplib . SMTPSenderRefused : <EOL> self . logger . error ( <EOL> "<STR_LIT>" . format ( setting . user . email ) ) <EOL> continue <EOL> except smtplib . SMTPException as e : <EOL> self . logger . error ( <EOL> "<STR_LIT>" . format ( e ) ) <EOL> self . logger . error ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:30> ) <EOL> except Exception as e : <EOL> self . logger . error ( <EOL> "<STR_LIT>" . format ( e ) ) <EOL> break </s>
