示例#1
0
def convert_types(mapping, row):
    """ Translate a row of input data (e.g. from a CSV file) into the
    structure understood by the dataset loader, i.e. where all 
    dimensions are dicts and all types have been converted. 

    This will validate the incoming data and emit a colander.Invalid
    exception if validation was unsuccessful."""
    out = {}
    errors = Invalid(SchemaNode(Mapping(unknown='preserve')))

    for dimension, meta in mapping.items():
        meta['dimension'] = dimension

        # handle AttributeDimensions, Measures and DateDimensions.
        # this is clever, but possibly not always true.
        if 'column' in meta and meta['type'] not in ('compound', 'geometry'):
            try:
                out[dimension] = _cast(row, meta, dimension)
            except Invalid, i:
                errors.add(i)

        # handle CompoundDimensions.
        else:
            out[dimension] = {}

            for attribute, ameta in meta.get('attributes', {}).items():
                try:
                    out[dimension][attribute] = \
                            _cast(row, ameta, dimension + '.' +
                                    attribute)
                except Invalid, i:
                    errors.add(i)
    def deserialize(self, field, pstruct):
        error = None

        result = {}

        if pstruct is null:
            pstruct = {}

        checkbox_schema = pstruct.get(self.checkbox_element, null)
        if checkbox_schema is null:
            return null

        result[self.checkbox_element] = True

        for num, subfield in enumerate(field.children):
            name = subfield.name
            subval = pstruct.get(name, null)

            try:
                result[name] = subfield.deserialize(subval)
            except Invalid as e:
                result[name] = e.value
                if error is None:
                    error = Invalid(field.schema, value=result)
                error.add(e, num)

        if error is not None:
            raise error

        result[self.checkbox_element] = True
        return result
示例#3
0
文件: rate.py 项目: Janaba/adhocracy3
 def validator(node, value):
     rate_validator = registry.getAdapter(value['object'], IRateValidator)
     if not rate_validator.validate(value['rate']):
         error = Invalid(node, msg='')
         msg = rate_validator.helpful_error_message()
         error.add(Invalid(node['rate'], msg=msg))
         raise error
示例#4
0
    def deserialize(self, field, pstruct):
        result = []
        error = None

        if pstruct is null:
            pstruct = []

        field.sequence_fields = []
        item_field = field.children[0]

        for num, substruct in enumerate(pstruct):
            subfield = item_field.clone()
            try:
                subval = subfield.deserialize(substruct)
            except Invalid as e:
                subval = e.value
                if error is None:
                    error = Invalid(field.schema, value=result)
                error.add(e, num)

            result.append(subval)
            field.sequence_fields.append(subfield)

        if error is not None:
            raise error

        return result
示例#5
0
def convert_types(mapping, row):
    """ Translate a row of input data (e.g. from a CSV file) into the
    structure understood by the dataset loader, i.e. where all 
    dimensions are dicts and all types have been converted. 

    This will validate the incoming data and emit a colander.Invalid
    exception if validation was unsuccessful."""
    out = {}
    errors = Invalid(SchemaNode(Mapping(unknown="preserve")))

    for dimension, meta in mapping.items():
        meta["dimension"] = dimension

        # handle AttributeDimensions, Measures and DateDimensions.
        # this is clever, but possibly not always true.
        if "column" in meta:
            try:
                out[dimension] = _cast(row, meta, dimension)
            except Invalid, i:
                errors.add(i)

        # handle CompoundDimensions.
        else:
            out[dimension] = {}

            for attribute, ameta in meta.get("attributes", {}).items():
                try:
                    out[dimension][attribute] = _cast(row, ameta, dimension + "." + attribute)
                except Invalid, i:
                    errors.add(i)
示例#6
0
文件: rate.py 项目: Janaba/adhocracy3
 def validator(node, value):
     user = request.user
     if user is None or user != value['subject']:
         error = Invalid(node, msg='')
         error.add(Invalid(node['subject'],
                           msg='Must be the currently logged-in user'))
         raise error
示例#7
0
    def deserialize(self, field, pstruct):
        result = []
        error = None

        if pstruct is null:
            pstruct = []

        field.sequence_fields = []
        item_field = field.children[0]

        for num, substruct in enumerate(pstruct):
            subfield = item_field.clone()
            if item_field.parent is not None:
                subfield._parent = weakref.ref(item_field.parent)
            try:
                subval = subfield.deserialize(substruct)
            except Invalid as e:
                subval = e.value
                if error is None:
                    error = Invalid(field.schema, value=result)
                error.add(e, num)

            subfield.cstruct = subval
            result.append(subval)
            field.sequence_fields.append(subfield)

        if error is not None:
            raise error

        return result
示例#8
0
def validate_deep(data, context, direction, attribute):
    relations = []
    node = sequence(direction)
    inv = Invalid(node)
    for i, relation_data in enumerate(data.get(direction, [])):
        try:
            other = relation_data.get(attribute, {})
            schema = context.network.get_relation_schema(
                relation_data.get('type'))
            if schema is None:
                raise Invalid(
                    node,
                    "Invalid relation type: %e" % relation_data.get('type'))
            relation = validate_relation(relation_data,
                                         schema,
                                         context,
                                         ignore_entities=True)

            schema = context.network.get_entity_schema(other.get('type'))
            if schema is None:
                raise Invalid(node,
                              "Invalid entity type: %e" % other.get('type'))
            relation[attribute] = validate_entity(other,
                                                  schema,
                                                  context,
                                                  deep=False)

            relations.append(relation)
        except Invalid as sub:
            inv.add(sub, i)
    return relations, inv
示例#9
0
 def validator(node, value):
     rate_validator = registry.getAdapter(value['object'], IRateValidator)
     if not rate_validator.validate(value['rate']):
         error = Invalid(node, msg='')
         msg = rate_validator.helpful_error_message()
         error.add(Invalid(node['rate'], msg=msg))
         raise error
示例#10
0
 def validator(node, value):
     user = request.user
     if user is None or user != value['subject']:
         error = Invalid(node, msg='')
         error.add(
             Invalid(node['subject'],
                     msg='Must be the currently logged-in user'))
         raise error
示例#11
0
 def validate_user_is_active(node: SchemaNode, value: dict):
     user = request.validated.get('user', None)
     if user is None:
         return
     elif not user.active:
         error = Invalid(node)
         error.add(Invalid(node[child_node_name],
                           msg='User account not yet activated'))
         raise error
示例#12
0
 def validate_rate_is_unique(node, value):
     existing = _get_rates_user_non_anonymized(context, request, value)
     existing += _get_rates_user_anonymized(context, request, value)
     existing = _remove_following_versions(existing, context, request)
     if existing:
         error = Invalid(node, msg='')
         msg = 'Another rate by the same user already exists'
         error.add(Invalid(node['object'], msg=msg))
         raise error
示例#13
0
文件: rate.py 项目: liqd/adhocracy3
 def validate_rate_is_unique(node, value):
     existing = _get_rates_user_non_anonymized(context, request, value)
     existing += _get_rates_user_anonymized(context, request, value)
     existing = _remove_following_versions(existing, context, request)
     if existing:
         error = Invalid(node, msg='')
         msg = 'Another rate by the same user already exists'
         error.add(Invalid(node['object'], msg=msg))
         raise error
示例#14
0
 def validate_user_is_active(node: SchemaNode, value: dict):
     user = request.validated.get('user', None)
     if user is None:
         return
     elif not user.active:
         error = Invalid(node)
         error.add(
             Invalid(node[child_node_name],
                     msg='User account not yet activated'))
         raise error
示例#15
0
 def validate_login_password(node: SchemaNode, value: dict):
     password = value['password']
     user = request.validated.get('user', None)
     if user is None:
         return
     valid = user.is_password_valid(registry, password)
     if not valid:
         error = Invalid(node)
         error.add(Invalid(node['password'], msg=error_msg_wrong_login))
         raise error
示例#16
0
 def validate_login_password(node: SchemaNode, value: dict):
     password = value['password']
     user = request.validated.get('user', None)
     if user is None:
         return
     valid = user.is_password_valid(registry, password)
     if not valid:
         error = Invalid(node)
         error.add(Invalid(node['password'], msg=error_msg_wrong_login))
         raise error
示例#17
0
 def validate_login_password(node: SchemaNode, value: dict):
     password = value['password']
     user = request.validated.get('user', None)
     if user is None:
         return
     sheet = registry.content.get_sheet(user, IPasswordAuthentication)
     valid = sheet.check_plaintext_password(password)
     if not valid:
         error = Invalid(node)
         error.add(Invalid(node['password'], msg=error_msg_wrong_login))
         raise error
示例#18
0
 def validate_login(node: SchemaNode, value: dict):
     login = value[child_node_name]
     locator = registry.getMultiAdapter((context, request), IUserLocator)
     if child_node_name == 'email':
         login = login.lower().strip()
         user = locator.get_user_by_email(login)
     else:
         user = locator.get_user_by_login(login)
     if user is None:
         error = Invalid(node)
         error.add(Invalid(node['password'], msg=error_msg_wrong_login))
         raise error
     else:
         request.validated['user'] = user
示例#19
0
def validate_entity(data, schema, context, deep=True):
    schema = entity_schema(schema)
    inv = Invalid(schema)
    try:
        data = schema.deserialize(data)
    except Invalid as inv_real:
        inv = inv_real

    if deep:
        for direction, attribute in (('incoming', 'source'), ('outgoing', 'target')):
            data[direction], i = validate_deep(data, context, direction, attribute)
            if len(i.children):
                inv.add(i)

    if len(inv.children):
        raise inv
    return data
示例#20
0
    def deserialize(self, field, pstruct):
        error = None
        
        result = {}

        if pstruct is null:
            pstruct = {}

        for num, subfield in enumerate(field.children):
            name = subfield.name
            subval = pstruct.get(name, null)
                            
            try:
                result[name] = subfield.deserialize(subval)
            except Invalid, e:
                result[name] = e.value
                if error is None:
                    error = Invalid(field.schema, value=result)
                error.add(e, num)
示例#21
0
def validate_entity(data, schema, context, deep=True):
    schema = entity_schema(schema)
    inv = Invalid(schema)
    try:
        data = schema.deserialize(data)
    except Invalid as inv_real:
        inv = inv_real

    if deep:
        for direction, attribute in (('incoming', 'source'), ('outgoing',
                                                              'target')):
            data[direction], i = validate_deep(data, context, direction,
                                               attribute)
            if len(i.children):
                inv.add(i)

    if len(inv.children):
        raise inv
    return data
示例#22
0
文件: rate.py 项目: Janaba/adhocracy3
 def validator(node, value):
     catalogs = find_service(context, 'catalogs')
     query = search_query._replace(
         references=(Reference(None, IRate, 'subject', value['subject']),
                     Reference(None, IRate, 'object', value['object'])),
         resolve=True,
     )
     same_rates = catalogs.search(query).elements
     if not same_rates:
         return
     item = find_interface(context, IRateItem)
     old_versions = registry.content.get_sheet_field(item,
                                                     IVersions,
                                                     'elements')
     for rate in same_rates:
         if rate not in old_versions:
             error = Invalid(node, msg='')
             msg = 'Another rate by the same user already exists'
             error.add(Invalid(node['object'], msg=msg))
             raise error
示例#23
0
 def validate_login(node: SchemaNode, value: dict):
     login = value[child_node_name]
     locator = registry.getMultiAdapter((context, request), IUserLocator)
     if child_node_name == 'email':
         login = login.lower().strip()
         user = locator.get_user_by_email(login)
     else:
         user = locator.get_user_by_login(login)
     if user is None:
         error = Invalid(node)
         error.add(Invalid(node['password'], msg=error_msg_wrong_login))
         raise error
     is_service_konto_user = _is_service_konto_user(registry, user)
     if is_service_konto_user:
         error = Invalid(node)
         error.add(
             Invalid(node[child_node_name],
                     msg='Please use ServiceKonto login'))
         raise error
     request.validated['user'] = user
示例#24
0
def validate_deep(data, context, direction, attribute):
    relations = []
    node = sequence(direction)
    inv = Invalid(node)
    for i, relation_data in enumerate(data.get(direction, [])):
        try:
            other = relation_data.get(attribute, {})
            schema = context.network.get_relation_schema(relation_data.get('type'))
            if schema is None:
                raise Invalid(node, "Invalid relation type: %e" % relation_data.get('type'))
            relation = validate_relation(relation_data, schema, context, ignore_entities=True)

            schema = context.network.get_entity_schema(other.get('type'))
            if schema is None:
                raise Invalid(node, "Invalid entity type: %e" % other.get('type'))
            relation[attribute] = validate_entity(other, schema, context, deep=False)

            relations.append(relation)
        except Invalid as sub:
            inv.add(sub, i)
    return relations, inv
示例#25
0
    def deserialize(self, field, pstruct):
        result = []
        error = None

        if pstruct is null:
            pstruct = []

        field.sequence_fields = []
        item_field = field.children[0]

        for num, substruct in enumerate(pstruct):
            subfield = item_field.clone()
            try:
                subval = subfield.deserialize(substruct)
            except Invalid, e:
                subval = e.value
                if error is None:
                    error = Invalid(field.schema, value=result)
                error.add(e, num)

            result.append(subval)
            field.sequence_fields.append(subfield)
    def deserialize(self, field, pstruct):
        error = None

        result = {}

        if pstruct is null:
            pstruct = {}

        # Convert the list of selected parent id's into a list of sqlalchemy parent objects in appstruct form.
        if 'parents' in pstruct:
            parents = []
            for parent_id in pstruct['parents']:
                parents.append(field.view.get_schema(parent_id).dictify())

            pstruct['parents'] = parents

        # Remove parent fields from the list of custom fields as they will be inherited through the parent 'link'
        if 'custom_field' in pstruct:
            for custom_field in pstruct['custom_fields']:
                if custom_field.parent_field:
                    pstruct.pop(custom_field)

        for num, subfield in enumerate(field.children):
            name = subfield.name
            subval = pstruct.get(name, null)

            try:
                result[name] = subfield.deserialize(subval)
            except Invalid as e:
                result[name] = e.value
                if error is None:
                    error = Invalid(field.schema, value=result)
                error.add(e, num)

        if error is not None:
            raise error

        return result
示例#27
0
def sequence_impl(self, node, value, callback, accept_scalar):
    if accept_scalar is None:
        accept_scalar = self.accept_scalar

    value = self._validate(node, value, accept_scalar)

    error = None
    result = []

    for num, subval in enumerate(value):
        try:
            result.append(callback(node.children[0], subval))
        except Invalid as e:
            if error is None:
                error = Invalid(node)
            error.add(e, num)

    if error is not None:
        raise error

    if not result:
        return null
    return result
示例#28
0
    def _impl(self, node, value, callback):
        value = self._validate(node, value)

        error = None
        result = {}

        for index, (k, v) in enumerate(value.iteritems()):
            key_node = node["key"]
            value_node = node["value"].clone()
            value_node.name = k

            try:
                name = callback(key_node, k)
                result[name] = callback(value_node, v)
            except Invalid as e:
                if error is None:
                    error = Invalid(node)
                error.add(e, index)

        if error is not None:
            raise error

        return result
示例#29
0
def sequence_impl(self, node, value, callback, accept_scalar):
    if accept_scalar is None:
        accept_scalar = self.accept_scalar

    value = self._validate(node, value, accept_scalar)

    error = None
    result = []

    for num, subval in enumerate(value):
        try:
            result.append(callback(node.children[0], subval))
        except Invalid as e:
            if error is None:
                error = Invalid(node)
            error.add(e, num)

    if error is not None:
        raise error

    if not result:
        return null
    return result
示例#30
0
    def deserialize(self, field, pstruct):
        error = None

        result = {}

        if pstruct is null:
            pstruct = {}

        for num, subfield in enumerate(field.children):
            name = subfield.name
            subval = get_dict_item(pstruct, name, null)

            try:
                result[name] = subfield.deserialize(subval)
            except Invalid as e:
                result[name] = e.value
                if error is None:
                    error = Invalid(field.schema, value=result)
                error.add(e, num)

        if error is not None:
            raise error

        return result