Ejemplo n.º 1
0
def validate_single_policy(policy):
    if not isinstance(policy, dict):
        raise ValidationError('Policy should be a list or an object')

    p = {}
    for key, value in policy.items():
        key = str(key).lower()
        if key not in POLICY:
            statements = ', '.join(POLICY)
            raise ValidationError('"%s" is not a valid statement. '
                                  'Must be one of %s' % (key, statements))
        types, check = POLICY[key]
        if not isinstance(value, types):
            raise ValidationError('not a valid %s statement' % key)
        if check:
            if isinstance(value, str):
                value = value.lower()
            if value not in check:
                raise ValidationError('not a valid %s statement' % key)
        p[key] = value

    if 'action' not in p:
        raise ValidationError('"action" must be defined')

    return p
Ejemplo n.º 2
0
def check_username(request, username):
    """Default function for checking username validity
    """
    correct = slugify(username)
    if correct != username:
        raise ValidationError('Username may only contain lowercase '
                              'alphanumeric characters or single hyphens, '
                              'cannot begin or end with a hyphen')
    elif len(correct) < 2:
        raise ValidationError('Too short')
    return username
Ejemplo n.º 3
0
def validate_policy(policy):
    if isinstance(policy, dict):
        return validate_single_policy(policy)
    elif isinstance(policy, list):
        if not policy:
            raise ValidationError('Policy empty')
        policies = []
        for single in policy:
            policies.append(validate_single_policy(single))
        return policies
    else:
        raise ValidationError('Policy should be a list or an object')
Ejemplo n.º 4
0
def check_username(request, username):
    correct = slugify(username)
    if correct != username:
        raise ValidationError('Username may only contain lowercase '
                              'alphanumeric characters or single hyphens, '
                              'and cannot begin or end with a hyphen')
    return username
Ejemplo n.º 5
0
    def create_model(self,
                     request,
                     instance=None,
                     data=None,
                     session=None,
                     **kw):
        auth_backend = request.cache.auth_backend
        if self.type == 1:
            # Create the user
            data.pop('password_repeat', None)
            user = auth_backend.create_user(request, **data)
        else:
            user = auth_backend.get_user(request, email=data['email'])
            if not user:
                raise ValidationError("Can't find user, sorry")

        days = request.config['ACCOUNT_ACTIVATION_DAYS']
        data = {
            'id': digest(user.email),
            'expiry': datetime.utcnow() + timedelta(days=days),
            'type': self.type,
            'user_id': user.id
        }
        with self.session(request, session=session) as session:
            reg = super().create_model(request,
                                       instance,
                                       data,
                                       session=session,
                                       **kw)
            send_email_confirmation(request, reg.obj)
        return reg
Ejemplo n.º 6
0
    def __call__(self, value, bfield):
        if not value and self.nullable:
            return value
        request = bfield.request
        owner = request.cache.owner
        if not owner:
            raise ValidationError('no owner')
        target = get_owned_model(request, self.model or bfield.form.model)
        if not target:
            raise ValidationError('not found')
        field = self.field or bfield.name

        with target.model.session(request) as session:
            query = target.query(session, owner, **{field: value})
            return self.test(value,
                             bfield,
                             target.model,
                             query=query,
                             session=session)
Ejemplo n.º 7
0
    def _clean(self, value, bfield):
        bits = value.split('/') if isinstance(value, str) else None
        if not bits or (len(bits) == 1 and not self.multiple):
            return super()._clean(value, bfield)

        request = bfield.request
        target = get_owned_model(request, self.model)
        if not target:
            raise ValidationError('not found')
        model = target.model

        with model.session(request) as session:
            query = target.query(session, *bits)
            if self.multiple:
                return model.get_list(request, query=query, session=session)
            else:
                try:
                    return model.get_instance(request,
                                              query=query,
                                              session=session)
                except Http404:
                    raise ValidationError(self.validation_error.format(model))