def get_tasks(req: Request): try: user = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) if bool(req.obj.get("getAll", False)): tasks = base.get_all_tasks(user) else: print("(webserver: get_tasks)", req.obj) tasks = base.get_tasks(user=user, task_set=None, id_list=req.obj.get("id", []), general=req.obj.get("general", ""), precise=bool(req.obj.get("precise", False)), title=req.obj.get("title", ""), description=req.obj.get("description", ""), date_created=req.obj.get("dateCreated", ""), due_date=req.obj.get("dueDate", ""), deadline=req.obj.get("deadline", "")) response = Response(body=tasks) except base.utils.AuthenticationError: response = error_response(401) except Exception as e: respone = Response(500, str(e)) finally: return response
def api_get_server(req: Request): try: name = req.obj.get("name", "NOTANAME") except AttributeError: name = "NOTANAME" if name == "NOTANAME": response = error_response(406, "'name'") else: try: req_server = base.get_server(name=name) if req_server != None: print("SERVER FOUND!") del req_server["password"] req_server["components"] = json.loads( req_server["components"]) for key, value in req_server["components"].items(): value["name"] = key response = Response(body=req_server) else: print(serverly.error_response_templates) response = error_response(404) except Exception as e: logger.handle_exception(e) response = Response(500, body=str(e)) return response
def login_user(req: Request): try: base.authenticate(req.obj.get("username"), req.obj.get("password")) response = Response() except base.utils.AuthenticationError: response = error_response(401) except Exception as e: serverly.logger.handle_exception(e) response = Response(500, str(e)) return response
def get_user(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) user = base.get_user(uao) response = Response(body=user) except base.utils.AuthenticationError: response = error_response(401) except base.utils.UserDoesNotExist: response = Response(404, body="User not found.") return response
def reset_tasks_for_user(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) base.delete_all_tasks(uao) response = Response() except base.utils.AuthenticationError: response = error_response(401) except base.utils.UserDoesNotExist: response = Response(404, body="User not found.") return response
def newsletter_unsubscribe(req: Request): try: username = req.obj["username"] base.mailservice.unsubscribe_newsletter(username) response = Response(body="Unsubscribed successfully") except KeyError: error_response(406) except Exception as e: serverly.logger.handle_exception(e) response = Response(500, body=str(e)) return response
def stats_say_hello(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) base.statistics.register_user_activity(uao) response = Response() except KeyError: response = error_response(406, "username", "password") except base.utils.AuthenticationError: response = error_response(401) except Exception as e: serverly.logger.handle_exception(e) response = Response(500, body=str(e)) return response
def newsletter_subscribe(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) base.mailservice.subcribe_to_newsletter(uao) response = Response(body="Subscribe successfully") except KeyError: return error_response(406, "username", "password") except base.utils.AuthenticationError: return error_response(401) except Exception as e: serverly.logger.handle_exception(e) response = Response(500, body=str(e)) return response
def api_register_server(req: Request): try: base.register_server(req.obj["name"], req.obj.get("description", None), req.obj.get( "repoURL", None), req.obj.get("mainStatus", None), req.obj.get("components", None), req.obj["password"]) create_server_details_page(base.get_server(req.obj["name"])) response = Response(201, body="Successfully registered server.") except KeyError: response = error_response(406, "'name'", "'password'") except (err.NameAlreadyUsedError, err.RepoURLAlreadyUsedError) as e: response = Response(406, body=str(e)) except Exception as e: logger.handle_exception(e) response = Response(500, body=type(e).__name__ + ": " + str(e)) return response
def send_user_data(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) base.mailservice.send_user_req.obj(uao) response = Response(body="Sent data.") except KeyError: return error_response(406, "username", "password") except base.utils.AuthenticationError: return error_response(401) except Exception as e: serverly.logger.handle_exception(e) reponse = Response(500, body=str(e)) return response
def register_new_user(req: Request): try: base.create_user(req.obj["username"], req.obj["firstName"], req.obj["lastName"], req.obj["password"], req.obj["email"]) response = Response(201) except KeyError: response = error_response(406, "username", "firstName", "lastName", "password", "email") except (base.utils.UserAlreadyExists, base.utils.EmailAlreadyLinked) as e: response = Response(406, str(e)) except Exception as e: response = Response(500, body=str(e)) return response
def stats_get_sessions(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) sessions = base.statistics.get_sessions(uao) response = Response(body=sessions) except KeyError: response = error_response(406, "username", "password") except base.utils.AuthenticationError: response = error_response(401) except Exception as e: serverly.logger.handle_exception(e) response = Response(500, str(e)) return response
def api_update_status(req: Request): try: components = req.obj.get("components", None) print("components:", components, type(components)) base.update_server(req.obj["name"], req.obj["password"], main_status=req.obj.get( "mainStatus", None), components=components) response = Response(body="Updated server.") except err.AuthenticationError: response = error_response(401) except KeyError: response = error_response( 406, "'name'", "'password'", "(optional) 'components'", "(optional) 'mainStatus'") except Exception as e: response = Response(500, body=str(e)) return response
def delete_task(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) base.delete_task(req.obj["id"], uao) response = Response(body="Deleted task with id " + str(req.obj["id"])) except base.utils.AuthenticationError: response = error_response(401) except base.utils.TaskDoesNotExist: response = error_response(404) except base.utils.InvalidParameterError as e: response = Response(406, body=str(e)) except KeyError: response_code = 406 response_msg = "Invalid parameters. Expected username, password and id." return {"code": response_code}, response_msg
def api_change_server(req: Request): try: id = req.obj.get("id", -1) if id == -1: server = base.get_server(name=req.obj["name"]) else: server = base.get_server(id=id) compo = req.obj.get("components", None) if compo != None: components = json.loads(compo) else: components = None base.authenticate(server.get("name"), req.obj["password"]) base.change_server(server.get("id"), req.obj.get("newName", None), req.obj.get("description", None), req.obj.get( "repoURL", None), req.obj.get("mainStatus", None), components, req.obj.get("newPassword", None)) response = Response(body="Changed server successfully.") except err.AuthenticationError: response = error_response(401) except KeyError: response = error_response(406, "'id'/'name'", "'password'") except err.ServerNotFoundError: response = error_response(404) except Exception as e: serverly.logger.handle_exception(e) response_msg = str(e) return response
def delete_user(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) user = base.get_user(uao) base.delete_user(user.get("id", 0), uao) response = Response() except base.utils.AuthenticationError as e: serverly.logger.handle_exception(e) response = error_response(401) except base.utils.UserDoesNotExist as e: serverly.logger.handle_exception(e) response = Response(404, body="User not found.") except base.utils.InvalidParameterError as e: serverly.logger.handle_exception(e) response = Response(406, body=str(e)) return response
def add_task(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) base.add_task(uao, title=req.obj.get("title", "New Task"), description=req.obj.get("description", ""), due_date=req.obj.get("dueDate", ""), deadline=req.obj.get("deadline", "")) response = Response() except base.utils.AuthenticationError: reponse = error_response(401) except KeyError: response = error_response(406, "username", "password") except Exception as e: serverly.logger.handle_exception(e) response = Response(500, body=str(e)) return response
def change_task(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) base.change_task(uao, req.obj["id"], req.obj.get("title", None), req.obj.get("description", None), req.obj.get("dueDate", None), req.obj.get("deadline", None)) response = Response() except KeyError: response = error_response(406, "username", "password", "id", "(optional) title", "(optional) description", "(optional) dueDate", "(optional) deadline") except base.utils.AuthenticationError: response = error_response(401) except Exception as e: serverly.logger.handle_exception(e) response = Response(500, body=str(e)) return response
def api_delete_server(req: Request): try: base.authenticate(req.obj.get("name", "notaname"), req.obj.get("password", "")) if req.obj.get("name", None) != None: base.delete_server(req.obj.get("name")) delete_server_details_page(req.obj.get("name")) else: server = base.get_server(id=req.obj.get("id", -1)) base.delete_server(id=server.get("id", -1)) delete_server_details_page(server.get("name", "notaname")) response = Response( body="Deleted server successfully (just from the Stater-system :P).") except err.AuthenticationError: response = error_response(401) except err.ServerNotFoundError: response = error_response(404) except Exception as e: response = Response( 500, body=f"Exception ({type(e).__name__}): " + str(e)) return response
def change_user(req: Request): try: uao = base.utils.UserAuthenticationObject(req.obj["username"], req.obj["password"]) base.change_user(uao, username=req.obj.get("newUsername", None), first_name=req.obj.get("firstName", None), last_name=req.obj.get("lastName", None), password=req.obj.get("newPassword", None), email=req.obj.get("email", None)) response = Response() except base.utils.AuthenticationError: response = error_response(401) except KeyError: response = error_response(406, "username", "password") except (base.utils.UserAlreadyExists, base.utils.InvalidParameterError, base.utils.EmailAlreadyLinked) as e: response = Response(406, str(e)) except Exception as e: serverly.logger.handle_exception(e) response = Response(500, str(e)) return response
def api_authenticate(req: Request): try: base.authenticate(req.obj["name"], req.obj["password"]) request = Request(body="Authorized.") except KeyError: response = error_response(406, "'name'", "'password'") except err.AuthenticationError: response = error_response(401) except err.ServerNotFoundError: response = error_response(404) except Exception as e: serverly.logger.handle_exception(e) response = Response(500, body=str(e)) return response
def api_update_component(req: Request): try: base.authenticate(req.obj["name"], req.obj["password"]) server_name = req.obj.get("serverName", req.obj["name"]) component_name = req.obj.get( "component", req.obj.get("componentName", "NOTACOMPONENT")) status = req.obj.get("status", req.obj.get("newStatus", "NOTASTATUS")) if server_name == "NOTANAME" or component_name == "NOTACOMPONENT" or status == "NOTASTATUS": raise KeyError() base.update_component( server_name, req.obj["password"], component_name, status) response = Response(body="Updated component.") except err.AuthenticationError: response = error_response(401) except KeyError: response = error_response( 406, "'name'", "'password'", "'componentName'", "'status'") except Exception as e: serverly.logger.handle_exception(e) response_msg = str(e) return response