Example #1
0
class Component(View):

    __request = None
    __response = None
    __helpers = None
    __form = None
    __logger = None
    __user_id = None
    __component = None
    __correlation_id = None

    def __init__(self):
        self.__request = Request()
        self.__response = Response()
        self.__helpers = Helpers()
        self.__form = Form()
        self.__component = ComponentModule()
        self.__logger = self.__helpers.get_logger(__name__)
        self.__form.add_validator(ExtraRules())

    @allow_if_authenticated
    def post(self, request, component_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": "",
            "description": "",
            "uptime": "",
            "group": ""
        })

        self.__form.add_inputs({
            'name': {
                'value': request_data["name"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 90],
                        'error':
                        _('Error! Component name must be 1 to 90 characters long.'
                          )
                    }
                }
            },
            'description': {
                'value': request_data["description"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'uptime': {
                'value': request_data["uptime"],
                'validate': {
                    'any_of': {
                        'param': [["on", "off"]],
                        'error': _('Error! Uptime is invalid.')
                    }
                }
            },
            'group': {
                'value': request_data["group"],
                'sanitize': {},
                '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.__component.update_one_by_id(
            component_id, {
                "name":
                self.__form.get_sinput("name"),
                "description":
                self.__form.get_sinput("description"),
                "uptime":
                self.__form.get_sinput("uptime"),
                "group_id":
                None if self.__form.get_sinput("group") == "" else
                self.__form.get_sinput("group")
            })

        if result:
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Component updated successfully.")
                    }], {}, self.__correlation_id))
        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while updating component.")
                }], {}, self.__correlation_id))

    @allow_if_authenticated
    def delete(self, request, component_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.__component.delete_one_by_id(component_id):
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Component deleted successfully.")
                    }], {}, self.__correlation_id))

        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while deleting component.")
                }], {}, self.__correlation_id))
Example #2
0
class Component(View):
    """Update and Delete Component Private Endpoint Controller"""
    def __init__(self):
        self.__request = Request()
        self.__response = Response()
        self.__helpers = Helpers()
        self.__form = Form()
        self.__component = ComponentModule()
        self.__component_group = ComponentGroupModule()
        self.__logger = self.__helpers.get_logger(__name__)
        self.__form.add_validator(ExtraRules())
        self.__user_id = None
        self.__correlation_id = ""

    @allow_if_authenticated
    def post(self, request, component_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": "",
            "description": "",
            "uptime": "",
            "group": ""
        })

        self.__form.add_inputs({
            'name': {
                'value': request_data["name"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 60],
                        'error':
                        _('Error! Component name must be 1 to 60 characters long.'
                          )
                    }
                }
            },
            'description': {
                'value': request_data["description"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [0, 150],
                        'error':
                        _('Error! Component name description must be less than 150 characters long.'
                          )
                    },
                    'optional': {}
                }
            },
            'uptime': {
                'value': request_data["uptime"],
                'validate': {
                    'any_of': {
                        'param': [["on", "off"]],
                        'error': _('Error! Uptime is invalid.')
                    }
                }
            },
            'group': {
                'value': int(request_data["group"]),
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'greater_than': {
                        'error': _('Error! Component group is invalid.'),
                        'param': [0]
                    },
                    'optional': {}
                }
            }
        })

        self.__form.process()

        if not self.__form.is_passed():
            return JsonResponse(
                self.__response.send_errors_failure(self.__form.get_errors(),
                                                    {}, self.__correlation_id))

        # Check if component name not used elsewhere
        current_component = self.__component.get_one_by_name(
            self.__form.get_sinput("name"))

        if current_component and not current_component["id"] == component_id:
            return JsonResponse(
                self.__response.send_private_failure(
                    [{
                        "type": "error",
                        "message": _("Error! Component name is used before.")
                    }], {}, self.__correlation_id))

        # Check if group id is valid
        if self.__form.get_sinput(
                "group") and not self.__component_group.get_one_by_id(
                    self.__form.get_sinput("group")):
            return JsonResponse(
                self.__response.send_private_failure(
                    [{
                        "type": "error",
                        "message": _("Error! Component group is invalid.")
                    }], {}, self.__correlation_id))

        result = self.__component.update_one_by_id(
            component_id, {
                "name":
                self.__form.get_sinput("name"),
                "description":
                self.__form.get_sinput("description"),
                "uptime":
                self.__form.get_sinput("uptime"),
                "group_id":
                None if self.__form.get_sinput("group") == "" else
                self.__form.get_sinput("group")
            })

        if result:
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Component updated successfully.")
                    }], {}, self.__correlation_id))
        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while updating component.")
                }], {}, self.__correlation_id))

    @allow_if_authenticated
    def delete(self, request, component_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.__component.delete_one_by_id(component_id):
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Component deleted successfully.")
                    }], {}, self.__correlation_id))

        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while deleting component.")
                }], {}, self.__correlation_id))
Example #3
0
class Component(View, Controller):
    """Update and Delete Component Private Endpoint Controller"""

    def __init__(self):
        self.__component = ComponentModule()
        self.__component_group = ComponentGroupModule()

    @allow_if_authenticated
    def post(self, request, component_id):

        request_data = self.get_request_data(request, "post", {
            "name": "",
            "description": "",
            "uptime": "",
            "group": ""
        })

        self.form().add_inputs({
            'name': {
                'value': request_data["name"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 60],
                        'error': _('Error! Component name must be 1 to 60 characters long.')
                    }
                }
            },
            'description': {
                'value': request_data["description"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [0, 150],
                        'error': _('Error! Component name description must be less than 150 characters long.')
                    },
                    'optional': {}
                }
            },
            'uptime': {
                'value': request_data["uptime"],
                'validate': {
                    'any_of': {
                        'param': [["on", "off"]],
                        'error': _('Error! Uptime is invalid.')
                    }
                }
            },
            'group': {
                'value': int(request_data["group"]),
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'greater_than': {
                        'error': _('Error! Component group is invalid.'),
                        'param': [0]
                    },
                    'optional': {}
                }
            }
        })

        self.form().process()

        if not self.form().is_passed():
            return self.json(self.form().get_errors())

        # Check if component name not used elsewhere
        current_component = self.__component.get_one_by_name(self.form().get_sinput("name"))

        if current_component and not current_component["id"] == component_id:
            return self.json([{
                "type": "error",
                "message": _("Error! Component name is used before.")
            }])

        # Check if group id is valid
        if self.form().get_sinput("group") and not self.__component_group.get_one_by_id(self.form().get_sinput("group")):
            return self.json([{
                "type": "error",
                "message": _("Error! Component group is invalid.")
            }])

        result = self.__component.update_one_by_id(component_id, {
            "name": self.form().get_sinput("name"),
            "description": self.form().get_sinput("description"),
            "uptime": self.form().get_sinput("uptime"),
            "group_id": None if self.form().get_sinput("group") == "" else self.form().get_sinput("group")
        })

        if result:
            return self.json([{
                "type": "success",
                "message": _("Component updated successfully.")
            }])
        else:
            return self.json([{
                "type": "error",
                "message": _("Error! Something goes wrong while updating component.")
            }])

    @allow_if_authenticated
    def delete(self, request, component_id):

        if self.__component.delete_one_by_id(component_id):
            return self.json([{
                "type": "success",
                "message": _("Component deleted successfully.")
            }])

        else:
            return self.json([{
                "type": "error",
                "message": _("Error! Something goes wrong while deleting component.")
            }])