Пример #1
0
 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
Пример #2
0
 def delete(self):
     # Extract and parse the data from request
     delete_data = delete_schema.load(request.get_json())
     # Create an object of requesting user
     active_user_object = ActiveModel.find_entry_by_uid(get_jwt_identity())
     # Check if the story is present in submissions and has valid status
     for story in active_user_object.submissions:
         if story.sid == delete_data['sid'] and StoryModel.check_story_status(story):
             # Set counter to zero
             ctr = 0
             # Delete all likes
             for fan in story.fans:
                 ctr += 1
                 if fan.delete_entry() == ERROR_DELETING_LIKE_TABLE:
                     return {'message': OPERATION_UNSUCCESSFUL}
             # Decrease the total likes of the user
             active_user_object.likes -= ctr
             # Set counter to zero
             ctr = 0
             # Delete all views
             for viewer in story.viewers:
                 ctr += 1
                 if viewer.delete_entry() == ERROR_DELETING_VIEWS_TABLE:
                     return {'message': OPERATION_UNSUCCESSFUL}
             # Decrease the total views of the user
             active_user_object.views -= ctr
             # Commit changes done to active user
             if active_user_object.create_active_user() == ERROR_WRITING_ACTIVE_TABLE:
                 return {'message': OPERATION_UNSUCCESSFUL}
             # Delete the story
             if story.delete_story() == ERROR_DELETING_STORY_TABLE:
                 return {'message': OPERATION_UNSUCCESSFUL}
             return {'message': OPERATION_SUCCESSFUL}
     return {'message': INVALID_REQUEST}