|
1 |
| -from fileupload.models import Picture |
2 |
| -from django.views.generic import CreateView, DeleteView |
| 1 | +# encoding: utf-8 |
| 2 | +from django.views.generic import CreateView, DeleteView, View |
| 3 | +from .models import Picture |
| 4 | +from .response import JSONResponse, response_mimetype |
| 5 | +from .serialize import serialize |
3 | 6 |
|
4 |
| -from django.http import HttpResponse, HttpResponseRedirect |
5 |
| -from django.utils import simplejson |
6 |
| -from django.core.urlresolvers import reverse |
7 |
| - |
8 |
| -from django.conf import settings |
9 |
| -import re |
10 |
| - |
11 |
| -def response_mimetype(request): |
12 |
| - if "application/json" in request.META['HTTP_ACCEPT']: |
13 |
| - return "application/json" |
14 |
| - else: |
15 |
| - return "text/plain" |
16 |
| - |
17 |
| -def orderName(name): |
18 |
| - name = re.sub (r'^.*/', '', name) |
19 |
| - if len(name)>20: |
20 |
| - return name[:10] + "..." + name[-7:] |
21 |
| - else: |
22 |
| - return name |
23 | 7 |
|
24 | 8 | class PictureCreateView(CreateView):
|
25 | 9 | model = Picture
|
26 | 10 |
|
27 | 11 | def form_valid(self, form):
|
28 | 12 | self.object = form.save()
|
29 |
| - f = self.request.FILES.get('file') |
30 |
| - files = [{ |
31 |
| - 'url': self.object.file.url, |
32 |
| - 'name': orderName(f.name), |
33 |
| - "type": "image/png", |
34 |
| - 'thumbnailUrl': self.object.file.url, |
35 |
| - 'size': f.size, |
36 |
| - 'deleteUrl': reverse('upload-delete', args=[self.object.id]), |
37 |
| - 'deleteType': "DELETE", |
38 |
| - }] |
| 13 | + files = [serialize(self.object)] |
39 | 14 | data = {"files": files}
|
40 |
| - response = JSONResponse(data, {}, response_mimetype(self.request)) |
| 15 | + response = JSONResponse(data, mimetype=response_mimetype(self.request)) |
41 | 16 | response['Content-Disposition'] = 'inline; filename=files.json'
|
42 | 17 | return response
|
43 | 18 |
|
| 19 | + |
44 | 20 | class BasicVersionCreateView(PictureCreateView):
|
45 | 21 | template_name_suffix = '_basic_form'
|
46 | 22 |
|
| 23 | + |
47 | 24 | class BasicPlusVersionCreateView(PictureCreateView):
|
48 | 25 | template_name_suffix = '_basicplus_form'
|
49 | 26 |
|
| 27 | + |
50 | 28 | class AngularVersionCreateView(PictureCreateView):
|
51 | 29 | template_name_suffix = '_angular_form'
|
52 | 30 |
|
| 31 | + |
53 | 32 | class jQueryVersionCreateView(PictureCreateView):
|
54 | 33 | template_name_suffix = '_jquery_form'
|
55 | 34 |
|
| 35 | + |
56 | 36 | class PictureDeleteView(DeleteView):
|
57 | 37 | model = Picture
|
58 | 38 |
|
59 | 39 | def delete(self, request, *args, **kwargs):
|
60 | 40 | self.object = self.get_object()
|
61 | 41 | self.object.delete()
|
62 |
| - response = JSONResponse(True, {}, response_mimetype(self.request)) |
| 42 | + response = JSONResponse(True, mimetype=response_mimetype(request)) |
63 | 43 | response['Content-Disposition'] = 'inline; filename=files.json'
|
64 | 44 | return response
|
65 | 45 |
|
66 |
| -def PictureListView(request): |
67 |
| - files = [] |
68 |
| - for obj in Picture.objects.all(): |
69 |
| - files += [{ |
70 |
| - 'name': orderName(obj.file.name), |
71 |
| - 'size': obj.file.size, |
72 |
| - 'url': obj.file.url, |
73 |
| - 'thumbnailUrl': obj.file.url, |
74 |
| - 'deleteUrl': reverse('upload-delete', args=[obj.id]), |
75 |
| - 'deleteType': "DELETE" |
76 |
| - }] |
77 |
| - data = {"files": files} |
78 |
| - response = JSONResponse(data, {}, response_mimetype(request)) |
79 |
| - response['Content-Disposition'] = 'inline; filename=files.json' |
80 |
| - return response |
81 |
| - |
82 |
| -class JSONResponse(HttpResponse): |
83 |
| - """JSON response class.""" |
84 |
| - def __init__(self,obj='',json_opts={},mimetype="application/json",*args,**kwargs): |
85 |
| - content = simplejson.dumps(obj,**json_opts) |
86 |
| - super(JSONResponse,self).__init__(content,mimetype,*args,**kwargs) |
| 46 | + |
| 47 | +class PictureListView(View): |
| 48 | + def get(self, request, *args, **kwargs): |
| 49 | + files = [] |
| 50 | + for obj in Picture.objects.all(): |
| 51 | + files.append(serialize(obj)) |
| 52 | + data = {'files': files} |
| 53 | + response = JSONResponse(data, mimetype=response_mimetype(request)) |
| 54 | + response['Content-Disposition'] = 'inline; filename=files.json' |
| 55 | + return response |
0 commit comments