class Reset_Password(View): __request = None __response = None __helpers = None __form = None __reset_password = None __logger = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__reset_password = Reset_Password_Module() self.__logger = self.__helpers.get_logger(__name__) @stop_request_if_authenticated def post(self, request): self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "reset_token": "", "new_password": "" }) self.__form.add_inputs({ 'reset_token': { 'value': request_data["reset_token"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': {} }, 'new_password': { 'value': request_data["new_password"], 'validate': { 'password': { 'error': _('Error! Password must contain at least uppercase letter, lowercase letter, numbers and special character.') }, 'length_between': { 'param': [7, 20], 'error': _('Error! Password length must be from 8 to 20 characters.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse(self.__response.send_private_failure(self.__form.get_errors(with_type=True))) if not self.__reset_password.check_token(self.__form.get_input_value("reset_token")): return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Reset token is expired or invalid.") }])) result = self.__reset_password.reset_password( self.__form.get_input_value("reset_token"), self.__form.get_input_value("new_password") ) result &= self.__reset_password.delete_reset_request(self.__form.get_input_value("reset_token")) if not result: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while resetting password.") }])) else: return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Password updated successfully.") }]))
class Search_Community_Images(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __image_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__image_module = Image_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id self.__request.set_request(request) request_data = self.__request.get_request_data("post", {"term": ""}) self.__form.add_inputs({ 'term': { 'value': request_data["term"], 'validate': { 'not_empty': { 'error': _('Error! Search term is required!') }, 'length_between': { 'param': [1, 100], 'error': _('Error! a valid search term is required!') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) if self.__image_module.set_host(self.__host_id).check_health(): result = self.__image_module.search( self.__form.get_input_value("term")) print(result) return JsonResponse(self.__response.send_private_success([], {})) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong with your host!") }]))
class Register(View): __request = None __response = None __helpers = None __form = None __register = None __logger = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__register = Register_Module() self.__logger = self.__helpers.get_logger(__name__) @stop_request_if_authenticated def post(self, request): self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "first_name": "", "last_name": "", "username": "", "email": "", "password": "" }) self.__form.add_inputs({ 'first_name': { 'value': request_data["first_name"], 'sanitize': { 'strip': {} }, 'validate': { 'names': { 'error': _('Error! First name contains invalid characters.') }, 'length_between': { 'param': [0, 20], 'error': _('Error! First name must be 1 to 20 characters long.') } } }, 'last_name': { 'value': request_data["last_name"], 'sanitize': { 'strip': {} }, 'validate': { 'names': { 'error': _('Error! Last name contains invalid characters.') }, 'length_between': { 'param': [0, 20], 'error': _('Error! Last name must be 1 to 20 characters long.') } } }, 'username': { 'value': request_data["username"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Username must be alpha numeric.') }, 'length_between': { 'param': [4, 10], 'error': _('Error! Username must be 5 to 10 characters long.') } } }, 'email': { 'value': request_data["email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'email': { 'error': _('Error! Admin email is invalid.') } } }, 'password': { 'value': request_data["password"], 'validate': { 'password': { 'error': _('Error! Password must contain at least uppercase letter, lowercase letter, numbers and special character.') }, 'length_between': { 'param': [7, 20], 'error': _('Error! Password length must be from 8 to 20 characters.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse(self.__response.send_private_failure(self.__form.get_errors(with_type=True))) if self.__register.username_used(self.__form.get_input_value("username")): return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Username is already used.") }])) if self.__register.email_used(self.__form.get_input_value("email")): return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Email is already used for other account.") }])) result = self.__register.create_user({ "username": self.__form.get_input_value("username"), "email": self.__form.get_input_value("email"), "first_name": self.__form.get_input_value("first_name"), "last_name": self.__form.get_input_value("last_name"), "password": self.__form.get_input_value("password"), }) if result: return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Account created successfully.") }])) else: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating your account.") }]))
class Pull_Image(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __task_module = None __notification_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__task_module = Task_Module() self.__notification_module = Notification_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id self.__request.set_request(request) request_data = self.__request.get_request_data("post", {"image_name": ""}) self.__form.add_inputs({ 'image_name': { 'value': request_data["image_name"], 'sanitize': { 'strip': {} }, 'validate': { 'not_empty': { 'error': _('Error! docker image is required!') }, 'length_between': { 'param': [1, 100], 'error': _('Error! a valid docker image is required!') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) _image_name = self.__form.get_input_value("image_name") if ":" not in _image_name: _image_name = "%s:latest" % _image_name task = self.__task_module.delay("pull_image", { "host_id": self.__host_id, "image_name": _image_name }, self.__user_id) if task: self.__notification_module.create_notification({ "highlight": "", "notification": "pulling docker image %s" % _image_name, "url": "#", "type": Notification_Module.PENDING, "delivered": False, "user_id": self.__user_id, "host_id": self.__host_id, "task_id": task.id }) return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Request is in progress!") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating request.") }]))
class Tag_Image_By_Id(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __task_module = None __notification_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__task_module = Task_Module() self.__notification_module = Notification_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "long_id": "", "repository": "", "tag": "", "force": "" }) self.__form.add_inputs({ 'long_id': { 'value': request_data["long_id"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'repository': { 'value': request_data["repository"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'tag': { 'value': request_data["tag"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'force': { 'value': request_data["force"], 'sanitize': { 'strip': {} }, 'validate': {} } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) _long_id = self.__form.get_input_value("long_id") _repository = self.__form.get_input_value("repository") _tag = self.__form.get_input_value("tag") _force = self.__form.get_input_value("force") task = self.__task_module.delay( "tag_image_by_id", { "host_id": self.__host_id, "long_id": _long_id, "repository": _repository, "tag": _tag, "force": _force }, self.__user_id) if task: self.__notification_module.create_notification({ "highlight": "", "notification": _("Tag docker image as %s:%s") % (_repository, _tag), "url": "#", "type": Notification_Module.PENDING, "delivered": False, "user_id": self.__user_id, "host_id": self.__host_id, "task_id": task.id }) return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Request is in progress!") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating request.") }]))
class Forgot_Password(View): __request = None __response = None __helpers = None __form = None __forgot_password = None __logger = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__forgot_password = Forgot_Password_Module() self.__logger = self.__helpers.get_logger(__name__) @stop_request_if_authenticated def post(self, request): self.__request.set_request(request) request_data = self.__request.get_request_data("post", {"email": ""}) self.__form.add_inputs({ 'email': { 'value': request_data["email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'email': { 'error': _('Error! Email is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__forgot_password.check_email( self.__form.get_input_value("email")): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Email is not exist.") }])) reset_request = self.__forgot_password.reset_request_exists( self.__form.get_input_value("email")) if reset_request: if self.__forgot_password.is_spam(reset_request): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Sorry! You already exceeded the maximum number of reset requests!" ) }])) token = self.__forgot_password.update_request(reset_request) else: token = self.__forgot_password.create_request( self.__form.get_input_value("email")) if not token: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating reset request." ) }])) message = self.__forgot_password.send_message( self.__form.get_input_value("email"), token) if not message: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while sending reset instructions." ) }])) else: return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Reset instructions sent successfully.") }]))
class Build_Image(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __task_module = None __notification_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__task_module = Task_Module() self.__notification_module = Notification_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "tag": "", "dockerfile": "", "rm": "", "nocache": "" }) self.__form.add_inputs({ 'tag': { 'value': request_data["tag"], 'sanitize': {}, 'validate': {} }, 'dockerfile': { 'value': request_data["dockerfile"], 'sanitize': {}, 'validate': {} }, 'rm': { 'value': request_data["rm"], 'sanitize': {}, 'validate': {} }, 'nocache': { 'value': request_data["nocache"], 'sanitize': {}, 'validate': {} } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) _tag = self.__form.get_input_value("tag") _fileobj = self.__form.get_input_value("dockerfile") _rm = bool(self.__form.get_input_value("rm") == "1") _nocache = bool(self.__form.get_input_value("nocache") == "1") task = self.__task_module.delay( "build_image", { "host_id": self.__host_id, "fileobj": _fileobj, "tag": _tag, "rm": _rm, "nocache": _nocache }, self.__user_id) if task: self.__notification_module.create_notification({ "highlight": "", "notification": "building docker image %s" % _tag, "url": "#", "type": Notification_Module.PENDING, "delivered": False, "user_id": self.__user_id, "host_id": self.__host_id, "task_id": task.id }) return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Request is in progress!") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating request.") }]))
class Install(View): __request = None __response = None __helpers = None __form = None __install = None __logger = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__install = Install_Module() self.__logger = self.__helpers.get_logger(__name__) @stop_request_if_installed def post(self, request): if self.__install.is_installed(): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Application is already installed.") }])) self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "app_name": "", "app_email": "", "app_url": "", "admin_username": "", "admin_email": "", "admin_password": "" }) self.__form.add_inputs({ 'app_name': { 'value': request_data["app_name"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Application name must be alpha numeric.') }, 'length_between': { 'param': [3, 10], 'error': _('Error! Application name must be 5 to 10 characters long.' ) } } }, 'app_email': { 'value': request_data["app_email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'email': { 'error': _('Error! Application email is invalid.') } } }, 'app_url': { 'value': request_data["app_url"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'url': { 'error': _('Error! Application url is invalid.') } } }, 'admin_username': { 'value': request_data["admin_username"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Username must be alpha numeric.') }, 'length_between': { 'param': [4, 10], 'error': _('Error! Username must be 5 to 10 characters long.') } } }, 'admin_email': { 'value': request_data["admin_email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'email': { 'error': _('Error! Admin email is invalid.') } } }, 'admin_password': { 'value': request_data["admin_password"], 'validate': { 'password': { 'error': _('Error! Password must contain at least uppercase letter, lowercase letter, numbers and special character.' ) }, 'length_between': { 'param': [7, 20], 'error': _('Error! Password length must be from 8 to 20 characters.' ) } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) self.__install.set_app_data(self.__form.get_input_value("app_name"), self.__form.get_input_value("app_email"), self.__form.get_input_value("app_url")) self.__install.set_admin_data( self.__form.get_input_value("admin_username"), self.__form.get_input_value("admin_email"), self.__form.get_input_value("admin_password")) if self.__install.install(): return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Application installed successfully.") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong during installing.") }]))
class Host(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "name": "", "slug": "", "server": "", "type": "", "auth_type": "", "tls_ca_certificate": "", "tls_certificate": "", "tls_key": "" }) self.__form.add_inputs({ 'name': { 'value': request_data["name"], 'validate': { 'host_name': { 'error': _("Error! Host Name is invalid.") }, 'length_between': { 'param': [3, 41], 'error': _("Error! Host slug length must be from 4 to 40 characters." ) } } }, 'slug': { 'value': request_data["slug"], 'validate': { 'host_slug': { 'error': _('Error! Host slug is not valid.') }, 'length_between': { 'param': [3, 21], 'error': _('Error! Host slug length must be from 4 to 20 characters.' ) } } }, 'server': { 'value': request_data["server"], 'validate': { 'host_server': { 'error': _('Error! Host server is not valid.') }, 'length_between': { 'param': [3, 60], 'error': _('Error! Host server length must be from 4 to 20 characters.' ) } } }, 'type': { 'value': request_data["type"], 'validate': { 'any_of': { 'param': [["docker"]], 'error': _('Error! Host type is invalid.') } } }, 'auth_type': { 'value': request_data["auth_type"], 'validate': { 'any_of': { 'param': [[ "no_auth", "tls_server_client", "tls_client_only", "tls_server_only", "tls_only" ]], 'error': _('Error! Auth type is invalid.') } } }, 'tls_ca_certificate': { 'value': request_data["tls_ca_certificate"], 'validate': { 'optional': {}, 'tls_certificate': { 'error': _('Error! TLS CA Certificate is invalid.') } } }, 'tls_certificate': { 'value': request_data["tls_certificate"], 'validate': { 'optional': {}, 'tls_certificate': { 'error': _('Error! TLS Certificate is invalid.') } } }, 'tls_key': { 'value': request_data["tls_key"], 'validate': { 'optional': {}, 'tls_certificate': { 'error': _('Error! TLS Key is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if self.__host_module.slug_used_elsewhere( self.__host_id, self.__form.get_input_value("slug"), self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Host slug is already used.") }])) result = self.__host_module.update_one_by_id( self.__host_id, { "name": self.__form.get_input_value("name"), "slug": self.__form.get_input_value("slug"), "server": self.__form.get_input_value("server"), "type": self.__form.get_input_value("type"), "auth_data": self.__helpers.json_dumps({ "auth_type": self.__form.get_input_value("auth_type"), "tls_ca_certificate": self.__form.get_input_value("tls_ca_certificate"), "tls_certificate": self.__form.get_input_value("tls_certificate"), "tls_key": self.__form.get_input_value("tls_key") }), "user_id": self.__user_id }) if result: return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Host updated successfully.") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating host.") }])) def delete(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) if self.__host_module.delete_host(self.__host_id): return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Host deleted successfully.") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting a host.") }]))
class Hosts(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_module = None __status = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__status = Status() self.__logger = self.__helpers.get_logger(__name__) def get(self, request): self.__user_id = request.user.id return JsonResponse( self.__response.send_private_success( [], { 'hosts': self.__format_host( self.__host_module.get_many_by_user( self.__user_id, "created_at", False)) })) def post(self, request): self.__user_id = request.user.id self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "name": "", "slug": "", "server": "", "type": "", "auth_type": "", "tls_ca_certificate": "", "tls_certificate": "", "tls_key": "" }) self.__form.add_inputs({ 'name': { 'value': request_data["name"], 'validate': { 'host_name': { 'error': _("Error! Host Name is invalid.") }, 'length_between': { 'param': [3, 41], 'error': _("Error! Host slug length must be from 4 to 40 characters." ) } } }, 'slug': { 'value': request_data["slug"], 'validate': { 'host_slug': { 'error': _('Error! Host slug is not valid.') }, 'length_between': { 'param': [3, 21], 'error': _('Error! Host slug length must be from 4 to 20 characters.' ) } } }, 'server': { 'value': request_data["server"], 'validate': { 'host_server': { 'error': _('Error! Host server is not valid.') }, 'length_between': { 'param': [3, 60], 'error': _('Error! Host server length must be from 4 to 20 characters.' ) } } }, 'type': { 'value': request_data["type"], 'validate': { 'any_of': { 'param': [["docker"]], 'error': _('Error! Host type is invalid.') } } }, 'auth_type': { 'value': request_data["auth_type"], 'validate': { 'any_of': { 'param': [[ "no_auth", "tls_server_client", "tls_client_only", "tls_server_only", "tls_only" ]], 'error': _('Error! Auth type is invalid.') } } }, 'tls_ca_certificate': { 'value': request_data["tls_ca_certificate"], 'validate': { 'optional': {}, 'tls_certificate': { 'error': _('Error! TLS CA Certificate is invalid.') } } }, 'tls_certificate': { 'value': request_data["tls_certificate"], 'validate': { 'optional': {}, 'tls_certificate': { 'error': _('Error! TLS Certificate is invalid.') } } }, 'tls_key': { 'value': request_data["tls_key"], 'validate': { 'optional': {}, 'tls_certificate': { 'error': _('Error! TLS Key is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if self.__host_module.slug_used(self.__form.get_input_value("slug"), self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Host slug is already used.") }])) result = self.__host_module.insert_one({ "name": self.__form.get_input_value("name"), "slug": self.__form.get_input_value("slug"), "server": self.__form.get_input_value("server"), "type": self.__form.get_input_value("type"), "auth_data": self.__helpers.json_dumps({ "auth_type": self.__form.get_input_value("auth_type"), "tls_ca_certificate": self.__form.get_input_value("tls_ca_certificate"), "tls_certificate": self.__form.get_input_value("tls_certificate"), "tls_key": self.__form.get_input_value("tls_key") }), "user_id": self.__user_id }) if result: return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Host created successfully.") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating host.") }])) def __format_host(self, hosts_list): _hosts_list = [] for host in hosts_list: _status = "up" if self.__status.set_host( host.id).ping() else "down" _hosts_list.append({ "id": host.id, "name": host.name, "slug": host.slug, "status": _status, "type": host.type.capitalize(), "created_at": host.created_at.strftime("%b %d %Y %H:%M:%S"), "view_url": reverse("app.web.admin.hosts.view", kwargs={'host_slug': host.slug}), "edit_url": reverse("app.web.admin.hosts.edit", kwargs={'host_slug': host.slug}), "delete_url": reverse("app.api.private.v1.admin.host.endpoint", kwargs={'host_id': host.id}) }) return _hosts_list
class Settings(View): __request = None __response = None __helpers = None __form = None __settings_module = None __logger = None __acl = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__settings_module = Settings_Module() self.__acl = ACL() self.__logger = self.__helpers.get_logger(__name__) def post(self, request): if not self.__acl.user_has_permission(request.user.id, "manage_settings"): return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "app_name": "", "app_email": "", "app_url": "", "app_description": "", "google_analytics_account": "", "reset_mails_messages_count": "", "reset_mails_expire_after": "", "access_tokens_expire_after": "", "prometheus_token": "" }) self.__form.add_inputs({ 'app_name': { 'value': request_data["app_name"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Application name must be alpha numeric.') }, 'length_between': { 'param': [3, 10], 'error': _('Error! Application name must be 5 to 10 characters long.') } } }, 'app_email': { 'value': request_data["app_email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'email': { 'error': _('Error! Application email is invalid.') } } }, 'app_url': { 'value': request_data["app_url"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'url': { 'error': _('Error! Application url is invalid.') } } }, 'app_description': { 'value': request_data["app_description"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 300], 'error': _('Error! App description is very long.') }, 'optional': {} } }, "prometheus_token": { 'value': request_data["prometheus_token"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 100], 'error': _('Error! Prometheus token is invalid.') }, 'optional': {} } }, 'google_analytics_account': { 'value': request_data["google_analytics_account"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 30], 'error': _('Error! Google analytics account is invalid.') }, 'optional': {} } }, 'reset_mails_messages_count': { 'value': request_data["reset_mails_messages_count"], 'sanitize': { 'strip': {} }, 'validate': { 'digit': { 'error': _('Error! Reset mails count is invalid.') }, 'greater_than': { 'error': _('Error! Reset mails count is invalid.'), 'param': [0] } } }, 'reset_mails_expire_after': { 'value': request_data["reset_mails_expire_after"], 'sanitize': { 'strip': {} }, 'validate': { 'digit': { 'error': _('Error! Reset mails expiry interval is invalid.') }, 'greater_than': { 'error': _('Error! Reset mails count is invalid.'), 'param': [0] } } }, 'access_tokens_expire_after': { 'value': request_data["access_tokens_expire_after"], 'sanitize': { 'strip': {} }, 'validate': { 'digit': { 'error': _('Error! Access token expiry interval is invalid.') }, 'greater_than': { 'error': _('Error! Access token expiry interval is invalid.'), 'param': [0] } } }, }) self.__form.process() if not self.__form.is_passed(): return JsonResponse(self.__response.send_private_failure(self.__form.get_errors(with_type=True))) result = self.__settings_module.update_options({ "app_name": self.__form.get_input_value("app_name"), "app_email": self.__form.get_input_value("app_email"), "app_url": self.__form.get_input_value("app_url"), "app_description": self.__form.get_input_value("app_description"), "google_analytics_account": self.__form.get_input_value("google_analytics_account"), "reset_mails_messages_count": self.__form.get_input_value("reset_mails_messages_count"), "reset_mails_expire_after": self.__form.get_input_value("reset_mails_expire_after"), "access_tokens_expire_after": self.__form.get_input_value("access_tokens_expire_after"), "prometheus_token": self.__form.get_input_value("prometheus_token") }) if result: return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Settings updated successfully.") }])) else: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while updating settings.") }]))
class Login(View): __request = None __response = None __helpers = None __form = None __login = None __logger = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__login = Login_Module() self.__logger = self.__helpers.get_logger(__name__) @stop_request_if_authenticated def post(self, request): if self.__login.is_authenticated(request): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! User is already authenticated.") }])) self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "username": "", "password": "" }) self.__form.add_inputs({ 'username': { 'value': request_data["username"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'username_or_email': { 'error': _("Error! Username or password is invalid.") } } }, 'password': { 'value': request_data["password"], 'validate': { 'password': { 'error': _("Error! Username or password is invalid.") }, 'length_between': { 'param': [7, 20], 'error': _("Error! Username or password is invalid.") } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if self.__login.authenticate(self.__form.get_input_value("username"), self.__form.get_input_value("password"), request): return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("You logged in successfully.") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Username or password is invalid.") }]))