def put(self, story_id): """Updates properties of the given story.""" story = story_fetchers.get_story_by_id(story_id, strict=False) version = self.payload.get('version') self._require_valid_version(version, story.version) commit_message = self.payload.get('commit_message') if commit_message is None: raise self.InvalidInputException( 'Expected a commit message but received none.') if len(commit_message) > constants.MAX_COMMIT_MESSAGE_LENGTH: raise self.InvalidInputException( 'Commit messages must be at most %s characters long.' % constants.MAX_COMMIT_MESSAGE_LENGTH) change_dicts = self.payload.get('change_dicts') change_list = [ story_domain.StoryChange(change_dict) for change_dict in change_dicts ] try: # Update the Story and its corresponding TopicSummary. topic_services.update_story_and_topic_summary( self.user_id, story_id, change_list, commit_message, story.corresponding_topic_id) except utils.ValidationError as e: raise self.InvalidInputException(e) story_dict = story_fetchers.get_story_by_id(story_id).to_dict() self.values.update({ 'story': story_dict }) self.render_json(self.values)
def post(self, topic_id): """Handles POST requests. Currently, this only adds the story to the canonical story id list of the topic. """ title = self.normalized_payload.get('title') description = self.normalized_payload.get('description') thumbnail_filename = self.normalized_payload.get('filename') thumbnail_bg_color = self.normalized_payload.get('thumbnailBgColor') raw_image = self.normalized_request.get('image') story_url_fragment = self.normalized_payload.get('story_url_fragment') story_domain.Story.require_valid_title(title) if story_services.does_story_exist_with_url_fragment( story_url_fragment): raise self.InvalidInputException( 'Story url fragment is not unique across the site.') new_story_id = story_services.get_new_story_id() # Add the story id to canonical_story_ids in the topic. # Topic validation occurs right before the field is updated. If there # is a validation failure, the story id will not be added to the # canonical_story_ids field in the Topic and the Story model does not # get created. Hence, topic_services.add_canonical_story is called # before story_services.save_new_story. topic_services.add_canonical_story(self.user_id, topic_id, new_story_id) story = story_domain.Story.create_default_story( new_story_id, title, description, topic_id, story_url_fragment) story_services.save_new_story(self.user_id, story) try: file_format = ( image_validation_services.validate_image_and_filename( raw_image, thumbnail_filename)) except utils.ValidationError as e: raise self.InvalidInputException(e) entity_id = new_story_id filename_prefix = 'thumbnail' image_is_compressible = (file_format in feconf.COMPRESSIBLE_IMAGE_FORMATS) fs_services.save_original_and_compressed_versions_of_image( thumbnail_filename, feconf.ENTITY_TYPE_STORY, entity_id, raw_image, filename_prefix, image_is_compressible) topic_services.update_story_and_topic_summary( self.user_id, new_story_id, [ story_domain.StoryChange({ 'cmd': 'update_story_property', 'property_name': 'thumbnail_filename', 'old_value': None, 'new_value': thumbnail_filename }), story_domain.StoryChange({ 'cmd': 'update_story_property', 'property_name': 'thumbnail_bg_color', 'old_value': None, 'new_value': thumbnail_bg_color }), ], 'Added story thumbnail.', topic_id) self.render_json({'storyId': new_story_id})