Commit b7cbdafd by x

NEW FEATURE: store

parent 2d6a151a
...@@ -123,6 +123,7 @@ INSTALLED_APPS = ( ...@@ -123,6 +123,7 @@ INSTALLED_APPS = (
'one', 'one',
'school', 'school',
'cloud', 'cloud',
'store',
'firewall', 'firewall',
'south', 'south',
'djcelery', 'djcelery',
......
...@@ -5,6 +5,7 @@ admin.autodiscover() ...@@ -5,6 +5,7 @@ admin.autodiscover()
import one.views import one.views
import firewall.views import firewall.views
#import store.views
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
...@@ -19,4 +20,5 @@ urlpatterns = patterns('', ...@@ -19,4 +20,5 @@ urlpatterns = patterns('',
url(r'^vm/port_del/(?P<iid>\d+)/(?P<proto>tcp|udp)/(?P<public>\d+)/$', 'one.views.vm_port_del', name='vm_port_del'), url(r'^vm/port_del/(?P<iid>\d+)/(?P<proto>tcp|udp)/(?P<public>\d+)/$', 'one.views.vm_port_del', name='vm_port_del'),
url(r'^reload/$', 'firewall.views.reload_firewall', name='reload_firewall'), url(r'^reload/$', 'firewall.views.reload_firewall', name='reload_firewall'),
url(r'^fwapi/$', 'firewall.views.firewall_api', name='firewall_api'), url(r'^fwapi/$', 'firewall.views.firewall_api', name='firewall_api'),
url(r'^store/$', 'store.views.index', name='store_index'),
) )
from django.db import models
from django.http import Http404
import json, requests, time
# Create your models here.
#TODO Handle exceptions locally
class StoreApi(models.Model):
store_url = 'https://store.cloud.ik.bme.hu'
# store_url = 'http://store.cloud.ik.bme.hu:8080'
store_user = 'admin'
store_password = 'IQu8Eice'
basic_auth = True
verify_ssl = False
@staticmethod
def ListFolder(neptun,path):
url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'LIST', 'PATH' : path })
headers = {'content-type': 'application/json'}
if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
else:
r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok:
tupplelist = json.loads(r.content)
for item in tupplelist:
item[3] = time.ctime(item[3])
return tupplelist
else:
raise Http404
@staticmethod
def RequestDownload(neptun,path):
url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'DOWNLOAD', 'PATH' : path })
headers = {'content-type': 'application/json'}
if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
else:
r = requests.post(url, data=payload, headers=headers)
response = json.loads(r.content)
return response['LINK']
@staticmethod
def RequestUpload(neptun,path):
url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'UPLOAD', 'PATH' : path })
headers = {'content-type': 'application/json'}
if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
else:
r = requests.post(url, data=payload, headers=headers)
response = json.loads(r.content)
if r.status_code == requests.codes.ok:
return response['LINK']
else:
raise Http404
@staticmethod
def RequestRemove(neptun,path):
url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'REMOVE', 'PATH' : path })
headers = {'content-type': 'application/json'}
if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
else:
r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok:
return True
else:
return False
@staticmethod
def RequestNewFolder(neptun,path):
url = StoreApi.store_url+'/'+neptun
payload = json.dumps({ 'CMD' : 'NEW_FOLDER', 'PATH' : path })
headers = {'content-type': 'application/json'}
if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
else:
r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok:
return True
else:
return False
@staticmethod
def UserExist(neptun):
url = StoreApi.store_url+'/'+neptun
if StoreApi.basic_auth:
r = requests.get(url, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
else:
r = requests.get(url)
if r.status_code == requests.codes.ok:
return True
else:
return False
@staticmethod
def CreateUser(neptun,password,key_list):
url = StoreApi.store_url+'/new/'+neptun
payload = json.dumps({ 'SMBPASSWD' : password,'KEYS' : key_list })
headers = {'content-type': 'application/json'}
if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
else:
r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok:
return True
else:
return False
@staticmethod
def UpdateAuthorizationInfo(neptun,password,key_list):
url = StoreApi.store_url+'/set/'+neptun
payload = json.dumps({ 'SMBPASSWD' : password,'KEYS' : key_list })
headers = {'content-type': 'application/json'}
if StoreApi.basic_auth:
r = requests.post(url, data=payload, headers=headers, auth=(StoreApi.store_user,StoreApi.store_password), verify=StoreApi.verify_ssl)
else:
r = requests.post(url, data=payload, headers=headers)
if r.status_code == requests.codes.ok:
return True
else:
return False
<table>
<tr>
<td>Welcome: {{username}}</td>
<td>
<form action="/store/" method="POST">
{% csrf_token %}
<input type="submit" value="CopyKeys">
<input type="hidden" name="auth" value="True">
</form>
</td>
<td></td>
<td></td>
<td>
<form action="/store/" method="POST"
target="uploader-popup"
onSubmit="window.open('/store/', 'uploader-popup', 'width=450,height=300,status=yes,resizable=yes,scrollbars=no,menubar=no,toolbar=no'">
{% csrf_token %}
<input type="submit" value="Upload">
<input type="hidden" name="path" value="{{ path }}">
<input type="hidden" name="ul" value="{{ path }}">
</form>
</td>
<td>
<form action="/store/" method="POST">
{% csrf_token %}
<input type="submit" value="New Folder">
<input type="hidden" name="path" value="{{ path }}">
<input type="text" name="new" value="Folder Name">
</form>
</td>
</tr>
<tr>
<td>Név</td>
<td>Típus</td>
<td>Méret (KB)</td>
<td>Utolsó modosítás</td>
<td>Műveletek</td>
</tr>
<tr>
<td>{% if path != '/' %}
<form action="/store/" method="POST">{% csrf_token %}
<input type="submit" value="{{ backpath }}">
{% if backpath == '/'%}
<input type="hidden" name="path" value="/">
{% else %}
<input type="hidden" name="path" value="{{ backpath }}/">
{% endif %}
</form>
{% endif %}
</td>
</tr>
{% if file_list %}
{% for item in file_list %}
<tr>
<td>
{% if item.1 == 'D' %}
<form action="/store/" method="POST">{% csrf_token %}
<input type="submit" value="{{ item.0 }}">
{% if item.0 != '/' %}
<input type="hidden" name="path" value="{{ path }}{{ item.0 }}/">
{% else %}
<input type="hidden" name="path" value="/">
{% endif %}
</form>
{% else %}
{{ item.0 }}
{% endif %}
</td>
<td>{{ item.1 }}</td>
<td>{{ item.2 }}</td>
<td>{{ item.3 }}</td>
<td>
{% if item.1 != 'D' %}
<form action="/store/" method="POST">{% csrf_token %}
<input type="submit" value="Download">
<input type="hidden" name="path" value="{{ path }}">
<input type="hidden" name="dl" value="{{ path }}{{ item.0 }}">
</form>
{% endif %}
</td>
<td>
<form action="/store/" method="POST">{% csrf_token %}
<input type="submit" value="Remove">
<input type="hidden" name="path" value="{{ path }}">
<input type="hidden" name="rm" value="{{ path }}{{ item.0 }}">
</form>
</td>
</tr>
{% endfor %}
{% endif %}
</table>
<form method="POST" action="{{URL}}" enctype="multipart/form-data">
{% csrf_token %}
<input name="data" type="file" />
<input type="submit" value="Feltöltés"/>
</form
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from store.models import StoreApi
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
import os
@login_required
def index(request):
user = request.user.username
try:
details = request.user.userclouddetails_set.all()[0]
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!')
if StoreApi.UserExist(user) != True:
#Create user
if StoreApi.CreateUser(user,password,key_list):
pass
else:
return HttpResponse('User does not exist on store! And could not create!')
#UpdateAuthorizationInfo
try:
auth=request.POST['auth']
result='ures'
result=UpdateAuthorizationInfo(user,password,key_list)
except:
return HttpResponse('Error: '+result)
#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)
return render_to_response('store/list.html', RequestContext(request,{'file_list': file_list, 'path' : path, 'backpath' : backpath, 'username' : user}))
def logout(request):
auth.logout(request)
return redirect('/')
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