예제 #1
0
    def fake_items(self, settings, elastic):
        ids = []
        for i in range(11):
            person_0 = factories.AbstractAgentFactory(type='share.person')
            person_1 = factories.AbstractAgentFactory(type='share.person')

            work = factories.AbstractCreativeWorkFactory(
                date_published=None if i %
                3 == 0 else fake.date_time_this_decade(), )
            if i % 3 == 1:
                factories.CreatorWorkRelationFactory(creative_work=work,
                                                     agent=person_0,
                                                     order_cited=0)
                factories.CreatorWorkRelationFactory(creative_work=work,
                                                     agent=person_1,
                                                     order_cited=1)
            if i % 3 == 2:
                factories.CreatorWorkRelationFactory(creative_work=work,
                                                     agent=person_0)

            # Works without identifiers won't be surfaced in search
            factories.WorkIdentifierFactory(creative_work=work)

            ids.append(work.id)

        tasks.index_model('creativework', ids)
        elastic.es_client.indices.refresh()
예제 #2
0
    def __init__(self,
                 elastic,
                 index=False,
                 num_identifiers=1,
                 num_sources=1,
                 date=None):
        self.elastic = elastic

        if date is None:
            self.work = factories.AbstractCreativeWorkFactory()
        else:
            models.AbstractCreativeWork._meta.get_field(
                'date_created').auto_now_add = False
            self.work = factories.AbstractCreativeWorkFactory(
                date_created=date,
                date_modified=date,
            )
            models.AbstractCreativeWork._meta.get_field(
                'date_created').auto_now_add = True

        self.sources = [factories.SourceFactory() for _ in range(num_sources)]
        self.work.sources.add(*[s.user for s in self.sources])

        for i in range(num_identifiers):
            factories.WorkIdentifierFactory(
                uri='http://example.com/{}/{}'.format(self.work.id, i),
                creative_work=self.work)

        if index:
            index_helpers(self)
예제 #3
0
    def test_related_objects(self):
        work = factories.AbstractCreativeWorkFactory()
        identifier = factories.WorkIdentifierFactory()

        assert identifier.creative_work != work

        identifier.administrative_change(creative_work=work)
        identifier.refresh_from_db()

        assert identifier.creative_work == work
예제 #4
0
    def fake_items(self, settings, elastic):
        ids = []
        for i in range(11):
            work = factories.AbstractCreativeWorkFactory(
                date_published=None if i %
                3 == 0 else fake.date_time_this_decade(), )
            # Works without identifiers won't be surfaced in search
            factories.WorkIdentifierFactory(creative_work=work)

            ids.append(work.id)

        tasks.index_model('creativework', ids)
        elastic.es_client.indices.refresh()
예제 #5
0
    def test_creativework_fetcher(self):
        works = [
            factories.AbstractCreativeWorkFactory(),
            factories.AbstractCreativeWorkFactory(is_deleted=True),
            factories.AbstractCreativeWorkFactory(),
            factories.AbstractCreativeWorkFactory(),
        ]

        for work in works[:-1]:
            factories.WorkIdentifierFactory(creative_work=work)

        factories.WorkIdentifierFactory.create_batch(5, creative_work=works[0])

        source = factories.SourceFactory()
        works[1].sources.add(source.user)

        # Trim trailing zeros
        def iso(x):
            return re.sub(r'(\.\d+?)0*\+', r'\1+', x.isoformat())

        fetched = list(fetchers.CreativeWorkFetcher()(work.id
                                                      for work in works))

        # TODO add more variance
        assert fetched == [{
            'id':
            util.IDObfuscator.encode(work),
            'type':
            work._meta.verbose_name,
            'types': [
                cls._meta.verbose_name for cls in type(work).mro()
                if hasattr(cls, '_meta') and cls._meta.proxy
            ],
            'title':
            work.title,
            'description':
            work.description,
            'date':
            iso(work.date_published),
            'date_created':
            iso(work.date_created),
            'date_modified':
            iso(work.date_modified),
            'date_published':
            iso(work.date_published),
            'date_updated':
            iso(work.date_updated),
            'is_deleted':
            work.is_deleted or not work.identifiers.exists(),
            'justification':
            getattr(work, 'justification', None),
            'language':
            work.language,
            'registration_type':
            getattr(work, 'registration_type', None),
            'retracted':
            work.outgoing_creative_work_relations.filter(
                type='share.retracted').exists(),
            'withdrawn':
            getattr(work, 'withdrawn', None),
            'identifiers':
            list(work.identifiers.values_list('uri', flat=True)),
            'sources': [user.source.long_title for user in work.sources.all()],
            'subjects': [],
            'subject_synonyms': [],
            'tags': [],
            'lists': {},
        } for work in works]