Beispiel #1
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
Beispiel #2
0
    def get_works(self, 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 not row.get('ignore') and [
                    val for val in row.itervalues() if val
            ]:
                info = {
                    'row': idx + 2,
                }
                works.append(info)

                try:
                    frbr_uri = self.get_frbr_uri(self.country, self.locality,
                                                 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.get('title')
                    work.country = self.country
                    work.locality = self.locality
                    work.publication_name = row.get('publication_name')
                    work.publication_number = row.get('publication_number')
                    work.created_by_user = self.request.user
                    work.updated_by_user = self.request.user

                    try:
                        work.publication_date = self.make_date(
                            row.get('publication_date'), 'publication_date')
                        work.commencement_date = self.make_date(
                            row.get('commencement_date'), 'commencement_date')
                        work.assent_date = self.make_date(
                            row.get('assent_date'), 'assent_date')
                        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'
                        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
Beispiel #3
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
Beispiel #4
0
    def get_works(self, table, form):
        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 not row.get('ignore') and [val for val in row.itervalues() if val]:
                info = {
                    'row': idx + 2,
                }
                works.append(info)

                try:
                    frbr_uri = self.get_frbr_uri(self.country, self.locality, 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'
                    if row.get('amends'):
                        info['amends'] = row.get('amends')
                    if row.get('commencement_date'):
                        info['commencement_date'] = row.get('commencement_date')

                except Work.DoesNotExist:
                    work = Work()

                    work.frbr_uri = frbr_uri
                    work.title = self.strip_title_string(row.get('title'))
                    work.country = self.country
                    work.locality = self.locality
                    work.publication_name = row.get('publication_name')
                    work.publication_number = row.get('publication_number')
                    work.created_by_user = self.request.user
                    work.updated_by_user = self.request.user
                    work.stub = not row.get('primary')

                    try:
                        work.publication_date = self.make_date(row.get('publication_date'), 'publication_date')
                        work.commencement_date = self.make_date(row.get('commencement_date'), 'commencement_date')
                        work.assent_date = self.make_date(row.get('assent_date'), 'assent_date')
                        work.full_clean()
                        work.save()

                        # link publication document
                        params = {
                            'date': row.get('publication_date'),
                            'number': work.publication_number,
                            'publication': work.publication_name,
                            'country': self.country.place_code,
                            'locality': self.locality.code if self.locality else None,
                        }
                        self.get_publication_document(params, work, form)

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

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

                        # TODO: neaten this up
                        if row.get('commenced_by'):
                            info['commenced_by'] = row.get('commenced_by')
                        if row.get('amends'):
                            info['amends'] = row.get('amends')
                        if row.get('repealed_by'):
                            info['repealed_by'] = row.get('repealed_by')
                        if row.get('with_effect_from'):
                            info['with_effect_from'] = row.get('with_effect_from')
                        if row.get('parent_work'):
                            info['parent_work'] = row.get('parent_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