1 from urllib.parse import unquote
2 from django.core.files.base import ContentFile
3 from cover.utils import get_wikimedia_data, URLOpener
7 def get_description_url(imgdata):
10 return imgdata.attributes['imageinfo'][0]['descriptionurl']
13 def descriptionurl(cls, arg):
14 def transform(get_value):
15 value = get_value(arg)
18 return cls.get_description_url(value)
22 def attribution(cls, arg):
23 def transform(get_value):
24 value = get_value(arg)
27 media_data = get_wikimedia_data(
28 cls.get_description_url(value)
33 media_data['license_name'],
35 parts = [p for p in parts if p]
36 attribution = ', '.join(parts)
41 def download(cls, arg):
42 def transform(get_value):
43 value = get_value(arg)
46 media_data = get_wikimedia_data(
47 cls.get_description_url(value)
49 download_url = media_data['download_url']
50 return Downloadable(download_url)
55 def __init__(self, url):
58 def apply_to_field(self, obj, attname):
59 t = URLOpener().open(self.url).read()
60 getattr(obj, attname).save(
61 unquote(self.url.rsplit('/', 1)[-1]),
66 def as_hint_json(self):