<s> import json <EOL> from django . db . models import Count <EOL> from django . http import HttpResponse <EOL> from polls . models import Question , Choice , Vote <EOL> from polls . resource import Action , Attribute , Resource , CollectionResource , SingleObjectMixin <EOL> from polls . features import can_create_question , can_delete_question , can_vote_choice <EOL> class RootResource ( Resource ) : <EOL> uri = '<STR_LIT:/>' <EOL> def get_relations ( self ) : <EOL> return { <EOL> '<STR_LIT>' : QuestionCollectionResource ( ) , <EOL> } <EOL> def can_embed ( self , relation ) : <EOL> return False <EOL> class QuestionResource ( Resource , SingleObjectMixin ) : <EOL> model = Question <EOL> def get_uri ( self ) : <EOL> return '<STR_LIT>' . format ( self . get_object ( ) . pk ) <EOL> def get_attributes ( self ) : <EOL> question = self . get_object ( ) <EOL> return { <EOL> '<STR_LIT>' : question . question_text , <EOL> '<STR_LIT>' : question . published_at . isoformat ( ) , <EOL> } <EOL> def get_relations ( self ) : <EOL> choices = self . get_object ( ) . choices . annotate ( vote_count = Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def choice_resource ( choice ) : <EOL> resource = ChoiceResource ( ) <EOL> resource . obj = choice <EOL> resource . request = getattr ( self , '<STR_LIT>' , None ) <EOL> return resource <EOL> return { <EOL> '<STR_LIT>' : map ( choice_resource , list ( choices ) ) , <EOL> } <EOL> def get_actions ( self ) : <EOL> actions = { } <EOL> if can_delete_question ( self . get_object ( ) , self . request ) : <EOL> actions [ '<STR_LIT>' ] = Action ( method = '<STR_LIT>' , attributes = None ) <EOL> return actions <EOL> def delete ( self , request , * args , ** kwargs ) : <EOL> if not can_delete_question ( self . get_object ( ) , request ) : <EOL> return self . http_method_not_allowed ( request ) <EOL> self . get_object ( ) . delete ( ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> class ChoiceResource ( Resource , SingleObjectMixin ) : <EOL> model = Choice <EOL> def get_uri ( self ) : <EOL> choice = self . get_object ( ) <EOL> return '<STR_LIT>' . format ( choice . question . pk , choice . pk ) <EOL> def get_attributes ( self ) : <EOL> choice = self . get_object ( ) <EOL> if not hasattr ( choice , '<STR_LIT>' ) : <EOL> choice . vote_count = choice . votes . count ( ) <EOL> return { <EOL> '<STR_LIT>' : choice . choice_text , <EOL> '<STR_LIT>' : choice . vote_count , <EOL> } <EOL> def get_actions ( self ) : <EOL> actions = { } <EOL> if can_vote_choice ( self . request ) : <EOL> actions [ '<STR_LIT>' ] = Action ( method = '<STR_LIT:POST>' , attributes = None ) <EOL> return actions <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if not can_vote_choice ( self . request ) : <EOL> return self . http_method_not_allowed ( request ) <EOL> self . get_object ( ) . vote ( ) <EOL> response = self . get ( request ) <EOL> response . status_code = <NUM_LIT> <EOL> return response <EOL> class QuestionCollectionResource ( CollectionResource ) : <EOL> resource = QuestionResource <EOL> model = Question <EOL> relation = '<STR_LIT>' <EOL> uri = '<STR_LIT>' <EOL> def get_actions ( self ) : <EOL> actions = { } <EOL> if can_create_question ( self . request ) : <EOL> actions [ '<STR_LIT>' ] = Action ( method = '<STR_LIT:POST>' , attributes = ( <EOL> Attribute ( name = '<STR_LIT>' , category = '<STR_LIT:text>' ) , <EOL> Attribute ( name = '<STR_LIT>' , category = '<STR_LIT>' ) , <EOL> ) ) <EOL> return actions <EOL> def post ( self , request ) : <EOL> if not can_create_question ( self . request ) : <EOL> return self . http_method_not_allowed ( request ) <EOL> body = json . loads ( request . body ) <EOL> question_text = body . get ( '<STR_LIT>' ) <EOL> choices = body . get ( '<STR_LIT>' ) <EOL> if not question_text or not isinstance ( choices , list ) : <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> question , created = self . get_or_create ( question_text , choices ) <EOL> resource = self . resource ( ) <EOL> resource . obj = question <EOL> resource . request = request <EOL> response = resource . get ( request ) <EOL> if created : <EOL> response . status_code = <NUM_LIT> <EOL> response [ '<STR_LIT>' ] = resource . get_uri ( ) <EOL> return response <EOL> def create_question ( self , question_text , choice_texts ) : <EOL> question = Question ( question_text = question_text ) <EOL> question . save ( ) <EOL> for choice_text in choice_texts : <EOL> Choice ( question = question , choice_text = choice_text ) . save ( ) <EOL> return question <EOL> def get_or_create ( self , question_text , choice_texts ) : <EOL> try : <EOL> question = Question . objects . filter ( question_text = question_text ) . first ( ) <EOL> except Question . DoesNotExist : <EOL> question = None <EOL> if question : <EOL> choices = map ( lambda c : c . choice_text , question . choices . order_by ( '<STR_LIT>' ) ) <EOL> if choices == sorted ( choice_texts ) : <EOL> return ( question , False ) <EOL> return ( self . create_question ( question_text , choice_texts ) , True ) </s>
