Re: Where to delete model image?

2012-07-13 Thread Ernesto Guevara
Hi!

I have this old code here.

# forms.py

class UploadFileForm(ModelForm):
title = forms.CharField(max_length=250 ,label="Nome:")
original_image = forms.ImageField(label="Imagem:")

class Meta:
model = Photo

# models.py

from django.dispatch import receiver
from django.db.models.signals import pre_delete

class Photo(models.Model):
title = models.CharField(max_length=255)
pub_date = models.DateField(auto_now = True)
original_image = models.ImageField(upload_to='photos')
slug_photo = AutoSlugField(populate_from=('title'), unique=True,
max_length=255, overwrite=True)
model = models.ForeignKey(Model, blank=True, null=True)

@receiver(pre_delete, sender=Photo)
def delete_image(sender, instance, **kwargs):
os.remove(unicode(instance.original_image.path)) # remove the image
from folder "photos"


# views.py

def upload_photo(request):
if request.method == 'POST':
model = get_object_or_404(Model,
pk=request.POST.get("model"))
form = UploadForm(request.POST, request.FILES)
photo_exist = Photo.objects.filter(model__id = model.id)
if not photo_exist:
if form.is_valid():
form.save()
messages.add_message(request, messages.SUCCESS, 'Image save
succesfull!')
return HttpResponseRedirect(reverse('detail_model',
args=[model.slug]))
else:
return
render_to_response('project/app/detail.html',locals(),context_instance=RequestContext(request))

else:
if form.is_valid():
photo_exist.delete()
form.save()
messages.add_message(request, messages.SUCCESS, 'Image
update succesfull!')
return HttpResponseRedirect(reverse('detail_model',
args=[model.slug]))
else:
photo = Photo.objects.get(model__id = model.id)  # send the
photo to detail page
return
render_to_response('project/app/detail.html',locals(),context_instance=RequestContext(request))



2012/7/13 m.paul 

> Hi All,
>
> I have a Model with an image field and I want to be able to change the
> image using a ModelForm. When changing the image, the old image should be
> deleted and replaced by the new image.
>
> I have tried to do this in the clean method of the ModelForm like this:
>
> def clean(self):
> cleaned_data = super(ModelForm, self).clean()
>
> old_profile_image = self.instance.image
> if old_profile_image:
> old_profile_image.delete(save=False)
> return cleaned_data
>
> This works fine unless the file indicated by the user is not correct (for
> example if its not an image), which result in the image being deleted
> without any new images being saved. I would like to know where is the best
> place to delete the old image? By this I mean where can I be sure that the
> new image is correct before deleting the old one? I prefer to do this in my
> ModelForm class if possible but doing it in the Model class would also be
> acceptable.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/kB0VSrr9O1MJ.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Where to delete model image?

2012-07-13 Thread m.paul
 

Hi All,

I have a Model with an image field and I want to be able to change the 
image using a ModelForm. When changing the image, the old image should be 
deleted and replaced by the new image. 

I have tried to do this in the clean method of the ModelForm like this:

def clean(self):
cleaned_data = super(ModelForm, self).clean()

old_profile_image = self.instance.image
if old_profile_image:
old_profile_image.delete(save=False)
return cleaned_data

This works fine unless the file indicated by the user is not correct (for 
example if its not an image), which result in the image being deleted 
without any new images being saved. I would like to know where is the best 
place to delete the old image? By this I mean where can I be sure that the 
new image is correct before deleting the old one? I prefer to do this in my 
ModelForm class if possible but doing it in the Model class would also be 
acceptable. 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/kB0VSrr9O1MJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.