From 3d655ddbfbd96abecb9a9c9bebf6e43eb710ab12 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sun, 10 Jan 2010 16:20:31 +0100 Subject: fixed manage.py, addBox working, some code formatted and cleaned --- module/web/ServerThread.py | 3 +- module/web/ajax/views.py | 52 ++- module/web/manage.py | 27 +- module/web/media/default/js/jquery.form.js | 660 +++++++++++++++++++++++++++++ module/web/pyload/views.py | 49 +-- module/web/run_unix.py | 21 + module/web/settings.py | 2 +- module/web/templates/default/base.html | 19 +- module/web/templates/default/home.html | 3 + module/web/templates/default/window.html | 7 +- 10 files changed, 768 insertions(+), 75 deletions(-) create mode 100644 module/web/media/default/js/jquery.form.js create mode 100755 module/web/run_unix.py (limited to 'module/web') diff --git a/module/web/ServerThread.py b/module/web/ServerThread.py index 8be3892b5..1276966ef 100644 --- a/module/web/ServerThread.py +++ b/module/web/ServerThread.py @@ -18,7 +18,6 @@ class WebServer(threading.Thread): def run(self): host = self.pycore.config['webinterface']['host'] port = self.pycore.config['webinterface']['port'] - command = ['python',join(self.pycore.path,"module","web","manage.py"), "runserver", "%s:%s" % (host,port)] if not exists(join(self.pycore.path,"module","web","pyload.db")): print "########## IMPORTANT ###########" @@ -32,6 +31,7 @@ class WebServer(threading.Thread): self.pycore.logger.info("Starting Webserver: %s:%s" % (host,port) ) if os.name == 'posix': + command = ['python',join(self.pycore.path,"module","web","run_unix.py"), "runserver", "%s:%s" % (host,port)] self.p = Popen(command, close_fds=True, stderr=PIPE, stdin=PIPE, stdout=PIPE) #os.system("python " + join(self.pycore.path,"module","web","manage.py runserver %s:%s" % (host,port))) #@TODO: better would be real python code @@ -41,6 +41,7 @@ class WebServer(threading.Thread): while self.running: sleep(1) else: + command = ['python',join(self.pycore.path,"module","web","manage.py"), "runserver", "%s:%s" % (host,port)] self.p = Popen(command, stderr=PIPE, stdin=PIPE, stdout=PIPE) while self.running: sleep(1) diff --git a/module/web/ajax/views.py b/module/web/ajax/views.py index b09a80581..952fbe2eb 100644 --- a/module/web/ajax/views.py +++ b/module/web/ajax/views.py @@ -1,16 +1,18 @@ # 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.conf import settings from django.utils import simplejson -from django.core.serializers import json def permission(perm): def _dec(view_func): - def _view(request, *args, **kwargs): + def _view(request, * args, ** kwargs): if request.user.has_perm(perm) and request.user.is_authenticated(): - return view_func(request, *args, **kwargs) + return view_func(request, * args, ** kwargs) else: return HttpResponseForbidden() @@ -25,17 +27,39 @@ def permission(perm): class JsonResponse(HttpResponse): def __init__(self, object): content = simplejson.dumps( - object, indent=2, cls=json.DjangoJSONEncoder, - ensure_ascii=False) + object, indent=2, cls=json.DjangoJSONEncoder, + ensure_ascii=False) super(JsonResponse, self).__init__( - content)#, content_type='application/json') - self['Cache-Control'] = 'no-cache, must-revalidate' - + content)#, content_type='application/json') #@TODO uncomment + self['Cache-Control'] = 'no-cache, must-revalidate' +@permission('pyload.can_add') def add_package(request): - a = {'b' : [1,2,3], 'dsfsd' : "sadd"} - return JsonResponse(a) + + name = request.POST['add_name'] + + if name is None or "": + return HttpResponseServerError() + + links = request.POST['add_links'].split("\n") + + try: + f = request.FILES['add_file'] + 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 + + links = filter(lambda x: x is not "", links) + + settings.PYLOAD.add_package(name, links) + + return JsonResponse("success") @permission('pyload.can_see_dl') @@ -96,12 +120,12 @@ def packages(request): return HttpResponseServerError() @permission('pyload.can_see_dl') -def package(request,id): +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)) + data['links'].append(settings.PYLOAD.get_file_info(file)) return JsonResponse(data) @@ -109,7 +133,7 @@ def package(request,id): return HttpResponseServerError() @permission('pyload.can_see_dl') -def link(request,id): +def link(request, id): try: data = settings.PYLOAD.get_file_info(int(id)) return JsonResponse(data) diff --git a/module/web/manage.py b/module/web/manage.py index 6a00ab565..34b964ffc 100755 --- a/module/web/manage.py +++ b/module/web/manage.py @@ -1,20 +1,13 @@ #!/usr/bin/env python -from __future__ import with_statement -import os +# -*- coding: utf-8 -*- +from django.core.management import execute_manager -pid = os.fork() -if pid: - with open("webserver.pid", "w") as f: - f.write(str(pid)) -else: - from django.core.management import execute_manager +try: + import settings # Assumed to be in the same directory. +except ImportError: + import sys + sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__) + sys.exit(1) - try: - import settings # Assumed to be in the same directory. - except ImportError: - import sys - sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__) - sys.exit(1) - - if __name__ == "__main__": - execute_manager(settings) +if __name__ == "__main__": + execute_manager(settings) \ No newline at end of file diff --git a/module/web/media/default/js/jquery.form.js b/module/web/media/default/js/jquery.form.js new file mode 100644 index 000000000..dde394270 --- /dev/null +++ b/module/web/media/default/js/jquery.form.js @@ -0,0 +1,660 @@ +/* + * jQuery Form Plugin + * version: 2.36 (07-NOV-2009) + * @requires jQuery v1.2.6 or later + * + * Examples and documentation at: http://malsup.com/jquery/form/ + * Dual licensed under the MIT and GPL licenses: + * http://www.opensource.org/licenses/mit-license.php + * http://www.gnu.org/licenses/gpl.html + */ +;(function($) { + +/* + Usage Note: + ----------- + Do not use both ajaxSubmit and ajaxForm on the same form. These + functions are intended to be exclusive. Use ajaxSubmit if you want + to bind your own submit handler to the form. For example, + + $(document).ready(function() { + $('#myForm').bind('submit', function() { + $(this).ajaxSubmit({ + target: '#output' + }); + return false; // <-- important! + }); + }); + + Use ajaxForm when you want the plugin to manage all the event binding + for you. For example, + + $(document).ready(function() { + $('#myForm').ajaxForm({ + target: '#output' + }); + }); + + When using ajaxForm, the ajaxSubmit function will be invoked for you + at the appropriate time. +*/ + +/** + * ajaxSubmit() provides a mechanism for immediately submitting + * an HTML form using AJAX. + */ +$.fn.ajaxSubmit = function(options) { + // fast fail if nothing selected (http://dev.jquery.com/ticket/2752) + if (!this.length) { + log('ajaxSubmit: skipping submit process - no element selected'); + return this; + } + + if (typeof options == 'function') + options = { success: options }; + + var url = $.trim(this.attr('action')); + if (url) { + // clean url (don't include hash vaue) + url = (url.match(/^([^#]+)/)||[])[1]; + } + url = url || window.location.href || ''; + + options = $.extend({ + url: url, + type: this.attr('method') || 'GET', + iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' + }, options || {}); + + // hook for manipulating the form data before it is extracted; + // convenient for use with rich editors like tinyMCE or FCKEditor + var veto = {}; + this.trigger('form-pre-serialize', [this, options, veto]); + if (veto.veto) { + log('ajaxSubmit: submit vetoed via form-pre-serialize trigger'); + return this; + } + + // provide opportunity to alter form data before it is serialized + if (options.beforeSerialize && options.beforeSerialize(this, options) === false) { + log('ajaxSubmit: submit aborted via beforeSerialize callback'); + return this; + } + + var a = this.formToArray(options.semantic); + if (options.data) { + options.extraData = options.data; + for (var n in options.data) { + if(options.data[n] instanceof Array) { + for (var k in options.data[n]) + a.push( { name: n, value: options.data[n][k] } ); + } + else + a.push( { name: n, value: options.data[n] } ); + } + } + + // give pre-submit callback an opportunity to abort the submit + if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) { + log('ajaxSubmit: submit aborted via beforeSubmit callback'); + return this; + } + + // fire vetoable 'validate' event + this.trigger('form-submit-validate', [a, this, options, veto]); + if (veto.veto) { + log('ajaxSubmit: submit vetoed via form-submit-validate trigger'); + return this; + } + + var q = $.param(a); + + if (options.type.toUpperCase() == 'GET') { + options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; + options.data = null; // data is null for 'get' + } + else + options.data = q; // data is the query string for 'post' + + var $form = this, callbacks = []; + if (options.resetForm) callbacks.push(function() { $form.resetForm(); }); + if (options.clearForm) callbacks.push(function() { $form.clearForm(); }); + + // perform a load on the target only if dataType is not provided + if (!options.dataType && options.target) { + var oldSuccess = options.success || function(){}; + callbacks.push(function(data) { + $(options.target).html(data).each(oldSuccess, arguments); + }); + } + else if (options.success) + callbacks.push(options.success); + + options.success = function(data, status) { + for (var i=0, max=callbacks.length; i < max; i++) + callbacks[i].apply(options, [data, status, $form]); + }; + + // are there files to upload? + var files = $('input:file', this).fieldValue(); + var found = false; + for (var j=0; j < files.length; j++) + if (files[j]) + found = true; + + var multipart = false; +// var mp = 'multipart/form-data'; +// multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); + + // options.iframe allows user to force iframe mode + // 06-NOV-09: now defaulting to iframe mode if file input is detected + if ((files.length && options.iframe !== false) || options.iframe || found || multipart) { + // hack to fix Safari hang (thanks to Tim Molendijk for this) + // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d + if (options.closeKeepAlive) + $.get(options.closeKeepAlive, fileUpload); + else + fileUpload(); + } + else + $.ajax(options); + + // fire 'notify' event + this.trigger('form-submit-notify', [this, options]); + return this; + + + // private function for handling file uploads (hat tip to YAHOO!) + function fileUpload() { + var form = $form[0]; + + if ($(':input[name=submit]', form).length) { + alert('Error: Form elements must not be named "submit".'); + return; + } + + var opts = $.extend({}, $.ajaxSettings, options); + var s = $.extend(true, {}, $.extend(true, {}, $.ajaxSettings), opts); + + var id = 'jqFormIO' + (new Date().getTime()); + var $io = $('