sortable categories
This commit is contained in:
parent
b26f9b9480
commit
4e9f12e0fb
|
@ -93,11 +93,12 @@ class CategoryPage(Directory):
|
|||
|
||||
|
||||
class CategoriesDirectory(Directory):
|
||||
_q_exports = ['', 'new']
|
||||
_q_exports = ['', 'new', 'update_order']
|
||||
|
||||
def _q_index [html] (self):
|
||||
get_response().breadcrumb.append( ('categories/', _('Categories')) )
|
||||
html_top('categories', title = _('Categories'))
|
||||
html_top('categories', title = _('Categories'),
|
||||
scripts = ['/js/prototype.js', '/js/scriptaculous/scriptaculous.js'] )
|
||||
|
||||
'<div class="explanation">'
|
||||
'<p>%s</p>' % _('Categories are used to sort the different forms.')
|
||||
|
@ -107,15 +108,44 @@ class CategoriesDirectory(Directory):
|
|||
<li><a href="new">%s</a></li>
|
||||
</ul>""" % _('New Category')
|
||||
|
||||
'<ul class="biglist">'
|
||||
for category in Category.select(order_by = 'name'):
|
||||
'<li>'
|
||||
'<ul class="biglist" id="category-list">'
|
||||
categories = Category.select()
|
||||
Category.sort_by_position(categories)
|
||||
for category in categories:
|
||||
'<li id="fieldlistitem_%s">' % category.id
|
||||
'<strong class="label">%s</strong>' % category.name
|
||||
'<p class="commands">'
|
||||
command_icon('%s/edit' % category.id, 'edit')
|
||||
command_icon('%s/delete' % category.id, 'remove')
|
||||
'</p></li>'
|
||||
'</ul>'
|
||||
'''<script type="text/javascript">
|
||||
// <![CDATA[
|
||||
Sortable.create('category-list', {dropOnEmpty:true,constraint:false,
|
||||
containement:["category-list"],
|
||||
onUpdate:function(){
|
||||
new Ajax.Request('update_order', {
|
||||
method:'post',
|
||||
parameters:Sortable.serialize('category-list'),
|
||||
onComplete:function(request){new Effect.Highlight('category-list',{});},
|
||||
evalScripts:true,
|
||||
asynchronous:true})}});\n
|
||||
// ]]>
|
||||
</script>'''
|
||||
|
||||
|
||||
def update_order(self):
|
||||
request = get_request()
|
||||
new_order = request.form['category-list[]']
|
||||
categories = Category.select()
|
||||
dict = {}
|
||||
for l in categories:
|
||||
dict[str(l.id)] = l
|
||||
for i, o in enumerate(new_order):
|
||||
dict[o].position = i + 1
|
||||
dict[o].store()
|
||||
return 'ok'
|
||||
|
||||
|
||||
def new [html] (self):
|
||||
get_response().breadcrumb.append( ('categories/', _('Categories')) )
|
||||
|
|
|
@ -272,7 +272,8 @@ class FormsDirectory(Directory):
|
|||
'<p>%s</p>' % _('You first have to define roles.')
|
||||
|
||||
|
||||
cats = Category.select(order_by = 'name')
|
||||
cats = Category.select()
|
||||
Category.sort_by_position(cats)
|
||||
one = False
|
||||
formdefs = FormDef.select(order_by = 'name')
|
||||
for c in cats:
|
||||
|
|
|
@ -6,6 +6,7 @@ class Category(StorableObject):
|
|||
name = None
|
||||
url_name = None
|
||||
description = None
|
||||
position = None
|
||||
|
||||
def __init__(self, name = None):
|
||||
StorableObject.__init__(self)
|
||||
|
@ -35,3 +36,15 @@ class Category(StorableObject):
|
|||
return False
|
||||
has_urlname = classmethod(has_urlname)
|
||||
|
||||
def sort_by_position(cls, categories):
|
||||
def cmp_position(x, y):
|
||||
if x.position == y.position:
|
||||
return 0
|
||||
if x.position is None:
|
||||
return 1
|
||||
if y.position is None:
|
||||
return -1
|
||||
return cmp(x.position, y.position)
|
||||
categories.sort(cmp_position)
|
||||
sort_by_position = classmethod(sort_by_position)
|
||||
|
||||
|
|
|
@ -661,7 +661,8 @@ class RootDirectory(AccessControlled, Directory):
|
|||
self.form_list(list_forms, category = self.category,
|
||||
session = session, user_forms = user_forms)
|
||||
else:
|
||||
cats = Category.select(order_by = 'name')
|
||||
cats = Category.select()
|
||||
Category.sort_by_position(cats)
|
||||
one = False
|
||||
for c in cats:
|
||||
l2 = [x for x in list_forms if x.category_id == c.id]
|
||||
|
|
Loading…
Reference in New Issue