forked from chu888chu888/Python-SAE-appdisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
126 lines (101 loc) · 3.66 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# coding: utf-8
from __future__ import unicode_literals
import json
import urllib2
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.http import require_GET, require_POST
from appdisk.utils import *
from django.conf import settings
# test compiled js?
USE_COMPILED_JS = True
def test(request):
ctx = RequestContext(request)
return render_to_response('appdisk/test.html', {},
context_instance=ctx)
def render_error_page(request, error_msg='出错了...'):
return render_to_response('appdisk/error.html', {
'error_msg': error_msg
}, context_instance=RequestContext(request))
def render_ok_json():
content_type = 'application/json; charset=utf-8'
ret = {'success': True}
return HttpResponse(json.dumps(ret), content_type=content_type)
def render_error_json(error_msg='出错了...'):
content_type = 'application/json; charset=utf-8'
ret = {'error': error_msg,
'success': False}
return HttpResponse(json.dumps(ret), content_type=content_type)
@require_GET
def files(request):
path = request.GET.get('p', '/')
files = get_dir_entries(path)
navlinks = gen_nav_links(path)
return render_to_response('appdisk/files.html', {
'path': path,
'navlinks': navlinks,
'files': files,
'SAE': settings.SAE,
'USE_COMPILED_JS': USE_COMPILED_JS,
}, context_instance=RequestContext(request))
@require_POST
def upload(request):
parent_dir = request.GET.get('p', '/')
try:
file_name, content = get_uploaded_file(request)
except Exception, e:
return render_error_page(request, '上传文件失败: %s' % e)
try:
add_file(parent_dir, file_name, content)
except Exception, e:
return render_error_page(request, '上传文件失败: %s' % e)
# TODO: add flash message
url = reverse(files)
url += '?p=' + urllib2.quote(parent_dir.encode('utf-8'))
return HttpResponseRedirect(url)
@require_POST
def newdir(request):
parent_dir = request.POST.get('p', None)
newdir_name = request.POST.get('name', None)
if not parent_dir or not newdir_name or not is_valid_name(newdir_name):
return render_error_json('错误的参数')
try:
add_dir(parent_dir, newdir_name)
except Exception, e:
return render_error_json('创建新文件夹失败: %s' % e)
return render_ok_json()
@require_POST
def remove(request):
path = request.POST.get('p', '')
ftype = request.POST.get('t', '')
if path == '' or path == '/' or ftype not in ['f', 'd']:
return render_error_json('错误的参数')
try:
if ftype == 'f':
remove_file(path)
else:
remove_dir(path)
except Exception, e:
return render_error_json('删除失败: %s' % e)
return render_ok_json()
def redirect_to_path(path):
url = reverse(files)
url += '?p=' + urllib2.quote(path.encode('utf-8'))
return HttpResponseRedirect(url)
def subdir(request):
ret = {}
content_type = 'application/json; charset=utf-8'
try:
path = request.GET['p']
except KeyError:
ret['error'] = "what's the path?"
return HttpResponse(json.dumps(ret), content_type=content_type)
if path == '':
ret['error'] = "what's the path?"
return HttpResponse(json.dumps(ret), content_type=content_type)
files = get_dir_entries(path)
ret['success'] = True
ret['files'] = files
return HttpResponse(json.dumps(ret), content_type=content_type)