def post(self): # Get the latest story latest_story_object = StoryModel.force_find_latest_story_by_uid(get_jwt_identity()) current_time = time.time() epoch = datetime.datetime.utcfromtimestamp(0) # Check if there is a latest story or not if latest_story_object is not None: story_time = (latest_story_object.time - epoch).total_seconds() if (current_time-story_time) < 86400: return {'message': NOT_BEFORE_A_DAY}, 400 # Loaded incoming data into story story_object = story_schema.load(request.get_json(), db.session) # Check if the the genres is valid or not # Accepts 3 genres only genre_list = story_object.genre.split(',') if not (len(genre_list) <= 3 and set(genre_list).issubset(set(genres))): return {'message': INVALID_GENRE}, 400 # Check word length of various elements if StoryModel.words_counter(story_object.title) > 7: return {'message': TITLE_TOO_LONG}, 400 if StoryModel.words_counter(story_object.summary) > 80: return {'message': SUMMARY_TOO_LONG}, 400 if StoryModel.words_counter(story_object.story) > 10000: return {'message': STORY_TOO_LONG}, 400 # Adding time, uid and sid fields to story object story_object.time = time.asctime(time.gmtime(time.time())) story_object.uid = get_jwt_identity() story_object.sid = StoryModel.generate_fresh_sid() story_object.status = 'unapproved' story_object.views = 0 story_object.likes = 0 # Creating a new entry in story table and checking for success if story_object.create_story() == ERROR_WRITING_STORY_TABLE: return {'message': ERROR_SUBMITTING_STORY}, 500 return {'message': STORY_SUCCESSFULLY_SUBMITTED}, 200
def put(self): # Loaded incoming data into dict story_data = edit_schema.load(request.get_json()) # Find the target story discovered_story = StoryModel.find_story_by_sid(story_data['sid']) # First check existence and then check for valid status if (discovered_story is None) or (not StoryModel.check_story_status(discovered_story)): return {'message': INVALID_REQUEST}, 400 else: # Check the genre data genre_list = story_data['genre'].split(',') if not (len(genre_list) <= 3 and set(genre_list).issubset(set(genres))): return {'message': INVALID_GENRE}, 400 # Check word length of various elements if StoryModel.words_counter(story_data['title']) > 7: return {'message': TITLE_TOO_LONG}, 400 if StoryModel.words_counter(story_data['summary']) > 80: return {'message': SUMMARY_TOO_LONG}, 400 if StoryModel.words_counter(story_data['story']) > 10000: return {'message': STORY_TOO_LONG}, 400 # Now update the fields of the story discovered_story.story = story_data['story'] discovered_story.summary = story_data['summary'] discovered_story.title = story_data['title'] discovered_story.genre = story_data['genre'] if discovered_story.create_story() == ERROR_WRITING_STORY_TABLE: return {'message': ERROR_SUBMITTING_STORY}, 500 return {'message': STORY_SUCCESSFULLY_UPDATED}, 200
class EditSchema(Schema): sid = fields.String(required=True, validate=lambda c: len(c) == 8) story = fields.String( required=True, validate=lambda c: StoryModel.words_counter(c) <= 10000) summary = fields.String( required=True, validate=lambda c: StoryModel.words_counter(c) <= 80) title = fields.String(required=True, validate=lambda c: StoryModel.words_counter(c) <= 10) genre = fields.String(required=True, validate=lambda c: len(c.split(',')) <= 3 and set( c.split(',')).issubset(set(genres)))