<s> from django . contrib import admin <EOL> from django . core . urlresolvers import resolve <EOL> from django . db import models <EOL> from django . template import Context , Template , TemplateSyntaxError <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> import datetime <EOL> import hashlib <EOL> import random <EOL> def is_link ( link_original , link_replaced ) : <EOL> """<STR_LIT>""" <EOL> from pennyblack . models import Link <EOL> if link_replaced == '<STR_LIT>' : <EOL> return False <EOL> try : <EOL> link_hash = resolve ( link_replaced [ len ( '<STR_LIT>' ) : ] ) [ <NUM_LIT:2> ] [ '<STR_LIT>' ] <EOL> link = Link . objects . get ( link_hash = link_hash ) <EOL> if link . link_target == link_original : <EOL> return True <EOL> except : <EOL> pass <EOL> return False <EOL> def check_if_redirect_url ( url ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' == url [ : len ( '<STR_LIT>' ) ] : <EOL> try : <EOL> result = resolve ( url [ len ( '<STR_LIT>' ) : ] ) <EOL> if result [ <NUM_LIT:0> ] . func_name == '<STR_LIT>' : <EOL> return True <EOL> except : <EOL> pass <EOL> return False <EOL> class Link ( models . Model ) : <EOL> job = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> identifier = models . CharField ( max_length = <NUM_LIT:100> , default = '<STR_LIT>' ) <EOL> link_hash = models . CharField ( max_length = <NUM_LIT:32> , verbose_name = _ ( "<STR_LIT>" ) , db_index = True , blank = True ) <EOL> link_target = models . CharField ( verbose_name = _ ( "<STR_LIT:address>" ) , max_length = <NUM_LIT> , default = '<STR_LIT>' ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> app_label = '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> return self . link_target <EOL> def click_count ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . clicks . count ( ) <EOL> click_count . short_description = '<STR_LIT>' <EOL> def click ( self , mail ) : <EOL> """<STR_LIT>""" <EOL> click = self . clicks . create ( mail = mail ) <EOL> return self . get_target ( mail ) <EOL> def get_target ( self , mail ) : <EOL> """<STR_LIT>""" <EOL> from pennyblack . models import Newsletter <EOL> if self . identifier != '<STR_LIT>' : <EOL> return Newsletter . get_view_link ( self . identifier ) <EOL> template = Template ( self . link_target ) <EOL> return template . render ( Context ( mail . get_context ( ) ) ) <EOL> def save ( self , ** kwargs ) : <EOL> if self . link_hash == u'<STR_LIT>' : <EOL> self . link_hash = hashlib . md5 ( str ( self . id ) + str ( random . random ( ) ) ) . hexdigest ( ) <EOL> super ( Link , self ) . save ( ** kwargs ) <EOL> class LinkClick ( models . Model ) : <EOL> link = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> mail = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> date = models . DateTimeField ( default = datetime . datetime . now ( ) ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> class LinkInline ( admin . TabularInline ) : <EOL> model = Link <EOL> max_num = <NUM_LIT:0> <EOL> can_delete = False <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , ) <EOL> readonly_fields = ( '<STR_LIT>' , ) <EOL> def queryset ( self , request ) : <EOL> """<STR_LIT>""" <EOL> queryset = super ( LinkInline , self ) . queryset ( request ) <EOL> return queryset . filter ( identifier = '<STR_LIT>' ) </s>
