def get(self, unused_exploration_id, playthrough_id): """Handles GET requests.""" playthrough = stats_services.get_playthrough_by_id(playthrough_id) if playthrough is None: raise self.PageNotFoundException('Invalid playthrough ID %s' % (playthrough_id)) self.render_json(playthrough.to_dict())
def post(self, exploration_id): """Handles POST requests. Appends to existing list of playthroughs or deletes it if already full. Args: exploration_id: str. The ID of the exploration. """ playthrough_data = self.payload.get('playthrough_data') try: unused_playthrough = stats_domain.Playthrough.from_backend_dict( playthrough_data) except utils.ValidationError as e: raise self.InvalidInputException(e) try: self.current_issue_schema_version = self.payload[ 'issue_schema_version'] except KeyError as e: raise self.InvalidInputException(e) try: self.current_playthrough_id = self.payload['playthrough_id'] except KeyError as e: raise self.InvalidInputException(e) exp_version = playthrough_data['exp_version'] exp_issues_model = stats_models.ExplorationIssuesModel.get_model( exploration_id, exp_version) self.current_exp_issues = stats_services.get_exp_issues_from_model( exp_issues_model) playthrough = stats_domain.Playthrough.from_dict(playthrough_data) # If playthrough already exists, update it in the datastore. if self.current_playthrough_id is not None: orig_playthrough = stats_services.get_playthrough_by_id( self.current_playthrough_id) if orig_playthrough.issue_type != playthrough.issue_type: self._move_playthrough_to_correct_issue( playthrough, orig_playthrough) stats_services.update_playthroughs_multi( [self.current_playthrough_id], [playthrough]) stats_services.save_exp_issues_model_transactional( self.current_exp_issues) self.render_json({}) return payload_return = {'playthrough_stored': True} playthrough_id = None try: playthrough_id = self._assign_playthrough_to_issue(playthrough) except Exception: payload_return['playthrough_stored'] = False stats_services.save_exp_issues_model_transactional( self.current_exp_issues) payload_return['playthrough_id'] = playthrough_id self.render_json(payload_return)