def recover_password(): data = json.loads(request.data) username = data.get('username', None) if (not username or username == ""): return jsonify({ "RESULT": "No ha brindado toda la informacion necesaria", "RETURNCODE": "-1", "METHOD": "POST" }) user = UsersHandler.getUserByUsername(username) if (user == None): return jsonify({ "RESULT": f"No existe ningun usuario con username {username}", "RETURNCODE": "1", "METHOD": "POST" }) else: return jsonify({ "RESULT": user.password, "RETURNCODE": "0", "METHOD": "POST" })
def remove_user(): data = json.loads(request.data) username = data.get('username', None) if (not username or username == ""): return jsonify({ "RESULT": "No ha brindado toda la informacion necesaria", "RETURNCODE": "-1", "METHOD": "POST" }) if (username == "admin"): return jsonify({ "RESULT": "No se puede borrar al usuario maestro", "RETURNCODE": "1", "METHOD": "POST" }) result = UsersHandler.removeUserByUsername(username) if result: return jsonify({ "RESULT": f"Usuario {username} eliminado correctamente", "RETURNCODE": "0", "METHOD": "POST" }) else: return jsonify({ "RESULT": f"No hay usuario con username {username}", "RETURNCODE": "2", "METHOD": "POST" })
def register_user(): data = json.loads(request.data) name = data.get('name', None) lastname = data.get('lastname', None) username = data.get('username', None) password = data.get('password', None) list = [name, lastname, username, password] for element in list: if (not element or element == ""): return jsonify({ "RESULT": "No ha brindado toda la informacion necesaria", "RETURNCODE": "-1", "METHOD": "POST" }) newUser = User(name, lastname, username, password, "client") result = UsersHandler.addUser(newUser) if result: return jsonify({ "RESULT": f"Usuario {username} Registrado con exito", "RETURNCODE": "0", "METHOD": "POST" }) else: return jsonify({ "RESULT": f"Ya existe un usario con username {username}", "RETURNCODE": "1", "METHOD": "POST" })
def search_users(): data = json.loads(request.data) username = data['username'] exactMatch = data['exactMatch'] if (username == None): return jsonify({ "RESULT": f"No se brindo un username para buscar", "RETURNCODE": "-1", "METHOD": "POST" }) foundUsers = UsersHandler.searchUsers(username, exactMatch) if (len(foundUsers) == 0): return jsonify({ "RESULT": f"No se encontro ningun usuario con los criterios dados", "RETURNCODE": "2", "METHOD": "POST" }) asList = UsersHandler.getUsersAsList(foundUsers) print(jsonify(asList)) return jsonify({"RESULT": asList, "RETURNCODE": "0", "METHOD": "POST"})
def addComment(): data = json.loads(request.data) author = data.get('author', None) body = data.get('body', None) recipeAuthor = data.get('recipeAuthor', None) recipeTitle = data.get('recipeTitle', None) list = [author, body, recipeAuthor, recipeTitle] for element in list: if (not element or element == ""): return jsonify({ "RESULT": "No ha brindado toda la informacion necesaria", "RETURNCODE": "-1", "METHOD": "POST" }) recipe = RecipesHandler.getRecipeByAuthorAndTitle(recipeAuthor, recipeTitle) if (recipe == None): return jsonify({ "RESULT": "No existe esa combinacion autor-receta", "RETURNCODE": "1", "METHOD": "POST" }) date = datetime.now().strftime("%d-%m-%Y %H:%M:%S") theAuthor = UsersHandler.getUserByUsername(author) if (theAuthor == None): return jsonify({ "RESULT": "No existe el autor del comentario", "RETURNCODE": "2", "METHOD": "POST" }) print("HERE IS THE AUTHOR FOR COMMENT", theAuthor.type) recipe.addComment(author, theAuthor.type, body, date) return jsonify({ "RESULT": "Comentario realizado correctamente", "RETURNCODE": "0", "METHOD": "POST" })
def modify_user(): data = json.loads(request.data) pastUsername = data.get('pastUsername', None) name = data.get('name', None) lastname = data.get('lastname', None) username = data.get('username', None) password = data.get('password', None) list = [pastUsername, name, lastname, username, password] for element in list: if (not element or element == ""): return jsonify({ "RESULT": "No ha brindado toda la informacion necesaria", "RETURNCODE": "-1", "METHOD": "POST" }) newUser = User(name, lastname, username, password) result = UsersHandler.modifyUser(pastUsername, newUser) if result == 0: return jsonify({ "RESULT": f"Usuario {pastUsername} modificado con exito", "RETURNCODE": "0", "METHOD": "POST" }) elif result == 2: return jsonify({ "RESULT": f"Un usuario ya existe con el nuevo username {username}", "RETURNCODE": "2", "METHOD": "POST" }) elif result == 3: return jsonify({ "RESULT": f"No existe un usario con username {pastUsername}", "RETURNCODE": "3", "METHOD": "POST" })
def login(): data = json.loads(request.data) username = data.get('username', None) password = data.get('password', None) response = {} if (not username or username == "" or not password or password == ""): return jsonify({ "RESULT": "No ha brindado toda la informacion necesaria", "RETURNCODE": "-1", "METHOD": "POST" }) result = UsersHandler.login(username, password) if result == 0: response = jsonify({ "RESULT": f"Iniciado sesion correctamente con {username}, bienvenido", "USERTYPE": UsersHandler.loggedUser.type, "RETURNCODE": "0", "METHOD": "POST" }) elif result == 1: response = jsonify({ "RESULT": f"No se ha encontrado ningun usuario con username {username}", "RETURNCODE": "1", "METHOD": "POST" }) elif result == 2: response = jsonify({ "RESULT": f"La contrasena ingresada para {username} no coincide con la registrada en el sistema", "RETURNCODE": "2", "METHOD": "POST" }) #response.headers.add('Access-Control-Allow-Origin', '*') return response
user = UsersHandler.getUserByUsername(username) if (user == None): return jsonify({ "RESULT": f"No existe ningun usuario con username {username}", "RETURNCODE": "1", "METHOD": "POST" }) else: return jsonify({ "RESULT": user.password, "RETURNCODE": "0", "METHOD": "POST" }) @app.route('/') def index(): return jsonify({ "MESSAGE": "BackEnd Python CUK", "RETURNCODE": "0", "METHOD": "POST" }) if __name__ == '__main__': # Threaded option to enable multiple instances for multiple user access support UsersHandler.addUser(User("Usuario", "Maestro", "admin", "admin", "admin")) app.run(threaded=True, port=5000)