class LatestNotifications(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __notification = None __correlation_id = None def __init__(self): self.__helpers = Helpers() self.__form = Form() self.__logger = self.__helpers.get_logger(__name__) self.__response = Response() self.__request = Request() self.__notification = NotificationModule() self.__form.add_validator(ExtraRules()) @allow_if_authenticated def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id return JsonResponse( self.__response.send_private_success( [], self.__notification.user_latest_notifications(self.__user_id), self.__correlation_id)) @allow_if_authenticated def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id self.__request.set_request(request) request_data = self.__request.get_request_data("post", {"notification_id": ""}) try: notification_id = int(request_data["notification_id"]) except Exception: return JsonResponse( self.__response.send_private_success([], {}, self.__correlation_id)) self.__notification.mark_notification(self.__user_id, notification_id) return JsonResponse( self.__response.send_private_success([], {}, self.__correlation_id))
class IncidentUpdatesNotify(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __incident_update = None __task = None __notification = None __subscriber = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__incident_update = IncidentUpdateModule() self.__task = Task_Module() self.__notification = NotificationModule() self.__subscriber = SubscriberModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request, incident_id, update_id): self.__correlation_id = request.META["X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id task = self.__task.delay("incident_update", { "incident_update_id": update_id, "user_id": self.__user_id }, self.__user_id) result = False if task: result = self.__notification.create_notification({ "highlight": "Incident Update", "notification": "notifying subscribers with the incident update", "url": "#", "type": NotificationModule.PENDING, "delivered": False, "user_id": self.__user_id, "task_id": task.id }) if task and result: return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Notification delivery started successfully.") }], {}, self.__correlation_id)) else: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while starting delivery.") }], {}, self.__correlation_id))
class IncidentUpdatesComponent(View): """Remove Component from Incident Update Private Endpoint Controller""" def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__incident_update_component = IncidentUpdateComponentModule() self.__logger = self.__helpers.get_logger(__name__) self.__user_id = None self.__correlation_id = "" self.__form.add_validator(ExtraRules()) @allow_if_authenticated def delete(self, request, incident_id, update_id, item_id): self.__correlation_id = request.META["X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id if self.__incident_update_component.delete_one_by_id(item_id): return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Affected component deleted successfully.") }], {}, self.__correlation_id)) else: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting affected component.") }], {}, self.__correlation_id))
class Health_Check(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_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, 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.") }])) health = self.__status.set_host(self.__host_id).ping() if health: return JsonResponse( self.__response.send_private_success([], {"status": "up"})) else: return JsonResponse( self.__response.send_private_success([], {"status": "down"}))
class Activities(View): """List Activities Private Endpoint Controller""" def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__activity = ActivityModule() self.__logger = self.__helpers.get_logger(__name__) self.__user_id = None self.__correlation_id = "" self.__form.add_validator(ExtraRules()) @allow_if_authenticated def get(self, request): self.__correlation_id = request.META["X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id self.__request.set_request(request) request_data = self.__request.get_request_data("get", { "offset": 0, "limit": 20 }) try: offset = int(request_data["offset"]) limit = int(request_data["limit"]) except Exception: offset = 0 limit = 20 return JsonResponse(self.__response.send_private_success([], { 'activities': self.__format_activities(self.__activity.get(self.__user_id, offset, limit)), 'metadata': { 'offset': offset, 'limit': limit, 'count': self.__activity.count(self.__user_id) } }, self.__correlation_id)) def __format_activities(self, activities): activities_list = [] for activity in activities: activities_list.append({ "id": activity.id, "activity": activity.activity, "created_at": activity.created_at.strftime("%b %d %Y %H:%M:%S") }) return activities_list
class Get_Image(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __image_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 get(self, request, host_id, image_id): self.__user_id = request.user.id self.__host_id = host_id self.__image_id = image_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.__image_module.set_host(self.__host_id).check_health(): _image = {'long_id': self.__image_id} return JsonResponse( self.__response.send_private_success([], {'image': _image})) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong with your host!") }], {'image': {}}))
class NewRelicApps(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __metric = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__metric = MetricModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" result = False try: result = self.__metric.get_new_relic_apps() except Exception as e: self.__logger.error( _("Error while listing newrelic applications: %(error)s {'correlationId':'%(correlationId)s'}" ) % { "error": str(e), "correlationId": self.__correlation_id }) if result is False: return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Connecting to New Relic.") }], {}, self.__correlation_id)) return JsonResponse( self.__response.send_private_success([], {'apps': result}, self.__correlation_id))
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 Incident(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __incident = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__incident = IncidentModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request, incident_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "name": "", "status": "", "datetime": "", }) self.__form.add_inputs({ 'name': { 'value': request_data["name"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 200], 'error': _('Error! Incident name must be 1 to 200 characters long.' ) } } }, 'datetime': { 'value': request_data["datetime"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'status': { 'value': request_data["status"], 'validate': { 'any_of': { 'param': [["open", "closed"]], 'error': _('Error! Incident is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) result = self.__incident.update_one_by_id( incident_id, { "name": self.__form.get_sinput("name"), "status": self.__form.get_sinput("status"), "datetime": DateTimeField().clean(self.__form.get_sinput("datetime")) }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Incident updated successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while updating incident.") }], {}, self.__correlation_id)) @allow_if_authenticated def delete(self, request, incident_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id if self.__incident.delete_one_by_id(incident_id): return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Incident deleted successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting incident.") }], {}, self.__correlation_id))
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 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 User(View): """Update and Delete User Private Endpoint Controller""" def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__user = UserModule() self.__logger = self.__helpers.get_logger(__name__) self.__user_id = None self.__correlation_id = "" self.__form.add_validator(ExtraRules()) @allow_if_authenticated_and_has_permission("manage_settings") def post(self, request, user_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "first_name": "", "last_name": "", "username": "", "role": "", "email": "", "update_password": "", "password": "" }) if request_data["update_password"] == "": self.__form.add_inputs({ 'first_name': { 'value': request_data["first_name"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_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': { 'sv_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': { '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': { 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! Email is invalid.') } } }, 'role': { 'value': request_data["role"], 'validate': { 'any_of': { 'param': [["admin", "user"]], 'error': _('Error! Role is invalid.') } } } }) else: self.__form.add_inputs({ 'first_name': { 'value': request_data["first_name"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_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': { 'sv_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': { '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': { 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! Email is invalid.') } } }, 'password': { 'value': request_data["password"], 'validate': { 'sv_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.' ) } } }, 'role': { 'value': request_data["role"], 'validate': { 'any_of': { 'param': [["admin", "user"]], 'error': _('Error! Role is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) if self.__user.username_used_elsewhere( user_id, self.__form.get_sinput("username")): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Username is already used.") }], {}, self.__correlation_id)) if self.__user.email_used_elsewhere(user_id, self.__form.get_sinput("email")): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Email is already used for other account.") }], {}, self.__correlation_id)) if request_data["update_password"] == "": result = self.__user.update_one_by_id( user_id, { "username": self.__form.get_sinput("username"), "email": self.__form.get_sinput("email"), "first_name": self.__form.get_sinput("first_name"), "last_name": self.__form.get_sinput("last_name"), "is_superuser": True if self.__form.get_sinput("role") == "admin" else False }) else: result = self.__user.update_one_by_id( user_id, { "username": self.__form.get_sinput("username"), "email": self.__form.get_sinput("email"), "first_name": self.__form.get_sinput("first_name"), "last_name": self.__form.get_sinput("last_name"), "password": self.__form.get_sinput("password"), "is_superuser": True if self.__form.get_sinput("role") == "admin" else False }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("User updated successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating your account." ) }], {}, self.__correlation_id)) @allow_if_authenticated_and_has_permission("manage_settings") def delete(self, request, user_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id if self.__user_id == user_id: return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! You can't delete your account.") }], {}, self.__correlation_id)) if self.__user.delete_one_by_id(user_id): return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("User deleted successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting a user.") }], {}, self.__correlation_id))
class Metric(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __metric = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__metric = MetricModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request, metric_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "title": "", "description": "", "source": "", "application": "", "metric": "", "x_axis": "", "y_axis": "" }) self.__form.add_inputs({ 'title': { 'value': request_data["title"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'description': { 'value': request_data["description"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'source': { 'value': request_data["source"], 'validate': { 'any_of': { 'param': [["newrelic"]], 'error': _('Error! Source is invalid.') } } }, 'application': { 'value': request_data["application"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'metric': { 'value': request_data["metric"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'x_axis': { 'value': request_data["x_axis"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'y_axis': { 'value': request_data["y_axis"], 'sanitize': { 'strip': {} }, 'validate': {} } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) result = self.__metric.update_one_by_id( metric_id, { "title": self.__form.get_sinput("title"), "description": self.__form.get_sinput("description"), "source": self.__form.get_sinput("source"), "x_axis": self.__form.get_sinput("x_axis"), "y_axis": self.__form.get_sinput("y_axis"), "data": '{"application":"%s", "metric":"%s"}' % (self.__form.get_sinput("application"), self.__form.get_sinput("metric")) }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric updated successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while updating metric.") }], {}, self.__correlation_id)) @allow_if_authenticated def delete(self, request, metric_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id if self.__metric.delete_one_by_id(metric_id): return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric deleted successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting metric.") }], {}, self.__correlation_id))
class Metrics(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __metric = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__metric = MetricModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "title": "", "description": "", "source": "", "application": "", "metric": "", "x_axis": "", "y_axis": "" }) self.__form.add_inputs({ 'title': { 'value': request_data["title"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'description': { 'value': request_data["description"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'source': { 'value': request_data["source"], 'validate': { 'any_of': { 'param': [["newrelic"]], 'error': _('Error! Source is invalid.') } } }, 'application': { 'value': request_data["application"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'metric': { 'value': request_data["metric"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'x_axis': { 'value': request_data["x_axis"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'y_axis': { 'value': request_data["y_axis"], 'sanitize': { 'strip': {} }, 'validate': {} } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) result = self.__metric.insert_one({ "title": self.__form.get_sinput("title"), "description": self.__form.get_sinput("description"), "source": self.__form.get_sinput("source"), "x_axis": self.__form.get_sinput("x_axis"), "y_axis": self.__form.get_sinput("y_axis"), "data": '{"application":"%s", "metric":"%s"}' % (self.__form.get_sinput("application"), self.__form.get_sinput("metric")) }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric created successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating metric.") }], {}, self.__correlation_id)) @allow_if_authenticated def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("get", { "offset": "", "limit": "" }) try: offset = int(request_data["offset"]) limit = int(request_data["limit"]) except Exception: offset = 0 limit = 0 return JsonResponse( self.__response.send_private_success( [], { 'metrics': self.__format_metrics(self.__metric.get_all(offset, limit)), 'metadata': { 'offset': offset, 'limit': limit, 'count': self.__metric.count_all() } }, self.__correlation_id)) def __format_metrics(self, metrics): metrics_list = [] for metric in metrics: metrics_list.append({ "id": metric.id, "title": metric.title, "source": metric.source.title(), "created_at": metric.created_at.strftime("%b %d %Y %H:%M:%S"), "edit_url": reverse("app.web.admin.metric.edit", kwargs={'metric_id': metric.id}), "delete_url": reverse("app.api.private.v1.admin.metric.endpoint", kwargs={'metric_id': metric.id}) }) return metrics_list
class IncidentUpdatesComponents(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __incident_update = None __task = None __notification = None __subscriber = None __incident_update_component = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__incident_update = IncidentUpdateModule() self.__task = Task_Module() self.__notification = NotificationModule() self.__subscriber = SubscriberModule() self.__logger = self.__helpers.get_logger(__name__) self.__incident_update_component = IncidentUpdateComponentModule() self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request, incident_id, update_id): self.__correlation_id = request.META["X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "type": "", "component_id": "" }) self.__form.add_inputs({ 'component_id': { 'value': request_data["component_id"], 'validate': { 'sv_numeric': { 'error': _('Error! Component is required.') } } }, 'type': { 'value': request_data["type"], 'validate': { 'any_of': { 'param': [["operational", "degraded_performance", "partial_outage", "major_outage", "maintenance"]], 'error': _('Error! Type is required.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse(self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) result = self.__incident_update_component.insert_one({ "component_id": int(self.__form.get_sinput("component_id")), "type": self.__form.get_sinput("type"), "incident_update_id": int(update_id) }) if result: return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Affected component created successfully.") }], {}, self.__correlation_id)) else: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating affected component.") }], {}, self.__correlation_id))
class Register(View): __request = None __response = None __helpers = None __form = None __user = None __logger = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__user = UserModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @stop_request_if_authenticated def post(self, request): self.__correlation_id = request.META["X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "register_request_token": "", "first_name": "", "last_name": "", "username": "", "email": "", "password": "" }) self.__form.add_inputs({ 'first_name': { 'value': request_data["first_name"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_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': { 'sv_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': { 'sv_email': { 'error': _('Error! Admin email is invalid.') } } }, 'password': { 'value': request_data["password"], 'validate': { 'sv_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_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) register_request = self.__user.get_register_request_by_token(request_data["register_request_token"]) if not register_request: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Register token is invalid or expired.") }], {}, self.__correlation_id)) payload = json.loads(register_request.payload) if self.__user.username_used(self.__form.get_sinput("username")): return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Username is already used.") }], {}, self.__correlation_id)) if self.__user.email_used(self.__form.get_sinput("email")): return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Email is already used for other account.") }], {}, self.__correlation_id)) result = self.__user.insert_one({ "username": self.__form.get_sinput("username"), "email": self.__form.get_sinput("email"), "first_name": self.__form.get_sinput("first_name"), "last_name": self.__form.get_sinput("last_name"), "password": self.__form.get_sinput("password"), "is_staff": False, "is_active": True, "is_superuser": True if payload["role"] == "admin" else False }) if result: self.__user.delete_register_request_by_token(request_data["register_request_token"]) return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Account created successfully.") }], {}, self.__correlation_id)) else: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating your account.") }], {}, self.__correlation_id))
class Users(View): """Create and List Users Private Endpoint Controller""" def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__user = UserModule() self.__logger = self.__helpers.get_logger(__name__) self.__user_id = None self.__correlation_id = "" self.__form.add_validator(ExtraRules()) @allow_if_authenticated_and_has_permission("manage_settings") def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "invitation": "", "first_name": "", "last_name": "", "username": "", "role": "", "email": "", "password": "" }) if request_data["invitation"] != "": self.__form.add_inputs({ 'first_name': { 'value': request_data["first_name"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_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': { 'sv_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': { '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': { 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! User email is invalid.') } } }, 'password': { 'value': request_data["password"], 'validate': { 'sv_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.' ) } } }, 'role': { 'value': request_data["role"], 'validate': { 'any_of': { 'param': [["admin", "user"]], 'error': _('Error! Role is invalid.') } } } }) else: self.__form.add_inputs({ 'email': { 'value': request_data["email"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! User email is invalid.') } } }, 'role': { 'value': request_data["role"], 'validate': { 'any_of': { 'param': [["admin", "user"]], 'error': _('Error! Role is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) if self.__user.email_used(self.__form.get_sinput("email")): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Email is already used for other account.") }], {}, self.__correlation_id)) if request_data["invitation"] != "" and self.__user.username_used( self.__form.get_sinput("username")): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Username is already used.") }], {}, self.__correlation_id)) if request_data["invitation"] != "": result = self.__user.insert_one({ "username": self.__form.get_sinput("username"), "email": self.__form.get_sinput("email"), "first_name": self.__form.get_sinput("first_name"), "last_name": self.__form.get_sinput("last_name"), "password": self.__form.get_sinput("password"), "is_staff": False, "is_active": True, "is_superuser": True if self.__form.get_sinput("role") == "admin" else False }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Account created successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating your account." ) }], {}, self.__correlation_id)) else: self.__user.delete_register_request_by_email( self.__form.get_sinput("email")) token = self.__user.create_register_request( self.__form.get_sinput("email"), self.__form.get_sinput("role")) if not token: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating reset request." ) }], {}, self.__correlation_id)) message = self.__user.send_register_request_message( self.__form.get_sinput("email"), token) if not message: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while sending register request." ) }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Register Request instructions sent successfully.") }], {}, self.__correlation_id)) @allow_if_authenticated_and_has_permission("manage_settings") def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("get", { "offset": 0, "limit": 20 }) try: offset = int(request_data["offset"]) limit = int(request_data["limit"]) except Exception: offset = 0 limit = 20 return JsonResponse( self.__response.send_private_success( [], { 'users': self.__format_users(self.__user.get_all(offset, limit)), 'metadata': { 'offset': offset, 'limit': limit, 'count': self.__user.count_all() } }, self.__correlation_id)) def __format_users(self, users): users_list = [] for user in users: users_list.append({ "id": user.id, "username": user.username, "first_name": user.first_name, "last_name": user.last_name, "email": user.email, "role": "Admin" if user.is_superuser == 1 else "User", "created_at": user.date_joined.strftime("%b %d %Y %H:%M:%S"), "edit_url": reverse("app.web.admin.user.edit", kwargs={'user_id': user.id}), "delete_url": reverse("app.api.private.v1.admin.user.endpoint", kwargs={'user_id': user.id}) }) return users_list
class Login(View): __request = None __response = None __helpers = None __form = None __login = None __logger = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__login = LoginModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @stop_request_if_authenticated def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" if self.__login.is_authenticated(request): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! User is already authenticated.") }], {}, self.__correlation_id)) 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': { 'sv_username_or_email': { 'error': _("Error! Username or password is invalid.") } } }, 'password': { 'value': request_data["password"], 'validate': { 'sv_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_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) if self.__login.authenticate(self.__form.get_sinput("username"), self.__form.get_sinput("password"), request): return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("You logged in successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Username or password is invalid.") }], {}, self.__correlation_id))
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 __notification = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__install = InstallModule() self.__notification = NotificationModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @stop_request_if_installed def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" if self.__install.is_installed(): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Application is already installed.") }], {}, self.__correlation_id)) 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': [2, 30], 'error': _('Error! Application name must be 2 to 30 characters long.' ) } } }, 'app_email': { 'value': request_data["app_email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! Application email is invalid.') } } }, 'app_url': { 'value': request_data["app_url"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'sv_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': { 'sv_email': { 'error': _('Error! Admin email is invalid.') } } }, 'admin_password': { 'value': request_data["admin_password"], 'validate': { 'sv_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_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) self.__install.set_app_data(self.__form.get_sinput("app_name"), self.__form.get_sinput("app_email"), self.__form.get_sinput("app_url")) self.__install.set_admin_data(self.__form.get_sinput("admin_username"), self.__form.get_sinput("admin_email"), self.__form.get_sinput("admin_password")) try: user_id = self.__install.install() except Exception as exception: self.__logger.error( _("Internal server error during installation: %(exception)s {'correlationId':'%(correlationId)s'}" ) % { "exception": exception, "correlationId": self.__correlation_id }) if user_id: self.__notification.create_notification({ "highlight": _('Installation'), "notification": _('Silverback installed successfully'), "url": "#", "type": NotificationModule.MESSAGE, "delivered": False, "user_id": user_id, "task_id": None }) return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Application installed successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong during installing.") }], {}, self.__correlation_id))
class Prune_All_Unused_Images(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 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.") }])) task = self.__task_module.delay("prune_all_unused_images", {"host_id": self.__host_id}, self.__user_id) if task: self.__notification_module.create_notification({ "highlight": "", "notification": _("prune all unused docker images"), "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 Subscribers(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __subscriber = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__subscriber = SubscriberModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "type": "", "email": "", "phone": "", "endpoint": "", "auth_token": "", "status": "" }) if request_data["type"] == "email": self.__form.add_inputs({ 'type': { 'value': request_data["type"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["email", "phone", "endpoint"]], 'error': _('Error! Type is invalid.') } } }, 'email': { 'value': request_data["email"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'status': { 'value': request_data["status"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["pending", "verified", "unverified"]], 'error': _('Error! Status is invalid.') } } } }) elif request_data["type"] == "phone": self.__form.add_inputs({ 'type': { 'value': request_data["type"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["email", "phone", "endpoint"]], 'error': _('Error! Type is invalid.') } } }, 'phone': { 'value': request_data["phone"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'status': { 'value': request_data["status"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["pending", "verified", "unverified"]], 'error': _('Error! Status is invalid.') } } } }) else: self.__form.add_inputs({ 'type': { 'value': request_data["type"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["email", "phone", "endpoint"]], 'error': _('Error! Type is invalid.') } } }, 'email': { 'value': request_data["email"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'endpoint': { 'value': request_data["endpoint"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'auth_token': { 'value': request_data["auth_token"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'status': { 'value': request_data["status"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["pending", "verified", "unverified"]], 'error': _('Error! Status is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) external_id = self.__helpers.generate_uuid() while self.__subscriber.get_one_by_external_id( external_id) is not False: external_id = self.__helpers.generate_uuid() if request_data["type"] == "email": result = self.__subscriber.insert_one({ "status": self.__form.get_sinput("status"), "type": self.__form.get_sinput("type"), "email": self.__form.get_sinput("email"), "phone": "", "endpoint": "", "auth_token": "", "external_id": external_id }) elif request_data["type"] == "phone": result = self.__subscriber.insert_one({ "status": self.__form.get_sinput("status"), "type": self.__form.get_sinput("type"), "email": "", "phone": self.__form.get_sinput("phone"), "endpoint": "", "auth_token": "", "external_id": external_id }) else: result = self.__subscriber.insert_one({ "status": self.__form.get_sinput("status"), "type": self.__form.get_sinput("type"), "email": self.__form.get_sinput("email"), "phone": "", "endpoint": self.__form.get_sinput("endpoint"), "auth_token": self.__form.get_sinput("auth_token"), "external_id": external_id }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Subscriber created successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating subscriber.") }], {}, self.__correlation_id)) @allow_if_authenticated def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("get", { "offset": "", "limit": "" }) try: offset = int(request_data["offset"]) limit = int(request_data["limit"]) except Exception: offset = 0 limit = 0 return JsonResponse( self.__response.send_private_success( [], { 'subscribers': self.__format_subscribers( self.__subscriber.get_all(offset, limit)), 'metadata': { 'offset': offset, 'limit': limit, 'count': self.__subscriber.count_all() } }, self.__correlation_id)) def __format_subscribers(self, subscribers): subscribers_list = [] for subscriber in subscribers: subscribers_list.append({ "id": subscriber.id, "status": subscriber.status.title(), "type": subscriber.type, "email": subscriber.email, "phone": subscriber.phone, "endpoint": subscriber.endpoint, "auth_token": subscriber.auth_token, "created_at": subscriber.created_at.strftime("%b %d %Y %H:%M:%S"), "edit_url": reverse("app.web.admin.subscriber.edit", kwargs={'subscriber_id': subscriber.id}), "delete_url": reverse("app.api.private.v1.admin.subscriber.endpoint", kwargs={'subscriber_id': subscriber.id}) }) return subscribers_list
class Get_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 get(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.__image_module.set_host(self.__host_id).check_health(): _host = self.__host_module.get_one_by_id(host_id) return JsonResponse( self.__response.send_private_success( [], { 'images': self.__format_image(self.__image_module.list(), host_id, _host.slug) })) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong with your host!") }], {'images': []})) def __format_image(self, images_list, host_id, host_slug): _image_list = [] for image in images_list: date = image["created"].split("T") image["created_at"] = date[0] image["size"] = image["size"]["MB"] _tags = [] for tag in image["tags"]: tag = tag.split(":") _tags.append({"name": tag[0], "version": tag[1]}) image["tags"] = _tags image["url"] = reverse("app.web.admin.hosts.view.image", kwargs={ 'host_slug': host_slug, 'image_id': image['long_id'] }) image["delete_url"] = reverse( "app.api.private.v1.admin.action.host.delete_image.endpoint", kwargs={'host_id': host_id}) _image_list.append(image) return _image_list
class Subscriber(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __subscriber = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__subscriber = SubscriberModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request, subscriber_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "type": "", "email": "", "phone": "", "endpoint": "", "auth_token": "", "status": "" }) if request_data["type"] == "email": self.__form.add_inputs({ 'type': { 'value': request_data["type"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["email", "phone", "endpoint"]], 'error': _('Error! Type is invalid.') } } }, 'email': { 'value': request_data["email"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'status': { 'value': request_data["status"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["pending", "verified", "unverified"]], 'error': _('Error! Status is invalid.') } } } }) elif request_data["type"] == "phone": self.__form.add_inputs({ 'type': { 'value': request_data["type"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["email", "phone", "endpoint"]], 'error': _('Error! Type is invalid.') } } }, 'phone': { 'value': request_data["phone"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'status': { 'value': request_data["status"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["pending", "verified", "unverified"]], 'error': _('Error! Status is invalid.') } } } }) else: self.__form.add_inputs({ 'type': { 'value': request_data["type"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["email", "phone", "endpoint"]], 'error': _('Error! Type is invalid.') } } }, 'email': { 'value': request_data["email"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'endpoint': { 'value': request_data["endpoint"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'auth_token': { 'value': request_data["auth_token"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'status': { 'value': request_data["status"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["pending", "verified", "unverified"]], 'error': _('Error! Status is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) if request_data["type"] == "email": result = self.__subscriber.update_one_by_id( subscriber_id, { "status": self.__form.get_sinput("status"), "type": self.__form.get_sinput("type"), "email": self.__form.get_sinput("email"), "phone": "", "endpoint": "", "auth_token": "" }) elif request_data["type"] == "phone": result = self.__subscriber.update_one_by_id( subscriber_id, { "status": self.__form.get_sinput("status"), "type": self.__form.get_sinput("type"), "email": "", "phone": self.__form.get_sinput("phone"), "endpoint": "", "auth_token": "" }) else: result = self.__subscriber.update_one_by_id( subscriber_id, { "status": self.__form.get_sinput("status"), "type": self.__form.get_sinput("type"), "email": self.__form.get_sinput("email"), "phone": "", "endpoint": self.__form.get_sinput("endpoint"), "auth_token": self.__form.get_sinput("auth_token") }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Subscriber updated successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while updating subscriber.") }], {}, self.__correlation_id)) @allow_if_authenticated def delete(self, request, subscriber_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id if self.__subscriber.delete_one_by_id(subscriber_id): return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Subscriber deleted successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting subscriber.") }], {}, self.__correlation_id))
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 ForgotPassword(View): __request = None __response = None __helpers = None __form = None __forgot_password = None __logger = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__forgot_password = ForgotPasswordModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @stop_request_if_authenticated def post(self, request): self.__correlation_id = request.META["X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" 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': { 'sv_email': { 'error': _('Error! Email is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse(self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) if not self.__forgot_password.check_email(self.__form.get_sinput("email")): return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Email is not exist.") }], {}, self.__correlation_id)) reset_request = self.__forgot_password.reset_request_exists(self.__form.get_sinput("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!") }], {}, self.__correlation_id)) token = self.__forgot_password.update_request(reset_request) else: token = self.__forgot_password.create_request(self.__form.get_sinput("email")) if not token: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating reset request.") }], {}, self.__correlation_id)) message = self.__forgot_password.send_message(self.__form.get_sinput("email"), token) if not message: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while sending reset instructions.") }], {}, self.__correlation_id)) else: return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Reset instructions sent successfully.") }], {}, self.__correlation_id))
class Incidents(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __incident = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__incident = IncidentModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "name": "", "status": "", "datetime": "", }) self.__form.add_inputs({ 'name': { 'value': request_data["name"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 200], 'error': _('Error! Incident name must be 1 to 200 characters long.' ) } } }, 'datetime': { 'value': request_data["datetime"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'status': { 'value': request_data["status"], 'validate': { 'any_of': { 'param': [["open", "closed"]], 'error': _('Error! Incident is invalid.') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) result = self.__incident.insert_one({ "name": self.__form.get_sinput("name"), "status": self.__form.get_sinput("status"), "datetime": DateTimeField().clean(self.__form.get_sinput("datetime")), "uri": self.__incident.generate_uri(6) }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Incident created successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating incident.") }], {}, self.__correlation_id)) @allow_if_authenticated def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("get", { "offset": "", "limit": "" }) try: offset = int(request_data["offset"]) limit = int(request_data["limit"]) except Exception: offset = 0 limit = 0 return JsonResponse( self.__response.send_private_success( [], { 'incidents': self.__format_incidents( self.__incident.get_all(offset, limit)), 'metadata': { 'offset': offset, 'limit': limit, 'count': self.__incident.count_all() } }, self.__correlation_id)) def __format_incidents(self, incidents): incidents_list = [] for incident in incidents: incidents_list.append({ "id": incident.id, "name": incident.name, "uri": incident.uri, "status": incident.status.title(), "created_at": incident.created_at.strftime("%b %d %Y %H:%M:%S"), "view_url": reverse("app.web.admin.incident.view", kwargs={'incident_id': incident.id}), "view_status_url": reverse("app.web.status_page_single", kwargs={'uri': incident.uri}), "edit_url": reverse("app.web.admin.incident.edit", kwargs={'incident_id': incident.id}), "delete_url": reverse("app.api.private.v1.admin.incident.endpoint", kwargs={'incident_id': incident.id}) }) return incidents_list
class Get_Volumes(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __volume_id = None __host_module = None __volume_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__volume_module = Volume_Module() self.__logger = self.__helpers.get_logger(__name__) def get(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.__volume_module.set_host(self.__host_id).check_health(): _host = self.__host_module.get_one_by_id(host_id) return JsonResponse( self.__response.send_private_success( [], { 'volumes': self.__format_volumes(self.__volume_module.list(), host_id, _host.slug) })) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong with your host!") }], {'volumes': []})) def __format_volumes(self, volumes_list, host_id, host_slug): _volumes_list = [] for volume in volumes_list: date = volume["created"].split("T") volume["created_at"] = date[0] volume["url"] = "#" volume["delete_url"] = "#" if volume["short_id"] in volume["name"]: volume["name"] = volume["short_id"] # volume["url"] = reverse("app.web.admin.hosts.view.volume", kwargs={'host_slug': host_slug, 'volume_id': volume['long_id']}) # volume["delete_url"] = reverse("app.api.private.v1.admin.action.host.delete_volume.endpoint", kwargs={'host_id': host_id}) _volumes_list.append(volume) return _volumes_list
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 Settings(View): """Update Settings Private Endpoint Controller""" def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__settings_module = SettingsModule() self.__acl = ACL() self.__activity_module = ActivityModule() self.__logger = self.__helpers.get_logger(__name__) self.__correlation_id = "" self.__form.add_validator(ExtraRules()) @allow_if_authenticated_and_has_permission("manage_settings") def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" 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": "", "newrelic_api_key": "" }) self.__form.add_inputs({ 'app_name': { 'value': request_data["app_name"], 'sanitize': { 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Application name must be alpha numeric.') }, 'length_between': { 'param': [2, 30], 'error': _('Error! Application name must be 2 to 30 characters long.' ) } } }, 'app_email': { 'value': request_data["app_email"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! Application email is invalid.') } } }, 'app_url': { 'value': request_data["app_url"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_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': {} } }, "newrelic_api_key": { 'value': request_data["newrelic_api_key"], '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': int(request_data["reset_mails_messages_count"]), 'sanitize': { 'strip': {} }, 'validate': { 'greater_than': { 'error': _('Error! Reset mails count is invalid.'), 'param': [0] } } }, 'reset_mails_expire_after': { 'value': int(request_data["reset_mails_expire_after"]), 'sanitize': { 'strip': {} }, 'validate': { 'greater_than': { 'error': _('Error! Reset mails count is invalid.'), 'param': [0] } } }, 'access_tokens_expire_after': { 'value': int(request_data["access_tokens_expire_after"]), 'sanitize': { 'strip': {} }, 'validate': { '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_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) result = self.__settings_module.update_options({ "app_name": self.__form.get_sinput("app_name"), "app_email": self.__form.get_sinput("app_email"), "app_url": self.__form.get_sinput("app_url"), "app_description": self.__form.get_sinput("app_description"), "google_analytics_account": self.__form.get_sinput("google_analytics_account"), "reset_mails_messages_count": self.__form.get_sinput("reset_mails_messages_count"), "reset_mails_expire_after": self.__form.get_sinput("reset_mails_expire_after"), "access_tokens_expire_after": self.__form.get_sinput("access_tokens_expire_after"), "prometheus_token": self.__form.get_sinput("prometheus_token"), "newrelic_api_key": self.__form.get_sinput("newrelic_api_key") }) if result: self.__activity_module.track( request.user.id, _('You updated application settings.')) return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Settings updated successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while updating settings.") }], {}, self.__correlation_id))