torrent_add:options

This commit is contained in:
Martijn Voncken 2008-02-12 18:14:18 +00:00
parent b0a9bf49fe
commit 0b306717b6
7 changed files with 69 additions and 30 deletions

View File

@ -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)

View File

@ -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?"

View File

@ -93,7 +93,7 @@ $for torrent in torrent_list:
<td>$fsize(torrent.total_size)</td>
<td class="progress_bar">
<div class="progress_bar_outer">
<div class="progress_bar" style="width:$(torrent.progress)%">
<div class="progress_bar" style="width:$(torrent.progress)%;overflow:hidden">
$torrent.message $int(torrent.progress) %
</div>
</div>

View File

@ -1,10 +1,12 @@
$def with (add_form, options_form)
$def with (add_form, options_form, error)
$:render.header(_("Add Torrent"))
<div class="panel">
<form method="POST" action="/torrent/add" ENCTYPE="multipart/form-data">
<input type="hidden" name="redir" value="$get('redir')">
$if error:
<div class="error">$error</div>
<div id="torrent_add" >
<table>
$: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";
}
</script>
$:render.footer()

View File

@ -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

View File

@ -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):

View File

@ -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)