Commit d9103dc8 by cloud

Modified code style added dictionary based list parameters

parent be2f21b2
...@@ -4,46 +4,46 @@ import json, requests, time ...@@ -4,46 +4,46 @@ import json, requests, time
# Create your models here. # Create your models here.
#TODO Handle exceptions locally #TODO Handle exceptions locally
class StoreApi(models.Model): class StoreApi(models.Model):
store_url = 'https://store.cloud.ik.bme.hu' # store_url = 'https://store.cloud.ik.bme.hu'
# store_url = 'http://store.cloud.ik.bme.hu:8080' store_url = 'http://store.cloud.ik.bme.hu:8080'
store_user = 'admin' store_user = 'admin'
store_password = 'IQu8Eice' store_password = 'IQu8Eice'
basic_auth = True basic_auth = True
verify_ssl = False verify_ssl = False
@staticmethod @staticmethod
def ListFolder(neptun,path): def listfolder(neptun, path):
url = StoreApi.store_url+'/'+neptun url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'LIST', 'PATH' : path }) payload = json.dumps({ 'CMD' : 'LIST', 'PATH' : path })
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
if StoreApi.basic_auth: if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl) r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user, StoreApi.store_password), verify=StoreApi.verify_ssl)
else: else:
r = requests.post(url, data=payload, headers=headers) r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
tupplelist = json.loads(r.content) tupplelist = json.loads(r.content)
for item in tupplelist: for item in tupplelist:
item[3] = time.ctime(item[3]) item['MTIME'] = time.ctime(item['MTIME'])
return tupplelist return tupplelist
else: else:
raise Http404 raise Http404
@staticmethod @staticmethod
def RequestDownload(neptun,path): def requestdownload(neptun, path):
url = StoreApi.store_url+'/'+neptun url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'DOWNLOAD', 'PATH' : path }) payload = json.dumps({ 'CMD' : 'DOWNLOAD', 'PATH' : path })
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
if StoreApi.basic_auth: if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl) r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user, StoreApi.store_password), verify=StoreApi.verify_ssl)
else: else:
r = requests.post(url, data=payload, headers=headers) r = requests.post(url, data=payload, headers=headers)
response = json.loads(r.content) response = json.loads(r.content)
return response['LINK'] return response['LINK']
@staticmethod @staticmethod
def RequestUpload(neptun,path): def requestupload(neptun, path):
url = StoreApi.store_url+'/'+neptun url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'UPLOAD', 'PATH' : path }) payload = json.dumps({ 'CMD' : 'UPLOAD', 'PATH' : path })
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
if StoreApi.basic_auth: if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl) r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user, StoreApi.store_password), verify=StoreApi.verify_ssl)
else: else:
r = requests.post(url, data=payload, headers=headers) r = requests.post(url, data=payload, headers=headers)
response = json.loads(r.content) response = json.loads(r.content)
...@@ -52,12 +52,12 @@ class StoreApi(models.Model): ...@@ -52,12 +52,12 @@ class StoreApi(models.Model):
else: else:
raise Http404 raise Http404
@staticmethod @staticmethod
def RequestRemove(neptun,path): def requestremove(neptun, path):
url = StoreApi.store_url+'/'+neptun url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'REMOVE', 'PATH' : path }) payload = json.dumps({ 'CMD' : 'REMOVE', 'PATH' : path })
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
if StoreApi.basic_auth: if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl) r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user, StoreApi.store_password), verify=StoreApi.verify_ssl)
else: else:
r = requests.post(url, data=payload, headers=headers) r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
...@@ -65,12 +65,12 @@ class StoreApi(models.Model): ...@@ -65,12 +65,12 @@ class StoreApi(models.Model):
else: else:
return False return False
@staticmethod @staticmethod
def RequestNewFolder(neptun,path): def requestnewfolder(neptun, path):
url = StoreApi.store_url+'/'+neptun url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'NEW_FOLDER', 'PATH' : path }) payload = json.dumps({ 'CMD' : 'NEW_FOLDER', 'PATH' : path })
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
if StoreApi.basic_auth: if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl) r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user, StoreApi.store_password), verify=StoreApi.verify_ssl)
else: else:
r = requests.post(url, data=payload, headers=headers) r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
...@@ -78,7 +78,7 @@ class StoreApi(models.Model): ...@@ -78,7 +78,7 @@ class StoreApi(models.Model):
else: else:
return False return False
@staticmethod @staticmethod
def UserExist(neptun): def userexist(neptun):
url = StoreApi.store_url+'/'+neptun url = StoreApi.store_url+'/'+neptun
if StoreApi.basic_auth: if StoreApi.basic_auth:
r = requests.get(url, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl) r = requests.get(url, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
...@@ -89,12 +89,12 @@ class StoreApi(models.Model): ...@@ -89,12 +89,12 @@ class StoreApi(models.Model):
else: else:
return False return False
@staticmethod @staticmethod
def CreateUser(neptun,password,key_list): def createuser(neptun, password, key_list):
url = StoreApi.store_url+'/new/'+neptun url = StoreApi.store_url+'/new/'+neptun
payload = json.dumps({ 'SMBPASSWD' : password,'KEYS' : key_list }) payload = json.dumps({ 'SMBPASSWD' : password, 'KEYS' : key_list })
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
if StoreApi.basic_auth: if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl) r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user, StoreApi.store_password), verify=StoreApi.verify_ssl)
else: else:
r = requests.post(url, data=payload, headers=headers) r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
...@@ -102,12 +102,12 @@ class StoreApi(models.Model): ...@@ -102,12 +102,12 @@ class StoreApi(models.Model):
else: else:
return False return False
@staticmethod @staticmethod
def UpdateAuthorizationInfo(neptun,password,key_list): def updateauthorizationinfo(neptun, password, key_list):
url = StoreApi.store_url+'/set/'+neptun url = StoreApi.store_url+'/set/'+neptun
payload = json.dumps({ 'SMBPASSWD' : password,'KEYS' : key_list }) payload = json.dumps({ 'SMBPASSWD' : password, 'KEYS' : key_list })
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
if StoreApi.basic_auth: if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl) r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user, StoreApi.store_password), verify=StoreApi.verify_ssl)
else: else:
r = requests.post(url, data=payload, headers=headers) r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
......
...@@ -53,29 +53,29 @@ ...@@ -53,29 +53,29 @@
{% for item in file_list %} {% for item in file_list %}
<tr> <tr>
<td> <td>
{% if item.1 == 'D' %} {% if item.TYPE == 'D' %}
<form action="/store/" method="POST">{% csrf_token %} <form action="/store/" method="POST">{% csrf_token %}
<input type="submit" value="{{ item.0 }}"> <input type="submit" value="{{ item.NAME }}">
{% if item.0 != '/' %} {% if item.NAME != '/' %}
<input type="hidden" name="path" value="{{ path }}{{ item.0 }}/"> <input type="hidden" name="path" value="{{ path }}{{ item.NAME }}/">
{% else %} {% else %}
<input type="hidden" name="path" value="/"> <input type="hidden" name="path" value="/">
{% endif %} {% endif %}
</form> </form>
{% else %} {% else %}
{{ item.0 }} {{ item.NAME }}
{% endif %} {% endif %}
</td> </td>
<td>{{ item.1 }}</td> <td>{{ item.TYPE }}</td>
<td>{{ item.2 }}</td> <td>{{ item.SIZE }}</td>
<td>{{ item.3 }}</td> <td>{{ item.MTIME }}</td>
<td> <td>
{% if item.1 != 'D' %} {% if item.TYPE != 'D' %}
<form action="/store/" method="POST">{% csrf_token %} <form action="/store/" method="POST">{% csrf_token %}
<input type="submit" value="Download"> <input type="submit" value="Download">
<input type="hidden" name="path" value="{{ path }}"> <input type="hidden" name="path" value="{{ path }}">
<input type="hidden" name="dl" value="{{ path }}{{ item.0 }}"> <input type="hidden" name="dl" value="{{ path }}{{ item.NAME }}">
</form> </form>
{% endif %} {% endif %}
</td> </td>
...@@ -83,7 +83,7 @@ ...@@ -83,7 +83,7 @@
<form action="/store/" method="POST">{% csrf_token %} <form action="/store/" method="POST">{% csrf_token %}
<input type="submit" value="Remove"> <input type="submit" value="Remove">
<input type="hidden" name="path" value="{{ path }}"> <input type="hidden" name="path" value="{{ path }}">
<input type="hidden" name="rm" value="{{ path }}{{ item.0 }}"> <input type="hidden" name="rm" value="{{ path }}{{ item.NAME }}">
</form> </form>
</td> </td>
</tr> </tr>
......
...@@ -18,41 +18,41 @@ def index(request): ...@@ -18,41 +18,41 @@ def index(request):
key_list.append(key.key) key_list.append(key.key)
except: except:
return HttpResponse('Can not acces to django database!') return HttpResponse('Can not acces to django database!')
if StoreApi.UserExist(user) != True: if StoreApi.userexist(user) != True:
#Create user #Create user
if not StoreApi.CreateUser(user,password,key_list): if not StoreApi.createuser(user,password,key_list):
return HttpResponse('User does not exist on store! And could not create!') return HttpResponse('User does not exist on store! And could not create!')
#UpdateAuthorizationInfo #UpdateAuthorizationInfo
try: try:
auth=request.POST['auth'] auth=request.POST['auth']
if not StoreApi.UpdateAuthorizationInfo(user,password,key_list): if not StoreApi.updateauthorizationinfo(user,password,key_list):
return HttpResponse('Can not update authorization information!') return HttpResponse('Can not update authorization information!')
except: except:
pass pass
#Download handler #Download handler
try: try:
dl = request.POST['dl'] dl = request.POST['dl']
return redirect(StoreApi.RequestDownload(user,dl)) return redirect(StoreApi.requestdownload(user,dl))
except: except:
pass pass
#Upload handler #Upload handler
try: try:
ul = request.POST['ul'] ul = request.POST['ul']
url = StoreApi.RequestUpload(user,ul) url = StoreApi.requestupload(user,ul)
return render_to_response('store/upload.html', RequestContext(request,{'URL' : url})) return render_to_response('store/upload.html', RequestContext(request,{'URL' : url}))
except: except:
pass pass
#Remove handler #Remove handler
try: try:
rm = request.POST['rm'] rm = request.POST['rm']
succes = StoreApi.RequestRemove(user,rm) succes = StoreApi.requestremove(user,rm)
except: except:
pass pass
#Remove handler #Remove handler
try: try:
path = request.POST['path'] path = request.POST['path']
new = request.POST['new'] new = request.POST['new']
succes = StoreApi.RequestNewFolder(user,path+'/'+new) succes = StoreApi.requestnewfolder(user,path+'/'+new)
except: except:
pass pass
#Simple file list #Simple file list
...@@ -63,8 +63,8 @@ def index(request): ...@@ -63,8 +63,8 @@ def index(request):
pass pass
#Normalize path (Need double dirname /folder/ -> /folder -> / #Normalize path (Need double dirname /folder/ -> /folder -> /
backpath = os.path.normpath(os.path.dirname(os.path.dirname(path))) backpath = os.path.normpath(os.path.dirname(os.path.dirname(path)))
file_list = StoreApi.ListFolder(user,path) file_list = StoreApi.listfolder(user,path)
return render_to_response('store/list.html', RequestContext(request,{'file_list': file_list, 'path' : path, 'backpath' : backpath, 'username' : user})) return render_to_response('store/list.html', RequestContext(request, {'file_list': file_list, 'path' : path, 'backpath' : backpath, 'username' : user}))
def logout(request): def logout(request):
auth.logout(request) auth.logout(request)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment