示例#1
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.commenced = bool(
                row.get('commencement_date') or row.get('commenced_by'))
            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
示例#2
0
    def get_works(self, view, 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):
            # ignore if it's blank or explicitly marked 'ignore' in the 'ignore' column
            if row.get('ignore') or not [val for val in row.values() if val]:
                continue

            info = {
                'row': idx + 2,
            }
            works.append(info)

            row = self.validate_row(view, row)

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

            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 = not row.get('principal')
                work.created_by_user = view.request.user
                work.updated_by_user = view.request.user

                try:
                    work.full_clean()
                    work.save_with_revision(view.request.user)

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

                    # info for links, extra properties
                    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

                    for header in headers:
                        info[header] = row.get(header)

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

                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'] = e.message

        return works
示例#3
0
    def create_work(self, view, row, idx):
        # handle spreadsheet that still uses 'principal'
        row['stub'] = row.get(
            'stub') if 'stub' in row else not row.get('principal')
        row = self.validate_row(view, row)
        row.status = None
        row.row_number = idx + 2

        if row.errors:
            return row

        frbr_uri = self.get_frbr_uri(row)

        try:
            row.work = Work.objects.get(frbr_uri=frbr_uri)
            row.status = 'duplicate'

        except Work.DoesNotExist:
            work = Work()

            work.frbr_uri = frbr_uri
            work.country = view.country
            work.locality = view.locality
            for attribute in [
                    'title', 'publication_name', 'publication_number',
                    'assent_date', 'publication_date', 'commenced', 'stub'
            ]:
                setattr(work, attribute, getattr(row, attribute, None))
            work.created_by_user = view.request.user
            work.updated_by_user = view.request.user
            self.add_extra_properties(work, row)

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

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

                # info for linking publication document
                row.params = {
                    'date': work.publication_date,
                    'number': work.publication_number,
                    'publication': work.publication_name,
                    'country': view.country.place_code,
                    'locality': view.locality.code if view.locality else None,
                }

                self.link_publication_document(work, row)

                if not work.stub:
                    self.create_task(work, row, task_type='import-content')

                row.work = work
                row.status = 'success'

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

        return row