<s> from models import Beacon <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . http import Http404 , HttpResponse , HttpResponseRedirect <EOL> from django . core . urlresolvers import resolve <EOL> from django . conf import settings <EOL> import base64 <EOL> transparent_gif_contents = '<STR_LIT>' <EOL> def trigger ( request , target_type , target_id , source_type = None , source_id = None , <EOL> entity_type = None , entity_id = None , ignore_errors = False , pixel = False ) : <EOL> """<STR_LIT>""" <EOL> target = None <EOL> entity = None <EOL> beacon = None <EOL> source = None <EOL> if pixel == False and ( target_type is not None and target_id is not None ) : <EOL> target = get_object_or_404 ( target_type , pk = target_id ) <EOL> if source_type is not None and source_id is not None : <EOL> try : <EOL> source = source_type . objects . get ( pk = source_id ) <EOL> except : <EOL> if ignore_errors is False : <EOL> raise Http404 ( '<STR_LIT>' ) <EOL> if entity_type is not None : <EOL> if entity_id is not None : <EOL> entity , created = entity_type . objects . get_or_create ( pk = entity_id ) <EOL> else : <EOL> entity = entity_type ( ) <EOL> if hasattr ( entity , '<STR_LIT>' ) and callable ( entity . beacon_update ) : <EOL> entity . beacon_update ( request ) <EOL> entity . save ( ) <EOL> beacon_arguments = { } <EOL> if target is not None : <EOL> beacon_arguments . update ( { <EOL> '<STR_LIT>' : ContentType . objects . get_for_model ( target_type ) , <EOL> '<STR_LIT>' : target . pk , <EOL> } ) <EOL> if source is not None : <EOL> beacon_arguments . update ( { <EOL> '<STR_LIT>' : ContentType . objects . get_for_model ( source_type ) , <EOL> '<STR_LIT>' : source . pk , <EOL> } ) <EOL> if entity is not None : <EOL> beacon_arguments . update ( { <EOL> '<STR_LIT>' : ContentType . objects . get_for_model ( entity_type ) , <EOL> '<STR_LIT>' : entity . pk , <EOL> } ) <EOL> try : <EOL> beacon = Beacon ( ** beacon_arguments ) <EOL> beacon . save ( ) <EOL> except : <EOL> if ignore_errors is False : <EOL> raise Http404 ( '<STR_LIT>' ) <EOL> try : <EOL> if pixel : <EOL> return HttpResponse ( base64 . decodestring ( transparent_gif_contents ) , mimetype = '<STR_LIT>' ) <EOL> else : <EOL> beacon_final_url = beacon . get_absolute_url ( ) <EOL> if getattr ( settings , '<STR_LIT>' , True ) : <EOL> resolve_match = resolve ( beacon_final_url ) <EOL> if resolve_match : <EOL> return resolve_match . func ( request , * match . args , ** match . kwargs ) <EOL> return HttpResponseRedirect ( beacon_final_url ) <EOL> except : <EOL> try : <EOL> return HttpResponseRedirect ( target . get_absolute_url ( ) ) <EOL> except : <EOL> raise Http404 ( '<STR_LIT>' ) </s>
