<s> import csv <EOL> from django . conf . urls import patterns , url <EOL> from django . contrib import admin <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from huxley . core . models import Assignment , Committee , Country , School <EOL> class AssignmentAdmin ( admin . ModelAdmin ) : <EOL> def list ( self , request ) : <EOL> '''<STR_LIT>''' <EOL> assignments = HttpResponse ( content_type = '<STR_LIT>' ) <EOL> assignments [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> writer = csv . writer ( assignments ) <EOL> writer . writerow ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> for assignment in Assignment . objects . all ( ) . order_by ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> writer . writerow ( [ <EOL> assignment . school , <EOL> assignment . committee , <EOL> assignment . country , <EOL> assignment . rejected <EOL> ] ) <EOL> return assignments <EOL> def load ( self , request ) : <EOL> '''<STR_LIT>''' <EOL> assignments = request . FILES <EOL> reader = csv . reader ( assignments [ '<STR_LIT>' ] ) <EOL> def get_model ( model , name , cache ) : <EOL> if not name in cache : <EOL> cache [ name ] = model . objects . get ( name = name ) <EOL> return cache [ name ] <EOL> def generate_assigments ( reader ) : <EOL> committees = { } <EOL> countries = { } <EOL> schools = { } <EOL> for row in reader : <EOL> if ( row [ <NUM_LIT:0> ] == '<STR_LIT>' and row [ <NUM_LIT:1> ] == '<STR_LIT>' and row [ <NUM_LIT:2> ] == '<STR_LIT>' ) : <EOL> continue <EOL> committee = get_model ( Committee , row [ <NUM_LIT:1> ] , committees ) <EOL> country = get_model ( Country , row [ <NUM_LIT:2> ] , countries ) <EOL> school = get_model ( School , row [ <NUM_LIT:0> ] , schools ) <EOL> if len ( row ) < <NUM_LIT:4> : <EOL> rejected = False <EOL> else : <EOL> rejected = ( row [ <NUM_LIT:3> ] . lower ( ) == '<STR_LIT:true>' ) <EOL> yield ( committee . id , country . id , school . id , rejected ) <EOL> Assignment . update_assignments ( generate_assigments ( reader ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> def get_urls ( self ) : <EOL> urls = super ( AssignmentAdmin , self ) . get_urls ( ) <EOL> urls += patterns ( '<STR_LIT>' , <EOL> url ( <EOL> r'<STR_LIT:list>' , <EOL> self . admin_site . admin_view ( self . list ) , <EOL> name = '<STR_LIT>' <EOL> ) , <EOL> url ( <EOL> r'<STR_LIT>' , <EOL> self . admin_site . admin_view ( self . load ) , <EOL> name = '<STR_LIT>' , <EOL> ) , <EOL> ) <EOL> return urls </s>
