From b564d47fdabc74745df3e563b696c632f6169579 Mon Sep 17 00:00:00 2001 From: ffont Date: Mon, 11 Mar 2024 16:55:01 +0100 Subject: [PATCH] Attribute sources of sounds as well https://github.com/MTG/freesound/issues/1681 --- sounds/models.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/sounds/models.py b/sounds/models.py index 514d3b661..79792c2c3 100644 --- a/sounds/models.py +++ b/sounds/models.py @@ -932,18 +932,27 @@ def get_license_history(self): @cached_property def attribution_texts(self): - return { - 'plain_text': f'{self.original_filename} by {self.user.username} -- {url2absurl(reverse("short-sound-link", args=[self.id]))} -- License: {self.license.name_with_version}', - 'html': f'{self.original_filename} by {self.user.username} | License: {self.license.name_with_version}', - 'json': json.dumps({ - 'sound_url': url2absurl(self.get_absolute_url()), - 'sound_name': self.original_filename, - 'author_url': url2absurl(reverse("account", args=[self.user.username])), - 'author_name': self.user.username, - 'license_url': self.license.deed_url, - 'license_name': self.license.name_with_version, - }) - } + attribution_texts = { + 'plain_text': f'{self.original_filename} by {self.user.username} -- {url2absurl(reverse("short-sound-link", args=[self.id]))} -- License: {self.license.name_with_version}', + 'html': f'{self.original_filename} by {self.user.username} | License: {self.license.name_with_version}', + 'json': json.dumps({ + 'sound_url': url2absurl(self.get_absolute_url()), + 'sound_name': self.original_filename, + 'author_url': url2absurl(reverse("account", args=[self.user.username])), + 'author_name': self.user.username, + 'license_url': self.license.deed_url, + 'license_name': self.license.name_with_version, + }) + } + if not self.sources.exists(): + return attribution_texts + else: + sources_attribution_texts = [s.attribution_texts for s in self.sources.all()] + attribution_texts['plain_text'] = "\n\n".join([attribution_texts['plain_text']] + [st['plain_text'] for st in sources_attribution_texts]) + attribution_texts['html'] = "
\n".join([attribution_texts['html']] + [st['html'] for st in sources_attribution_texts]) + attribution_texts['json'] = json.dumps([json.loads(attribution_texts['json'])] + [json.loads(st['json']) for st in sources_attribution_texts]) + return attribution_texts + def get_sound_tags(self, limit=None): """