Beispiel #1
0
    def test_cwr21_processing_error_reporting(self):
        """
        Test EDI part of a CWR2.1 import.
        """

        with open(__file__, 'rb') as f:
            e = EdiFile(f)
            with self.assertRaises(FileError):
                list(e.get_groups())

        with open(CWR2_PATH, 'rb') as f:
            e = EdiFile(f)
            with self.assertWarns(DeprecationWarning):
                e.get_header()
            for group in e.list_groups():
                with self.assertWarns(DeprecationWarning):
                    f = group.get_file()
                self.assertEqual(e, f)
                with self.assertWarns(DeprecationWarning):
                    group.get_header()
                with self.assertRaises(RuntimeError):
                    # can not be run twice
                    list(group.get_transactions())
                with self.assertWarns(DeprecationWarning):
                    group.get_trailer()
            with self.assertWarns(DeprecationWarning):
                e.get_trailer()
        with open(CWR2_PATH, 'rb') as f:
            e = EdiFile(f)
            for group in e.get_groups():
                transactions = group.list_transactions()
                for transaction in transactions:
                    list(transaction.split_into_records())
Beispiel #2
0
    def post(self, request, *args, **kwargs):
        """Respond to POST."""
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            f = request.FILES['file']
            edi_file = EdiFile(f)
            edi_file.seek(0)
            edi_file.reconfigure(encoding='latin1')
        else:
            return render(request, 'cwr_file_start.html', {
                'title': 'CWR 2.x to Excel - Conversion WITHOUT Validation',
                'form': form
            })
        wb = load_workbook(os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'template.xlsx'))
        works = wb.get_sheet_by_name('Works')
        recordings = wb.get_sheet_by_name('Recordings')

        for group in edi_file.get_groups():
            for transaction in group.get_transactions():
                d = transaction.to_dict(1)
                work_keys = [
                    'submitter_work_number',
                    'work_title',
                    'iswc',
                    'recordings',
                    'duration',
                    'language_code',
                    'musical_work_distribution_category',
                    'version_type',
                    'other_titles',
                    'writers',
                    'performing_artists',
                    'original_publishers',
                ]
                values = []
                for key in work_keys:
                    if key == 'recordings':
                        isrcs = []
                        for recording in d['recordings']:
                            isrc = recording.get('isrc')
                            if isrc:
                                isrcs.append(isrc['value'])
                            recordings.append([
                                d.get('submitter_work_number', {}).get(
                                    'value', ''),
                                d.get('work_title').get('value', ''),
                                recording.get(
                                    'isrc', {}
                                ).get('value', ''),
                                recording.get(
                                    'release_date', {}
                                ).get('value', ''),
                                recording.get(
                                    'recording_duration', {}
                                ).get('value', ''),
                                recording.get(
                                    'album_title', {}
                                ).get('value', ''),
                                recording.get(
                                    'ean', {}
                                ).get('value', ''),
                                recording.get(
                                    'album_label', {}
                                ).get('value', ''),
                            ])
                        value = ' | '.join(isrcs)
                    elif key == 'other_titles':
                        value = ' | '.join([
                            t['title']['value'] or '' for t in
                            d[key]])
                    elif key == 'writers':
                        writer_strings = []
                        for writer in d['writers']:
                            controlled = writer.get('controlled')
                            ln = writer.get(
                                'writer_last_name',
                                {'value': '<UNKNOWN>'}
                            )['value']
                            fn = writer.get(
                                'writer_first_name', {}
                            ).get('value', '')
                            ipi_name = writer.get(
                                'writer_ipi_name_number', {}
                            ).get('value', '')
                            role = writer.get(
                                'writer_role', {}
                            ).get('value', '')
                            pr_share = writer.get(
                                'pr_ownership_share', {}
                            ).get('value', '') or 0
                            mr_share = writer.get(
                                'mr_ownership_share', {}
                            ).get('value', '') or 0
                            sr_share = writer.get(
                                'sr_ownership_share', {}
                            ).get('value', '') or 0
                            if controlled:
                                writer_strings.append(
                                    f'{ln}, {fn} [{ipi_name}] ({role}) '
                                    f'{{{pr_share * 100}%,{mr_share * 100}%'
                                    f',{sr_share * 100}%}} *'
                                )
                            else:
                                writer_strings.append(
                                    f'{ln}, {fn} [{ipi_name}] ({role}) '
                                    f'{{{pr_share * 100}%,{mr_share * 100}%'
                                    f',{sr_share * 100}%}}'
                                )
                        value = ' | '.join(writer_strings)
                    elif key == 'performing_artists':
                        artist_strings = []
                        for artist in d['performing_artists']:
                            ln = artist.get(
                                'performing_artist_name',
                                {'value': '<UNKNOWN>'}
                            )['value']
                            fn = artist.get(
                                'performing_artist_first_name', {}
                            ).get('value', '')
                            if fn:
                                artist_strings.append(
                                    f'{ ln }, { fn }'
                                )
                            else:
                                artist_strings.append(ln)
                        value = ' | '.join(artist_strings)
                    elif key == 'original_publishers':
                        publisher_strings = []
                        for publisher in d['original_publishers']:
                            controlled = publisher.get('controlled')
                            n = publisher.get(
                                'publisher_name',
                                {'value': '<UNKNOWN>'}
                            )['value']
                            ipi_name = publisher.get(
                                'publisher_ipi_name_number', {}
                            ).get('value', '')
                            role = publisher.get(
                                'publisher_role', {}
                            ).get('value', '')
                            pr_share = publisher.get(
                                'pr_ownership_share', {}
                            ).get('value', '') or 0
                            mr_share = publisher.get(
                                'mr_ownership_share', {}
                            ).get('value', '') or 0
                            sr_share = publisher.get(
                                'sr_ownership_share', {}
                            ).get('value', '') or 0

                            if controlled:
                                publisher_strings.append(
                                    f'{ n } [{ ipi_name }] ({role}) *'
                                    f'{{{pr_share * 100}%,{mr_share * 100}%'
                                    f',{sr_share * 100}%}} *'
                                )
                            else:
                                publisher_strings.append(
                                    f'{n} [{ipi_name}] ({role}) '
                                    f'{{{pr_share * 100}%,{mr_share * 100}%'
                                    f',{sr_share * 100}%}}'
                                )
                        value = ' | '.join(publisher_strings)
                    else:
                        value = d[key].get('value', '') if d.get(key) else ''
                    values.append(value)
                works.append(values)

        response = HttpResponse(
            excel_writer.excel.save_virtual_workbook(wb),
            content_type='application/vnd.ms-excel')
        s = 'attachment; filename="{}.xlsx"'.format(
            edi_file.name)
        response['Content-Disposition'] = s
        return response