<s> import math <EOL> import json <EOL> from collections import OrderedDict <EOL> from django . core import serializers <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import render <EOL> from django . db . models import Count <EOL> from django . db . models . query import QuerySet <EOL> from django . contrib . auth import authenticate , login , logout <EOL> from django . contrib . auth . decorators import login_required , user_passes_test <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from hackday . voting . forms import VoteForm , AuthorizeForm , VoteAuthorizeForm <EOL> from hackday . voting . models import VoteCart , Vote , VoteStatus , STATUS <EOL> from hackday . voting . moremodels import Category , TYPE <EOL> from hackday . teams . models import Team , STATUS as TEAM_STATUS <EOL> from hackday . users . models import User , UserProfile <EOL> class DjangoJSONEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , QuerySet ) : <EOL> return json . loads ( serializers . serialize ( '<STR_LIT>' , obj ) ) <EOL> return JSONEncoder . default ( self , obj ) <EOL> def ipad_vote ( request ) : <EOL> logout ( request ) <EOL> env = { } <EOL> try : <EOL> online = VoteStatus . objects . all ( ) [ <NUM_LIT:0> ] . online <EOL> except : <EOL> online = False <EOL> if not online : <EOL> return render ( request , '<STR_LIT>' ) ; <EOL> error_message = None <EOL> user = None <EOL> votes = [ ] <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = VoteAuthorizeForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> user = authenticate ( username = form . cleaned_data [ '<STR_LIT>' ] , <EOL> password = form . cleaned_data [ '<STR_LIT:password>' ] ) <EOL> if user : <EOL> try : <EOL> cart = VoteCart . objects . get ( user = user . id ) <EOL> except : <EOL> cart = VoteCart ( user = user , status = STATUS . ACTIVE ) <EOL> cart . save ( ) <EOL> for key , team in form . cleaned_data . items ( ) : <EOL> if key . startswith ( '<STR_LIT>' ) : <EOL> category_id = key . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> category = Category . objects . get ( id = category_id ) <EOL> _insert_or_update_vote ( cart , category , team ) <EOL> votes . append ( { '<STR_LIT>' : category , '<STR_LIT>' : team } ) <EOL> env = { '<STR_LIT:user>' : user } <EOL> env [ '<STR_LIT>' ] = votes <EOL> return render ( request , '<STR_LIT>' , env ) ; <EOL> else : <EOL> error_message = "<STR_LIT>" <EOL> else : <EOL> form = VoteAuthorizeForm ( ) <EOL> env [ '<STR_LIT>' ] = form <EOL> env [ '<STR_LIT>' ] = error_message <EOL> return render ( request , '<STR_LIT>' , env ) <EOL> @ login_required <EOL> def vote ( request ) : <EOL> env = { } <EOL> try : <EOL> online = VoteStatus . objects . all ( ) [ <NUM_LIT:0> ] . online <EOL> except : <EOL> online = False <EOL> if not online : <EOL> return render ( request , '<STR_LIT>' ) ; <EOL> try : <EOL> cart = VoteCart . objects . get ( user = request . user . id ) <EOL> except : <EOL> cart = VoteCart ( user = request . user , status = STATUS . ACTIVE ) <EOL> cart . save ( ) <EOL> if cart . status == STATUS . COMPLETED : <EOL> return render ( request , '<STR_LIT>' ) ; <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = VoteForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> for key , team in form . cleaned_data . items ( ) : <EOL> category_id = key . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> category = Category . objects . get ( id = category_id ) <EOL> _insert_or_update_vote ( cart , category , team ) <EOL> return render ( request , '<STR_LIT>' ) ; <EOL> else : <EOL> votes = Vote . objects . filter ( cart = cart . id ) <EOL> form_init = { } <EOL> for vote in votes : <EOL> form_init [ '<STR_LIT>' % vote . category . id ] = vote . team . id <EOL> form = VoteForm ( form_init ) <EOL> env [ '<STR_LIT>' ] = form <EOL> return render ( request , '<STR_LIT>' , env ) <EOL> def info ( request ) : <EOL> categories = Category . objects . filter ( type = TYPE . VOTED ) . order_by ( '<STR_LIT:id>' ) <EOL> teams = Team . objects . filter ( status = TEAM_STATUS . ACTIVE ) . order_by ( '<STR_LIT:id>' ) <EOL> judged = Category . objects . filter ( type = TYPE . JUDGED ) . order_by ( '<STR_LIT:id>' ) <EOL> d = { } <EOL> for category in judged : <EOL> d [ category . name ] = [ t for t in teams if t . category . id == category . id ] <EOL> d [ category . name ] . sort ( ) <EOL> ordered_teams = [ ] <EOL> o_dict = OrderedDict ( sorted ( d . items ( ) , key = lambda t : len ( t ) ) ) <EOL> for item in o_dict . values ( ) : <EOL> ordered_teams += item <EOL> env = { '<STR_LIT>' : categories , <EOL> '<STR_LIT>' : ordered_teams } <EOL> return render ( request , '<STR_LIT>' , env ) <EOL> @ csrf_exempt <EOL> def api_login ( request ) : <EOL> env = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = AuthorizeForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> user = authenticate ( username = form . cleaned_data [ '<STR_LIT>' ] , <EOL> password = form . cleaned_data [ '<STR_LIT:password>' ] ) <EOL> if user : <EOL> try : <EOL> user_profile = UserProfile . objects . get ( user = user ) <EOL> except : <EOL> user_profile = None <EOL> if user . is_active and user_profile : <EOL> login ( request , user ) <EOL> env = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( env ) , mimetype = "<STR_LIT:application/json>" ) <EOL> @ csrf_exempt <EOL> @ login_required <EOL> def api ( request ) : <EOL> categories = Category . objects . filter ( type = TYPE . VOTED ) . order_by ( '<STR_LIT:id>' ) <EOL> teams = Team . objects . filter ( status = TEAM_STATUS . ACTIVE ) . order_by ( '<STR_LIT:id>' ) <EOL> users = User . objects . filter ( is_active = True ) . order_by ( '<STR_LIT:id>' ) <EOL> judged = Category . objects . filter ( type = TYPE . JUDGED ) . order_by ( '<STR_LIT:id>' ) <EOL> saved = False <EOL> try : <EOL> online = VoteStatus . objects . all ( ) [ <NUM_LIT:0> ] . online <EOL> except : <EOL> online = False <EOL> try : <EOL> cart = VoteCart . objects . get ( user = request . user . id ) <EOL> except : <EOL> cart = VoteCart ( user = request . user , status = STATUS . ACTIVE ) <EOL> cart . save ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = VoteForm ( request . POST ) <EOL> if form . is_valid ( ) and online : <EOL> for key , team in form . cleaned_data . items ( ) : <EOL> category_id = key . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> category = Category . objects . get ( id = category_id ) <EOL> _insert_or_update_vote ( cart , category , team ) <EOL> saved = True <EOL> vote_dict = { } <EOL> votes = Vote . objects . filter ( cart = cart . id ) <EOL> for vote in votes : <EOL> vote_dict [ vote . category . id ] = vote . team . id <EOL> env = { '<STR_LIT>' : categories , <EOL> '<STR_LIT>' : judged , <EOL> '<STR_LIT>' : teams , <EOL> '<STR_LIT>' : users , <EOL> '<STR_LIT>' : online , <EOL> '<STR_LIT>' : vote_dict , <EOL> '<STR_LIT>' : saved , <EOL> } <EOL> return HttpResponse ( json . dumps ( env , cls = DjangoJSONEncoder ) , <EOL> mimetype = "<STR_LIT:application/json>" ) <EOL> @ user_passes_test ( lambda u : u . is_superuser ) <EOL> def results ( request ) : <EOL> vote_results = [ ] ; <EOL> categories = Category . objects . filter ( type = TYPE . VOTED ) . order_by ( '<STR_LIT:id>' ) <EOL> totals = { } <EOL> for t in Vote . objects . values ( '<STR_LIT>' ) . annotate ( total = Count ( '<STR_LIT>' ) ) : <EOL> totals [ t [ '<STR_LIT>' ] ] = t [ '<STR_LIT>' ] <EOL> for category in categories : <EOL> votes = Vote . objects . filter ( category = category ) . values ( '<STR_LIT>' ) . annotate ( <EOL> votes = Count ( '<STR_LIT:id>' ) ) . order_by ( '<STR_LIT>' ) . reverse ( ) <EOL> votes = [ { '<STR_LIT>' : v [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : _get_percentage ( v [ '<STR_LIT>' ] , totals [ category . id ] ) , <EOL> '<STR_LIT>' : Team . objects . get ( id = v [ '<STR_LIT>' ] ) } <EOL> for v in votes ] [ : <NUM_LIT:10> ] <EOL> if '<STR_LIT>' in request . GET . keys ( ) : <EOL> _fix_votes ( votes , totals [ category . id ] ) <EOL> vote_results . append ( { '<STR_LIT>' : category , <EOL> '<STR_LIT>' : votes , <EOL> '<STR_LIT>' : totals . get ( category . id , <NUM_LIT:0> ) } ) <EOL> env = { '<STR_LIT>' : vote_results } <EOL> return render ( request , '<STR_LIT>' , env ) <EOL> def _fix_votes ( votes , total ) : <EOL> for idx , vote in enumerate ( votes ) : <EOL> if vote [ '<STR_LIT>' ] . name == "<STR_LIT>" : <EOL> goat = votes [ idx ] <EOL> goat [ '<STR_LIT>' ] = <NUM_LIT:100> <EOL> goat [ '<STR_LIT>' ] = total <EOL> votes . remove ( goat ) <EOL> votes . insert ( <NUM_LIT:0> , goat ) <EOL> else : <EOL> votes [ idx ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> votes [ idx ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> def _get_percentage ( votes , total ) : <EOL> percent = ( <NUM_LIT:1.0> * votes ) / ( <NUM_LIT:1.0> * total ) <EOL> percent = <NUM_LIT:100> * percent <EOL> return int ( math . ceil ( percent ) ) <EOL> def _insert_or_update_vote ( cart , category , team ) : <EOL> if team is not None and category is not None : <EOL> if ( ( category . is_concept and team . is_implemented ) or <EOL> ( category . is_implemented and team . is_concept ) ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> try : <EOL> vote = Vote . objects . get ( cart = cart , category = category ) <EOL> if team is None : <EOL> vote . delete ( ) <EOL> else : <EOL> vote . team = team <EOL> vote . save ( ) <EOL> except : <EOL> if team is not None : <EOL> vote = Vote ( cart = cart , category = category , team = team ) <EOL> vote . save ( ) </s>
