views.py
6.96 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Create your views here.
from django.core.context_processors import csrf
from django.http import HttpResponse
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from store.api import StoreApi
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
import os
import json
import base64
def estabilish_store_user(request, user):
try:
details = request.user.cloud_details
password = details.smb_password
quota = details.disk_quota * 1024
key_list = []
for key in request.user.sshkey_set.all():
key_list.append(key.key)
except:
return HttpResponse('Can not acces to django database!', status=404)
#Create user
if not StoreApi.createuser(user, password, key_list, str(quota)):
return HttpResponse('User does not exist on store! And could not create!')
@login_required
def index(request):
user = request.user.username
if StoreApi.userexist(user) != True:
estabilish_store_user(request, user)
#UpdateAuthorizationInfo
try:
auth=request.POST['auth']
try:
details = request.user.cloud_details
password = details.smb_password
key_list = []
for key in request.user.sshkey_set.all():
key_list.append(key.key)
except:
return HttpResponse('Can not acces to django database!', status=404)
if not StoreApi.updateauthorizationinfo(user, password, key_list):
return HttpResponse('Can not update authorization information!')
except:
pass
#Download handler
try:
dl = request.POST['dl']
return redirect(StoreApi.requestdownload(user, dl))
except:
pass
#Upload handler
try:
ul = request.POST['ul']
url = StoreApi.requestupload(user,ul)
return render_to_response('store/upload.html', RequestContext(request,{'URL' : url}))
except:
pass
#Remove handler
try:
rm = request.POST['rm']
succes = StoreApi.requestremove(user,rm)
except:
pass
#Remove handler
try:
path = request.POST['path']
new = request.POST['new']
succes = StoreApi.requestnewfolder(user,path+'/'+new)
except:
pass
#Simple file list
path = '/'
try:
path = request.POST['path']
except:
pass
#Normalize path (Need double dirname /folder/ -> /folder -> /
backpath = os.path.normpath(os.path.dirname(os.path.dirname(path)))
file_list = StoreApi.listfolder(user,path)
quota = StoreApi.requestquota(user)
return render_to_response('store/list.html', RequestContext(request, {'file_list': file_list, 'path' : path, 'backpath' : backpath, 'username' : user, 'quota' : quota}))
@login_required
def ajax_listfolder(request):
user = request.user.username
if StoreApi.userexist(user) != True:
estabilish_store_user(request, user)
path = '/'
try:
path = request.POST['path']
except:
pass
#Normalize path (Need double dirname /folder/ -> /folder -> /
backpath = os.path.normpath(os.path.dirname(os.path.dirname(path)))
file_list = StoreApi.listfolder(user,path)
return HttpResponse(json.dumps(file_list))
@login_required
def ajax_quota(request):
user = request.user.username
if StoreApi.userexist(user) != True:
estabilish_store_user(request, user)
return HttpResponse(json.dumps(StoreApi.requestquota(user)))
#return HttpResponse(json.dumps({'Used':20,'Soft':160,'Hard':200}))
@login_required
def ajax_download(request):
user = request.user.username
try:
dl = request.POST['dl']
return HttpResponse(json.dumps({'url':StoreApi.requestdownload(user,dl)}))
except:
pass
return HttpResponse('File not found!', status=404)
@login_required
def ajax_upload(request):
user = request.user.username
try:
ul = request.POST['ul']
url = StoreApi.requestupload(user,ul)
return HttpResponse(json.dumps({'url':url}))
except:
pass
return HttpResponse('Error!', status=404)
@login_required
def ajax_delete(request):
user = request.user.username
try:
rm = request.POST['rm']
return HttpResponse(json.dumps({'success':StoreApi.requestremove(user,rm)}))
except:
pass
return HttpResponse('File not found!', status=404)
@login_required
def ajax_new_folder(request):
user = request.user.username
try:
path = request.POST['path']
new = request.POST['new']
success = StoreApi.requestnewfolder(user,path+'/'+new)
return HttpResponse(json.dumps({'success':success}))
except:
pass
return HttpResponse('Error!', status=404)
@login_required
def ajax_rename(request):
user = request.user.username
try:
path = request.POST['path']
new = request.POST['new']
success = StoreApi.requestrename(user,path,new)
return HttpResponse(json.dumps({'success':success}))
except:
pass
return HttpResponse('Error!', status=404)
@login_required
def toplist(request):
user = request.user.username
path = backpath = '/'
file_list = StoreApi.toplist(user)
return render_to_response('store/list.html', RequestContext(request, {'file_list': file_list, 'path' : path, 'backpath' : backpath, 'username' : user}))
@login_required
def ajax_toplist(request):
user = request.user.username
path = backpath = '/'
file_list = StoreApi.toplist(user)
return HttpResponse(json.dumps(file_list))
@login_required
def gui(request):
user = request.user.username
if request.method == 'GET':
return render_to_response('store/gui.html', RequestContext(request, {'username' : user, 'host' : StoreApi.get_host()}))
elif request.method == 'POST':
try:
details = request.user.cloud_details
password = details.smb_password
key_list = []
for key in request.user.sshkey_set.all():
key_list.append(key.key)
except:
return HttpResponse('Can not acces to django database!', status=404)
try:
lab_key_decoded = base64.b64decode(request.POST['KEY'])
key_list.append(lab_key_decoded)
except:
if StoreApi.updateauthorizationinfo(user, password, key_list):
return HttpResponse('Keys resetted succesfully!')
else:
return HttpResponse('Can not update authorization information!')
if StoreApi.updateauthorizationinfo(user, password, key_list):
return HttpResponse('https://cloud.ik.bme.hu/home/'+'?neptun='+user+'&'+'host='+StoreApi.get_host())
else:
return HttpResponse('Can not update authorization information!')
else:
return HttpResponse('Method not found!', status=404)
def logout(request):
auth.logout(request)
return redirect('/')