Exemplo n.º 1
0
    def test_published_publication_document_trusted_url(self):
        work = Work.objects.get(frbr_uri='/za/act/2014/10')
        pub_doc = PublicationDocument(
            work=work, trusted_url='https://example.com/foo.pdf')
        pub_doc.save()

        response = self.client.get(
            '/api/v1/za/act/2014/10/[email protected]')
        assert_equal(response.status_code, 200)

        assert_equal(response.data['publication_document']['url'],
                     'https://example.com/foo.pdf')
Exemplo n.º 2
0
    def test_published_publication_document(self):
        work = Work.objects.get(frbr_uri='/za/act/2014/10')
        pub_doc = PublicationDocument(work=work, mime_type="application/pdf")
        pub_doc.save()

        response = self.client.get(
            '/api/v1/za/act/2014/10/[email protected]')
        assert_equal(response.status_code, 200)

        assert_equal(
            response.data['publication_document']['url'],
            'http://testserver/api/v1/za/act/2014/10/eng@2014-02-12/media/za-act-2014-10-publication-document.pdf'
        )
Exemplo n.º 3
0
    def link_publication_document(self, work, info):
        params = info.get('params')
        locality_code = self.locality.code if self.locality else None
        finder = plugins.for_locale('publications', self.country.code, None,
                                    locality_code)

        if not finder or not params.get('date'):
            return self.create_task(work,
                                    info,
                                    task_type='link-publication-document')

        try:
            publications = finder.find_publications(params)
        except requests.HTTPError:
            return self.create_task(work,
                                    info,
                                    task_type='link-publication-document')

        if len(publications) != 1:
            return self.create_task(work,
                                    info,
                                    task_type='link-publication-document')

        pub_doc_details = publications[0]
        pub_doc = PublicationDocument()
        pub_doc.work = work
        pub_doc.file = None
        pub_doc.trusted_url = pub_doc_details.get('url')
        pub_doc.size = pub_doc_details.get('size')
        pub_doc.save()
Exemplo n.º 4
0
    def link_publication_document(self, work, row):
        locality_code = self.locality.code if self.locality else None
        finder = plugins.for_locale('publications', self.country.code, None,
                                    locality_code)

        if not finder or not row.params.get('date'):
            return self.create_task(work, row, task_type='link-gazette')

        try:
            publications = finder.find_publications(row.params)
        except requests.HTTPError:
            return self.create_task(work, row, task_type='link-gazette')

        if len(publications) != 1:
            return self.create_task(work, row, task_type='link-gazette')

        # don't actually create it for dry_run
        if not self.dry_run:
            pub_doc_details = publications[0]
            pub_doc = PublicationDocument()
            pub_doc.work = work
            pub_doc.file = None
            pub_doc.trusted_url = pub_doc_details.get('url')
            pub_doc.size = pub_doc_details.get('size')
            pub_doc.save()
Exemplo n.º 5
0
    def get_publication_document(self, params, work, form):
        finder = plugins.for_locale('publications', self.country, None, self.locality)

        if finder:
            try:
                publications = finder.find_publications(params)

                if len(publications) == 1:
                    pub_doc_details = publications[0]
                    pub_doc = PublicationDocument()
                    pub_doc.work = work
                    pub_doc.file = None
                    pub_doc.trusted_url = pub_doc_details.get('url')
                    pub_doc.size = pub_doc_details.get('size')
                    pub_doc.save()
                    self.pub_doc_task(work, form, task_type='check')

                else:
                    self.pub_doc_task(work, form, task_type='link')

            except ValueError as e:
                raise ValidationError({'message': e.message})

        else:
            self.pub_doc_task(work, form, task_type='link')
Exemplo n.º 6
0
    def get_publication_document(self, params, work, user):
        finder = plugins.for_work('publications', work)

        if finder:
            try:
                publications = finder.find_publications(params)

                if len(publications) == 1:
                    pub_doc_details = publications[0]
                    self.stdout.write(
                        self.style.NOTICE(
                            "Linking publication document {} to {}".format(
                                pub_doc_details.get('url'), work)))

                    pub_doc = PublicationDocument()
                    pub_doc.work = work
                    pub_doc.file = None
                    pub_doc.trusted_url = pub_doc_details.get('url')
                    pub_doc.size = pub_doc_details.get('size')
                    if not self.dry_run:
                        pub_doc.save()

                else:
                    self.pub_doc_task(work, user)

            except ValueError as e:
                raise ValidationError({'message': str(e)})

        else:
            self.pub_doc_task(work, user)
Exemplo n.º 7
0
    def save_publication_document(self):
        pub_doc_file = self.cleaned_data['publication_document_file']
        if pub_doc_file:
            try:
                pub_doc = self.instance.publication_document
            except PublicationDocument.DoesNotExist:
                pub_doc = PublicationDocument(work=self.instance)

            pub_doc.file = pub_doc_file
            pub_doc.size = pub_doc_file.size
            # we force a particular filename
            pub_doc.filename = 'publication-document.pdf'
            pub_doc.mime_type = pub_doc_file.content_type

            pub_doc.save()

        if self.cleaned_data['delete_publication_document']:
            try:
                self.instance.publication_document.delete()
            except PublicationDocument.DoesNotExist:
                pass
Exemplo n.º 8
0
    def save_publication_document(self):
        pub_doc_file = self.cleaned_data['publication_document_file']
        pub_doc_url = self.cleaned_data['publication_document_trusted_url']

        try:
            existing = self.instance.publication_document
        except PublicationDocument.DoesNotExist:
            existing = None

        if self.cleaned_data['delete_publication_document']:
            if existing:
                existing.delete()

        elif pub_doc_file:
            if existing:
                # ensure any previously uploaded file is deleted
                existing.delete()

            pub_doc = PublicationDocument(work=self.instance)
            pub_doc.trusted_url = None
            pub_doc.file = pub_doc_file
            pub_doc.size = pub_doc_file.size
            pub_doc.mime_type = pub_doc_file.content_type
            pub_doc.save()

        elif pub_doc_url:
            if existing:
                # ensure any previously uploaded file is deleted
                existing.delete()

            pub_doc = PublicationDocument(work=self.instance)
            pub_doc.file = None
            pub_doc.trusted_url = pub_doc_url
            pub_doc.size = self.cleaned_data['publication_document_size']
            pub_doc.mime_type = self.cleaned_data[
                'publication_document_mime_type']
            pub_doc.save()