<s> from collections import defaultdict <EOL> from django . db . models import Count <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from brabeion import badges <EOL> from brabeion . models import BadgeAward <EOL> def badge_list ( request ) : <EOL> if request . user . is_authenticated ( ) : <EOL> user_badges = set ( <EOL> ( slug , level ) for slug , level in <EOL> BadgeAward . objects . filter ( <EOL> user = request . user <EOL> ) . values_list ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> else : <EOL> user_badges = [ ] <EOL> badges_awarded = BadgeAward . objects . values ( "<STR_LIT>" , "<STR_LIT>" ) . annotate ( <EOL> num = Count ( "<STR_LIT>" ) <EOL> ) <EOL> badges_dict = defaultdict ( list ) <EOL> for badge in badges_awarded : <EOL> badges_dict [ badge [ "<STR_LIT>" ] ] . append ( { <EOL> "<STR_LIT>" : badge [ "<STR_LIT>" ] , <EOL> "<STR_LIT:name>" : badges . _registry [ badge [ "<STR_LIT>" ] ] . levels [ badge [ "<STR_LIT>" ] ] . name , <EOL> "<STR_LIT:description>" : badges . _registry [ badge [ "<STR_LIT>" ] ] . levels [ badge [ "<STR_LIT>" ] ] . description , <EOL> "<STR_LIT:count>" : badge [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : ( badge [ "<STR_LIT>" ] , badge [ "<STR_LIT>" ] ) in user_badges <EOL> } ) <EOL> for badge_group in badges_dict . values ( ) : <EOL> badge_group . sort ( key = lambda o : o [ "<STR_LIT>" ] ) <EOL> return render_to_response ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : sorted ( badges_dict . items ( ) ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> def badge_detail ( request , slug , level ) : <EOL> badge = badges . _registry [ slug ] . levels [ int ( level ) ] <EOL> badge_awards = BadgeAward . objects . filter ( <EOL> slug = slug , <EOL> level = level <EOL> ) . order_by ( "<STR_LIT>" ) <EOL> badge_count = badge_awards . count ( ) <EOL> latest_awards = badge_awards [ : <NUM_LIT:50> ] <EOL> return render_to_response ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : badge , <EOL> "<STR_LIT>" : badge_count , <EOL> "<STR_LIT>" : latest_awards , <EOL> } , context_instance = RequestContext ( request ) ) </s>
