summaryrefslogtreecommitdiffstats
path: root/module/web/ajax
diff options
context:
space:
mode:
authorGravatar mkaay <mkaay@mkaay.de> 2010-01-27 20:05:23 +0100
committerGravatar mkaay <mkaay@mkaay.de> 2010-01-27 20:05:23 +0100
commit6d1ec2baa795b0d90d5da33b0447120b50f10c17 (patch)
tree5fceb5474c83e709fda396f70121a6923b0be106 /module/web/ajax
parentFirst Version, works but doesn't reconnect (diff)
parentfix (diff)
downloadpyload-6d1ec2baa795b0d90d5da33b0447120b50f10c17.tar.xz
merge
Diffstat (limited to 'module/web/ajax')
-rw-r--r--module/web/ajax/__init__.py0
-rw-r--r--module/web/ajax/models.py2
-rw-r--r--module/web/ajax/tests.py23
-rw-r--r--module/web/ajax/urls.py29
-rw-r--r--module/web/ajax/views.py196
5 files changed, 250 insertions, 0 deletions
diff --git a/module/web/ajax/__init__.py b/module/web/ajax/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/module/web/ajax/__init__.py
diff --git a/module/web/ajax/models.py b/module/web/ajax/models.py
new file mode 100644
index 000000000..35e0d6486
--- /dev/null
+++ b/module/web/ajax/models.py
@@ -0,0 +1,2 @@
+
+# Create your models here.
diff --git a/module/web/ajax/tests.py b/module/web/ajax/tests.py
new file mode 100644
index 000000000..2247054b3
--- /dev/null
+++ b/module/web/ajax/tests.py
@@ -0,0 +1,23 @@
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these 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.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
diff --git a/module/web/ajax/urls.py b/module/web/ajax/urls.py
new file mode 100644
index 000000000..d50e7121e
--- /dev/null
+++ b/module/web/ajax/urls.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+from django.conf.urls.defaults import *
+
+
+urlpatterns = patterns('ajax',
+ # Example:
+ # (r'^pyload/', include('pyload.foo.urls')),
+
+ # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
+ # to INSTALLED_APPS to enable admin documentation:
+ # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
+
+ # Uncomment the next line to enable the admin:
+ (r'^add_package$', 'views.add_package'),
+ (r'^abort_link/(\d+)$', 'views.abort_link'),
+ (r'^status$', 'views.status'),
+ (r'^links$', 'views.links'), #currently active links
+ (r'^queue$', 'views.queue'),
+ (r'^pause$', 'views.pause'),
+ (r'^unpause$', 'views.unpause'),
+ (r'^cancel$', 'views.cancel'),
+ (r'^packages$', 'views.packages'),
+ (r'^package/(\d+)$', 'views.package'),
+ (r'^link/(\d+)$', 'views.link'),
+ (r'^remove_package/(\d+)$', 'views.remove_package'),
+ (r'^restart_package/(\d+)$', 'views.restart_package'),
+ (r'^remove_link/(\d+)$', 'views.remove_link'),
+ (r'^restart_link/(\d+)$', 'views.restart_link'),
+ ) \ No newline at end of file
diff --git a/module/web/ajax/views.py b/module/web/ajax/views.py
new file mode 100644
index 000000000..4d9762ab4
--- /dev/null
+++ b/module/web/ajax/views.py
@@ -0,0 +1,196 @@
+# Create your views here.
+from os.path import join
+
+from django.conf import settings
+from django.core.serializers import json
+from django.http import HttpResponse
+from django.http import HttpResponseForbidden
+from django.http import HttpResponseServerError
+from django.utils import simplejson
+
+def permission(perm):
+ def _dec(view_func):
+ def _view(request, * args, ** kwargs):
+ if request.user.has_perm(perm) and request.user.is_authenticated():
+ return view_func(request, * args, ** kwargs)
+ else:
+ return HttpResponseForbidden()
+
+ _view.__name__ = view_func.__name__
+ _view.__dict__ = view_func.__dict__
+ _view.__doc__ = view_func.__doc__
+
+ return _view
+
+ return _dec
+
+class JsonResponse(HttpResponse):
+ def __init__(self, object):
+ content = simplejson.dumps(
+ object, indent=2, cls=json.DjangoJSONEncoder,
+ ensure_ascii=False)
+ super(JsonResponse, self).__init__(
+ content)#, content_type='application/json') #@TODO uncomment
+ self['Cache-Control'] = 'no-cache, must-revalidate'
+
+
+@permission('pyload.can_add')
+def add_package(request):
+
+ name = request.POST['add_name']
+
+ links = request.POST['add_links'].replace(" ","\n").split("\n")
+
+ try:
+ f = request.FILES['add_file']
+
+ if name == None or name == "":
+ name = f.name
+
+ fpath = join(settings.DL_ROOT, f.name)
+ destination = open(fpath, 'wb')
+ for chunk in f.chunks():
+ destination.write(chunk)
+ destination.close()
+ links.insert(0, fpath)
+ except:
+ pass
+
+ if name == None or name == "":
+ return HttpResponseServerError()
+
+ links = filter(lambda x: x != "", links)
+
+ settings.PYLOAD.add_package(name, links)
+
+ return JsonResponse("success")
+
+@permission('pyload.can_add_dl')
+def remove_link(request, id):
+ try:
+ settings.PYLOAD.del_links([int(id)])
+ return JsonResponse("sucess")
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_see_dl')
+def status(request):
+ try:
+ return JsonResponse(settings.PYLOAD.status_server())
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_see_dl')
+def links(request):
+ try:
+ links = settings.PYLOAD.status_downloads()
+ ids = map(lambda x: x['id'], links)
+ data = {}
+ data['links'] = links
+ data['ids'] = ids
+ return JsonResponse(data)
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_see_dl')
+def queue(request):
+ try:
+ return JsonResponse(settings.PYLOAD.get_queue())
+
+ except:
+ return HttpResponseServerError()
+
+
+@permission('pyload.can_change_satus')
+def pause(request):
+ try:
+ return JsonResponse(settings.PYLOAD.pause_server())
+
+ except:
+ return HttpResponseServerError()
+
+
+@permission('pyload.can_change_status')
+def unpause(request):
+ try:
+ return JsonResponse(settings.PYLOAD.unpause_server())
+
+ except:
+ return HttpResponseServerError()
+
+
+@permission('pyload.can_change_status')
+def cancel(request):
+ try:
+ return JsonResponse(settings.PYLOAD.stop_downloads())
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_see_dl')
+def packages(request):
+ try:
+ data = settings.PYLOAD.get_queue()
+
+ for package in data:
+ package['links'] = []
+ for file in settings.PYLOAD.get_package_files(package['id']):
+ package['links'].append(settings.PYLOAD.get_file_info(file))
+
+ return JsonResponse(data)
+
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_see_dl')
+def package(request, id):
+ try:
+ data = settings.PYLOAD.get_package_data(int(id))
+ data['links'] = []
+ for file in settings.PYLOAD.get_package_files(data['id']):
+ data['links'].append(settings.PYLOAD.get_file_info(file))
+
+ return JsonResponse(data)
+
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_see_dl')
+def link(request, id):
+ try:
+ data = settings.PYLOAD.get_file_info(int(id))
+ return JsonResponse(data)
+
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_add_dl')
+def remove_package(request, id):
+ try:
+ settings.PYLOAD.del_packages([int(id)])
+ return JsonResponse("sucess")
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_add_dl')
+def restart_package(request, id):
+ try:
+ settings.PYLOAD.restart_package(int(id))
+ return JsonResponse("sucess")
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_add_dl')
+def restart_link(request, id):
+ try:
+ settings.PYLOAD.restart_file(int(id))
+ return JsonResponse("sucess")
+ except:
+ return HttpResponseServerError()
+
+@permission('pyload.can_add_dl')
+def abort_link(request, id):
+ try:
+ settings.PYLOAD.stop_download("link", int(id))
+ return JsonResponse("sucess")
+ except:
+ return HttpResponseServerError() \ No newline at end of file