<s> import redis <EOL> import itertools <EOL> import operator <EOL> import urlparse <EOL> from django import forms <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . shortcuts import render <EOL> from django . shortcuts import redirect <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . contrib . admin . views . decorators import staff_member_required <EOL> r = redis . StrictRedis . from_url ( settings . REDIS_URL ) <EOL> class RedirectForm ( forms . Form ) : <EOL> _domain = '<STR_LIT>' <EOL> url = forms . URLField ( label = '<STR_LIT>' , initial = '<STR_LIT>' % _domain ) <EOL> title = forms . CharField ( label = '<STR_LIT>' , required = False ) <EOL> def clean_url ( self ) : <EOL> url = urlparse . urlparse ( self . cleaned_data [ '<STR_LIT:url>' ] ) <EOL> if url . netloc != self . _domain : <EOL> raise forms . ValidationError ( '<STR_LIT>' % self . _domain ) <EOL> return self . cleaned_data [ '<STR_LIT:url>' ] <EOL> def redirect_to_term ( request , version , term ) : <EOL> oneoff_redirect = find_oneoff_redirect ( version , term ) <EOL> if oneoff_redirect : <EOL> return oneoff_redirect <EOL> form = RedirectForm ( request . GET or None ) <EOL> if '<STR_LIT:url>' in request . GET : <EOL> if form . is_valid ( ) : <EOL> url = form . cleaned_data [ '<STR_LIT:url>' ] <EOL> r . sadd ( '<STR_LIT>' % ( version , term ) , url ) <EOL> r . incr ( '<STR_LIT>' % ( version , term , url ) ) <EOL> return redirect ( request . GET . get ( '<STR_LIT>' , url ) ) <EOL> urls = get_urls ( version , term ) <EOL> if urls : <EOL> scoregroups = group_urls ( urls ) <EOL> score , winners = scoregroups . next ( ) <EOL> if len ( winners ) == <NUM_LIT:1> : <EOL> url = winners [ <NUM_LIT:0> ] <EOL> r . incr ( '<STR_LIT>' % ( version , term , url ) ) <EOL> return redirect ( url ) <EOL> losers = [ losing_group for score , losing_group in scoregroups ] <EOL> else : <EOL> winners = losers = None <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : term , <EOL> '<STR_LIT>' : winners , <EOL> '<STR_LIT>' : losers , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:version>' : version , <EOL> } ) <EOL> def show_term ( request , version , term ) : <EOL> user_is_staff = request . user . is_staff <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : term , <EOL> '<STR_LIT:version>' : version , <EOL> '<STR_LIT>' : get_urls ( version , term ) , <EOL> '<STR_LIT>' : user_is_staff , <EOL> } ) <EOL> @ csrf_exempt <EOL> @ staff_member_required <EOL> def manage_oneoffs ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if '<STR_LIT:action>' in request . POST : <EOL> try : <EOL> action , version , term , target = ( request . POST [ '<STR_LIT:action>' ] , <EOL> request . POST [ '<STR_LIT:version>' ] , <EOL> request . POST [ '<STR_LIT>' ] , <EOL> request . POST [ '<STR_LIT:target>' ] ) <EOL> except KeyError : <EOL> return redirect ( manage_oneoffs ) <EOL> if action == '<STR_LIT>' : <EOL> add_oneoff ( version , term , target ) <EOL> elif action == '<STR_LIT>' : <EOL> kill_oneoff ( version , term ) <EOL> return redirect ( manage_oneoffs ) <EOL> keys = r . keys ( '<STR_LIT>' ) <EOL> targets = r . mget ( keys ) if keys else [ ] <EOL> oneoffs = [ ] <EOL> for ( k , target ) in zip ( keys , targets ) : <EOL> k = k . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if '<STR_LIT::>' in k : <EOL> version , term = k . split ( '<STR_LIT::>' ) <EOL> else : <EOL> version , term = None , k <EOL> oneoffs . append ( ( version , term , target ) ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : oneoffs , <EOL> } ) <EOL> def find_oneoff_redirect ( version , term ) : <EOL> """<STR_LIT>""" <EOL> oneoff_keys = [ '<STR_LIT>' % term , <EOL> '<STR_LIT>' % ( version , term ) ] <EOL> oneoff_redirects = r . mget ( oneoff_keys ) <EOL> for k , target in zip ( oneoff_keys , oneoff_redirects ) : <EOL> if target : <EOL> r . incr ( '<STR_LIT>' % ( version , term , target ) ) <EOL> return redirect ( target ) <EOL> return None <EOL> def add_oneoff ( version , term , target ) : <EOL> if version : <EOL> k = '<STR_LIT>' % ( version , term ) <EOL> else : <EOL> k = '<STR_LIT>' % term <EOL> r . set ( k , target ) <EOL> def kill_oneoff ( version , term ) : <EOL> if version : <EOL> k = '<STR_LIT>' % ( version , term ) <EOL> else : <EOL> k = '<STR_LIT>' % term <EOL> print "<STR_LIT>" , k <EOL> r . delete ( k ) <EOL> def get_urls ( version , term ) : <EOL> """<STR_LIT>""" <EOL> urls = r . sort ( '<STR_LIT>' % ( version , term ) , <EOL> by = '<STR_LIT>' % ( version , term ) , <EOL> get = ( '<STR_LIT>' % ( version , term ) , '<STR_LIT:#>' ) , <EOL> desc = True ) <EOL> return zip ( urls [ : : <NUM_LIT:2> ] , urls [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) <EOL> def group_urls ( urls ) : <EOL> """<STR_LIT>""" <EOL> for ( score , group ) in itertools . groupby ( urls , operator . itemgetter ( <NUM_LIT:0> ) ) : <EOL> yield ( score , [ url for score , url in group ] ) <EOL> def firstof ( list ) : <EOL> for i in list : <EOL> if i : <EOL> return i </s>
