summaryrefslogtreecommitdiffstats
path: root/module/web/pyload
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/pyload
parentFirst Version, works but doesn't reconnect (diff)
parentfix (diff)
downloadpyload-6d1ec2baa795b0d90d5da33b0447120b50f10c17.tar.xz
merge
Diffstat (limited to 'module/web/pyload')
-rw-r--r--module/web/pyload/__init__.py0
-rw-r--r--module/web/pyload/admin.py15
-rw-r--r--module/web/pyload/models.py31
-rw-r--r--module/web/pyload/tests.py23
-rw-r--r--module/web/pyload/urls.py22
-rw-r--r--module/web/pyload/views.py153
6 files changed, 244 insertions, 0 deletions
diff --git a/module/web/pyload/__init__.py b/module/web/pyload/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/module/web/pyload/__init__.py
diff --git a/module/web/pyload/admin.py b/module/web/pyload/admin.py
new file mode 100644
index 000000000..99cb28836
--- /dev/null
+++ b/module/web/pyload/admin.py
@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+from django.contrib import admin
+from models import Prefs
+from django.contrib.auth.models import User
+from django.contrib.auth.admin import UserAdmin as RealUserAdmin
+
+
+class UserProfileInline(admin.StackedInline):
+ model = Prefs
+
+class UserAdmin(RealUserAdmin):
+ inlines = [ UserProfileInline ]
+
+admin.site.unregister(User)
+admin.site.register(User, UserAdmin) \ No newline at end of file
diff --git a/module/web/pyload/models.py b/module/web/pyload/models.py
new file mode 100644
index 000000000..86962f23c
--- /dev/null
+++ b/module/web/pyload/models.py
@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+from django.db import models
+from django.contrib.auth.models import User
+# Create your models here.
+
+class Prefs(models.Model):
+ """ Permissions setting """
+
+ user = models.ForeignKey(User, unique=True)
+ template = models.CharField(max_length=30, default='default', null=False, blank=False) #@TODO: currently unused
+
+ class Meta:
+ permissions = (
+ ('can_see_dl', 'User can see Downloads'),
+ ('can_change_status', 'User can change Status'),
+ ('can_download', 'User can download'),
+ ('can_add', 'User can add Links'),
+ ('can_delete', 'User can delete Links'),
+ ('can_see_logs', 'User can see Logs'),
+ )
+ verbose_name = "Preferences"
+ verbose_name_plural = "Preferences"
+
+ def __unicode__(self):
+ return "Preferences for %s" % self.user
+
+
+def user_post_save(sender, instance, **kwargs):
+ profile, new = Prefs.objects.get_or_create(user=instance)
+
+models.signals.post_save.connect(user_post_save, User) \ No newline at end of file
diff --git a/module/web/pyload/tests.py b/module/web/pyload/tests.py
new file mode 100644
index 000000000..2247054b3
--- /dev/null
+++ b/module/web/pyload/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/pyload/urls.py b/module/web/pyload/urls.py
new file mode 100644
index 000000000..73ba08c21
--- /dev/null
+++ b/module/web/pyload/urls.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+
+from os.path import join
+
+from django.conf import settings
+from django.conf.urls.defaults import *
+
+
+urlpatterns = patterns('pyload',
+ (r'^home/$', 'views.home'),
+ (r'^downloads/$', 'views.downloads',{},'downloads'),
+ (r'^download/(?P<path>[a-zA-z\.0-9\-/_%]+)$', 'views.download',{},'download'),
+ (r'^queue/$', 'views.queue',{}, 'queue'),
+ (r'^logs/$', 'views.logs',{}, 'logs'),
+ (r'^logs/(?P<page>\d+)$', 'views.logs',{}, 'logs'),
+ (r'^$', 'views.home',{}, 'home'),
+ )
+
+urlpatterns += patterns('django.contrib.auth',
+ (r'^login/$', 'views.login', {'template_name': join(settings.TEMPLATE, 'login.html')}),
+ (r'^logout/$', 'views.logout', {'template_name': join(settings.TEMPLATE, 'logout.html')}, 'logout'),
+) \ No newline at end of file
diff --git a/module/web/pyload/views.py b/module/web/pyload/views.py
new file mode 100644
index 000000000..ea9d5f1be
--- /dev/null
+++ b/module/web/pyload/views.py
@@ -0,0 +1,153 @@
+# -*- coding: utf-8 -*-
+
+# Create your views here.
+import mimetypes
+from os import listdir
+from os import stat
+from os.path import isdir
+from os.path import isfile
+from os.path import join
+
+from django.conf import settings
+from django.contrib.auth.decorators import login_required
+from django.http import HttpResponse
+from django.http import HttpResponseNotFound
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+
+
+def check_server(function):
+ def _dec(view_func):
+ def _view(request, * args, ** kwargs):
+ try:
+ version = settings.PYLOAD.get_server_version()
+ except Exception, e:
+ return base(request, messages=['Can\'t connect to pyLoad. Please check your configuration and make sure pyLoad is running.', str(e)])
+ return view_func(request, * args, ** kwargs)
+
+ _view.__name__ = view_func.__name__
+ _view.__dict__ = view_func.__dict__
+ _view.__doc__ = view_func.__doc__
+
+ return _view
+
+ if function is None:
+ return _dec
+ else:
+ return _dec(function)
+
+
+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 base(request, messages=['You don\'t have permission to view this page.'])
+
+ _view.__name__ = view_func.__name__
+ _view.__dict__ = view_func.__dict__
+ _view.__doc__ = view_func.__doc__
+
+ return _view
+
+ return _dec
+
+
+
+def status_proc(request):
+ return {'status': settings.PYLOAD.status_server()}
+
+
+def base(request, messages):
+ return render_to_response(join(settings.TEMPLATE, 'base.html'), {'messages': messages}, RequestContext(request))
+
+@login_required
+@permission('pyload.can_see_dl')
+@check_server
+def home(request):
+ return render_to_response(join(settings.TEMPLATE, 'home.html'), RequestContext(request, {'content': settings.PYLOAD.status_downloads()}, [status_proc]))
+
+
+@login_required
+@permission('pyload.can_see_dl')
+@check_server
+def queue(request):
+ queue = settings.PYLOAD.get_queue()
+ for pack in queue:
+ pack["children"] = map(settings.PYLOAD.get_file_info, settings.PYLOAD.get_package_files(pack["id"]))
+ return render_to_response(join(settings.TEMPLATE, 'queue.html'), RequestContext(request, {'content': queue}, [status_proc]))
+
+
+@login_required
+@permission('pyload.can_download')
+@check_server
+def downloads(request):
+ if not isdir(settings.DL_ROOT):
+ return base(request, ['Download directory not found.'])
+ data = {
+ 'folder': [],
+ 'files': []
+ }
+
+ for item in listdir(settings.DL_ROOT):
+ if isdir(join(settings.DL_ROOT, item)):
+ folder = {
+ 'name': item,
+ 'files': []
+ }
+ for file in listdir(join(settings.DL_ROOT, item)):
+ if isfile(join(settings.DL_ROOT, item, file)):
+ folder['files'].append(file)
+
+ data['folder'].append(folder)
+ elif isfile(join(settings.DL_ROOT, item)):
+ data['files'].append(item)
+
+
+ return render_to_response(join(settings.TEMPLATE, 'downloads.html'), RequestContext(request, {'files': data}, [status_proc]))
+
+@login_required
+@permission('pyload.can_download')
+@check_server
+def download(request, path):
+ path = path.split("/")
+
+ dir = join(settings.DL_ROOT, path[1].replace('..', ''))
+ if isdir(dir) or isfile(dir):
+ if isdir(dir): filepath = join(dir, path[2])
+ elif isfile(dir): filepath = dir
+
+ print filepath
+ if isfile(filepath):
+ try:
+ type, encoding = mimetypes.guess_type(filepath)
+ if type is None:
+ type = 'application/octet-stream'
+
+ response = HttpResponse(mimetype=type)
+ response['Content-Length'] = str(stat(filepath).st_size)
+
+ if encoding is not None:
+ response['Content-Encoding'] = encoding
+
+ response.write(file(filepath, "rb").read())
+ return response
+
+ except Exception, e:
+ return HttpResponseNotFound("File not Found. %s" % str(e))
+
+ return HttpResponseNotFound("File not Found.")
+
+@login_required
+@permission('pyload.can_see_logs')
+@check_server
+def logs(request, page=0):
+
+ log = file(join(settings.LOG_ROOT, "log.txt")).readlines()
+ data = []
+ page = int(page)
+ for i in range(page, page + 20):
+ data.append({'line': i + 1, 'content':log[i]})
+
+ return render_to_response(join(settings.TEMPLATE, 'logs.html'), RequestContext(request, {'log': data, 'next': str(page + 20), 'prev': 0 if page-20 < 0 else page-20}, [status_proc]))