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 transform(get_value):
56 value = get_value(arg)
57 return Appendable(value)
61 class Appendable(str):
62 def as_hint_json(self):
69 def __init__(self, url):
72 def apply_to_field(self, obj, attname):
73 t = URLOpener().open(self.url).read()
74 getattr(obj, attname).save(
75 unquote(self.url.rsplit('/', 1)[-1]),
80 def as_hint_json(self):