<s> try : <EOL> from urllib . request import urlopen <EOL> from urllib . error import URLError <EOL> except ImportError : <EOL> from urllib2 import urlopen , URLError <EOL> import copy <EOL> import logging <EOL> from datetime import datetime <EOL> try : <EOL> import pytz <EOL> except ImportError : <EOL> pytz = None <EOL> from boto . regioninfo import RegionInfo <EOL> from boto . ses import SESConnection <EOL> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from django . views . decorators . http import require_POST <EOL> from django . core . cache import cache <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> from django . utils import simplejson as json <EOL> from django_ses import settings <EOL> from django_ses import signals <EOL> from django_ses import utils <EOL> logger = logging . getLogger ( __name__ ) <EOL> def superuser_only ( view_func ) : <EOL> """<STR_LIT>""" <EOL> def _inner ( request , * args , ** kwargs ) : <EOL> if not request . user . is_superuser : <EOL> raise PermissionDenied <EOL> return view_func ( request , * args , ** kwargs ) <EOL> return _inner <EOL> def stats_to_list ( stats_dict , localize = pytz ) : <EOL> """<STR_LIT>""" <EOL> result = stats_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> result = copy . deepcopy ( result ) <EOL> datapoints = [ ] <EOL> if localize : <EOL> current_tz = localize . timezone ( settings . TIME_ZONE ) <EOL> else : <EOL> current_tz = None <EOL> for dp in result [ '<STR_LIT>' ] : <EOL> if current_tz : <EOL> utc_dt = datetime . strptime ( dp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> utc_dt = localize . utc . localize ( utc_dt ) <EOL> dp [ '<STR_LIT>' ] = current_tz . normalize ( <EOL> utc_dt . astimezone ( current_tz ) ) <EOL> datapoints . append ( dp ) <EOL> datapoints . sort ( key = lambda x : x [ '<STR_LIT>' ] ) <EOL> return datapoints <EOL> def quota_parse ( quota_dict ) : <EOL> """<STR_LIT>""" <EOL> return quota_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def emails_parse ( emails_dict ) : <EOL> """<STR_LIT>""" <EOL> result = emails_dict [ '<STR_LIT>' ] [ <EOL> '<STR_LIT>' ] <EOL> emails = [ email for email in result [ '<STR_LIT>' ] ] <EOL> return sorted ( emails ) <EOL> def sum_stats ( stats_data ) : <EOL> """<STR_LIT>""" <EOL> t_bounces = <NUM_LIT:0> <EOL> t_complaints = <NUM_LIT:0> <EOL> t_delivery_attempts = <NUM_LIT:0> <EOL> t_rejects = <NUM_LIT:0> <EOL> for dp in stats_data : <EOL> t_bounces += int ( dp [ '<STR_LIT>' ] ) <EOL> t_complaints += int ( dp [ '<STR_LIT>' ] ) <EOL> t_delivery_attempts += int ( dp [ '<STR_LIT>' ] ) <EOL> t_rejects += int ( dp [ '<STR_LIT>' ] ) <EOL> return { <EOL> '<STR_LIT>' : t_bounces , <EOL> '<STR_LIT>' : t_complaints , <EOL> '<STR_LIT>' : t_delivery_attempts , <EOL> '<STR_LIT>' : t_rejects , <EOL> } <EOL> @ superuser_only <EOL> def dashboard ( request ) : <EOL> """<STR_LIT>""" <EOL> cache_key = '<STR_LIT>' <EOL> cached_view = cache . get ( cache_key ) <EOL> if cached_view : <EOL> return cached_view <EOL> region = RegionInfo ( <EOL> name = settings . AWS_SES_REGION_NAME , <EOL> endpoint = settings . AWS_SES_REGION_ENDPOINT ) <EOL> ses_conn = SESConnection ( <EOL> aws_access_key_id = settings . ACCESS_KEY , <EOL> aws_secret_access_key = settings . SECRET_KEY , <EOL> region = region ) <EOL> quota_dict = ses_conn . get_send_quota ( ) <EOL> verified_emails_dict = ses_conn . list_verified_email_addresses ( ) <EOL> stats = ses_conn . get_send_statistics ( ) <EOL> quota = quota_parse ( quota_dict ) <EOL> verified_emails = emails_parse ( verified_emails_dict ) <EOL> ordered_data = stats_to_list ( stats ) <EOL> summary = sum_stats ( ordered_data ) <EOL> extra_context = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ordered_data , <EOL> '<STR_LIT>' : quota [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : quota [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : float ( quota [ '<STR_LIT>' ] ) - <EOL> float ( quota [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : quota [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : verified_emails , <EOL> '<STR_LIT>' : summary , <EOL> '<STR_LIT>' : ses_conn . gs_access_key_id , <EOL> '<STR_LIT>' : True if pytz else False , <EOL> } <EOL> response = render_to_response ( <EOL> '<STR_LIT>' , <EOL> extra_context , <EOL> context_instance = RequestContext ( request ) ) <EOL> cache . set ( cache_key , response , <NUM_LIT> * <NUM_LIT:15> ) <EOL> return response <EOL> @ require_POST <EOL> def handle_bounce ( request ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( request , '<STR_LIT:body>' ) : <EOL> raw_json = request . body <EOL> else : <EOL> raw_json = request . raw_post_data <EOL> try : <EOL> notification = json . loads ( raw_json ) <EOL> except ValueError as e : <EOL> logger . warning ( '<STR_LIT>' , e ) <EOL> return HttpResponseBadRequest ( ) <EOL> if ( settings . VERIFY_BOUNCE_SIGNATURES and <EOL> not utils . verify_bounce_message ( notification ) ) : <EOL> logger . info ( '<STR_LIT>' , <EOL> notification . get ( '<STR_LIT>' ) , <EOL> extra = { <EOL> '<STR_LIT>' : notification , <EOL> } , <EOL> ) <EOL> return HttpResponse ( ) <EOL> if notification . get ( '<STR_LIT>' ) in ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> logger . info ( '<STR_LIT>' , <EOL> notification . get ( '<STR_LIT>' ) , <EOL> extra = { <EOL> '<STR_LIT>' : notification , <EOL> } , <EOL> ) <EOL> subscribe_url = notification . get ( '<STR_LIT>' ) <EOL> try : <EOL> urlopen ( subscribe_url ) . read ( ) <EOL> except URLError as e : <EOL> logger . error ( '<STR_LIT>' , e , <EOL> extra = { <EOL> '<STR_LIT>' : notification , <EOL> } , <EOL> exc_info = True , <EOL> ) <EOL> elif notification . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> try : <EOL> message = json . loads ( notification [ '<STR_LIT>' ] ) <EOL> except ValueError as e : <EOL> logger . warning ( '<STR_LIT>' , e , extra = { <EOL> '<STR_LIT>' : notification , <EOL> } ) <EOL> else : <EOL> mail_obj = message . get ( '<STR_LIT>' ) <EOL> notification_type = message . get ( '<STR_LIT>' ) <EOL> if notification_type == '<STR_LIT>' : <EOL> bounce_obj = message . get ( '<STR_LIT>' , { } ) <EOL> feedback_id = bounce_obj . get ( '<STR_LIT>' ) <EOL> bounce_type = bounce_obj . get ( '<STR_LIT>' ) <EOL> bounce_subtype = bounce_obj . get ( '<STR_LIT>' ) <EOL> logger . info ( <EOL> '<STR_LIT>' , <EOL> feedback_id , bounce_type , bounce_subtype , <EOL> extra = { <EOL> '<STR_LIT>' : notification , <EOL> } , <EOL> ) <EOL> signals . bounce_received . send ( <EOL> sender = handle_bounce , <EOL> mail_obj = mail_obj , <EOL> bounce_obj = bounce_obj , <EOL> raw_message = raw_json , <EOL> ) <EOL> elif notification_type == '<STR_LIT>' : <EOL> complaint_obj = message . get ( '<STR_LIT>' , { } ) <EOL> feedback_id = complaint_obj . get ( '<STR_LIT>' ) <EOL> feedback_type = complaint_obj . get ( '<STR_LIT>' ) <EOL> logger . info ( '<STR_LIT>' , <EOL> feedback_id , feedback_type , <EOL> extra = { <EOL> '<STR_LIT>' : notification , <EOL> } , <EOL> ) <EOL> signals . complaint_received . send ( <EOL> sender = handle_bounce , <EOL> mail_obj = mail_obj , <EOL> complaint_obj = complaint_obj , <EOL> raw_message = raw_json , <EOL> ) <EOL> else : <EOL> logger . warning ( "<STR_LIT>" , extra = { <EOL> '<STR_LIT>' : notification , <EOL> } ) <EOL> else : <EOL> logger . info ( '<STR_LIT>' , <EOL> notification . get ( '<STR_LIT>' ) , <EOL> extra = { <EOL> '<STR_LIT>' : notification , <EOL> } , <EOL> ) <EOL> return HttpResponse ( ) </s>
