<s> from django . contrib . auth . decorators import login_required <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import F <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . shortcuts import render_to_response , get_object_or_404 <EOL> from django . template import RequestContext <EOL> from django . utils . functional import wraps <EOL> from pennyblack . models import Newsletter , Link , Mail , Job <EOL> import types <EOL> def needs_mail ( function ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( function ) <EOL> def wrapper ( request , mail_hash = None , * args , ** kwargs ) : <EOL> try : <EOL> mail = Mail . objects . get ( mail_hash = mail_hash ) <EOL> except ObjectDoesNotExist : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> return function ( request , mail = mail , * args , ** kwargs ) <EOL> return wrapper <EOL> def needs_link ( function ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( function ) <EOL> def wrapper ( request , link_hash = None , * args , ** kwargs ) : <EOL> try : <EOL> link = Link . objects . get ( link_hash = link_hash ) <EOL> except ObjectDoesNotExist : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> return function ( request , link = link , * args , ** kwargs ) <EOL> return wrapper <EOL> @ login_required <EOL> def preview ( request , newsletter_id ) : <EOL> """<STR_LIT>""" <EOL> newsletter = Newsletter . objects . filter ( pk = newsletter_id ) [ <NUM_LIT:0> ] <EOL> request . content_context = { <EOL> '<STR_LIT>' : newsletter , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> job_id = request . GET . get ( '<STR_LIT>' , None ) <EOL> if job_id : <EOL> job = get_object_or_404 ( Job , pk = job_id ) <EOL> if job . mails . count ( ) : <EOL> mail = job . mails . all ( ) [ <NUM_LIT:0> ] <EOL> request . content_context . update ( mail . get_context ( ) ) <EOL> request . content_context . update ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> return render_to_response ( newsletter . template . path , request . content_context , context_instance = RequestContext ( request ) ) <EOL> @ needs_mail <EOL> @ needs_link <EOL> def redirect_link ( request , mail , link ) : <EOL> """<STR_LIT>""" <EOL> mail . on_landing ( request ) <EOL> target = link . click ( mail ) <EOL> if isinstance ( target , types . FunctionType ) : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( mail . mail_hash , link . link_hash ) ) ) <EOL> ga_tracking = "<STR_LIT>" % ( <EOL> mail . job . newsletter . utm_source , mail . job . newsletter . utm_medium , <EOL> mail . job . utm_campaign ) <EOL> if target . find ( '<STR_LIT:?>' ) > <NUM_LIT:0> : <EOL> target = '<STR_LIT>' % ( target , ga_tracking ) <EOL> else : <EOL> target = '<STR_LIT>' % ( target , ga_tracking ) <EOL> return HttpResponseRedirect ( target ) <EOL> @ needs_mail <EOL> def ping ( request , mail , filename ) : <EOL> mail . mark_viewed ( ) <EOL> return HttpResponseRedirect ( mail . job . newsletter . header_image . get_absolute_url ( ) ) <EOL> @ needs_mail <EOL> def view ( request , mail ) : <EOL> mail . mark_viewed ( ) <EOL> return HttpResponse ( mail . get_content ( webview = True ) ) <EOL> @ needs_mail <EOL> @ needs_link <EOL> def proxy ( request , mail , link ) : <EOL> return link . get_target ( mail ) ( request , mail . person , mail . job . group_object ) </s>
