lib/keywords.py
branchxbelweb
changeset 47 2781ac85b807
child 51 7015d65beb3d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/keywords.py	Fri Oct 24 17:04:29 2003 -0400
@@ -0,0 +1,47 @@
+def get_keywords(form, name):
+	kws = form.getvalue(name)
+	if not isinstance(kws, type([])):
+		if kws:
+			kws = [kws]
+		else:
+			kws = []
+	return map(int, kws)
+
+def set_selection(db, bk, sel, exc = []):
+	if len(bk) > 0:
+		ids, names = map(list,apply(zip,db.get_keywords(bk)))
+		for key in exc:
+			if key not in ids:
+				ids.append(key)
+				names.append(db.get_keyword(key))
+		allkw = map(lambda x,y: [x, y], ids, names)
+	else:
+		allkw = db.get_all_keywords()
+	(kw, cnt) = map(list,apply(zip, db.get_keywords_count(bk)))
+	res = []
+	for key in allkw:
+		is_selected = key[0] in sel
+		is_excluded = key[0] in exc
+		is_unselected = not (is_selected or is_excluded)
+		if key[0] in kw:
+			kcnt = cnt[kw.index(key[0])]
+		else:
+			kcnt = 0
+		res.append({'id':key[0],
+			'keyword': key[1],
+			'count' : kcnt,
+			'selected' : is_selected,
+			'excluded' : is_excluded,
+			'unselected' : is_unselected})
+	return res
+
+def sort_keywords(kw, pref):
+	global _sk_rev_fact
+	global _sk_sort
+	if pref['keywords_reverse']:
+		_sk_rev_fact = -1
+	else:
+		_sk_rev_fact = 1
+	_sk_sort = pref['keywords_sort'] 
+	kw.sort(lambda l, r: _sk_rev_fact*cmp(l[_sk_sort], r[_sk_sort]))
+	return kw