class ExtraSchema(Schema):
    key = Str()
    value = Str()
class XMLDatasetSchema(Schema):
    ext_ident = Str(data_key='extIdent',
                    validate=validate.Length(max=36),
                    required=True)
    int_ident = Int(data_key='intIdent')
    status = Str(data_key='@status',
                 validate=validate.OneOf(choices=['draft', 'published']))
    title_pl = Str()
    title_en = Str()
    notes_pl = Str()
    notes_en = Str()
    url = URL(allow_none=True)
    update_frequency = Str(data_key='updateFrequency')
    license = Str()
    license_chosen = Int(allow_none=True)
    license_condition_db_or_copyrighted = Str(allow_none=True)
    license_condition_modification = Bool(allow_none=True)
    license_condition_personal_data = Str(allow_none=True)
    license_condition_responsibilities = Str(allow_none=True)
    license_condition_source = Bool(allow_none=True)
    created = DateTime(data_key='created', allow_none=True)
    modified = DateTime(data_key='lastUpdateDate', allow_none=True)
    categories = List(Str())
    resources = Nested(XMLResourceSchema, many=True)
    tags = Nested(XMLTagSchema, many=True)

    class Meta:
        ordered = True
        unknown = EXCLUDE

    @pre_load
    def prepare_data(self, data, **kwargs):
        if 'title' in data and isinstance(data.get('title'), dict):
            data['title_en'] = data['title'].get('english', '')
            data['title_pl'] = data['title'].get('polish', '')
        if 'conditions' in data:
            data['license_condition_source'] = data['conditions'].get(
                'source', False)
            data['license_condition_modification'] = data['conditions'].get(
                'modification', False)
            data['license_condition_responsibilities'] = data[
                'conditions'].get('responsibilities')
            data['license_condition_db_or_copyrighted'] = data[
                'conditions'].get('dbOrCopyrighted')
            license_text_to_num = dict(
                (row[1], row[0]) for row in Dataset.LICENSES)
            license_chosen_text = data['conditions'].get(
                'dbOrCopyrightedLicenseChosen')
            data['license_chosen'] = license_text_to_num.get(
                license_chosen_text)
            data['license_condition_personal_data'] = data['conditions'].get(
                'personalData')
        if 'description' in data and isinstance(data.get('description'), dict):
            data['notes_en'] = data['description'].get('english', '')
            data['notes_pl'] = data['description'].get('polish', '')
        if 'tags' in data:
            data['tags'] = data['tags'].get('tag', [])
        if 'categories' in data:
            if 'category' in data['categories']:  # XSD SCHEMA >= 1.1
                data['categories'] = data['categories']['category']
            else:
                data['categories'] = [str(row) for row in data['categories']]
        if 'resources' in data:
            data['resources'] = data['resources'].get('resource', [])
        int_ident = data.get('intIdent')
        if int_ident:
            self.context['dataset_int_ident'] = int_ident
        return data

    @validates_schema
    def validate_int_ident(self, data, **kwargs):
        int_ident = data.get('int_ident')
        organization = self.context['organization']
        if int_ident and organization and not Dataset.raw.filter(
                id=int_ident, organization=organization).exists():
            msg = _(
                'Dataset with id: %(d_id)s and institution: "%(ins)s" was not found.'
            ) % {
                'd_id': int_ident,
                'ins': organization.title
            }
            raise ValidationError(msg, field_name='int_ident')

    @validates_schema
    def validate_license_condition_personal_data(self, data, **kwargs):
        field_name = 'license_condition_personal_data'
        if data.get(field_name):
            raise ValidationError(
                message=
                _('Chosen conditions for re-use mean that they contain personal data. '
                  'Please contact the administrator at [email protected].'),
                field_name=field_name,
            )

    @validates_schema
    def validate_license_condition_db_or_copyrighted(self, data, **kwargs):
        field_name = 'license_condition_db_or_copyrighted'
        if data.get(field_name) and not data.get('license_chosen'):
            raise ValidationError(
                message=
                _("Field 'dbOrCopyrightedLicenseChosen' is required if field 'dbOrCopyrighted' is provided."
                  ),
                field_name=field_name,
            )

    @validates_schema
    def validate_license_chosen(self, data, **kwargs):
        field_name = 'license_chosen'
        if data.get(field_name
                    ) and not data.get('license_condition_db_or_copyrighted'):
            raise ValidationError(
                message=
                _("Field 'dbOrCopyrighted' is required if field 'dbOrCopyrightedLicenseChosen' is provided."
                  ),
                field_name=field_name,
            )
class DatasetSchema(Schema):
    author = Str()
    author_email = Str()
    creator_user_id = UUID()
    extras = Nested(ExtraSchema, many=True)
    groups = Nested(CategorySchema, many=True)
    license_id = Str()
    license_title = Str()
    license_url = URL()
    maintainer = Str()
    maintainer_email = Str()
    created = DateTime(data_key='metadata_created')
    modified = DateTime(data_key='metadata_modified', allow_none=True)
    slug = Str(data_key='name')
    notes = Str()
    num_resources = Int()
    num_tags = Int()
    ext_ident = Str(data_key='id', validate=validate.Length(max=36))
    isopen = Bool()
    organization = Nested(OrganizationSchema, many=False)
    owner_org = UUID()
    private = Bool()
    relationships_as_object = Nested(RelationshipObjectSchema, many=True)
    relationships_as_subject = Nested(RelationshipSubjectSchema, many=True)
    resources = Nested(ResourceSchema, many=True)
    revision_id = UUID()
    status = Str(data_key='state')
    tags = Nested(TagSchema, many=True)
    title = Str()
    type = Str()
    url = Str()
    version = Str()

    class Meta:
        exclude = [
            'author', 'author_email', 'creator_user_id', 'extras', 'groups',
            'license_title', 'license_url', 'maintainer', 'maintainer_email',
            'num_resources', 'num_tags', 'isopen', 'owner_org', 'private',
            'relationships_as_object', 'relationships_as_subject',
            'revision_id', 'type', 'status', 'url', 'version'
        ]
        ordered = True
        unknown = EXCLUDE
Exemple #4
0
 class FrozenListSchema(Schema):
     items = fields.FrozenList(Str(), allow_none=True)
Exemple #5
0
 class SetSchema(Schema):
     items = fields.Set(Str(), allow_none=True)
class SampleSchema(Schema):
    name = Str(required=True)
    salary = Int(required=True)