def post(UserID): note = request.json["content"] topic = request.json["topic"] #RECEIVE DATA AS JSON common_obj = common() info = common.get_info(common_obj) info_dict = literal_eval(info) #CONVERT TO DICTIONARY base_path = info_dict["base_path"] #GETTING BASE PATH FROM config.txt DBconfig = info_dict["DBconfig"] #GETTING INFO TO CONFIGURE DB CONNECTION UIDs = common.getusers(common_obj, DBconfig, base_path) if UserID not in UIDs: return "404: ID not found", 404 Notes = common.getnotes(common_obj, DBconfig, base_path, UserID) if str(topic + ".txt") in Notes: return "404: Note already exists - to edit/delete please use the rewrite/delete functionality.", 404 Archives = common.getarchives(common_obj, DBconfig, base_path, UserID) if str(topic + ".txt") in Archives: return "404: Note with same filename exists in archives. Recommended: add \'(n)\' after filename where n is version number.", 404 new_entry_obj = New_Entry() New_Entry.new_note_in_db(new_entry_obj, UserID, str(topic + ".txt"), str(datetime.datetime.now())) New_Entry.close_connections(new_entry_obj) f = open((base_path + "Storage/" + UserID + "_notes/" + topic + ".txt"), "w") #NEW NOTE FILE f.write(note) f.close() return "201 : posting successful", 201
def recall_archive(UserID, topic): file_to_search = str(topic) + ".txt" #BASE DEFINITIONS common_obj = common() info = common.get_info(common_obj) info_dict = literal_eval(info) base_path = info_dict["base_path"] #GETTING BASE PATH FROM config.txt DBconfig = info_dict[ "DBconfig"] #GETTING INFO TO CONFIGURE DB CONNECTION UIDs = common.getusers(common_obj, DBconfig, base_path) if UserID not in UIDs: return "404: ID not found", 404 Archives = common.getarchives(common_obj, DBconfig, base_path, UserID) if file_to_search in Archives: new_entry_obj = New_Entry() New_Entry.delete_archive_in_db(new_entry_obj, UserID, str(topic + ".txt")) New_Entry.new_note_in_db(new_entry_obj, UserID, str(topic + ".txt"), str(datetime.datetime.now())) New_Entry.close_connections(new_entry_obj) src = base_path + "Storage/" + UserID + "_notes/Archives/" + file_to_search with gzip.open(src + ".gz", 'rb') as f_in, open(src, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) #UNZIP os.remove((base_path + "Storage/" + UserID + "_notes/Archives/" + file_to_search + ".gz")) dst = base_path + "Storage/" + UserID + "_notes/" + file_to_search shutil.move(src, dst) #MOVE TEXT FILE INTO USER FOLDER return "200: unzip successful", 200 else: return "404: file not found", 404
def create_user(): Uname = request.json["username"] Identification = request.json["userID"] common_obj = common() info = common.get_info(common_obj) info_dict = literal_eval(info) base_path = info_dict[ "base_path"] #GATHERING BASE PATH FROM config.txt DBconfig = info_dict[ "DBconfig"] #GETTING INFO TO CONFIGURE DB CONNECTION UIDs = common.getusers(common_obj, DBconfig, base_path) if Identification in UIDs: return "403: ID already exists, provide different ID", 403 else: new_entry_obj = New_Entry() New_Entry.new_user(new_entry_obj, Uname, Identification) #EXECUTING SQL New_Entry.close_connections(new_entry_obj) if not os.path.exists( (base_path + "Storage/" + Identification + "_notes/Archives")): os.makedirs((base_path + "Storage/" + Identification + "_notes/Archives")) #CREATE USER STORAGE return "201: User Created", 201 else: return "403: ID already exists, provide different ID", 403
def get(UserID): common_obj = common() info = common.get_info(common_obj) info_dict = literal_eval(info) #CONVERT TO DICTIONARY base_path = info_dict["base_path"] #GET BASE PATH FROM config.txt DBconfig = info_dict["DBconfig"] #GET INFO TO CONFIGURE DB CONNECTION UIDs = common.getusers(common_obj, DBconfig, base_path) if UserID not in UIDs: return "404: ID not found", 404 output = {} #DICTIONARY WILL BE CONVERTED TO JSON OUTPUT output['NOTES'] = [] output['ARCHIVES'] = [] Notes = common.getnoteswithdate(common_obj, DBconfig, base_path, UserID) for index in range(0, len(Notes), 2): f = open( (base_path + "Storage/" + UserID + "_notes/" + Notes[index]), "r") content = (f.read()).replace("\n", " ") f.close() note_output = { 'topic': Notes[index], 'datetime': Notes[index + 1], 'actual_note': content } output['NOTES'].append(note_output) Archives = common.getarchives(common_obj, DBconfig, base_path, UserID) for entry in Archives: output['ARCHIVES'].append(entry) return jsonify(output), 200
def prepend(UserID): addition = request.json["content"] topic = request.json["topic"] index = 0 file_found = False file_to_search = str(topic) + ".txt" common_obj = common() info = common.get_info(common_obj) info_dict = literal_eval(info) #CONVERT TO DICTIONARY base_path = info_dict[ "base_path"] #GATHERING BASE PATH FROM config.txt DBconfig = info_dict[ "DBconfig"] #GETTING INFO TO CONFIGURE DB CONNECTION UIDs = common.getusers(common_obj, DBconfig, base_path) if UserID not in UIDs: return "404: ID not found", 404 Notes = common.getnotes(common_obj, DBconfig, base_path, UserID) if file_to_search in Notes: f = open( (base_path + "Storage/" + UserID + "_notes/" + file_to_search), "r+") old = f.read() f.seek(0) f.write(addition + " " + old) f.close() return "201: file updated", 201 else: return "404: file not found", 404
def remove_user(UserID): common_obj = common() info = common.get_info(common_obj) info_dict = literal_eval(info) base_path = info_dict[ "base_path"] #GATHERING BASE PATH FROM config.txt DBconfig = info_dict[ "DBconfig"] #GETTING INFO TO CONFIGURE DB CONNECTION UIDs = common.getusers(common_obj, DBconfig, base_path) if UserID not in UIDs: return "404: ID does not exist", 404 new_entry_obj = New_Entry() New_Entry.delete_user(new_entry_obj, UserID) #EXECUTING SQL New_Entry.close_connections(new_entry_obj) shutil.rmtree((base_path + "Storage/" + UserID + "_notes")) return "201: User Deleted", 201
def delete_archive(UserID, topic): file_to_search = str(topic) + ".txt" common_obj = common() info = common.get_info(common_obj) info_dict = literal_eval(info) #CONVERT TO DICTIONARY base_path = info_dict[ "base_path"] #GATHERING BASE PATH FROM config.txt DBconfig = info_dict["DBconfig"] UIDs = common.getusers(common_obj, DBconfig, base_path) if UserID not in UIDs: return "404: ID not found", 404 Archives = common.getarchives(common_obj, DBconfig, base_path, UserID) if file_to_search in Archives: new_entry_obj = New_Entry() New_Entry.delete_archive_in_db(new_entry_obj, UserID, file_to_search) #EXECUTING SQL New_Entry.close_connections(new_entry_obj) os.remove((base_path + "Storage/" + UserID + "_notes/Archives/" + file_to_search + ".gz")) return "200 : Operation successful", 200 else: return "404 : file could not be found", 404