From 0b306717b665a0479064460e61c6e1354b1a7cec Mon Sep 17 00:00:00 2001 From: Martijn Voncken Date: Tue, 12 Feb 2008 18:14:18 +0000 Subject: [PATCH] torrent_add:options --- deluge/ui/webui/webui_plugin/config.py | 14 +------ .../webui/webui_plugin/lib/newforms_plus.py | 8 +++- .../templates/advanced/index.html | 2 +- .../templates/deluge/torrent_add.html | 11 ++++- deluge/ui/webui/webui_plugin/torrent_add.py | 42 +++++++++++++------ deluge/ui/webui/webui_plugin/utils.py | 18 ++++++++ .../ui/webui/webui_plugin/webserver_common.py | 4 +- 7 files changed, 69 insertions(+), 30 deletions(-) diff --git a/deluge/ui/webui/webui_plugin/config.py b/deluge/ui/webui/webui_plugin/config.py index 737fe5b7f..c89992d4b 100644 --- a/deluge/ui/webui/webui_plugin/config.py +++ b/deluge/ui/webui/webui_plugin/config.py @@ -37,6 +37,7 @@ from render import render from lib.webpy022.http import seeother import sys import os +import utils groups = [] blocks = forms.utils.datastructures.SortedDict() @@ -90,18 +91,7 @@ class config_page: @deco.deluge_page def POST(self,name): - form_class = self.get_form_class(name) - fields = form_class.base_fields.keys() - form_data = web.Storage() - vars = web.input() - for field in fields: - form_data[field] = vars.get(field) - #DIRTY HACK: (for multiple-select) - if isinstance(form_class.base_fields[field], - forms.MultipleChoiceField): - form_data[field] = web.input(**{field:[]})[field] - #/DIRTY HACK - + form_data = utils.get_newforms_data(form_class) form = form_class(form_data) if form.is_valid(): ws.log.debug('save config %s' % form_data) diff --git a/deluge/ui/webui/webui_plugin/lib/newforms_plus.py b/deluge/ui/webui/webui_plugin/lib/newforms_plus.py index e093e5c20..46ba19ddf 100644 --- a/deluge/ui/webui/webui_plugin/lib/newforms_plus.py +++ b/deluge/ui/webui/webui_plugin/lib/newforms_plus.py @@ -10,9 +10,14 @@ from newforms.forms import BoundField import sys, os - import webpy022 as web #todo:remove this dependency. + + + + + + #Form class FilteredForm(newforms.Form): """ @@ -99,7 +104,6 @@ class Form(FilteredForm): def validate(self, data): pass - #convenience Input Fields. class CheckBox(newforms.BooleanField): "Non Required BooleanField,why the f is it required by default?" diff --git a/deluge/ui/webui/webui_plugin/templates/advanced/index.html b/deluge/ui/webui/webui_plugin/templates/advanced/index.html index e5684560e..5652b78d6 100644 --- a/deluge/ui/webui/webui_plugin/templates/advanced/index.html +++ b/deluge/ui/webui/webui_plugin/templates/advanced/index.html @@ -93,7 +93,7 @@ $for torrent in torrent_list: $fsize(torrent.total_size)
-
+
$torrent.message $int(torrent.progress) %
diff --git a/deluge/ui/webui/webui_plugin/templates/deluge/torrent_add.html b/deluge/ui/webui/webui_plugin/templates/deluge/torrent_add.html index 0488f6f8b..849b171f9 100644 --- a/deluge/ui/webui/webui_plugin/templates/deluge/torrent_add.html +++ b/deluge/ui/webui/webui_plugin/templates/deluge/torrent_add.html @@ -1,10 +1,12 @@ -$def with (add_form, options_form) +$def with (add_form, options_form, error) $:render.header(_("Add Torrent"))
+$if error: +
$error
$:add_form.as_table() @@ -83,10 +85,17 @@ function toggle_options(){ el = document.getElementById("torrent_add_options"); if (el.style.display == "block"){ el.style.display = "none"; + setCookie("torrent_add_options","hide"); } else{ el.style.display = "block"; + setCookie("torrent_add_options","show"); } } + +if (getCookie("torrent_add_options") == "show") { + el = document.getElementById("torrent_add_options"); + el.style.display = "block"; +} $:render.footer() diff --git a/deluge/ui/webui/webui_plugin/torrent_add.py b/deluge/ui/webui/webui_plugin/torrent_add.py index d4185c073..622c43afd 100644 --- a/deluge/ui/webui/webui_plugin/torrent_add.py +++ b/deluge/ui/webui/webui_plugin/torrent_add.py @@ -30,10 +30,11 @@ # statement from all source files in the program, then also delete it here. # from webserver_common import ws -from utils import * +import utils from render import render, error_page import page_decorators as deco import lib.newforms_plus as forms +import lib.webpy022 as web import base64 class OptionsForm(forms.Form): @@ -57,19 +58,27 @@ class OptionsForm(forms.Form): return ws.proxy.get_config() class AddForm(forms.Form): - url = forms.CharField(label=_("Url"), + url = forms.CharField(label=_("Url"), required=False, widget=forms.TextInput(attrs={'size':60})) - torrent = forms.CharField(label=_("Upload torrent"), + torrent = forms.CharField(label=_("Upload torrent"), required=False, widget=forms.FileInput(attrs={'size':60})) - hash = forms.CharField(label=_("Hash"), + hash = forms.CharField(label=_("Hash"), required=False, widget=forms.TextInput(attrs={'size':60})) ret = forms.CheckBox(_('Add more')) class torrent_add: + def add_page(self,error = None): + form_data = utils.get_newforms_data(AddForm) + options_data = None + if error: + options_data = utils.get_newforms_data(OptionsForm) + return render.torrent_add(AddForm(form_data),OptionsForm(options_data), error) + @deco.deluge_page def GET(self, name): - return render.torrent_add(AddForm(),OptionsForm()) + return self.add_page() + @deco.check_session def POST(self, name): @@ -80,8 +89,14 @@ class torrent_add: *posting of data as string(for greasemonkey-private) """ - vars = web.input(url = None, torrent = {}) + options = dict(utils.get_newforms_data(OptionsForm)) + options_form = OptionsForm(options) + if not options_form.is_valid(): + print self.add_page(error = _("Error in torrent options.")) + return + + vars = web.input(url = None, torrent = {}) torrent_name = None torrent_data = None if vars.torrent.filename: @@ -89,14 +104,17 @@ class torrent_add: torrent_data = vars.torrent.file.read() if vars.url and torrent_name: - error_page(_("Choose an url or a torrent, not both.")) + #error_page(_("Choose an url or a torrent, not both.")) + print self.add_page(error = _("Choose an url or a torrent, not both.")) + return if vars.url: - ws.proxy.add_torrent_url(vars.url) - do_redirect() + ws.proxy.add_torrent_url(vars.url, options) + utils.do_redirect() elif torrent_name: data_b64 = base64.b64encode(torrent_data) #b64 because of strange bug-reports related to binary data - ws.proxy.add_torrent_filecontent(vars.torrent.filename, data_b64) - do_redirect() + ws.proxy.add_torrent_filecontent(vars.torrent.filename, data_b64, options) + utils.do_redirect() else: - error_page(_("no data, press back button and try again")) + print self.add_page(error = _("No data")) + return diff --git a/deluge/ui/webui/webui_plugin/utils.py b/deluge/ui/webui/webui_plugin/utils.py index 4a9986302..1d7ca20c7 100644 --- a/deluge/ui/webui/webui_plugin/utils.py +++ b/deluge/ui/webui/webui_plugin/utils.py @@ -296,6 +296,24 @@ def get_category_choosers(torrent_list): return filter_tabs, category_tabs +def get_newforms_data(form_class): + """ + glue for using web.py and newforms. + returns a storified dict with name/value of the post-data. + """ + import lib.newforms_plus as forms + fields = form_class.base_fields.keys() + form_data = web.Storage() + vars = web.input() + for field in fields: + form_data[field] = vars.get(field) + #DIRTY HACK: (for multiple-select) + if isinstance(form_class.base_fields[field], + forms.MultipleChoiceField): + form_data[field] = web.input(**{field:[]})[field] + #/DIRTY HACK + return form_data + #/utils class WebUiError(Exception): diff --git a/deluge/ui/webui/webui_plugin/webserver_common.py b/deluge/ui/webui/webui_plugin/webserver_common.py index f9b901601..61d3bf8f1 100644 --- a/deluge/ui/webui/webui_plugin/webserver_common.py +++ b/deluge/ui/webui/webui_plugin/webserver_common.py @@ -178,7 +178,7 @@ class Ws: #MONKEY PATCH, TODO->REMOVE!!! - def add_torrent_filecontent(name , data_b64): + def add_torrent_filecontent(name , data_b64, options): self.log.debug('monkeypatched add_torrent_filecontent:%s,len(data:%s))' % (name , len(data_b64))) @@ -191,7 +191,7 @@ class Ws: f.write(base64.b64decode(data_b64)) f.close() - self.proxy.add_torrent_file([filename]) + self.proxy.add_torrent_file([filename] , options) self.proxy.add_torrent_filecontent = add_torrent_filecontent self.log.debug('cfg-file %s' % self.config_file)