Ejemplo n.º 1
0
 def update_status(self, data):
     if self.status == 'deleted':
         raise errors.ForbiddenStatusError(self._id)
     status = data.pop('status')
     if data:
         raise DataError({'status': "This is the only field that can be updated with this endpoint"})
     if not isinstance(status, str):
         raise DataError({'status': f"Value must be string, but found {type(status).__name__}"})
     self.import_data({'status': status})
Ejemplo n.º 2
0
 def update_car(self, data):
     if self.status == 'deleted':
         raise errors.ForbiddenStatusError(self._id)
     if 'status' in data:
         raise DataError({'status': "Can not be set with this endpoint. "
                                    "Please use /api/cars/:obj_id/status"})
     if 'vin' in data:
         raise DataError({'vin': "Can not be set with this endpoint. "
                                 "Please use /api/cars/:obj_id/vin"})
     self.import_data(data)
    def validate_body_schematic(self):
        if self._request_model.request_schematic_validation is None:
            return True

        # If the HTTP Response Validation Schematic has been set
        else:
            try:
                # The last part to test is does the Parsed JSON match the schematic validation
                self._request_model.request_schematic_validation(self._request_model.body) \
                    .validate()
                return True

            except DataError as e:
                message = """The request was unable to conform to the validation schematic.
                             The error provided by the schematic validation is {0}
                             To fix this you can either modify the schematic validation or remove it entirely
                             """.format(str(e))
                raise DataError(message)

            except TypeError as e:
                message = """The request was unable to conform to the validation schematic. The
                             schematic applied was {0}
                             The error provided by the schematic validation is {1}
                             To fix this you can either modify the schematic validation or remove it entirely
                             """.format(str(self._request_model.request_schematic_validation), e)
                raise TypeError(message)
def validate_auth(auth_type, auth_data):
    """
    Functionality:
        Validate the state of the two auth objects that will be used to apply authentication
        Test if the auth_schematic passed
        We also do a second test to make sure the auth_schematic.value is also a model
    """
    if auth_type is None and auth_data is None:
        return True

    if auth_type in authentication_choices and auth_type == "basic":
        if auth_data.get('username') is None or auth_data.get('password') is None:
            message = """For the Basic Authentication a username and password is required. The
                         current authentication data passed to this function is incorrect. The authentication
                         data is {0}""".format(str(auth_data))
            raise DataError(message)

        HTTPBasicAuth(auth_data.get('username'),
                      auth_data.get('password'))
        return True

    else:
        message = """We are unable to find the authentication defined within the http_method_choices var.
                     Please verify if the authentication type {0} exists within the http_method_choices
                     variable. If it does not then you need to either change the authentication type
                     or add the authentication variable inside the http_method_choices var""" \
            .format(str(auth_type))
        raise NotImplementedError(message)
Ejemplo n.º 5
0
 def import_data(self, raw_value, strict=False):
     if not self._schema.compare_tag_name(raw_value['tag']):
         raise DataError({raw_value['tag']: 'Mismatched tag name'})
     self._import_attributes(raw_value.get('attrib', {}), strict)
     self._import_children(raw_value.get('children', []), strict)
     self._import_content(raw_value.get('text'), strict)
     return self
Ejemplo n.º 6
0
 def _on_production_year_set(self, value):
     if not value:
         return
     try:
         datetime.strptime(value, '%Y')
     except ValueError:
         raise DataError({'production_year': "productionYear should match pattern '%Y'"})
Ejemplo n.º 7
0
 def _import_content(self, content, strict=False):
     if content is not None:
         for field_name, v in self._schema.content.items():
             self._data[field_name] = v.from_content(content)
             break
         else:
             if strict:
                 raise DataError({'text': 'Rogue content'})
Ejemplo n.º 8
0
    def validate(self, partial=False, convert=True, app_data=None, **kwargs):
        """Validate model."""
        super().validate(partial, convert, app_data, **kwargs)

        # role list checks
        if not isinstance(self.roles, list):
            raise DataError({'roles': 'Value is not a list'})
        # pylint: disable=not-an-iterable
        for role in self.roles:
            if role not in ROLES:
                raise DataError({'roles': 'Unknown role "%s"' % role})
        if len(self.roles) != len(set(self.roles)):
            raise DataError({'roles': 'Duplicate roles'})
        # pylint: disable=unsupported-membership-test
        if 'none' in self.roles and len(self.roles) > 1:
            raise DataError(
                {'roles': 'Role "none" can\'t be used with orher roles'})
Ejemplo n.º 9
0
 def _validate_post_request(self):
     """Load the post request into the validation class and perform validations."""
     if "audio" not in self.request.files:
         raise DataError(dict(audio="No audio file included in request"))
     if "metadata" not in self.request.files:
         raise DataError(
             dict(metadata="No metadata file included in request"))
     metadata = json.loads(self.request.files["metadata"].read().decode())
     upload_request = UploadRequest(
         dict(
             wake_word=metadata.get("wake_word"),
             engine=metadata.get("engine"),
             timestamp=metadata.get("timestamp"),
             model=metadata.get("model"),
         ))
     upload_request.validate()
     self.request_data = upload_request.to_native()
Ejemplo n.º 10
0
 def _import_attributes(self, attrib, strict=False):
     for attrib_name, attrib_value in attrib.items():
         for field_name, v in self._schema.attributes.items():
             if v.matches(field_name, attrib_name):
                 self._data[field_name] = v.from_attr(
                     field_name, attrib_value)
                 break
         else:
             if strict:
                 raise DataError({attrib_name: 'Rogue attribute'})
Ejemplo n.º 11
0
 def _get_skill(self):
     """Retrieve the skill associated with the request."""
     skill_repo = SkillRepository(self.db)
     self.skill = skill_repo.get_skill_by_global_id(
         self.request.json['skill_gid'])
     if self.skill is None:
         err_msg = ('No skill on database for skill ' +
                    self.request.json['skill_gid'])
         _log.error(err_msg)
         raise DataError(dict(skill_gid=[err_msg]))
Ejemplo n.º 12
0
        def validate_bar(cls, data, value):
            errors = {}

            if 'a' not in value:
                errors['a'] = ValidationError('Key a must be set.')
            if 'b' not in value:
                errors['b'] = ValidationError('Key b must be set.')

            if errors:
                raise DataError(errors)
Ejemplo n.º 13
0
    def validate(self, partial=False, convert=True, app_data=None, **kwargs):
        """Validate model."""
        super().validate(partial, convert, app_data, **kwargs)

        choices = []
        for district_choices in self.choices.values():
            for choice in district_choices.values():
                for choice_id in choice.keys():
                    if choice_id in choices:
                        raise DataError(
                            {'choices': f'Duplicate choice ID: {choice_id}'})
                    choices.append(choice_id)
Ejemplo n.º 14
0
 def _import_children(self, children, strict=False):
     for child in children:
         for field_name, v in self._schema.children.items():
             match_result = v.matches(field_name, child)
             if match_result:
                 self._data[field_name] = v.incorporate_child(
                     match_result, self._data.get(field_name, Undefined),
                     child)
                 break
         else:
             if strict:
                 raise DataError({child['tag']: 'Rogue child'})
Ejemplo n.º 15
0
    def _get_device_skill(self, device_id):
        """Retrieve the device's skill entry from the database."""
        device_skill = self.device_skill_repo.get_skill_settings_for_device(
            device_id, self.skill.id)
        if device_skill is None:
            error_msg = (
                'Received skill setting definition before manifest for '
                'skill ' + self.skill.skill_gid)
            _log.error(error_msg)
            raise DataError(dict(skill_gid=[error_msg]))

        return device_skill
Ejemplo n.º 16
0
 def _validate_post_request(self):
     """Load the post request into the validation class and perform validations."""
     upload_request = UploadRequest(
         dict(
             wake_word=self.request.form.get("wake_word"),
             engine=self.request.form.get("engine"),
             timestamp=self.request.form.get("timestamp"),
             model=self.request.form.get("model"),
         ))
     upload_request.validate()
     self.request_data = upload_request.to_native()
     if "audio_file" not in self.request.files:
         raise DataError(
             dict(audio_file="No audio file included in request"))
    def validate_body_schematic(self, json_decoded_response):
        if self._response_model.response_schematic_validation is None or \
                self._response_model.response is None:
            return True

        if issubclass(self._response_model.response_schematic_validation, Model) is True:
            try:
                if isinstance(json_decoded_response, dict) is False:
                    raise ValueError()

                # The last part to test is does the Parsed JSON match the schematic validation
                self._response_model.response_schematic_validation(json_decoded_response) \
                    .validate()

                return True

            except DataError as e:
                message = """The response was unable to conform to the validation schematic.
                             The error provided by the schematic validation is {0}
                             To fix this you can either modify the schematic validation or remove it entirely
                             """.format(str(e))
                raise DataError(message)

            except ValueError as e:
                message = """Unable to parse the response as JSON.
                             The response received was {0}.
                             Full error from the JSON parse attempt {1}
                             """.format(str(self._response_model.response.content), e)
                raise ValueError(message)

        else:
            message = """The proxy schematic does not contain the correct type.
                         The provided type is {0}. The function requires a valid schematic as a argument
                         This allows a response object to be tested against the schematic""" \
                .format(str(type(self._response_model.response_schematic_validation)))
            raise TypeError(message)
Ejemplo n.º 18
0
 def test_handle_data_error(self):
     e = DataError({"A1": "A1 Data Error"})
     resp = baseapp.handle_data_error(e)
     assert resp.status_code == 400
Ejemplo n.º 19
0
def get_figure(
        data,  # type: DataFrame
        filters=[],  # type: List[dict]
        group=None,  # type: Optional[dict]
        pivot=None,  # type: Optional[dict]
        kind='bar',  # type: str
        color_scheme={},  # type: Dict[str, str]
        x_axis=None,  # type: Optional[str]
        y_axis=None,  # type: Optional[str]
        title=None,  # type: Optional[str]
        x_title=None,  # type: Optional[str]
        y_title=None,  # type: Optional[str]
        bins=50,  # type: int
        bar_mode='stack',  # type: str
):
    '''
    Generates a plotly figure dictionary from given data and manipulations.

    Args:
        data (DataFrame): Data.
        filters (list[dict], optional): List of filters for data. Default: [].
        group (dict, optional): Grouping operation. Default: None.
        pivot (dict, optional): Pivot operation. Default: None.
        kind (str, optional): Kind of plot. Default: bar.
        color_scheme (dict[str, str], optional): Color scheme. Default: {}.
        x_axis (str): Column to use as x axis: Default: None.
        y_axis (str): Column to use as y axis: Default: None.
        title (str, optional): Title of plot. Default: None.
        x_title (str, optional): Title of x axis. Default: None.
        y_title (str, optional): Title of y axis. Default: None.
        bins (int, optional): Number of bins if histogram. Default: 50.
        bar_mode (str, optional): How bars in bar graph are presented.
            Default: stack.

    Raises:
        DataError: If any filter in filters is invalid.
        DataError: If group is invalid.
        DataError: If pivot is invalid.

    Returns:
        dict: Plotly Figure as dictionary.
    '''
    data = data.copy()

    # filter
    for f in filters:
        f = cfg.FilterAction(f)
        try:
            f.validate()
        except DataError as e:
            raise DataError({'Invalid filter': e.to_primitive()})

        f = f.to_primitive()
        if len(data) == 0:
            break
        data = filter_data(data, f['column'], f['comparator'], f['value'])

    # group
    if group is not None:
        grp = group  # type: Any
        grp = cfg.GroupAction(grp)
        try:
            grp.validate()
        except DataError as e:
            raise DataError({'Invalid group': e.to_primitive()})
        grp = grp.to_primitive()

        data = group_data(
            data,
            grp['columns'],
            grp['metric'],
            datetime_column=grp['datetime_column'],
        )

    # pivot
    if pivot is not None:
        pvt = pivot  # type: Any
        pvt = cfg.PivotAction(pvt)
        try:
            pvt.validate()
        except DataError as e:
            raise DataError({'Invalid pivot': e.to_primitive()})
        pvt = pvt.to_primitive()

        data = pivot_data(data,
                          pvt['columns'],
                          values=pvt['values'],
                          index=pvt['index'])

    # create figure
    figure = data.iplot(kind=kind,
                        asFigure=True,
                        theme='henanigans',
                        colorscale='henanigans',
                        x=x_axis,
                        y=y_axis,
                        title=title,
                        xTitle=x_title,
                        yTitle=y_title,
                        barmode=bar_mode,
                        bins=bins).to_dict()  # type: dict
    figure['layout']['title']['font']['color'] = '#F4F4F4'
    figure['layout']['xaxis']['title']['font']['color'] = '#F4F4F4'
    figure['layout']['yaxis']['title']['font']['color'] = '#F4F4F4'
    if color_scheme != {}:
        figure = conform_figure(figure, color_scheme)

    # makes area traces stackable
    if kind == 'area':
        for trace in figure['data']:
            trace['stackgroup'] = 1

    return figure
Ejemplo n.º 20
0
        dict(
            name='config',
            type='string',
            description='Database configuration as JSON string.',
            required=True,
            default='',
        )
    ],
         responses={
             200:
             dict(
                 description='Shekels database successfully initialized.',
                 content='application/json',
             ),
             400:
             dict(description='Invalid configuration.',
                  example=dict(
                      error='''
DataError(
    {'data_path': ValidationError([ErrorMessage("/foo.bar is not in a valid CSV file.", None)])}
)'''[1:],
                      success=False,
                  ))
         }))
def initialize():
    # type: () -> flask.Response
    '''
    Initialize database with given config.

    Raises:
        RuntimeError: If config is invalid.
Ejemplo n.º 21
0
 def _on_make_set(self, value):
     if not value:
         return
     if not value.isupper():
         raise DataError({'make': 'make should be uppercase'})
Ejemplo n.º 22
0
 def _on_model_set(self, value):
     if not value:
         return
     if not value.isupper():
         raise DataError({'model': 'model should be uppercase'})
Ejemplo n.º 23
0
    def post(self, team_id):
        """
        Message all team members
        ---
        tags:
          - teams
        produces:
          - application/json
        parameters:
            - in: header
              name: Authorization
              description: Base64 encoded session token
              required: true
              type: string
              default: Token sessionTokenHere==
            - name: team_id
              in: path
              description: Unique team ID
              required: true
              type: integer
              default: 1
            - in: body
              name: body
              required: true
              description: JSON object for creating message
              schema:
                properties:
                    subject:
                        type: string
                        default: Thanks
                        required: true
                    message:
                        type: string
                        default: Thanks for your contribution
                        required: true
        responses:
            200:
                description: Message sent successfully
            401:
                description: Unauthorized - Invalid credentials
            403:
                description: Forbidden
            500:
                description: Internal Server Error
        """
        try:
            authenticated_user_id = token_auth.current_user()
            team_id = request.view_args["team_id"]
            message_dto = MessageDTO(request.get_json())
            # Validate if team is present
            try:
                team = TeamService.get_team_by_id(team_id)
            except NotFound:
                return {"Error": "Team not found"}, 404

            is_manager = TeamService.is_user_team_manager(
                team_id, authenticated_user_id)
            if not is_manager:
                raise ValueError
            message_dto.from_user_id = authenticated_user_id
            message_dto.validate()
            if not message_dto.message.strip(
            ) or not message_dto.subject.strip():
                raise DataError({"Validation": "Empty message not allowed"})
        except DataError as e:
            current_app.logger.error(f"Error validating request: {str(e)}")
            return {"Error": "Request payload did not match validation"}, 400
        except ValueError:
            return {
                "Error": "Unauthorised to send message to team members"
            }, 403

        try:
            threading.Thread(
                target=TeamService.send_message_to_all_team_members,
                args=(team_id, team.name, message_dto),
            ).start()

            return {"Success": "Message sent successfully"}, 200
        except ValueError as e:
            return {"Error": str(e)}, 403
        except Exception as e:
            error_msg = f"Send message all - unhandled error: {str(e)}"
            current_app.logger.critical(error_msg)
            return {"Error": "Unable to send messages to team members"}, 500
Ejemplo n.º 24
0
            name='write_mode',
            type='string',
            description='How assets will be extracted to hidebound/data directory.',
            required=False,
            default='copy',
            enum=['copy', 'move'],
        )
    ],
    responses={
        200: dict(
            description='Hidebound database successfully initialized.',
            content='application/json',
        ),
        400: dict(
            description='Invalid configuration.',
            example=dict(
                error='''
DataError(
    {'write_mode': ValidationError([ErrorMessage("foo is not in ['copy', 'move'].", None)])}
)'''[1:],
                success=False,
            )
        )
    }
))
def initialize():
    # type: () -> flask.Response
    '''
    Initialize database with given config.

    Returns:
        Response: Flask Response instance.