def save(self, commit=True, use_card_filenames=False): instance = super(TinyPngForm, self).save(commit=False) for field in self.fields.keys(): if (hasattr(instance, field) and field in dir(self.Meta.model) and type(self.Meta.model._meta.get_field(field)) == models.models.ImageField): image = self.cleaned_data[field] if image and (isinstance(image, InMemoryUploadedFile) or isinstance(image, TemporaryUploadedFile)): filename = image.name _, extension = os.path.splitext(filename) if extension.lower() == '.png': image = shrinkImageFromData(image.read(), filename) if use_card_filenames and field in models.cardsImagesToName: image.name = models.cardsImagesToName[field]({ 'id': instance.id, 'firstname': instance.idol.name.split(' ')[-1] if instance.idol and instance.idol.name else 'Unknown', }) else: image.name = randomString(32) + extension setattr(instance, field, image) if commit: instance.save() return instance
def handle(self, *args, **options): all_cards = models.Card.objects.all().filter(id=1070) for card in all_cards: for (field, f) in models.cardsImagesToName.items(): # if ((card.is_special and 'idolized' in field) # or (card.is_promo and 'idolized' not in field)): # continue name = f({ 'id': card.id, 'firstname': card.name.split(' ')[-1] if card.name else 'Unknown', }) if getattr(card, field) and unicode(getattr( card, field)) and unicode(getattr( card, field)).split('/')[-1].split('?')[0] != name: data = getattr(card, field).read() print u'#{id} {old} => {new}...'.format( id=card.id, old=unicode(getattr(card, field)), new=name), sys.stdout.flush() image = shrinkImageFromData(data, name) image.name = name setattr(card, field, image) card.save() print u'Done.'
def save(self, commit=True): instance = super(TinyPngForm, self).save(commit=False) for field in self.fields.keys(): if (hasattr(instance, field) and field in dir(self.Meta.model) and type(self.Meta.model._meta.get_field(field)) == models.models.ImageField): image = self.cleaned_data[field] if image and (isinstance(image, InMemoryUploadedFile) or isinstance(image, TemporaryUploadedFile)): filename = image.name image = shrinkImageFromData(image.read(), filename) setattr(instance, field, image) if commit: instance.save() return instance
def _tinypng_images(self, validated_data): idolName = self.context['request'].data.get('idol', None) if not idolName: idolName = self.instance.idol idolId = validated_data['id'] if 'id' in validated_data else self.instance.id for (field, value) in validated_data.items(): if value and (isinstance(value, InMemoryUploadedFile) or isinstance(value, TemporaryUploadedFile)): filename = value.name value = shrinkImageFromData(value.read(), filename) validated_data[field] = value if field in models.cardsImagesToName: value.name = models.cardsImagesToName[field]({ 'id': idolId, 'firstname': idolName.split(' ')[-1] if idolName else 'Unknown', }) return validated_data
def _postsave(self, validated_data, instance): for field, value in validated_data.items(): # Optimize images with TinyPNG if type(self.Meta.model._meta.get_field( field)) == models.models.ImageField: value = getattr(instance, field) filename = value.name content = value.read() if not content: setattr(instance, field, None) continue image = shrinkImageFromData( content, filename, settings=getattr(self.Meta.model, 'tinypng_settings', {}).get(field, {})) image.name = self.Meta.model._meta.get_field(field).upload_to( instance, filename) setattr(instance, field, image) return instance
def handle(self, *args, **options): all_cards = models.Card.objects.all() for card in all_cards: for (field, f) in models.cardsImagesToName.items(): if ((card.is_special and 'idolized' in field) or (card.is_promo and 'idolized' not in field)): continue name = f({ 'id': card.id, 'firstname': card.name.split(' ')[-1] if card.name else 'Unknown', }) if getattr(card, field) and unicode(getattr(card, field)) and unicode(getattr(card, field)).split('/')[-1].split('?')[0] != name: data = getattr(card, field).read() print u'#{id} {old} => {new}...'.format( id=card.id, old=unicode(getattr(card, field)), new=name ), sys.stdout.flush() image = shrinkImageFromData(data, name) image.name = name setattr(card, field, image) card.save() print u'Done.'