webui:move filter-logic to organize-plugin

This commit is contained in:
Martijn Voncken 2008-03-04 19:52:06 +00:00
parent 8cf72ca2dc
commit db24c543fe
5 changed files with 66 additions and 118 deletions

View File

@ -162,39 +162,53 @@ class index:
@deco.deluge_page
@deco.auto_refreshed
def GET(self, name):
vars = web.input(sort=None, order=None ,filter=None , category=None)
vars = web.input(sort=None, order=None ,state=None , tracker=None,keyword=None)
torrent_list = get_torrent_list()
all_torrents = torrent_list[:]
#filter-state
if vars.filter:
torrent_list = filter_torrent_state(torrent_list, vars.filter)
setcookie("filter", vars.filter)
organize_filters = {}
if 'Organize' in proxy.get_enabled_plugins():
filter_dict = {}
#organize-filters
#todo: DRY (in less lines of code)
if vars.state:
filter_dict['state'] = vars.state
setcookie("state", vars.state)
else:
setcookie("filter", "")
#filter-cat
if vars.category:
torrent_list = [t for t in torrent_list if t.category == vars.category]
setcookie("category", vars.category)
if vars.tracker:
filter_dict['tracker'] = vars.tracker
setcookie("tracker", vars.tracker)
else:
setcookie("category", "")
setcookie("tracker", "")
if vars.keyword:
filter_dict['keyword'] = vars.keyword
setcookie("keyword", vars.keyword)
else:
setcookie("keyword", "")
torrent_ids = proxy.organize_get_session_state(filter_dict)
organize_filters = Storage(proxy.organize_all_filter_items())
else:
torrent_ids = proxy.get_session_state()
torrent_list = utils.get_enhanced_torrent_list(torrent_ids)
#sorting:
if vars.sort:
try:
torrent_list.sort(key=attrgetter(vars.sort))
except:
log.debug('Sorting Failed')
log.error('Sorting Failed')
if vars.order == 'up':
torrent_list = list(reversed(torrent_list))
setcookie("order", vars.order)
setcookie("sort", vars.sort)
return render.index(torrent_list, all_torrents)
log.debug("filters=%s,plugins=%s" % (organize_filters, proxy.get_enabled_plugins()))
return render.index(torrent_list, organize_filters)
class torrent_info:
@deco.deluge_page

View File

@ -93,10 +93,12 @@ def error_page(error):
print render.error(error)
#template-defs:
"""
obsolete:
def category_tabs(torrent_list):
filter_tabs, category_tabs = get_category_choosers(torrent_list)
return render.part_categories(filter_tabs, category_tabs)
"""
def template_crop(text, end):
try:
@ -160,7 +162,6 @@ def ftime(val):
template.Template.globals.update({
'sort_head': template_sort_head,
'part_stats':template_part_stats,
'category_tabs':category_tabs,
'crop': template_crop,
'crop_left': template_crop_left,
'_': _ , #gettext/translations

View File

@ -1,4 +1,4 @@
$def with (torrent_list, all_torrents)
$def with (torrent_list, organize_filters)
$:render.header(_('Torrent list'))
<script language="javascript">
@ -19,7 +19,8 @@ $for id, title, image, flag, method, url, important in toolbar_items:
title='$title'><img class='toolbar_btn'
src='/static/images/tango/$image'></a>
$:category_tabs(all_torrents)
$if organize_filters:
$:render.part_categories(organize_filters)
</div>

View File

@ -1,27 +1,28 @@
$def with (filter_tabs, category_tabs)
$def with (filters)
<form method="GET" id="category_form">
<input type="hidden" name="sort" value="$get('sort')">
<input type="hidden" name="order" value="$get('order')">
<select name='filter' id='filter'
<select name='state' id='state'
onchange="document.getElementById('category_form').submit()"
title="$_('Filter on state')">
$for tab in filter_tabs:
<option value="$tab.filter"
$if tab.filter == get('filter'):
$for state, num in filters.state:
<option value="$state"
$if state == get('state'):
selected
>
$tab.title
$_(state) ($num)
</option>
</select>
<select name='category' id='category'
<select name='tracker' id='tracker'
onchange="document.getElementById('category_form').submit()"
title="$_('Filter on Tracker')">
$for tab in category_tabs:
<option value="$tab.category"
$if tab.category == get('category'):
<option value="">$_('Trackers')</option>
$for tracker, num in filters.tracker:
<option value="$tracker"
$if tracker == get('tracker'):
selected
>
$tab.title
$tracker ($num)
</option>
</select>

View File

@ -67,22 +67,10 @@ def start_session():
log.debug('start session')
session_id = str(random.random())
ws.SESSIONS.append(session_id)
#if len(ws.SESSIONS) > 20: #save max 20 sessions?
# ws.SESSIONS = ws.SESSIONS[-20:]
#not thread safe! , but a verry rare bug.
#f = open(ws.session_file,'wb')
#pickle.dump(ws.SESSIONS, f)
#f.close()
setcookie("session_id", session_id)
def end_session():
session_id = getcookie("session_id")
#if session_id in ws.SESSIONS:
# ws.SESSIONS.remove(session_id)
#not thread safe! , but a verry rare bug.
#f = open(ws.session_file,'wb')
#pickle.dump(ws.SESSIONS, f)
#f.close()
setcookie("session_id","")
def do_redirect():
@ -91,25 +79,33 @@ def do_redirect():
ck = cookies()
url_vars = {}
#redirect to a non-default page.
if vars.redir:
seeother(vars.redir)
return
#todo:cleanup
#for the filters:
if ("order" in ck and "sort" in ck):
url_vars.update({'sort':ck['sort'] ,'order':ck['order'] })
if ("filter" in ck) and ck['filter']:
url_vars['filter'] = ck['filter']
if ("category" in ck) and ck['category']:
url_vars['category'] = ck['category']
if 'Organize' in proxy.get_enabled_plugins():
#todo:DRY
if ("state" in ck) and ck['state']:
url_vars['state'] = ck['state']
if ("tracker" in ck) and ck['tracker']:
url_vars['tracker'] = ck['tracker']
if ("keyword" in ck) and ck['keyword']:
url_vars['keyword'] = ck['keyword']
#redirect.
seeother(url("/index", **url_vars))
def getcookie(key, default = None):
"because i'm too lazy to type 3 lines for something this simple"
key = str(key).strip()
ck = cookies()
return ck.get(key, default)
def get_stats():
stats = Storage()
@ -126,13 +122,8 @@ def get_stats():
async_proxy.force_call(block=True)
#log.debug(str(stats))
stats.download_rate = fspeed(stats.download_rate)
stats.upload_rate = fspeed(stats.upload_rate)
#stats.max_upload = stats.max_upload
#stats.max_download = stats.max_download
if stats.max_upload < 0:
stats.max_upload = _("")
@ -153,6 +144,7 @@ def enhance_torrent_status(torrent_id,status):
"""
status = Storage(status)
#add missing values for deluge 0.6:
#todo : Remove this!, and clean up the rest
for key in TORRENT_KEYS:
if not key in status:
status[key] = 0
@ -222,75 +214,14 @@ def get_torrent_status(torrent_id):
status = proxy.get_torrent_status(torrent_id,TORRENT_KEYS)
return enhance_torrent_status(torrent_id, status)
def get_torrent_list():
def get_enhanced_torrent_list(torrent_ids):
"""
returns a list of storified-torrent-dicts.
"""
torrent_dict = proxy.get_torrents_status(
proxy.get_session_state(), TORRENT_KEYS)
torrent_dict = proxy.get_torrents_status(torrent_ids, TORRENT_KEYS)
return [enhance_torrent_status(id, status)
for id, status in torrent_dict.iteritems()]
def get_categories(torrent_list):
trackers = [(torrent['category'] or 'unknown') for torrent in torrent_list]
categories = {}
for tracker in trackers:
categories[tracker] = categories.get(tracker,0) + 1
return categories
def filter_torrent_state(torrent_list,filter_name):
#redesign filters on status field.
filters = {
'allocating': lambda t: (t.state == 'Allocating'),
'checking': lambda t: (t.state == 'Checking'),
'downloading': lambda t: (t.state == 'Downloadig'),
'seeding':lambda t: (t.state == 'Seeding'),
'paused':lambda t: (t.state == 'Paused'),
'error':lambda t: (t.state == 'Error'),
'queued':lambda t: (t.state == 'Queued'),
'traffic':lambda t: (t.download_rate > 0 or t.upload_rate > 0)
}
filter_func = filters[filter_name]
return [t for t in torrent_list if filter_func(t)]
def get_category_choosers(torrent_list):
"""
todo: split into 2 parts...
"""
categories = get_categories(torrent_list)
filter_tabs = [Storage(title='All (%s)' % len(torrent_list),
filter='', category=None)]
#static filters
for title, filter_name in [
(_('Allocating'),'allocating') ,
(_('Checking'),'checking') ,
(_('Downloading'),'downloading') ,
(_('Seeding'),'seeding') ,
(_('Paused'),'paused'),
(_('Error'),'error'),
(_('Queued'),'queued'),
(_('Traffic'),'traffic')
]:
title += ' (%s)' % (
len(filter_torrent_state(torrent_list, filter_name)), )
filter_tabs.append(Storage(title=title, filter=filter_name))
categories = [x for x in get_categories(torrent_list).iteritems()]
categories.sort()
#trackers:
category_tabs = []
category_tabs.append(
Storage(title=_('Trackers'),category=''))
for title,count in categories:
category = title
title += ' (%s)' % (count, )
category_tabs.append(Storage(title=title, category=category))
return filter_tabs, category_tabs
def get_newforms_data(form_class):
"""
glue for using web.py and newforms.