Exemplo n.º 1
0
    def prepare_object(self, validated_data, instance=None):
        collection = instance if instance else Collection()
        collection.version = validated_data.get('version', collection.version) or HEAD
        collection.mnemonic = validated_data.get(self.Meta.lookup_field, collection.mnemonic)
        collection.name = validated_data.get('name', collection.name)
        collection.full_name = validated_data.get('full_name', collection.full_name)
        collection.description = validated_data.get('description', collection.description)
        collection.collection_type = validated_data.get('collection_type', collection.collection_type)
        collection.custom_validation_schema = validated_data.get(
            'custom_validation_schema', collection.custom_validation_schema
        )
        collection.public_access = validated_data.get('public_access', collection.public_access or DEFAULT_ACCESS_TYPE)
        collection.default_locale = validated_data.get('default_locale', collection.default_locale or DEFAULT_LOCALE)
        collection.website = validated_data.get('website', collection.website)

        supported_locales = validated_data.get('supported_locales')
        if not supported_locales:
            supported_locales = collection.supported_locales
        if supported_locales and isinstance(supported_locales, str):
            supported_locales = supported_locales.split(',')

        collection.supported_locales = supported_locales
        collection.extras = validated_data.get('extras', collection.extras)
        collection.external_id = validated_data.get('external_id', collection.external_id)
        collection.user_id = validated_data.get('user_id', collection.user_id)
        collection.organization_id = validated_data.get('organization_id', collection.organization_id)
        collection.user = validated_data.get('user', collection.user)
        collection.organization = validated_data.get('organization', collection.organization)
        return collection
Exemplo n.º 2
0
 def update(self, instance, validated_data):
     original_schema = instance.custom_validation_schema
     collection = self.prepare_object(validated_data, instance)
     user = self.context['request'].user
     errors = Collection.persist_changes(collection, user, original_schema)
     self._errors.update(errors)
     return collection
Exemplo n.º 3
0
    def prepare_object(self, validated_data, instance=None):
        collection = instance if instance else Collection()
        collection.version = validated_data.get('version',
                                                collection.version) or HEAD
        collection.mnemonic = validated_data.get(self.Meta.lookup_field,
                                                 collection.mnemonic)
        collection.public_access = validated_data.get(
            'public_access', collection.public_access or DEFAULT_ACCESS_TYPE)
        collection.default_locale = validated_data.get(
            'default_locale', collection.default_locale or DEFAULT_LOCALE)
        supported_locales = validated_data.get('supported_locales')
        if not supported_locales:
            supported_locales = collection.supported_locales
        if supported_locales and isinstance(supported_locales, str):
            supported_locales = supported_locales.split(',')

        collection.supported_locales = supported_locales or [
            collection.default_locale
        ]

        for attr in [
                'experimental',
                'locked_date',
                'text',
                'revision_date',
                'immutable',
                'copyright',
                'purpose',
                'jurisdiction',
                'contact',
                'publisher',
                'identifier',
                'canonical_url',
                'retired',
                'released',
                'organization',
                'user',
                'organization_id',
                'user_id',
                'external_id',
                'extras',
                'preferred_source',
                'repository_type',
                'custom_resources_linked_source',
                'website',
                'custom_validation_schema',
                'collection_type',
                'description',
                'name',
                'meta',
        ]:
            setattr(collection, attr,
                    validated_data.get(attr, get(collection, attr)))

        collection.full_name = validated_data.get(
            'full_name', collection.full_name) or collection.name

        return collection
Exemplo n.º 4
0
    def get_queryset(self):
        queryset = Collection.get_base_queryset(
            compact_dict_by_values(self.get_filter_params())).select_related(
                'user', 'organization')

        if self.is_verbose():
            queryset = queryset.select_related('created_by', 'updated_by')

        return queryset
Exemplo n.º 5
0
    def get_kwargs_filters(self):
        filters = self.get_facet_filters_from_kwargs()
        is_source_child_document_model = self.is_source_child_document_model()
        is_version_specified = 'version' in self.kwargs
        is_collection_specified = 'collection' in self.kwargs
        is_source_specified = 'source' in self.kwargs

        if is_version_specified and is_source_specified:
            filters['source_version'] = self.kwargs['version']
        if is_version_specified and is_collection_specified:
            filters['collection_version'] = self.kwargs['version']

        if is_source_child_document_model:
            if is_version_specified:
                container_version = self.kwargs['version']
                is_latest_released = container_version == LATEST
                if is_latest_released:
                    params = dict(
                        user__username=self.kwargs.get('user'),
                        organization__mnemonic=self.kwargs.get('org'))
                    if is_source_specified:
                        from core.sources.models import Source
                        latest_released_version = Source.find_latest_released_version_by(
                            {
                                **params, 'mnemonic': self.kwargs['source']
                            })
                        filters['source_version'] = get(
                            latest_released_version, 'version')
                    elif is_collection_specified:
                        from core.collections.models import Collection
                        latest_released_version = Collection.find_latest_released_version_by(
                            {
                                **params, 'mnemonic': self.kwargs['collection']
                            })
                        filters['collection_version'] = get(
                            latest_released_version, 'version')

            if is_collection_specified:
                owner_type = filters.pop('ownerType', None)
                owner = filters.pop('owner', None)
                if owner_type == USER_OBJECT_TYPE:
                    filters['collection_owner_url'] = "/users/{}/".format(
                        owner)
                if owner_type == ORG_OBJECT_TYPE:
                    filters['collection_owner_url'] = "/orgs/{}/".format(owner)
                if not is_version_specified:
                    filters['collection_version'] = HEAD
            if is_source_specified and not is_version_specified:
                filters['source_version'] = HEAD
        return filters
Exemplo n.º 6
0
 def set_parent_resource(self, __pop=True):
     from core.sources.models import Source
     source = self.kwargs.pop('source', None) if __pop else self.kwargs.get(
         'source', None)
     collection = self.kwargs.pop('collection',
                                  None) if __pop else self.kwargs.get(
                                      'collection', None)
     container_version = self.kwargs.pop(
         'version', HEAD) if __pop else self.kwargs.get('version', HEAD)
     parent_resource = None
     if 'org' in self.kwargs:
         filters = dict(organization__mnemonic=self.kwargs['org'])
     else:
         username = self.request.user.username if self.user_is_self else self.kwargs.get(
             'user')
         filters = dict(user__username=username)
     if source:
         parent_resource = Source.get_version(source, container_version
                                              or HEAD, filters)
     if collection:
         from core.collections.models import Collection
         parent_resource = Collection.get_version(source, container_version
                                                  or HEAD, filters)
     self.kwargs['parent_resource'] = self.parent_resource = parent_resource
Exemplo n.º 7
0
 def process(self):
     coll = Collection(**self.data)
     errors = Collection.persist_new_version(coll, self.user)
     return errors or UPDATED
Exemplo n.º 8
0
 def create_version(self, validated_data):
     collection = self.prepare_object(validated_data)
     user = self.context['request'].user
     errors = Collection.persist_new_version(collection, user)
     self._errors.update(errors)
     return collection
Exemplo n.º 9
0
 def update(self, instance, validated_data):
     collection = self.prepare_object(validated_data, instance)
     user = self.context['request'].user
     errors = Collection.persist_changes(collection, user)
     self._errors.update(errors)
     return collection
Exemplo n.º 10
0
 def test_parent_id(self):
     self.assertIsNone(Collection().parent_id)
     self.assertEqual(Collection(user_id=1).parent_id, 1)
     self.assertEqual(Collection(organization_id=1).parent_id, 1)
Exemplo n.º 11
0
 def test_is_versioned(self):
     self.assertTrue(Collection().is_versioned)
Exemplo n.º 12
0
 def test_collection(self):
     self.assertEqual(Collection(mnemonic='coll').collection, 'coll')
     self.assertEqual(Collection().collection, '')
Exemplo n.º 13
0
    def get_base_queryset(  # pylint: disable=too-many-branches,too-many-locals,too-many-statements
            cls,
            params,
            distinct_by='updated_at'):
        queryset = cls.objects.filter(is_active=True)
        user = params.get('user', None)
        org = params.get('org', None)
        collection = params.get('collection', None)
        source = params.get('source', None)
        container_version = params.get('version', None)
        concept = params.get('concept', None)
        concept_version = params.get('concept_version', None)
        is_latest = params.get('is_latest', None) in [True, 'true']
        uri = params.get('uri', None)
        include_retired = params.get(INCLUDE_RETIRED_PARAM,
                                     None) in [True, 'true']
        updated_since = parse_updated_since_param(params)
        latest_released_version = None
        is_latest_released = container_version == LATEST
        if is_latest_released:
            filters = dict(user__username=user, organization__mnemonic=org)
            if source:
                from core.sources.models import Source
                latest_released_version = Source.find_latest_released_version_by(
                    {
                        **filters, 'mnemonic': source
                    })
            elif collection:
                from core.collections.models import Collection
                latest_released_version = Collection.find_latest_released_version_by(
                    {
                        **filters, 'mnemonic': collection
                    })

            if not latest_released_version:
                return cls.objects.none()

        if collection:
            queryset = queryset.filter(
                cls.get_iexact_or_criteria('collection_set__mnemonic',
                                           collection))
            if user:
                queryset = queryset.filter(
                    cls.get_iexact_or_criteria(
                        'collection_set__user__username', user))
            if org:
                queryset = queryset.filter(
                    cls.get_iexact_or_criteria(
                        'collection_set__organization__mnemonic', org))
            if is_latest_released:
                queryset = queryset.filter(
                    cls.get_iexact_or_criteria(
                        'collection_set__version',
                        get(latest_released_version, 'version')))
            if container_version and not is_latest_released:
                queryset = queryset.filter(
                    cls.get_iexact_or_criteria('collection_set__version',
                                               container_version))
        if source:
            queryset = queryset.filter(
                cls.get_iexact_or_criteria('sources__mnemonic', source))
            if user:
                queryset = queryset.filter(
                    cls.get_iexact_or_criteria('parent__user__username', user))
            if org:
                queryset = queryset.filter(
                    cls.get_iexact_or_criteria(
                        'parent__organization__mnemonic', org))
            if is_latest_released:
                queryset = queryset.filter(
                    cls.get_iexact_or_criteria(
                        'sources__version',
                        get(latest_released_version, 'version')))
            if container_version and not is_latest_released:
                queryset = queryset.filter(
                    cls.get_iexact_or_criteria('sources__version',
                                               container_version))

        if concept:
            queryset = queryset.filter(mnemonic__exact=concept)
        if concept_version:
            queryset = queryset.filter(
                cls.get_iexact_or_criteria('version', concept_version))
        if is_latest:
            queryset = queryset.filter(is_latest_version=True)
        if not include_retired and not concept:
            queryset = queryset.filter(retired=False)
        if updated_since:
            queryset = queryset.filter(updated_at__gte=updated_since)
        if uri:
            queryset = queryset.filter(uri__icontains=uri)

        if distinct_by:
            queryset = queryset.distinct(distinct_by)

        return queryset
Exemplo n.º 14
0
 def get_queryset(self):
     return Collection.get_base_queryset(
         compact_dict_by_values(self.get_filter_params()))
Exemplo n.º 15
0
    def prepare_object(self, validated_data, instance=None):
        collection = instance if instance else Collection()
        collection.version = validated_data.get('version',
                                                collection.version) or HEAD
        collection.mnemonic = validated_data.get(self.Meta.lookup_field,
                                                 collection.mnemonic)
        collection.name = validated_data.get('name', collection.name)
        collection.full_name = validated_data.get(
            'full_name', collection.full_name) or collection.name
        collection.description = validated_data.get('description',
                                                    collection.description)
        collection.collection_type = validated_data.get(
            'collection_type', collection.collection_type)
        collection.custom_validation_schema = validated_data.get(
            'custom_validation_schema', collection.custom_validation_schema)
        collection.public_access = validated_data.get(
            'public_access', collection.public_access or DEFAULT_ACCESS_TYPE)
        collection.default_locale = validated_data.get(
            'default_locale', collection.default_locale or DEFAULT_LOCALE)
        collection.website = validated_data.get('website', collection.website)
        collection.custom_resources_linked_source = validated_data.get(
            'custom_resources_linked_source',
            collection.custom_resources_linked_source)
        collection.repository_type = validated_data.get(
            'repository_type', collection.repository_type)
        collection.preferred_source = validated_data.get(
            'preferred_source', collection.preferred_source)
        supported_locales = validated_data.get('supported_locales')
        if not supported_locales:
            supported_locales = collection.supported_locales
        if supported_locales and isinstance(supported_locales, str):
            supported_locales = supported_locales.split(',')

        collection.supported_locales = supported_locales or [
            collection.default_locale
        ]
        collection.extras = validated_data.get('extras', collection.extras)
        collection.external_id = validated_data.get('external_id',
                                                    collection.external_id)
        collection.user_id = validated_data.get('user_id', collection.user_id)
        collection.organization_id = validated_data.get(
            'organization_id', collection.organization_id)
        collection.user = validated_data.get('user', collection.user)
        collection.organization = validated_data.get('organization',
                                                     collection.organization)
        collection.released = validated_data.get('released',
                                                 collection.released)
        collection.retired = validated_data.get('retired', collection.retired)

        collection.canonical_url = validated_data.get('canonical_url',
                                                      collection.canonical_url)
        collection.identifier = validated_data.get('identifier',
                                                   collection.identifier)
        collection.publisher = validated_data.get('publisher',
                                                  collection.publisher)
        collection.contact = validated_data.get('contact', collection.contact)
        collection.jurisdiction = validated_data.get('jurisdiction',
                                                     collection.jurisdiction)
        collection.purpose = validated_data.get('purpose', collection.purpose)
        collection.copyright = validated_data.get('copyright',
                                                  collection.copyright)
        collection.immutable = validated_data.get('immutable',
                                                  collection.immutable)
        collection.revision_date = validated_data.get('revision_date',
                                                      collection.revision_date)
        collection.text = validated_data.get('text', collection.text)

        return collection
Exemplo n.º 16
0
 def test_resource_type(self):
     self.assertEqual(Collection().resource_type, 'Collection')