def _save_images(self, artist, metadata): for i in metadata.get('images', []): itype = ImageType.objects.get_or_create(name=i['type'])[0] size = Size.objects.get_or_create(width=i['width'], height=i['height'])[0] handler = ImageHandlerArtist() handler.load_by_url(i['uri']) handler.save_thumbnails('PNG') filename = handler.storage.filename ArtistImage.objects.create(type=itype, size=size, filename=filename, artist=artist) if i['type'] == 'primary': artist.image = filename artist.save() if metadata.has_key('images'): del metadata['images'] return metadata
def __init__(self, *args, **kwargs): super(Artist, self).__init__(*args, **kwargs) self.image_handler = ImageHandlerArtist() if self.image: self.image_handler.load(self.image)
class Artist(models.Model): discogs_id = models.IntegerField(unique=True) name = models.CharField(max_length=128, unique=True) description = models.CharField(max_length=256, null=True, blank=True) about = models.TextField(null=True, blank=True) metadata = JSONField(null=True, blank=True) membership = models.ManyToManyField('Artist', through='ArtistMembership') is_group = models.BooleanField(default=False) image = models.CharField(max_length=256, null=True, blank=True) active_members_count = models.IntegerField(default=0) inactive_members_count = models.IntegerField(default=0) albums_count = models.IntegerField(default=0) def __unicode__(self): return self.name_display def __init__(self, *args, **kwargs): super(Artist, self).__init__(*args, **kwargs) self.image_handler = ImageHandlerArtist() if self.image: self.image_handler.load(self.image) @property def thumb_url(self): if self.image: return self.image_handler.url('thumb') else: return '/media/img/user.thumb.png' @property def icon_url(self): if self.image: return self.image_handler.url('icon') else: return '/media/img/user.icon.png' @property def huge_url(self): if self.image: return self.image_handler.url('huge') else: return '/media/img/user.huge.png' @property def short_name(self): name = self.name_display splited = name.split(' ') if splited[0].lower() in ['the', 'a']: return ' '.join(splited[1:]) else: return name @property def name_display(self): splited = self.name.split(', ') if len(splited) > 1: end = splited[-1].strip() if end.lower() in ['the', 'a']: n = ', '.join(splited[:-1]) return "%s %s" % (end, n) else: return self.name @property def metadata_display(self): return metadata_display(self.get_metadata_object() or {}) @property def active_members(self): return [i.member for i in ArtistMembership.objects.filter(active=True, artist=self)] @property def inactive_members(self): return [i.member for i in ArtistMembership.objects.filter(active=False, artist=self)] @property def albums_by_year(self): return self.albums.all().order_by('year')