Skip to content

Commit

Permalink
make delete buttons work
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdga committed Jun 12, 2011
1 parent ea9cfe6 commit 2b6b3b3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion fileupload/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def forwards(self, orm):
db.create_table('fileupload_picture', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('file', self.gf('django.db.models.fields.files.ImageField')(max_length=100)),
('slug', self.gf('django.db.models.fields.SlugField')(db_index=True, max_length=50, blank=True)),
))
db.send_create_signal('fileupload', ['Picture'])

Expand All @@ -26,7 +27,8 @@ def backwards(self, orm):
'fileupload.picture': {
'Meta': {'object_name': 'Picture'},
'file': ('django.db.models.fields.files.ImageField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'slug': ('django.db.models.fields.SlugField', [], {'db_index': 'True', 'max_length': '50', 'blank': 'True'})
}
}

Expand Down
4 changes: 4 additions & 0 deletions fileupload/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class Picture(models.Model):

file = models.ImageField(upload_to="pictures")
slug = models.SlugField(max_length=50, blank=True)

def __unicode__(self):
return self.file
Expand All @@ -11,3 +12,6 @@ def __unicode__(self):
def get_absolute_url(self):
return ('upload-new', )

def save(self, *args, **kwargs):
self.slug = self.file.name
super(Picture, self).save(*args, **kwargs)
3 changes: 2 additions & 1 deletion fileupload/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.conf.urls.defaults import *
from fileupload.views import PictureCreateView
from fileupload.views import PictureCreateView, PictureDeleteView

urlpatterns = patterns('',
(r'^new/$', PictureCreateView.as_view(), {}, 'upload-new'),
(r'^delete/(?P<slug>.+)$', PictureDeleteView.as_view(), {}, 'upload-delete'),
)

13 changes: 11 additions & 2 deletions fileupload/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from fileupload.models import Picture
from django.views.generic import CreateView
from django.views.generic import CreateView, DeleteView

from django.http import HttpResponse
from django.utils.functional import Promise
from django.utils.encoding import force_unicode
from django.utils import simplejson
from django.core.urlresolvers import reverse

from django.conf import settings

Expand All @@ -14,9 +15,17 @@ class PictureCreateView(CreateView):
def form_valid(self, form):
self.object = form.save()
f = self.request.FILES.get('file')
data = [{'name': f.name, 'url': settings.MEDIA_URL + "pictures/" + f.name, 'thumbnail_url': settings.MEDIA_URL + "pictures/" + f.name, 'delete_url': "index.html", 'delete_type': "DELETE"}]
data = [{'name': f.name, 'url': settings.MEDIA_URL + "pictures/" + f.name, 'thumbnail_url': settings.MEDIA_URL + "pictures/" + f.name, 'delete_url': reverse('upload-delete', args=[f.name]), 'delete_type': "DELETE"}]
return JSONResponse(data)

class PictureDeleteView(DeleteView):
model = Picture

def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.delete()
return JSONResponse(True)

class JSONResponse(HttpResponse):
""" JSON response class """
def __init__(self,content='',json_opts={},mimetype="application/json",*args,**kwargs):
Expand Down

0 comments on commit 2b6b3b3

Please sign in to comment.