Esempio n. 1
0
    def post(self):
        us = ServiceLocator.resolve(ServiceLocator.USERS)

        v = Validator(check_params_schema)
        args = v.validated(request.get_json())

        if args is None:
            return ApiResponse(status=4001, errors=v.errors)

        try:
            login = args.get('login', None)
            email = args.get('email', None)

            login_result = login and us.check_login(login)
            email_result = email and us.check_email(email)

            return {
                'login': login_result,
                'email': email_result
            }

        except Exception as ex:
            error(u'UsersCheckAPI.post', ex)

            return {
                'login': None,
                'email': None
            }
Esempio n. 2
0
    def post(self):
        us = ServiceLocator.resolve(ServiceLocator.USERS)
        ss = ServiceLocator.resolve(ServiceLocator.SESSIONS)

        v = Validator(user_signin_schema)

        args = v.validated(request.get_json())
        if args is None:
            return ApiResponse(status=4001, errors=v.errors)

        login = args.get(u'login')
        password = args.get(u'password')

        user = us.sign_in(login, password)
        if user is None:
            return ApiResponse(status=4001, errors=errors.SignErrors.login_or_password_wrong(['login', 'password']))

        token = us.make_auth_token(user)

        # Save the user to session
        ss.create(user, token)

        user[u'auth_token'] = token

        return user
    def validate(self):

        """
        Validates the action_block based on the cerberus schema
        example:
        action_block :: sample ::
        - name: manipulate_inventory
          type: shell
          path: /tmp/shellscripts
          actions:
            - thisisshell.sh
        """

        schema = {
            'name': {'type': 'string', 'required': True},
            'type': {
                'type': 'string',
                'allowed': ['shell', 'subprocess']
            },
            'path': {'type': 'string', 'required': False},
            'context': {'type': 'boolean', 'required': False},
            'actions': {
                'type': 'list',
                'schema': {'type': 'string'},
                'required': True
            }
        }

        v = Validator(schema)
        status = v.validate(self.action_data)

        if not status:
            raise HookError("Invalid Syntax: {0}".format(str(v.errors)))
        else:
            return status
Esempio n. 4
0
 def _validate_equals(self, ref_value, field, value):
     """ {'type': 'string'} """
     if isinstance(ref_value, list):
         Validator._validate_allowed(self, ref_value, field, value)
     else:
         if value != ref_value:
             self._error(field, "Must be equal to " + ref_value)
Esempio n. 5
0
async def registration(request: web.Request) -> Dict:
    payload = await base.get_payload(request)

    validator = Validator(schema=users.schema)
    if not validator.validate(payload):
        raise ValidationError(validator.errors)

    async with request.app['engine'].acquire() as conn:
        count = await conn.scalar(
            sqlalchemy.select([sqlalchemy.func.count()])
                .select_from(users.table)
                .where(users.table.c.login == payload['login'])
        )

        if count:
            raise ValidationError({'login': '******'})

        user = {
            'login': payload['login'],
            'password': users.encrypt_password(payload['password']),
            'created_on': datetime.now()
        }
        user = await create_instance(user, users.table, conn)

    return base.json_response({
        'id': user['id'],
        'login': user['login']
    }, status=201)
Esempio n. 6
0
def validate_info():
    v = Validator()
    schema = {
        'title': {'required': True, 'type': 'string'},
        'version': {'required': True, 'type': 'string'},
        'description': {'type': 'string'},
        'termsOfService': {'type': 'string'},
        'contact': {
            'type': 'dict',
            'schema': {
                'name': {'type': 'string'},
                'url': {'type': 'string', 'validator': _validate_url},
                'email': {
                    'type': 'string',
                    'regex':
                    '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
                }
            }
        },
        'license': {
            'type': 'dict',
            'schema': {
                'name': {'type': 'string', 'required': True},
                'url': {'type': 'string', 'validator': _validate_url}
            }
        },
    }
    if eve_swagger.INFO not in app.config:
        raise ConfigException('%s setting is required in Eve configuration.' %
                              eve_swagger.INFO)

    if not v.validate(app.config[eve_swagger.INFO], schema):
        raise ConfigException('%s is misconfigured: %s' % (
            eve_swagger.INFO, v.errors))
Esempio n. 7
0
def add_friend_to_group(id):
    user = current_user._get_current_object()  
    form = request.get_json()
    schema = {
                'id':{'type':'integer','empty':False},
            } 
    v = Validator(schema)   
    if v.validate(form) is False:
        return api_validation_error(v.errors)  
    #first see if this is a valid group
    group = FriendsGroup.query.filter(and_(FriendsGroup.user_id == user.id,FriendsGroup.id == id)).first()

    if group:
        #lets see if this is a confirmed friend of yours
        friend = Friends.query.filter(and_(Friends.user_id == user.id,Friends.friend_id == form['id'])).first()

        if friend:
            #lets see if this friend is not in this group
            if friend.group_id == group.id:
                return api_error("This friend is already in this group")
            else:
                friend.group_id = group.id
                meeple.db.session.commit()
                return api_package()
        else:
            return api_error("Friend does not exist")
    else:
        return api_error("This group does not exist.")
Esempio n. 8
0
 def validate(self):
     # pdb.set_trace()
     v = Validator(self.schema)
     status = v.validate(self.result)
     if not status:
         return v.errors
     return None
Esempio n. 9
0
    def post(self):
        u"""
        Creates new card and adds it to group.
        :return: Card
        """

        v = Validator(card_schema)
        args = v.validated(request.get_json())

        if args is None:
            return ApiResponse(status=4001, errors=v.errors)

        foreign = args.get(u'foreign')
        native = args.get(u'native')
        group_id = args.get(u'group_id')

        transcription = args.get(u'transcription', u'')
        context = args.get(u'context', u'')

        user = self.ss.get_user()
        group = self.gs.single(group_id, user)

        exists_card = self.cs.exists(user, foreign, user.foreign_lng)
        if exists_card:
            return ApiResponse(status=409, errors=CardsErrors.card_already_exists(foreign, [u'foreign']))

        card = self.cs.create(user, group, foreign, native, transcription, context)
        if card is None:
            return ApiResponse(status=500, errors=ServerErrors.internal_server_error([]))

        return card
Esempio n. 10
0
File: etl.py Progetto: kianho/bxrec
def check_bx_users(fn):
    """Check the consistentcy of the BX-Users.csv file.

    """

    tab = etl.fromcsv(fn, delimiter=DELIMITER,
            quoting=QUOTE_ALL, encoding=ENCODING)

    v = Validator({
            "User-ID" : {
                "type" : "string",
                "regex" : USER_ID_PAT,
                "required" : True
            },
            "Location" : {
                "type" : "string",
                "required" : True
            },
            "Age" : {
                "type" : "string",
                "validator" : validate_age,
                "required" : True
            }
        })

    for row_num, r in enumerate(tab.dicts(), 1):
        is_valid = v.validate(r)
        if not is_valid:
            print "row %d -> %r, %r" % (row_num, v.errors, r)

    return
Esempio n. 11
0
File: etl.py Progetto: kianho/bxrec
def check_bx_book_ratings(fn):
    """Check the consistency of the BX-Book-Ratings.csv file.

    """

    tab = etl.fromcsv(fn, delimiter=DELIMITER,
            quoting=QUOTE_ALL, encoding=ENCODING)

    v = Validator({
            "User-ID" : {
                "type" : "string",
                "required" : True
            },
            "ISBN" : {
                "type" : "string",
                "required" : True
            },
            "Book-Rating" : {
                "type" : "string",
                "validator" : validate_rating,
                "required" : True
            }
        })

    for row_num, r in enumerate(tab.dicts(), 1):
        is_valid = v.validate(r)
        if not is_valid:
            print "row %d -> %r, %r" % (row_num, v.errors, r)

    return
Esempio n. 12
0
def allowed_for_single_value():
    """对于值进行限制, 只能使预定义的几个值中的一个。
    """
    schema = {"label": {"type": "integer", "allowed": [1, 2, 3]}}
    v = Validator(schema)
    
    print(v.validate({"label": 1}))
Esempio n. 13
0
def validate_cred(cred, f):
    v = Validator()
    valid = v.validate(cred, schema)
    for e in v.errors:
        logger.error("[validate_cred] Validation Error: %s, %s - %s" % (f, e, v.errors[e]))

    return valid
Esempio n. 14
0
def test_ignore_none_values():
    field = 'test'
    schema = {field: {'type': 'string', 'empty': False, 'required': False}}
    document = {field: None}

    # Test normal behaviour
    validator = Validator(schema, ignore_none_values=False)
    assert_fail(document, validator=validator)
    validator.schema[field]['required'] = True
    validator.schema.validate()
    _errors = assert_fail(document, validator=validator)
    assert_not_has_error(_errors, field, (field, 'required'),
                         errors.REQUIRED_FIELD, True)

    # Test ignore None behaviour
    validator = Validator(schema, ignore_none_values=True)
    validator.schema[field]['required'] = False
    validator.schema.validate()
    assert_success(document, validator=validator)
    validator.schema[field]['required'] = True
    _errors = assert_fail(schema=schema, document=document, validator=validator)
    assert_has_error(_errors, field, (field, 'required'), errors.REQUIRED_FIELD,
                     True)
    assert_not_has_error(_errors, field, (field, 'type'), errors.BAD_TYPE,
                         'string')
Esempio n. 15
0
    def put(self, request, *args, **kwargs):
        team_info = json.loads(request.body.decode('utf-8'))

        validator = Validator(team_schema)
        if not validator.validate(team_info):
            return JsonResponse({'error': validator.errors})

        try:
            team = Team.objects.get(id=int(self.kwargs['team_id']))
        except ObjectDoesNotExist:
            return JsonResponse({'error': [{"Team ID": "Team not found for ID in request"}]})

        check_scope(request, team)

        report = team.report_set.first()
        if 'send_time' in team_info:
            report.survey_send_time = parser.parse(team_info['send_time']).replace(second=0, microsecond=0)
        if 'summary_time' in team_info:
            report.summary_send_time = parser.parse(team_info['summary_time']).replace(second=0, microsecond=0)
        if 'days_of_week' in team_info:
            rule = recurrence.Rule(recurrence.WEEKLY, byday=team_info['days_of_week'])
            rec = recurrence.Recurrence(rrules=[rule])
            report.recurrences = rec
        if 'name' in team_info:
            team.name = team_info['name']
        report.save()

        try:
            team.save()
        except IntegrityError:
            return JsonResponse({'error': {"name": _("team with this name already exists")}})

        team_dict = model_to_dict(team, exclude=['users'])
        team_dict['report'] = self.report_dict(team)
        return JsonResponse({'team': team_dict})
    def validate(self):

        """
        Validates the action_block based on the cerberus schema
        """

        schema = {
            'name': {'type': 'string', 'required': True},
            'type': {'type': 'string', 'allowed': ['ruby']},
            'path': {'type': 'string', 'required': False},
            'context': {'type': 'boolean', 'required': False},
            'actions': {
                'type': 'list',
                'schema': {'type': 'string'},
                'required': True
            }
        }

        v = Validator(schema)
        status = v.validate(self.action_data)

        if not status:
            raise HookError("Invalid syntax: {0}".format(str((v.errors))))
        else:
            return status
Esempio n. 17
0
def new_place():
    if flask.request.method == 'POST':
        v = Validator({
            'name': {'type': 'string', 'minlength': 3},
            'description': {'type': 'string', 'required': True},
            'location': {'type': 'string', 'required': True},
        })

        form = dict(flask.request.form.items())

        try:
            del form['g-recaptcha-response']
        except KeyError:
            pass

        if check_recaptcha() and v.validate(form):
            author = make_author()
            place = Place(v.document['name'], v.document['description'],
                          v.document['location'], author)
            place.scale = PlaceScale()
            flask.g.sql_session.add(place)
            flask.g.sql_session.commit()
            return flask.redirect(flask.url_for('.place', slug=place.slug))
        else:
            return flask.render_template('place/new.html', errors=v.errors)
    return flask.render_template('place/new.html')
Esempio n. 18
0
    def validate_filter(filter):
        for key, value in filter.items():
            if '*' not in allowed and key not in allowed:
                return "filter on '%s' not allowed" % key

            if key in ('$or', '$and', '$nor'):
                if not isinstance(value, list):
                    return "operator '%s' expects a list of sub-queries" % key
                for v in value:
                    if not isinstance(v, dict):
                        return "operator '%s' expects a list of sub-queries" \
                            % key
                    r = validate_filter(v)
                    if r:
                        return r
            else:
                if config.VALIDATE_FILTERS:
                    res_schema = config.DOMAIN[resource]['schema']
                    if key not in res_schema:
                        return "filter on '%s' is invalid"
                    else:
                        field_schema = res_schema.get(key)
                        v = Validator({key: field_schema})
                        if not v.validate({key: value}):
                            return "filter on '%s' is invalid"
                        else:
                            return None
Esempio n. 19
0
def confirm_friend():
    user = current_user._get_current_object()
    form = request.get_json()
    schema = {
        'id':{'type':'integer','empty':False}
    }
    v = Validator(schema)
    if v.validate(form) is False:
        return api_validation_error(v.errors)

    friend_request = FriendRequests.query.filter(FriendRequests.id == form['id']).first()
    if friend_request:

        #we are confirming this, so create friendships that last both ways.
        my_friend = Friends(user_id = user.id,friend_id = friend_request.friend_id)
        their_friend = Friends(user_id = friend_request.friend_id,friend_id = user.id)

        meeple.db.session.add(my_friend)
        meeple.db.session.add(their_friend)
        meeple.db.session.delete(friend_request) #remove this friend request
        meeple.db.session.commit()

        #now return who this friend is back to them.
        return api_package(data=my_friend.as_dict())
    else:
        return api_error("This request does not exist")
Esempio n. 20
0
    def __init__(self, environment_name, nova_descriptor_file=None):
        self._nova_descriptor_file = nova_descriptor_file or 'nova.yml'
        self._environment_name = environment_name
        self._environment = None
        self._codedeploy_app = None

        self.templates_used = dict()
        yaml.add_constructor("!include", yaml_include)

        with open(os.path.join(spec.__path__[0], 'nova_service_schema.yml'), 'r') as schemaYaml:
            schema = yaml.load(schemaYaml)

        v = Validator(schema)
        try:
            with open(self._nova_descriptor_file, 'r') as novaYaml:
                self.service_spec = yaml.safe_load(novaYaml)

            # Validate loaded dictionary
            valid = v.validate(self.service_spec)
            if not valid:
                raise NovaError("Invalid nova service descriptor file '%s': %s" % (self._nova_descriptor_file, v.errors))
            else:
                self.service = Service.load(self.service_spec)
                self.service_name = self.service.name
                self.service_port = self.service.port
                self.service_healthcheck_url = self.service.healthcheck_url
        except IOError:
            raise NovaError("No nova service descriptor found at '%s'" % self._nova_descriptor_file)
Esempio n. 21
0
    def _validate(self, doc, **kwargs):
        lookup = {'act': doc['act'], 'type': doc[ITEM_TYPE]}
        use_headline = kwargs and 'headline' in kwargs
        validators = superdesk.get_resource_service('validators').get(req=None, lookup=lookup)
        for validator in validators:
            v = Validator()
            v.allow_unknown = True
            v.validate(doc['validate'], validator['schema'])
            error_list = v.errors
            response = []
            for e in error_list:
                if error_list[e] == 'required field' or type(error_list[e]) is dict:
                    message = '{} is a required field'.format(e.upper())
                elif 'min length is' in error_list[e]:
                    message = '{} is too short'.format(e.upper())
                elif 'max length is' in error_list[e]:
                    message = '{} is too long'.format(e.upper())
                else:
                    message = '{} {}'.format(e.upper(), error_list[e])

                if use_headline:
                    response.append('{}: {}'.format(doc['validate'].get('headline',
                                                                        doc['validate'].get('_id')), message))
                else:
                    response.append(message)
            return response
        else:
            return ['validator was not found for {}'.format(doc['act'])]
Esempio n. 22
0
 def _validate_page(self, page):
     schema = {'page': {'type': 'integer', 'min': 1}}
     v = Validator(schema)
     try:
         return v.validate({'page': int(page)})
     except Exception:
         return False
Esempio n. 23
0
def validate_oauth_authorization_request(response_type, client_id,
                                         redirect_uri, scope, state):
    """Validate an OAuth authentication request for an implicit grant.

    See https://tools.ietf.org/html/rfc6749#section-4.2.1

    Returns:
        str: The actual URL the client should be redirected to.
    """
    oauth_schema = {
        'response_type': {
            'required': True,
            'type': 'string',
            'allowed': ['token']
        },
        'client_id': {
            'required': True,
            'type': 'string',
            'empty': False
        },
        'redirect_uri': {
            'type': 'string',
            'nullable': True,
            'regex': REDIRECT_URI_REGEX
        },
        'scope': {
            'nullable': True,
            'type': 'string'
        },
        'state': {
            'nullable': True,
            'type': 'string'
        }
    }
    validator = Validator(oauth_schema)
    valid = validator.validate({
        'response_type': response_type,
        'client_id': client_id,
        'redirect_uri': redirect_uri,
        'scope': scope,
        'state': state
    })
    if not valid:
        abort(422, 'Invalid parameters. Please contact the author of the page '
                   'that sent you here. Errors: %s' % validator.errors)

    db = current_app.data.driver.db['oauthclients']
    client = db.find_one({'client_id': client_id})
    if not client:
        abort(422, 'Unknown client_id. Please contact the author of the page '
                   'that sent you here.')

    if not redirect_uri:
        redirect_uri = client['redirect_uri']

    if not redirect_uri.startswith(client['redirect_uri']):
        abort(422, "Redirect URI is not whitelisted for client_id!")

    return redirect_uri
Esempio n. 24
0
def validate_json(json, schema):
    v = Validator(schema)
    if json is None \
            or 'data' not in json \
            or not isinstance(json['data'], dict) \
            or not v.validate(json['data']):
        return False, v.errors
    return True, {}
Esempio n. 25
0
    def _validate_numberRange(self, ref_range, field, value):
        # check is min < max
        if ref_range[0] >= ref_range[1]:
            raise Exception('min > max in range value')

        if value.isdigit():
            Validator._validate_min(self, ref_range[0], field, float(value))
            Validator._validate_max(self, ref_range[1], field, float(value))
Esempio n. 26
0
def test_coerce_not_destructive():
    schema = {
        'amount': {'coerce': int}
    }
    v = Validator(schema)
    doc = {'amount': '1'}
    v.validate(doc)
    assert v.document is not doc
def validate_request_password_reset(req, res, resource, params):
    schema = {
        'email': FIELDS['email']
    }

    v = Validator(schema)
    if not v.validate(req.context['data']):
        raise falcon.HTTPBadRequest('Bad request', v.errors)
def validate_confirm_password_reset(req, res, resource, params):
    schema = {
        'code': FIELDS['code'],
        'password': FIELDS['password_create']
    }

    v = Validator(schema)
    if not v.validate(req.context['data']):
        raise falcon.HTTPBadRequest('Bad request', v.errors)
Esempio n. 29
0
def set_type_example():
    """如果要求list中的元素不可以重复, 那么可以使用set类型。但是注意! 流行的文档
    格式Json,和mongodb中的document, 都不支持python原生set类型。所以该功能在用于
    数据库入库的检查一类的应用, 都要慎用。下一例提供了一种解决方案。
    """
    schema = {"tag": {"type": "set"}}
    v = Validator(schema)
    
    print(v.validate({"tag": set(["drama", "crime"])}))
Esempio n. 30
0
def dependency_example():
    """通过``dependencies``可以定义某项存在的前提条件是其他项。
    """
    schema = {"level1": {"type": "number"},
              "level2": {"type": "number", "dependencies": ["level1"]}}
    v = Validator(schema)
    
    print(v.validate({"level2": 100}))
    print(v.errors)
 def validate_cancel_parking_slot_booking_api(data):
     vf = Validator(APIValidator.cancel_parking_slot_booking_validator)
     return vf.validate(
         data), APIValidator.cancel_parking_slot_booking_validator
Esempio n. 32
0
    },
    "zip": {
        'type': 'integer',
        'required': True
    },
    "email": {
        'type': 'string',
        'required': True,
        'regex': '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
    },
    "web": {
        'type': 'string',
        'required': True
    },
    "age": {
        'type': 'integer',
        'required': True,
        'min': 18,
        'max': 99
    }
}

validator = Validator(schema)


def partialValidator(data):
    partialSchema = {}
    for key in data.keys():
        partialSchema[key] = schema[key]
    return Validator(partialSchema)
Esempio n. 33
0
def test_repr():
    v = Validator({'foo': {'type': 'string'}})
    assert repr(v.schema) == "{'foo': {'type': 'string'}}"
Esempio n. 34
0
def test_coerce_not_destructive():
    schema = {'amount': {'coerce': int}}
    v = Validator(schema)
    doc = {'amount': '1'}
    v.validate(doc)
    assert v.document is not doc
Esempio n. 35
0
def f_test_major(major):
    v = Validator(api_schemas.major_schema)
    correct = v.validate(major)
    assert correct == True
Esempio n. 36
0
    def post(self, request):
        """
        """
        validator = Validator({
            "malls": {
                "required": True,
                "empty": False,
                "type": "list",
                "schema": {
                    "type": "number",
                    "required": True,
                    "empty": False
                }
            },
            "email": {
                "required": True,
                "empty": False,
                "type": "string",
                "regex": '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
            },
            "temperature": {
                "required": True,
                "empty": False,
                "type": "number"
            }
        })
        if not validator.validate(request.data):
            return Response(
                {
                    "code": "invalid_body",
                    "detail": "Invalid request data",
                    "data": validator.errors
                },
                status=status.HTTP_400_BAD_REQUEST)

        visitor_instance = Visitor.objects.filter(
            email=request.data.pop("email"))

        if (not visitor_instance.exists() or not all(
                map(lambda x: Mall.objects.filter(pk=x).exists(),
                    set(request.data.get("malls"))))):
            return Response(
                {
                    "code":
                    ("visitor_not_found"
                     if not visitor_instance.exists() else "mall_not_found"),
                    "detail":
                    "Requested dependency was not found"
                },
                status=status.HTTP_404_NOT_FOUND)

        if not visitor_instance.first().enabled:
            if timezone.now() - Registry.objects.filter(
                    visitor=visitor_instance.first()).order_by(
                        "-created_at")[0].created_at > timedelta(days=6):
                visitor_instance.update(enabled=True)
            else:
                return Response(
                    {
                        "code": "dangerous_visitor",
                        "detail": "You are still sick, go home and rest",
                    },
                    status=status.HTTP_400_BAD_REQUEST)

        serializer = RegistrySerializer(data={
            "visitor": visitor_instance.first().pk,
            **request.data
        })
        if not serializer.is_valid():
            return Response(
                {
                    "code": "invalid_body",
                    "detail": "There was and error creating an instance",
                    "data": serializer.errors
                },
                status=status.HTTP_400_BAD_REQUEST)

        instance = serializer.create(validated_data={
            "visitor": visitor_instance.first().pk,
            **request.data
        })
        if request.data.get("temperature") > 38:
            visitor_instance.update(enabled=False)
            return Response(
                {
                    "code": "dangerous_visitor",
                    "detail": "You are sick, go home and rest"
                },
                status=status.HTTP_200_OK)

        return Response(status=status.HTTP_200_OK)
Esempio n. 37
0
def update_performer(curr_id):
    v = Validator(purge_unknown=True)
    schema = {
        'name': {
            'type': 'string'
        },
        'phone': {
            'type': 'string'
            # 'regex' : REGEX_PHONE
        },
        'description': {
            'type': 'string'
        },
        'newPhoto': {},
        'services': {
            'type': 'list'
        },
        'work_beg': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'work_end': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'lunch_beg': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'lunch_end': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'non_working_days': {
            'type': 'list',
            'default': []
        }
    }
    req_data = v.validated(request.get_json(), schema)
    if not req_data:
        abort(400, v.errors)
    performer = Performer.get(curr_id)
    if not performer:
        return jsonify(
            {"msg": "{0} doesn't exist".format(type(performer).__name__)}), 400
    if 'services' in req_data.keys():
        old_services = set(list(map(lambda x: x.id, performer.services)))
        new_services = set(req_data['services'])
        to_remove = old_services.difference(new_services)
        to_add = new_services.difference(old_services)
        try:
            if len(to_remove) > 0:
                db.session.execute(performer_service.delete().where(
                    and_(performer_service.c.performer_id == curr_id,
                         performer_service.c.service_id.in_(to_remove))))
            if len(to_add) > 0:
                db.session.execute(performer_service.insert(),
                                   [{
                                       'performer_id': curr_id,
                                       'service_id': x
                                   } for x in to_add])
        except:
            db.session.rollback()
            return jsonify({"msg":
                            "Probably performers parameter is invalid"}), 400

    if 'photo' in req_data.keys():
        if performer.photo_id:
            Image.query.filter(Image.id == performer.photo_id).delete()

        if req_data['photo']:
            photo = Image(data=req_data['photo'])
            db.session.add(photo)
            db.session.commit()
            performer.photo_id = photo.id

    return update(performer, {
        key: req_data[key]
        for key in req_data if key not in ['services', 'photo']
    })
Esempio n. 38
0
def create_performer():
    v = Validator(purge_unknown=True)
    schema = {
        'name': {
            'type': 'string',
            'required': True
        },
        'phone': {
            'type': 'string',
            'required': True
            # 'regex' : REGEX_PHONE
        },
        'description': {
            'type': 'string',
            'required': True
        },
        'photo': {},
        'services': {
            'type': 'list'
        },
        'work_beg': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'work_end': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'lunch_beg': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'lunch_end': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'non_working_days': {
            'type': 'list',
            'default': []
        }
    }
    req_data = v.validated(request.get_json(), schema)
    if not req_data:
        abort(400, v.errors)
    performer = db.session.query(Performer).filter(
        Performer.name == req_data['name']).first()
    if performer:
        return jsonify({"msg": "Performer already exists"}), 400
    performer = Performer(business_id=Business.get(get_jwt_claims()['id']).id)

    if ('photo' in req_data.keys()) and req_data['photo']:
        photo = Image(data=req_data['photo'])
        db.session.add(photo)
        db.session.commit()
        performer.photo_id = photo.id

    msg = add(performer, {
        key: req_data[key]
        for key in req_data if key not in ['services', 'photo']
    })
    try:
        if ('services' in req_data.keys()) and (len(req_data['services']) > 0):
            db.session.execute(performer_service.insert(),
                               [{
                                   'performer_id': performer.id,
                                   'service_id': x
                               } for x in req_data['services']])
            db.session.commit()
    except:
        return jsonify({"msg": "Probably services parameter is invalid"}), 400

    return msg
Esempio n. 39
0
    def create_initial_data():
        if mongo.Users().find_one() is not None:
            logger.info("we already have users. not creating initial data")
            return

        user_document = {
            "username":
            "******",
            "password_hash":
            generate_password_hash(
                os.getenv("MANAGER_ACCOUNT_PASSWORD", "manager")),
            "email":
            "*****@*****.**",
            "role":
            mongo.Users.MANAGER_ROLE,
            "channel":
            "kiwix",
            "active":
            True,
        }

        validator = Validator(mongo.Users.schema)
        if not validator.validate(user_document):
            logger.info("user_document is not valid for schema")
        else:
            logger.info("created user: {}".format(
                mongo.Users().insert_one(user_document)))

        channel_document = {
            "slug":
            "kiwix",
            "name":
            "Kiwix",
            "active":
            True,
            "private":
            False,
            "sender_name":
            "Kiwix",
            "sender_email":
            "*****@*****.**",
            "sender_address":
            "c/o Studio Banana\nAvenue des acacias 7\n1 006 Lausanne, Switzerland",
        }

        validator = Validator(mongo.Channels.schema)
        if not validator.validate(channel_document):
            logger.info("channel_document is not valid for schema")
        else:
            logger.info("created channel: {}".format(
                mongo.Channels().insert_one(channel_document)))

        warehouse_document = {
            "slug":
            "kiwix",
            "upload_uri":
            os.getenv(
                "DEFAULT_WAREHOUSE_UPLOAD_URI",
                "ftp://warehouse.cardshop.hotspot.kiwix.org:2121",
            ),
            "download_uri":
            os.getenv(
                "DEFAULT_WAREHOUSE_DOWNLOAD_URI",
                "ftp://warehouse.cardshop.hotspot.kiwix.org:2121",
            ),
            "active":
            True,
        }

        validator = Validator(mongo.Warehouses.schema)
        if not validator.validate(warehouse_document):
            logger.info("warehouse_document is not valid for schema")
        else:
            logger.info("created warehouse: {}".format(
                mongo.Warehouses().insert_one(warehouse_document)))

        warehouse_document = {
            "slug":
            "download",
            "upload_uri":
            os.getenv(
                "DOWNLOAD_WAREHOUSE_UPLOAD_URI",
                "ftp://download.cardshop.hotspot.kiwix.org:2221",
            ),
            "download_uri":
            os.getenv(
                "DOWNLOAD_WAREHOUSE_DOWNLOAD_URI",
                "https://download.cardshop.hotspot.kiwix.org",
            ),
            "active":
            True,
        }

        validator = Validator(mongo.Warehouses.schema)
        if not validator.validate(warehouse_document):
            logger.info("warehouse_document is not valid for schema")
        else:
            logger.info("created warehouse: {}".format(
                mongo.Warehouses().insert_one(warehouse_document)))
Esempio n. 40
0
def test_allow_unknown_with_purge_unknown():
    validator = Validator(purge_unknown=True)
    schema = {'foo': {'type': 'dict', 'allow_unknown': True}}
    document = {'foo': {'bar': True}, 'bar': 'foo'}
    expected = {'foo': {'bar': True}}
    assert_normalized(document, expected, schema, validator)
Esempio n. 41
0
def test_allow_unknown_wo_schema():
    # https://github.com/pyeve/cerberus/issues/302
    v = Validator({'a': {'type': 'dict', 'allow_unknown': True}})
    v({'a': {}})
Esempio n. 42
0
def test_purge_unknown():
    validator = Validator(purge_unknown=True)
    schema = {'foo': {'type': 'string'}}
    document = {'bar': 'foo'}
    expected = {}
    assert_normalized(document, expected, schema, validator)
Esempio n. 43
0
        "type": "datetime"
    }
}

schema_auth = {
    "username": {
        "type": "string",
        "required": True
    },
    "password": {
        "type": "string",
        "required": True
    }
}

validator = Validator(schema)
validator_auth = Validator(schema_auth)


class UserModel(db.Model):
    __tablename__ = 'users'
    _id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20))
    password = db.Column(db.String(250))
    role = db.Column(db.String(12), server_default='user')
    created_at = db.Column(db.DateTime, default=datetime)
    updated_at = db.Column(db.DateTime)

    def __init__(self, username, password, role, updated_at=None):
        self.username = username
        self.role = role
Esempio n. 44
0
def schema_validator(route):
    schema = {'route': {'type': 'string', 'maxlength': 1}}
    v = Validator(schema)
    info = {'route': route}
    return v.validate(info)
Esempio n. 45
0
import yaml
from cerberus import Validator


def load_doc():
    with open('./my_yaml.yaml', 'r') as stream:
        try:
            return yaml.load(stream)
        except yaml.YAMLError as exception:
            raise exception


schema = eval(open('./schema.py', 'r').read())
v = Validator(schema)
doc = load_doc()
print(v.validate(doc, schema))
print(v.errors)
Esempio n. 46
0
    def test_get_subitems(self):
        kit = models.FirstAidKit.objects.get(pk=1)
        dummy_common = {
            "molecules": {
                1: {
                    "exp_dates": [],
                    "quantity": 0,
                    "contents": [],
                    "has_nc": False,
                    "has_date_warning": False,
                    "has_date_expired": False
                },
                2: {
                    "exp_dates": [],
                    "quantity": 0,
                    "contents": [],
                    "has_nc": False,
                    "has_date_warning": False,
                    "has_date_expired": False
                },
                3: {
                    "exp_dates": [],
                    "quantity": 0,
                    "contents": [],
                    "has_nc": False,
                    "has_date_warning": False,
                    "has_date_expired": False
                }
            },
            "equipments": {
                1: {
                    "exp_dates": [],
                    "quantity": 0,
                    "contents": [],
                    "has_nc": False,
                    "has_date_warning": False,
                    "has_date_expired": False
                },
                2: {
                    "exp_dates": [],
                    "quantity": 0,
                    "contents": [],
                    "has_nc": False,
                    "has_date_warning": False,
                    "has_date_expired": False
                },
                3: {
                    "exp_dates": [],
                    "quantity": 0,
                    "contents": [],
                    "has_nc": False,
                    "has_date_warning": False,
                    "has_date_expired": False
                }
            }
        }

        output = first_aid.get_subitems(self.params, kit, dummy_common)

        schema_path = settings.VALIDATOR_PATH.joinpath("parsers", "first_aid",
                                                       "subitems.json")
        schema = json.loads(schema_path.read_text())
        validator = Validator(schema)
        result = validator.validate({"data": output})
        if not result:
            log.error(validator.errors)
            log.debug(output)
        self.assertTrue(result)
Esempio n. 47
0
def validateOutput() : 
    url = "http://localhost:7071/api/Deliveries"
    header = {
   'Authorization': 'Bearer 997a139c203f4c2183cef6babc3ce469'}
    r = requests.get(url, headers = header)
    if (r.status_code == 200) :
        j = r.json()
        j = makeJsonObjectReadable(j)
        schema = {
        "rows" : {"type" : "list", "schema" : {
            "Type" : "dict", "schema" : {
            "Id" : {"type" : "string"}, 
            "CurrentId" : {"type" : "dict", "schema" : {
            "Id" : {"type" : "string"}, 
            "Address" : {"type" : "string"}, 
            "IsWarehouse" : {"type" : "boolean"}, 
            "Latitude" : {"type" : "number"}, 
            "Longitude" : {"type" : "number"}, 
            "Place" : {"type" : "string"}, 
            "PostalCode" : {"type" : "string"}
            }}, 
            "CustomerDistanceInKilometers" : {"type" : "number"}, 
            "CustomerETA" : {"type" : "string"}, 
            "CustomerId" : {"type" : "dict", "schema" : {
            "Id" : {"type" : "string"}, 
            "Address" : {"type" : "string"}, 
            "IsWarehouse" : {"type" : "boolean"}, 
            "Latitude" : {"type" : "number"}, 
            "Longitude" : {"type" : "number"}, 
            "Place" : {"type" : "string"}, 
            "PostalCode" : {"type" : "string"}
            }}, 
            "CustomerPhoneNumber" : {"type" : "string"}, 
            "DeliveredAt" : {"type" : "string"}, 
            "DelivererId" : {"type" : "string"}, 
            "DueDate" : {"type" : "string"}, 
            "PaymentMethod" : {"type" : "integer"}, 
            "Price" : {"type" : "number"}, 
             "StartedAtId" : {"type" : "dict", "schema" : {
            "Id" : {"type" : "string"}, 
            "Address" : {"type" : "string"}, 
            "IsWarehouse" : {"type" : "boolean"}, 
            "Latitude" : {"type" : "number"}, 
            "Longitude" : {"type" : "number"}, 
            "Place" : {"type" : "string"}, 
            "PostalCode" : {"type" : "string"}
            }}, 
            "Status" : {"type" : "integer"}, 
            "Tip" : {"type" : "number"}, 
            "Vehicle" : {"type" : "integer"}, 
            "WarehouseDistanceInKilometers" : {"type" : "number"}, 
            "WarehouseETA" : {"type" : "string"}, 
            "WarehouseId" : {"type" : "dict", "schema" : {
            "Id" : {"type" : "string"}, 
            "Address" : {"type" : "string"}, 
            "IsWarehouse" : {"type" : "boolean"}, 
            "Latitude" : {"type" : "number"}, 
            "Longitude" : {"type" : "number"}, 
            "Place" : {"type" : "string"}, 
            "PostalCode" : {"type" : "string"}
            }}, 
            "WarehousePickUpAt" : {"type" : "string"}
            }
            }}
        }
        v = Validator(schema)
        assert v.validate(j)
    
    assert r.status_code == 200|204
Esempio n. 48
0
def flexio_handler(flex):

    # generate values using following library:
    # project: https://github.com/joke2k/faker
    # documentation: https://faker.readthedocs.io/en/latest/index.html
    # providers: https://faker.readthedocs.io/en/latest/providers.html
    faker = Faker()

    # get the input
    input = flex.input.read()
    input = json.loads(input)
    if not isinstance(input, list):
        input = []

    # define the expected parameters and map the values to the parameter names
    # based on the positions of the keys/values
    params = OrderedDict()
    params['properties'] = {
        'required': False,
        'validator': validator_list,
        'coerce': to_list,
        'default': '*'
    }
    params['filter'] = {
        'required': False,
        'type': 'string',
        'default': ''
    }  # placeholder to match form of index-styled functions
    params['config'] = {
        'required': False,
        'type': 'string',
        'default': ''
    }  # index-styled config string
    input = dict(zip(params.keys(), input))

    # validate the mapped input against the validator
    v = Validator(params, allow_unknown=True)
    input = v.validated(input)
    if input is None:
        raise ValueError

    # get the properties to return and the property map;
    # if we have a wildcard, get all the properties
    properties = [p.lower().strip() for p in input['properties']]
    if len(properties) == 1 and (properties[0] == '' or properties[0] == '*'):
        properties = list(get_item_info(faker).keys())

    # get any configuration settings
    config = urllib.parse.parse_qs(input['config'])
    config = {k: v[0] for k, v in config.items()}
    limit = int(config.get('limit', 100))
    headers = config.get('headers', 'true').lower()
    if headers == 'true':
        headers = True
    else:
        headers = False

    # write the output
    flex.output.content_type = 'application/json'
    flex.output.write('[')

    first_row = True
    if headers is True:
        result = json.dumps(properties)
        first_row = False
        flex.output.write(result)

    for item in get_data(faker, limit):
        result = json.dumps([item.get(p) for p in properties])
        if first_row is False:
            result = ',' + result
        first_row = False
        flex.output.write(result)

    flex.output.write(']')
Esempio n. 49
0
                    'type': 'integer',
                    'min': 0,
                    'required': True
                },
                'emails_opened': {
                    'type': 'integer',
                    'min': 0,
                    'required': True
                }
            }
        },
        'required': True,
        'empty': False
    }
}
validator = Validator(INPUT_SCHEMA)


class MyBasicAuth(BasicAuth):
    def check_auth(self, username, password, allowed_roles, resource, method):
        if str(resource) in ('bayesian_bandit'):
            # 'bayesian_bandit' is public
            return True
        else:
            # all the other resources are secured
            return username == 'admin' and password == 'qHQByAswXuEADz4'


AUTH_HEADER = {"Authorization": "Basic YWRtaW46cUhRQnlBc3dYdUVBRHo0"}

app = Eve(auth=MyBasicAuth)
Esempio n. 50
0
        'type': 'integer',
        'required': False
    },
    'orig_to_dest_only': {
        'type': 'boolean',
        'required': True
    },
    'depart_loc': {
        'type': 'string',
        'required': True
    },
    'dest_loc': {
        'type': 'string',
        'required': True
    },
    'depart_start': {
        'type': 'string',
        'required': True,
        'minlength': 13,
        'maxlength': 19
    },
    'depart_end': {
        'type': 'string',
        'required': True,
        'minlength': 13,
        'maxlength': 19
    }
}

basicInputValidator = Validator(input_schema)
Esempio n. 51
0
def test_empty_schema():
    validator = Validator()
    with pytest.raises(SchemaError, message=errors.SCHEMA_ERROR_MISSING):
        validator({}, schema=None)
Esempio n. 52
0
def step_impl(context):
    v = Validator(event_schema)
    for event in context.all_events:
        assert v.validate(event), str(
            v.errors) + " in validating " + event['title']
Esempio n. 53
0
def partialValidator(data):
    partialSchema = {}
    for key in data.keys():
        partialSchema[key] = schema[key]
    return Validator(partialSchema)
Esempio n. 54
0
		'maxlength': 100
	},
	'password': {
		'type': 'string',
		'required': True,
		'minlength': 2,
		'regex': '(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$'

	},
	'is_admin': {
		'type': 'boolean',
		'required': False
	}
}

validate_user_schema = Validator(user_schema)
validate_login_schema = Validator(login_schema)


# useful functions
def format_inputs(word):
	"""
	formats input string
	:param word:
	:return: string
	"""
	json_input = word.lower().strip()
	split_input = re.sub(' +', " ", json_input)
	return "".join(split_input)

Esempio n. 55
0
season_schema = {
    'season': {
        'type': ['string', 'integer'],
        'coerce': int
    },
    'url': {
        'type': 'string'
    }
}
season_validator = Validator(
    {
        'Seasons': {
            'type': 'list',
            'schema': {
                'type': 'dict',
                'schema': season_schema
            }
        }
    },
    allow_unknown=True)


def seasons_formatter(res_data, req):
    if req['resource'] != 'seasons':
        return None

    mrd = res_data['MRData']

    if not season_validator.validate(mrd['SeasonTable']):
        raise RuntimeError(season_validator.errors)
Esempio n. 56
0
 def validate(self, payload):
     validator = Validator()
     return validator.validate(payload, self.scheme)
Esempio n. 57
0
                               cascade="all, delete-orphan")


_user_creation_schema = {
    'email': {
        'type': 'string',
        'required': True,
        'empty': False
    },
    'password': {
        'type': 'string',
        'required': True,
        'empty': False
    }
}
create_user_validator = Validator(_user_creation_schema)


class Project(db.Model):
    """Tabla proyectos de la base de datos."""
    project_id = db.Column(db.String(50),
                           primary_key=True,
                           default=generate_uuid)
    name = db.Column(db.String(100), nullable=False)

    user_id = db.Column(db.String(50),
                        db.ForeignKey('user.user_id', ondelete='CASCADE'))

    requests = db.relationship('Request',
                               backref=db.backref('project'),
                               cascade="all, delete-orphan")
Esempio n. 58
0
    def _upsert_comment(self,
                        post_identifier: str,
                        apps: set,
                        post: Post = None,
                        update_root=True):
        if not post or not isinstance(post, Post):
            try:
                post = self._get_post_from_blockchain(post_identifier)
            except PostDoesNotExist:
                post = {'identifier': post_identifier}
                mark_post_as_deleted(post)
                logger.info('Post marked as deleted: "%s"', post_identifier)
                return
            except Exception as e:
                logger.exception('Failed to get post from blockchain: %s', e)
                return

        logger.info('Update post "%s"', post_identifier)
        try:
            for app in apps:
                collections = APP_COLLECTIONS.get(app)
                if not collections:
                    return

                if isinstance(post, Post):
                    if post.is_main_post():

                        v = Validator(POST_SCHEMA, allow_unknown=True)
                        if not v.validate(post):
                            logger.error(
                                'Failed to validate post %s. List of errors: %s',
                                post_identifier, v.errors)
                            return

                        validated_post = v.document

                        if app == Application.steepshot and not has_images(
                                validated_post.get('body', '')):
                            mark_post_as_deleted(validated_post)
                            logger.info('Post marked as deleted: "%s"',
                                        post_identifier)

                        result = retry(
                            getattr(
                                self.mongo,
                                collections[CollectionType.posts]).update_one,
                            5, (DuplicateKeyError, ConnectionFailure))(
                                {
                                    'identifier': post_identifier
                                }, {
                                    '$set': validated_post
                                },
                                upsert=True)
                        if isinstance(result, Exception):
                            logger.error(
                                'Failed to insert post: "%s". Error: %s',
                                post_identifier, result)

                        comments = retry(Post.get_all_replies, 5,
                                         Exception)(post)
                        if isinstance(comments, Exception):
                            logger.error(
                                'Failed to get comments for post: "%s". Error: %s',
                                post_identifier, comments)
                        else:
                            for comment in comments:
                                self._upsert_comment(comment['identifier'],
                                                     {app},
                                                     comment,
                                                     update_root=False)
                    else:
                        result = retry(
                            getattr(self.mongo, collections[
                                CollectionType.comments]).update_one, 5,
                            (DuplicateKeyError, ConnectionFailure))(
                                {
                                    'identifier': post_identifier
                                }, {
                                    '$set': post
                                },
                                upsert=True)
                        if isinstance(result, Exception):
                            logger.error(
                                'Failed to insert comment: "%s". Error: %s',
                                post_identifier, result)

                        if update_root:
                            self._upsert_comment(post.root_identifier, {app})
                else:
                    for collection in collections.values():
                        result = retry(
                            getattr(self.mongo, collection).update_one, 5,
                            (DuplicateKeyError, ConnectionFailure))(
                                {
                                    'identifier': post_identifier
                                },
                                {
                                    '$set': post
                                },
                            )
                        if isinstance(result, Exception):
                            logger.error(
                                'Failed to mark post as deleted: "%s". Error: %s',
                                post_identifier, result)
        except AttributeError as e:
            logger.error('Failed to update post: "%s". Error: %s',
                         post_identifier, e)
        except Exception as e:
            logger.exception('Failed to process post "%s". Error: %s',
                             post_identifier, e)
Esempio n. 59
0
def test_rename_handler():
    validator = Validator(allow_unknown={'rename_handler': int})
    schema = {}
    document = {'0': 'foo'}
    expected = {0: 'foo'}
    assert_normalized(document, expected, schema, validator)
Esempio n. 60
0
def validateForgotPassword(req):
    validator = Validator({'email': {'type': 'string', 'minlength': 4, 'required': True}})
    validator.allow_unknown = False
    valid = validator.validate(req.json)
    if (valid != True):
        return abort(400, 'Bad request')