<s> import tweepy <EOL> import os <EOL> import json <EOL> import requests <EOL> from BeautifulSoup import BeautifulSoup <EOL> from email . MIMEImage import MIMEImage <EOL> from hackday . common import common_env <EOL> from hackday . users . models import User , UserProfile <EOL> from django . core . mail import EmailMultiAlternatives <EOL> from django . conf import settings <EOL> from django . template import loader , Context <EOL> from django . contrib . sites . models import Site <EOL> class BlogNotification ( object ) : <EOL> def __init__ ( self , entry ) : <EOL> self . entry = entry <EOL> self . site = Site . objects . get_current ( ) <EOL> self . context = { '<STR_LIT>' : self . entry , <EOL> '<STR_LIT>' : settings . STATIC_URL , <EOL> '<STR_LIT>' : self . get_site_domain ( ) , <EOL> '<STR_LIT>' : self . site . name } <EOL> def send_email ( self ) : <EOL> profiles = UserProfile . objects . filter ( notify_by_email = True ) <EOL> recipients = [ p . user . email for p in profiles ] <EOL> subject = '<STR_LIT>' . format ( self . site . name , self . entry . title ) <EOL> body = self . render_template ( '<STR_LIT>' ) <EOL> message = EmailMultiAlternatives ( subject , <EOL> body , <EOL> settings . DEFAULT_FROM_EMAIL , <EOL> [ settings . DEFAULT_FROM_EMAIL ] , <EOL> recipients ) <EOL> message . mixed_subtype = "<STR_LIT>" <EOL> try : <EOL> body_html = self . render_template ( '<STR_LIT>' ) <EOL> soup = BeautifulSoup ( body_html ) <EOL> images = { } <EOL> for index , tag in enumerate ( soup . findAll ( self . _image_finder ) ) : <EOL> if tag [ '<STR_LIT:src>' ] not in images . keys ( ) : <EOL> images [ tag [ '<STR_LIT:src>' ] ] = "<STR_LIT>" . format ( index ) <EOL> tag [ '<STR_LIT:src>' ] = '<STR_LIT>' . format ( images [ tag [ '<STR_LIT:src>' ] ] ) <EOL> body_html = str ( soup ) <EOL> for filename , file_id in images . items ( ) : <EOL> image_file = self . _file_finder ( filename ) <EOL> if image_file : <EOL> msg_image = MIMEImage ( image_file . read ( ) ) <EOL> image_file . close ( ) <EOL> msg_image . add_header ( '<STR_LIT>' , '<STR_LIT>' . format ( file_id ) ) <EOL> message . attach ( msg_image ) <EOL> message . attach_alternative ( body_html , "<STR_LIT>" ) <EOL> except Exception , e : <EOL> raise e <EOL> pass <EOL> message . send ( ) <EOL> def _file_finder ( self , filename ) : <EOL> filename = filename . replace ( settings . STATIC_URL , '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) <EOL> for static_dir in settings . STATICFILES_DIRS : <EOL> image_path = os . path . join ( static_dir , filename ) <EOL> if os . path . exists ( image_path ) : <EOL> return open ( image_path , '<STR_LIT:rb>' ) <EOL> return None <EOL> def _image_finder ( self , tag ) : <EOL> return ( tag . name == u'<STR_LIT>' ) <EOL> def send_tweet ( self ) : <EOL> if not hasattr ( settings , '<STR_LIT>' ) : <EOL> return ; <EOL> auth = tweepy . OAuthHandler ( settings . TWITTER_CONSUMER_KEY , <EOL> settings . TWITTER_CONSUMER_SECRET ) <EOL> auth . set_access_token ( settings . TWITTER_ACCESS_TOKEN , <EOL> settings . TWITTER_ACCESS_TOKEN_SECRET ) <EOL> update = self . render_template ( '<STR_LIT>' ) <EOL> api = tweepy . API ( auth ) <EOL> try : <EOL> api . update_status ( update ) <EOL> except tweepy . TweepError : <EOL> pass <EOL> def send_push ( self ) : <EOL> if not hasattr ( settings , '<STR_LIT>' ) : <EOL> return ; <EOL> key = settings . URBAN_AIRSHIP_KEY <EOL> secret = settings . URBAN_AIRSHIP_SECRET <EOL> url = '<STR_LIT>' <EOL> update = self . render_template ( '<STR_LIT>' ) <EOL> notification = { '<STR_LIT>' : "<STR_LIT:all>" , <EOL> '<STR_LIT>' : "<STR_LIT:all>" , <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT>" : update , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:default>" } , <EOL> } <EOL> } <EOL> try : <EOL> response = requests . post ( url , <EOL> data = json . dumps ( notification ) , <EOL> auth = ( key , secret ) , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : "<STR_LIT>" } ) <EOL> except : <EOL> pass <EOL> def render_template ( self , template ) : <EOL> t = loader . get_template ( template ) <EOL> return t . render ( Context ( self . context ) ) <EOL> def get_site_domain ( self ) : <EOL> if not self . site . domain . startswith ( '<STR_LIT:http>' ) : <EOL> return '<STR_LIT>' . format ( self . site . domain ) <EOL> else : <EOL> return self . site . domain </s>
