Ejemplo n.º 1
0
 def post(self):
     # Extract user who tends to like from jwt
     current_user = get_jwt_identity()
     # Create an object using request data
     view_object = view_schema.load(request.get_json())
     # Check if the target valid or not
     discovered_story = StoryModel.find_story_by_sid(view_object.target)
     if discovered_story is None or discovered_story.status == 'unapproved':
         return {'message': INVALID_REQUEST}, 400
     if current_user is not None:
         # Make an object of current user
         active_user_object = ActiveModel.find_entry_by_uid(current_user)
         # Check if already viewed or not
         if view_object.target not in [view.target for view in active_user_object.viewed]:
             view_object.source = current_user
             view_object.time = time.asctime(time.gmtime(time.time()))
             if view_object.create_entry() == ERROR_WRITING_VIEWS_TABLE:
                 return {'message': VIEW_UNSUCCESSFUL}, 500
             # Add the number of views by one
             StoryModel.add_views_by_one(view_object.target)
             ActiveModel.add_views_by_one(discovered_story.author.uid)
     # Use dump to create a dictionary
     story_data = story_schema.dump(discovered_story)
     # Pop status and add the name of the author
     story_data.pop('status')
     story_data.pop('author')
     story_data.pop('viewers')
     story_data.pop('fans')
     story_data['name'] = discovered_story.author.name
     story_data['uid'] = discovered_story.author.uid
     if current_user is not None:
         story_data['already_liked'] = view_object.target in [like.target for like in active_user_object.favourites]
     return story_data
Ejemplo n.º 2
0
 def delete(self):
     # Extract user who tends to like from jwt
     current_user = get_jwt_identity()
     # Create an object using request data
     unlike_object = like_schema.load(request.get_json())
     # Check target story is present and has valid status
     discovered_story = StoryModel.find_story_by_sid(unlike_object.target)
     if discovered_story is None or discovered_story.status == 'unapproved':
         return {'message': INVALID_REQUEST}, 500
     # Creating an for the requesting active user
     active_user_object = ActiveModel.find_entry_by_uid(current_user)
     # Check if the requesting user is the author of target story or not
     if unlike_object.target in [
             story.sid for story in active_user_object.submissions
     ]:
         return {'message': CANNOT_UNLIKE_OWN_STORY}, 400
     # Run a loop for checking and perform database operation
     for list_item in active_user_object.favourites:
         if list_item.target == unlike_object.target:
             if list_item.delete_entry() == ERROR_DELETING_LIKE_TABLE:
                 return {'message': UNLIKE_UNSUCCESSFUL}, 500
             else:
                 # Now reduce the number of likes by one for story as well as author
                 StoryModel.reduce_likes_by_one(unlike_object.target)
                 ActiveModel.reduce_likes_by_one(
                     discovered_story.author.uid)
                 return {'message': UNLIKE_SUCCESSFUL}, 200
     return {'message': NOT_LIKED}, 400
Ejemplo n.º 3
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
Ejemplo n.º 4
0
 def post(self):
     # Extract user who tends to like from jwt
     current_user = get_jwt_identity()
     # Create an object using request data
     like_object = like_schema.load(request.get_json())
     # Creating an for the requesting active user
     active_user_object = ActiveModel.find_entry_by_uid(current_user)
     # Check target story is present and has valid status
     discovered_story = StoryModel.find_story_by_sid(like_object.target)
     if discovered_story is None or discovered_story.status == 'unapproved':
         return {'message': INVALID_REQUEST}, 500
     # Check if the requesting user is the author of target story or not
     if like_object.target in [
             story.sid for story in active_user_object.submissions
     ]:
         return {'message': CANNOT_LIKE_OWN_STORY}, 400
     # Check if the requesting user has already liked the target story
     if like_object.target in [
             story.target for story in active_user_object.favourites
     ]:
         return {'message': ALREADY_LIKED}, 400
     # If valid then fill the object fields with remaining data
     like_object.source = current_user
     like_object.time = time.asctime(time.gmtime(time.time()))
     # Check for write errors and return appropriate messages
     if like_object.create_entry() == ERROR_WRITING_LIKE_TABLE:
         return {'message': LIKE_UNSUCCESSFUL}, 500
     # Now add the number of likes by one for story as well as author
     StoryModel.add_likes_by_one(like_object.target)
     ActiveModel.add_likes_by_one(discovered_story.author.uid)
     # Send a success message
     return {'message': LIKE_SUCCESSFUL}, 202