Example #1
0
    def validate(self, value, form_index):
        super().validate(value, form_index)

        if not value:
            value = None

        if value is not None and type(value) is not dict:
            raise InvalidInputError(
                self.name,
                'request.body.form_array.{}.{}'.format(form_index, self.name))

        provider = self.provider

        if not value and self.required:
            raise RequiredInputError(
                self.name,
                'request.body.form_array.{}.{}'.format(form_index, self.name))

        if provider == 'doqer':
            if value is not None:
                valid = reduce(
                    and_,
                    map(lambda attr: attr in value and value[attr] is not None,
                        ['id', 'mime', 'name', 'type']))

                if not valid:
                    raise InvalidInputError(
                        self.name, 'request.body.form_array.{}.{}'.format(
                            form_index, self.name))
        else:
            raise MisconfiguredProvider(
                'File provider `{}` not implemented'.format(provider))

        return value
Example #2
0
    def validate(self, value, form_index):
        super().validate(value, form_index)

        curated = None

        if not value:
            curated = None
        elif isinstance(value, dict):
            if 'label' not in value or 'href' not in value:
                raise InvalidInputError(
                    self.name,
                    'request.body.form_array.{}.{}'.format(
                        form_index,
                        self.name
                    )
                )

            label = value['label']
            href = value['href']

            link_valid = re.match('^(https?://)[a-z0-9.]+/?$', href)

            if not link_valid:
                raise InvalidInputError(
                    self.name,
                    'request.body.form_array.{}.{}'.format(
                        form_index,
                        self.name
                    )
                )

            curated = {
                'label': label,
                'href': href,
            }
        else:
            raise InvalidInputError(
                self.name,
                'request.body.form_array.{}.{}'.format(
                    form_index,
                    self.name
                )
            )

        if not curated and self.required:
            raise RequiredInputError(
                self.name,
                'request.body.form_array.{}.{}'.format(form_index, self.name)
            )

        return curated
Example #3
0
    def validate(self, value, form_index):
        super().validate(value, form_index)

        if value is None:
            value = []

        if type(value) == str:
            try:
                value = ast.literal_eval(value)
            except SyntaxError:
                value = []

        if type(value) is not list:
            raise RequiredListError(
                self.name,
                'request.body.form_array.{}.{}'.format(form_index, value)
            )

        for val in value:
            if val not in self:
                raise InvalidInputError(
                    self.name,
                    'request.body.form_array.{}.{}'.format(
                        form_index,
                        self.name
                    )
                )

        return value
Example #4
0
    def validate_input(self, json_data):
        if 'response' not in json_data:
            raise RequiredInputError('response', 'request.body.response')

        if json_data['response'] not in self.VALID_RESPONSES:
            raise InvalidInputError('response', 'request.body.response')

        if json_data['response'] == 'reject':
            if 'inputs' not in json_data:
                raise RequiredInputError('inputs', 'request.body.inputs')

            if any([
                    type(json_data['inputs']) is not list,
                    len(json_data['inputs']) == 0,
            ]):
                raise RequiredListError('inputs', 'request.body.inputs')

            for index, field in enumerate(json_data['inputs']):
                errors = []
                try:
                    self.validate_field(field, index)
                except InputError as e:
                    errors.append(e.to_json())

                if errors:
                    raise BadRequest(errors)

            if 'comment' not in json_data:
                raise RequiredInputError('comment', 'request.body.comment')

            if type(json_data['comment']) is not str:
                raise BadRequest([{
                    'detail': '\'comment\' must be a str',
                    'code': 'validation.invalid',
                    'where': 'request.body.comment',
                }])

        return [
            Form.state_json(self.id, [
                {
                    'name': 'response',
                    'value': json_data['response'],
                    'value_caption': json_data['response'],
                },
                {
                    'name': 'comment',
                    'value': json_data['comment'],
                    'value_caption': json_data['comment'],
                },
                {
                    'name': 'inputs',
                    'value': json_data.get('inputs'),
                    'value_caption': json.dumps(json_data.get('inputs')),
                },
            ])
        ]
Example #5
0
    def validate_field(self, field, index):
        if type(field) != dict:
            raise RequiredDictError('inputs.{}'.format(index),
                                    'request.body.inputs.{}'.format(index))

        if 'ref' not in field:
            raise RequiredInputError(
                'inputs.{}.ref'.format(index),
                'request.body.inputs.{}.ref'.format(index))

        try:
            node, actor, ref, input = field['ref'].split('.')
            index, ref = ref.split(':')
        except ValueError:
            raise InvalidInputError('inputs.{}.ref'.format(index),
                                    'request.body.inputs.{}.ref'.format(index))

        if not self.in_dependencies(field['ref']):
            raise InvalidInputError('inputs.{}.ref'.format(index),
                                    'request.body.inputs.{}.ref'.format(index))
Example #6
0
def execution_add_user(id):
    ''' adds the user as a candidate for solving the given node, only if the
    node has an active pointer. '''
    # TODO possible race condition introduced here. How does this code work in
    # case the handler is moving the pointer?

    # get execution
    execution = Execution.get_or_exception(id)

    # validate the members needed
    validate_json(request.json, ['identifier', 'node_id'])

    identifier = request.json['identifier']
    node_id = request.json['node_id']

    # get actual pointer
    try:
        pointer = next(execution.proxy.pointers.q().filter(node_id=node_id))
    except StopIteration:
        raise BadRequest([{
            'detail': f'{node_id} does not have a live pointer',
            'code': 'validation.no_live_pointer',
            'where': 'request.body.node_id',
        }])

    # get user
    user = User.get_by('identifier', identifier)
    if user is None:
        raise InvalidInputError('user_id', 'request.body.identifier')

    # update user
    user.proxy.tasks.add(pointer)

    # update pointer
    collection = mongo.db[app.config['POINTER_COLLECTION']]
    db_pointer = collection.find_one({'id': pointer.id})
    user_json = user.to_json()
    notified_users = db_pointer.get('notified_users', [])

    if user_json not in notified_users:
        notified_users.append(user.to_json())

    collection.update_one(
        {'id': pointer.id},
        {'$set': {
            'notified_users': notified_users
        }},
    )

    return jsonify(user_json), 200
Example #7
0
    def validate(self, value, form_index):
        super().validate(value, form_index)

        if type(value) is not str and value is not None:
            raise RequiredStrError(
                self.name,
                'request.body.form_array.{}.{}'.format(form_index, self.name))

        if value not in self:

            raise InvalidInputError(
                self.name,
                'request.body.form_array.{}.{}'.format(form_index, self.name))

        return value
Example #8
0
    def validate_input(self, json_data):
        if 'response' not in json_data:
            raise RequiredInputError('response', 'request.body.response')

        if json_data['response'] not in self.VALID_RESPONSES:
            raise InvalidInputError('response', 'request.body.response')

        if json_data['response'] == 'reject':
            if 'inputs' not in json_data:
                raise RequiredInputError('inputs', 'request.body.inputs')

            if type(json_data['inputs']) is not list:
                raise RequiredListError('inputs', 'request.body.inputs')

            for index, field in enumerate(json_data['inputs']):
                errors = []
                try:
                    self.validate_field(field, index)
                except InputError as e:
                    errors.append(e.to_json())

                if errors:
                    raise BadRequest(errors)

        return [
            Form.state_json(self.id, [
                {
                    'name': 'response',
                    'value': json_data['response'],
                },
                {
                    'name': 'comment',
                    'value': json_data['comment'],
                },
                {
                    'name': 'inputs',
                    'value': json_data.get('inputs'),
                },
            ])
        ]