def post(self): result = Authentication.isAuthenticated() if (result == None): return make_response(jsonify({"status": "You are not Logged In"}), 401) profile_id = result['profile_id'] try: if request.method == 'POST': # check if the post request has the image part if 'image' not in request.files: print('No image part') abort(404) image = request.files['image'] # if user does not select image, browser also # submit a empty part without filename if image.filename == '': print('No image part') abort(404) if image and allowed_file(image.filename): filename = secure_filename(image.filename) unique_filename = str( uuid.uuid4()) + "." + filename.rsplit('.', 1)[1] image.save( os.path.join(settings.UPLOAD_FOLDER, unique_filename)) DatabaseConnection.callprocONE( "NewImage", (profile_id, image.filename, unique_filename)) DatabaseConnection.commit() return make_response( jsonify({"status": "Image Uploaded Successfully"}), 201) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def put(self): if not request.json: return make_response(jsonify({"status": "Bad Request"}), 400) # bad request parser = reqparse.RequestParser() parser.add_argument('display_name') args = parser.parse_args() try: # Check Authenticated result = Authentication.isAuthenticated() if (result == None): return make_response( jsonify({"status": "You are not Logged In"}), 401) profile_id = result['profile_id'] DatabaseConnection.callprocONE("UpdateProfile", (profile_id, args['display_name'])) DatabaseConnection.commit() except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500) return make_response(jsonify({"status": "Profile Updated"}), 202)
def delete(self): parser = reqparse.RequestParser() parser.add_argument('image_id') args = parser.parse_args() try: # Check Authenticated result = Authentication.isAuthenticated() if (result == None): return make_response( jsonify({"status": "You are not Logged In"}), 401) profile_id = result['profile_id'] result = DatabaseConnection.callprocONE("GetImageUsage", (args['image_id'], "")) if (result['numPosts'] == 0): DatabaseConnection.callprocONE("DeleteImage", (args['image_id'], "")) DatabaseConnection.commit() return make_response( jsonify({"status": "Image Delete Successfully"}), 200) else: return make_response( jsonify({"status": "Image is being referenced by a Post"}), 400) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def delete(self): parser = reqparse.RequestParser() parser.add_argument('post_id') args = parser.parse_args() try: # Check Authenticated result = Authentication.isAuthenticated() if (result == None): abort(401, "Unauthorised") profile_id = result['profile_id'] # Delete all foreign key references prior to deleting the post DatabaseConnection.callprocONE("DeleteAllStars", (args['post_id'], "")) DatabaseConnection.callprocONE("DeleteAllTags", (args['post_id'], "")) DatabaseConnection.callprocONE("DeletePost", (args['post_id'], "")) DatabaseConnection.commit() return make_response(jsonify({"status": "Post has been deleted"}), 201) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def get(self): parser = reqparse.RequestParser() parser.add_argument('profile_id') args = parser.parse_args() try: profile_id = args['profile_id'] # If profile_id is not defined return current users profile if args['profile_id'] == None: # Check Authentication result = Authentication.isAuthenticated() if (result == None): return make_response( jsonify({"status": "You are not Logged In"}), 401) profile_id = result['profile_id'] # Get profile result = DatabaseConnection.callprocONE("GetProfile", (profile_id, "")) # If null then 404 if (result == None): return make_response(jsonify({"status": "Profile Not Found"}), 404) else: return make_response(jsonify({"profile": result}), 200) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def getStarredPosts(): try: result = Authentication.isAuthenticated() profile_id = result['profile_id'] if (profile_id == None): return {"result": {"status", "You are not Logged In"}, "code": 402} # Gets a list of starred posts result = DatabaseConnection.callprocALL("GetStarredPosts", (profile_id, "")) if (result == None): {"result": {"status": "Posts Do not Exist"}, "code": 400} return {"result": {"posts": result}, "code": 200} except: return {"result": {"status": "Internal Server Error"}, "code": 500}
def GetFollowedPosts(): try: result = Authentication.isAuthenticated() profile_id = result['profile_id'] if (profile_id == None): abort(401, "Unauthorised") result = DatabaseConnection.callprocALL("GetFollowedPosts", (profile_id, "")) if (result == None): return make_response(jsonify({"message": "Posts Do not Exist"}), 404) return make_response(jsonify({"posts": result}), 200) except: return make_response(jsonify({"status": "Internal Server Error"}), 500)
def post(self): parser = reqparse.RequestParser() parser.add_argument('followed_profile_id') args = parser.parse_args() try: # Check Authenticated result = Authentication.isAuthenticated() if(result == None): return make_response(jsonify({"status": "You are not Logged In"}), 401) profile_id = result['profile_id'] result = DatabaseConnection.callprocONE("CreateFollow", (args['followed_profile_id'], profile_id)) DatabaseConnection.commit() return make_response(jsonify({"status": "Successfully Followed Someone"}), 201) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def GetFollowers(): parser = reqparse.RequestParser() parser.add_argument('profile_id') args = parser.parse_args() profile_id = args['profile_id'] try: if(profile_id == None): # Check Authenticated result = Authentication.isAuthenticated() if(result == None): return make_response(jsonify({"status": "You are not Logged In"}), 401) profile_id = result['profile_id'] results = DatabaseConnection.callprocALL("GetFollowers", (profile_id, "")) return make_response(jsonify({"followers":results}), 200) except: return make_response(jsonify({"status": "Internal Server Error"}), 500)
def delete(self): parser = reqparse.RequestParser() parser.add_argument('comment_id') args = parser.parse_args() try: # Check Authenticated result = Authentication.isAuthenticated() if (result == None): return make_response( jsonify({"status": "You are not Logged In"}), 401) profile_id = result['profile_id'] DatabaseConnection.callprocONE("DeleteComment", (args['comment_id'], "")) DatabaseConnection.commit() return make_response( jsonify({"status": "Comment has been deleted"}), 200) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def put(self): parser = reqparse.RequestParser() parser.add_argument('post_id') parser.add_argument('title') parser.add_argument('description') parser.add_argument('tags', action='append') args = parser.parse_args() try: # Check Authenticated result = Authentication.isAuthenticated() if (result == None): abort(401, "Unauthorised") # Update the post DatabaseConnection.callprocONE( "UpdatePost", (args['post_id'], args['title'], args['description'])) DatabaseConnection.commit() except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500) # Update the tags # First Get a list of the current tags on the post # Then get a list of the new tags being put on the post # then determine the tags that are new and deleted from the post # Then loop through the new tags create tags that dont exist and attach to DB # Then loop through deleted tags and remove them from the post try: # Get all tags from the current post result = DatabaseConnection.callprocALL("GetTags", (args['post_id'], "")) currentTags = [] for tag in result: currentTags.append(tag['id']) # Get the new and removed tags from the args list newTags = [] removedTags = [] for tag in args['tags']: try: tag_id = -1 # Get tag if it exists result = DatabaseConnection.callprocONE( "GetTagID", (tag, "")) if (result == None): # Tag doesnt exist create it result = DatabaseConnection.callprocONE( "CreateTag", (tag, "")) tag_id = result['LAST_INSERT_ID()'] else: # tag exists set tag_id tag_id = result['id'] # If tag_id is not currently on the post add it if tag_id not in currentTags: DatabaseConnection.callprocONE( "AddTags", (args['post_id'], tag_id)) # Append all tags to the new tags array (it is the new list on the DB) newTags.append(tag_id) except: tag_id = -1 # Loop through the old current tags on the post for tag_id in currentTags: try: # If the current tag from the before update post does not exist in new tags remove it from the post if tag_id not in newTags: DatabaseConnection.callprocONE( "DeleteTags", (args['post_id'], tag_id)) except: tag_id = -1 DatabaseConnection.commit() return redirect(settings.APP_HOST + ":" + str(settings.APP_PORT) + "/post?post_id=" + str(args['post_id']), code=302) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def post(self): if not request.json: return make_response(jsonify({"status": "Bad Request"}), 400) # bad request parser = reqparse.RequestParser() parser.add_argument('image_id') parser.add_argument('title') parser.add_argument('description') parser.add_argument('tags', action='append') args = parser.parse_args() try: #Check Authenticated result = Authentication.isAuthenticated() if (result == None): abort(401, "Unauthorised") profile_id = result['profile_id'] # Check if all required args are present if args['image_id'] == None or args['title'] == None or args[ 'description'] == None: return make_response(jsonify({"status": "Bad Request"}), 400) # Create the post result = DatabaseConnection.callprocONE( "NewPost", (profile_id, args['image_id'], args['title'], args['description'])) # Get the new post ID. If it is None there was an error if result['LAST_INSERT_ID()'] == None: return make_response(jsonify({"status": "Bad Request"}), 400) # Commit the changes to the DB DatabaseConnection.commit() # Organize and Init the tags post_id = result['LAST_INSERT_ID()'] # For each tag which was passed in: # Check if it exists: # if not create # Add the tag ID to the post for tag in args['tags']: try: # Get tag if it exists in the DB already result = DatabaseConnection.callprocONE( "GetTagID", (tag, "")) # if result is None then the tag is not yet present if (result == None): #create tag result = DatabaseConnection.callprocONE( "CreateTag", (tag, "")) #get that tags new id if result['LAST_INSERT_ID()'] == None: return make_response( jsonify({"status": "Bad Request"}), 400) tag_id = result['LAST_INSERT_ID()'] else: tag_id = result['id'] # tag_id is now the tag to be added to the post # add the tag to the post DatabaseConnection.callprocONE("AddTags", (post_id, tag_id)) DatabaseConnection.commit() except: # If we end up here we encountered a problem with one of the tags # So it skips that tag and continues tag_id = -1 # When the post is fully built redirect the user to their new post return redirect(settings.APP_HOST + ":" + str(settings.APP_PORT) + "/post?post_id=" + str(post_id), code=302) except: # There was an issue if we got here... DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)