|
7 | 7 |
|
8 | 8 | from django.conf import settings
|
9 | 9 |
|
| 10 | +def response_mimetype(request): |
| 11 | + if "application/json" in request.META['HTTP_ACCEPT']: |
| 12 | + return "application/json" |
| 13 | + else: |
| 14 | + return "text/plain" |
| 15 | + |
10 | 16 | class PictureCreateView(CreateView):
|
11 | 17 | model = Picture
|
12 | 18 |
|
13 | 19 | def form_valid(self, form):
|
14 | 20 | self.object = form.save()
|
15 | 21 | f = self.request.FILES.get('file')
|
16 |
| - 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"}] |
17 |
| - return JSONResponse(data) |
| 22 | + 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=[self.object.id]), 'delete_type': "DELETE"}] |
| 23 | + return JSONResponse(data, {}, response_mimetype(self.request)) |
18 | 24 |
|
19 | 25 | class PictureDeleteView(DeleteView):
|
20 | 26 | model = Picture
|
21 | 27 |
|
22 | 28 | def delete(self, request, *args, **kwargs):
|
| 29 | + """ |
| 30 | + This does not actually delete the file, only the database record. But |
| 31 | + that is easy to implement. |
| 32 | + """ |
23 | 33 | self.object = self.get_object()
|
24 | 34 | self.object.delete()
|
25 |
| - return JSONResponse(True) |
| 35 | + return JSONResponse(True, {}, response_mimetype(self.request)) |
26 | 36 |
|
27 | 37 | class JSONResponse(HttpResponse):
|
28 |
| - """JSON response class. This does not help browsers not liking application/json.""" |
| 38 | + """JSON response class.""" |
29 | 39 | def __init__(self,obj='',json_opts={},mimetype="application/json",*args,**kwargs):
|
30 | 40 | content = simplejson.dumps(obj,**json_opts)
|
31 | 41 | super(JSONResponse,self).__init__(content,mimetype,*args,**kwargs)
|
0 commit comments