From 98991aa87cc8c4c3377247afc007823124cdc658 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 1 Mar 2022 01:01:36 +0100 Subject: [PATCH] Cover import fixes. --- requirements/requirements.txt | 2 +- src/cover/forms.py | 5 +++++ src/cover/utils.py | 17 +++++++++++------ src/cover/views.py | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 91d928e7..8ec45da3 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -10,7 +10,7 @@ python-slugify python-docx==0.8.10 Wikidata==0.6.1 -librarian==2.3.2 +librarian==2.3.4 ## Django Django==3.1.13 diff --git a/src/cover/forms.py b/src/cover/forms.py index 3be4e211..550e4a5f 100644 --- a/src/cover/forms.py +++ b/src/cover/forms.py @@ -50,6 +50,11 @@ class ImageAddForm(forms.ModelForm): % {'url': same_source.first().get_absolute_url()})) return source_url + clean_cut_top = lambda self: self.cleaned_data.get('cut_top') or 0 + clean_cut_bottom = lambda self: self.cleaned_data.get('cut_bottom') or 0 + clean_cut_left = lambda self: self.cleaned_data.get('cut_left') or 0 + clean_cut_right = lambda self: self.cleaned_data.get('cut_right') or 0 + def clean(self): cleaned_data = super(ImageAddForm, self).clean() download_url = cleaned_data.get('download_url', None) diff --git a/src/cover/utils.py b/src/cover/utils.py index 3bf3e9f9..55fac474 100644 --- a/src/cover/utils.py +++ b/src/cover/utils.py @@ -78,6 +78,7 @@ def get_wikimedia_data(url): """ file_name = url.rsplit('/', 1)[-1].rsplit(':', 1)[-1] d = json.loads(URLOpener().open('https://commons.wikimedia.org/w/api.php?action=query&titles=File:{}&prop=imageinfo&iiprop=url|user|extmetadata&iimetadataversion=latest&format=json'.format(file_name)).read().decode('utf-8')) + d = list(d['query']['pages'].values())[0]['imageinfo'][0] ext = d['extmetadata'] @@ -86,18 +87,22 @@ def get_wikimedia_data(url): 'author': d['user'], 'source_url': d['descriptionurl'], 'download_url': d['url'], - 'license_url': ext['LicenseUrl']['value'], + 'license_url': ext.get('LicenseUrl', {}).get('value', ''), 'license_name': ext['LicenseShortName']['value'], } - + + if meta['license_name'] == 'Public domain': + meta['license_name'] = 'domena publiczna' + meta['license_url'] = 'https://pl.wikipedia.org/wiki/Domena_publiczna' + + return meta def get_mnw_data(url): """ - >>> get_mnw_data('https://cyfrowe.mnw.art.pl/pl/katalog/794032') - {'title': 'Pejzaż z podwójnym świerkiem', 'author': 'nieznany, Altdorfer, Albrecht (ca 1480-1538)', 'source_url': 'https://cyfrowe.mnw.art.pl/pl/katalog/794032', 'download_url': 'https://cyfrowe-cdn.mnw.art.pl/upload/multimedia/49/58/49583b3e9b23e2d25f372fe6021ae220.jpg', 'license_url': 'https://pl.wikipedia.org/wiki/Domena_publiczna', 'license_name': 'DOMENA PUBLICZNA'} - + >>> get_mnw_data('https://cyfrowe.mnw.art.pl/pl/katalog/511078') + {'title': 'Chłopka (Baba ukraińska)', 'author': 'Krzyżanowski, Konrad (1872-1922)', 'source_url': 'https://cyfrowe.mnw.art.pl/pl/katalog/511078', 'download_url': 'https://cyfrowe-cdn.mnw.art.pl/upload/multimedia/32/60/3260ae1704cc530cc62befa9b7d58cbd.jpg', 'license_url': 'https://pl.wikipedia.org/wiki/Domena_publiczna', 'license_name': 'domena publiczna'} """ nr = url.rsplit('/', 1)[-1] d = list( @@ -120,7 +125,7 @@ def get_mnw_data(url): license_name = d['copyrights.0.name'] if license_name == 'DOMENA PUBLICZNA': license_name = 'domena publiczna' - license_url = '' + license_url = 'https://pl.wikipedia.org/wiki/Domena_publiczna' return { 'title': d['title'], diff --git a/src/cover/views.py b/src/cover/views.py index 02d642c4..620ad1aa 100644 --- a/src/cover/views.py +++ b/src/cover/views.py @@ -127,7 +127,7 @@ def image_file(request, pk): @active_tab('cover') def image_list(request): return render(request, "cover/image_list.html", { - 'object_list': Image.objects.all(), + 'object_list': Image.objects.all().order_by('-id'), 'can_add': request.user.has_perm('cover.add_image'), }) -- 2.20.1