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 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 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 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 get(self): parser = reqparse.RequestParser() parser.add_argument('post_id') parser.add_argument('profile_id') args = parser.parse_args() try: if args['post_id'] == None and args['profile_id'] == None: return make_response(jsonify({"status": "Bad Request"}), 400) if args['post_id'] != None: result = DatabaseConnection.callprocONE( "GetPost", (args['post_id'], "")) if (result == None): return make_response( jsonify({"message": "Posts Do not Exist"}), 404) return make_response(jsonify({"post": result}), 200) if args['profile_id'] != None: result = DatabaseConnection.callprocALL( "GetUserPosts", (args['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 delete(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'] DatabaseConnection.callprocONE("UnFollow", (args['followed_profile_id'], profile_id)) DatabaseConnection.commit() return make_response(jsonify({"status": "Successfully UnFollowed"}), 200) 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 put(self): parser = reqparse.RequestParser() parser.add_argument('comment_id') parser.add_argument('comment_body') args = parser.parse_args() try: # Check Authenticated result = Authentication.isAuthenticated() if (result == None): return make_response( jsonify({"status": "You are not Logged In"}), 401) DatabaseConnection.callprocONE( "UpdateComment", (args['comment_id'], args['comment_body'])) DatabaseConnection.commit() return make_response( jsonify({"status": "Comment has been updated"}), 200) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def post(self): parser = reqparse.RequestParser() parser.add_argument('post_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'] # Create star link DatabaseConnection.callprocONE("CreateStar", (args['post_id'], profile_id)) DatabaseConnection.commit() return make_response( jsonify({"status": "Successfully starred a post"}), 201) except: DatabaseConnection.rollback() return make_response(jsonify({"status": "Internal Server Error"}), 500)
def isAuthenticated(): if 'username' in session: #Hit DB get the profile_id result = DatabaseConnection.callprocONE('GetProfileID', (session['username'], "")) #if the DB doesnt have one set it to "" if (result == None): return None #return object for use in the calling function return { "profile_id": result['id'], "username": session['username'] } else: #Not authenticated returns None return None
def getImage(image_id): result = DatabaseConnection.callprocONE("GetImage", (image_id, "")) return result['uri']
def post(self): if not request.json: abort(400) # bad request # Parse the json parser = reqparse.RequestParser() try: # Check for required attributes in json document, create a dictionary parser.add_argument('username', type=str, required=True) parser.add_argument('password', type=str, required=True) request_params = parser.parse_args() except: abort(400) # bad request # Already logged in if request_params['username'] in session: response = {'status': 'success'} responseCode = 200 else: try: ldapServer = Server(host=settings.LDAP_HOST) ldapConnection = Connection( ldapServer, raise_exceptions=True, user='******' + request_params['username'] + ', ou=People,ou=fcs,o=unb', password=request_params['password']) ldapConnection.open() ldapConnection.start_tls() ldapConnection.bind() # At this point we have sucessfully authenticated. session['username'] = request_params['username'] response = {'status': 'success'} responseCode = 201 except (LDAPException, error_message): response = {'status': 'Access denied'} responseCode = 403 finally: ldapConnection.unbind() try: if responseCode == 200 or responseCode == 201: result = DatabaseConnection.callprocONE( "GetProfileID", (session['username'], "")) if result == None: try: result = DatabaseConnection.callprocONE( "NewProfile", (session['username'], "")) profile_id = result['LAST_INSERT_ID()'] DatabaseConnection.commit() except: DatabaseConnection.rollback() return make_response( jsonify({"status": "Unknown error has occurred"}), 500) else: profile_id = result['id'] return redirect(settings.APP_HOST + ":" + str(settings.APP_PORT) + "/profile?profile_id=" + str(profile_id), code=302) except: DatabaseConnection.rollback() print("Profile Not Created") abort(500) return make_response(jsonify(response), responseCode)
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)