Beispiel #1
0
    def get_works(self, country, table):
        works = []

        # clean up headers
        headers = [h.split(' ')[0].lower() for h in table[0]]

        # transform rows into list of dicts for easy access
        rows = [{header: row[i]
                 for i, header in enumerate(headers) if header}
                for row in table[1:]]

        for idx, row in enumerate(rows):
            if not row['ignore']:
                info = {
                    'row': idx + 2,
                }
                works.append(info)

                try:
                    frbr_uri = self.get_frbr_uri(country, row)
                except ValueError as e:
                    info['status'] = 'error'
                    info['error_message'] = e.message
                    continue

                try:
                    work = Work.objects.get(frbr_uri=frbr_uri)
                    info['work'] = work
                    info['status'] = 'duplicate'

                # TODO one day: also mark first work as duplicate if user is trying to import two of the same (currently only the second one will be)

                except Work.DoesNotExist:
                    work = Work()

                    work.frbr_uri = frbr_uri
                    work.title = row['title']
                    work.country = country
                    work.publication_name = row['publication_name']
                    work.publication_number = row['publication_number']
                    work.publication_date = self.make_date(
                        row['publication_date'])
                    work.commencement_date = self.make_date(
                        row['commencement_date'])
                    work.assent_date = self.make_date(row['assent_date'])
                    work.created_by_user = self.request.user
                    work.updated_by_user = self.request.user

                    try:
                        work.full_clean()
                        work.save()

                        # signals
                        work_changed.send(sender=work.__class__,
                                          work=work,
                                          request=self.request)

                        info['status'] = 'success'
                        info['work'] = work

                    except ValidationError as e:
                        info['status'] = 'error'
                        info['error_message'] = ' '.join([
                            '%s: %s' % (f, '; '.join(errs))
                            for f, errs in e.message_dict.items()
                        ])

        return works
Beispiel #2
0
    def create_work(self, view, row, idx, dry_run):
        # copy all row details
        info = row
        info['row'] = idx + 2

        row = self.validate_row(view, row)

        if row.get('errors'):
            info['status'] = 'error'
            info['error_message'] = row['errors']
            return info

        frbr_uri = self.get_frbr_uri(row)

        try:
            work = Work.objects.get(frbr_uri=frbr_uri)
            info['work'] = work
            info['status'] = 'duplicate'
            info['amends'] = row.get('amends') or None
            info['commencement_date'] = row.get('commencement_date') or None

        except Work.DoesNotExist:
            work = Work()

            work.frbr_uri = frbr_uri
            work.country = view.country
            work.locality = view.locality
            work.title = row.get('title')
            work.publication_name = row.get('publication_name')
            work.publication_number = row.get('publication_number')
            work.publication_date = row.get('publication_date')
            work.commencement_date = row.get('commencement_date')
            work.assent_date = row.get('assent_date')
            work.stub = row.get('stub')
            # handle spreadsheet that still uses 'principal'
            if 'stub' not in info:
                work.stub = not row.get('principal')
            work.created_by_user = view.request.user
            work.updated_by_user = view.request.user
            self.add_extra_properties(work, info)

            try:
                work.full_clean()
                if not dry_run:
                    work.save_with_revision(view.request.user)

                    # signals
                    work_changed.send(sender=work.__class__,
                                      work=work,
                                      request=view.request)

                    # info for links
                    pub_doc_params = {
                        'date': row.get('publication_date'),
                        'number': work.publication_number,
                        'publication': work.publication_name,
                        'country': view.country.place_code,
                        'locality':
                        view.locality.code if view.locality else None,
                    }
                    info['params'] = pub_doc_params

                    self.link_publication_document(work, info)

                    if not work.stub:
                        self.create_task(work, info, task_type='import')

                info['work'] = work
                info['status'] = 'success'

            except ValidationError as e:
                info['status'] = 'error'
                if hasattr(e, 'message_dict'):
                    info['error_message'] = ' '.join([
                        '%s: %s' % (f, '; '.join(errs))
                        for f, errs in e.message_dict.items()
                    ])
                else:
                    info['error_message'] = str(e)

        return info