lib/folders.py
author Fabien Ninoles <fabien@tzone.org>
Sun, 28 Dec 2008 15:19:40 -0500
changeset 63 8f246bc7059d
parent 58 004a32370ba5
child 74 6784c4350b41
permissions -rwxr-xr-x
Move template_dir inside config.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     1
from templates import Template
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     2
from keywords import set_selection, sort_keywords
54
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
     3
from __future__ import nested_scopes
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     4
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     5
def do_folder_action(db, form, folder):
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     6
   if form['action'].value == 'add':
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     7
    	if form.has_key('sel'):
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     8
		keyword = int(form['sel'].value)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     9
		db.add_folder(keyword, folder)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    10
		print "<em>Folder successfully add.</em>"
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    11
		return keyword
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    12
   elif form['action'].value == 'delete':
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    13
   	if form.has_key('delete'):
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    14
		keyword = int(form['delete'].value)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    15
		db.remove_folders(keyword)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    16
		print "<em>Folder successfully remove.</em>"
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    17
		return keyword
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    18
   return -1
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    19
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    20
def sort_kw_with_removed(l, r):
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    21
	result = l['removed'] - r['removed']
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    22
	if result == 0:
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    23
		result = l['count'] - r['count']
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    24
	return -int(result)
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    25
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 54
diff changeset
    26
def output(prefs, folder, parents, children, bookmarks, keywords, status, status_msg):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 54
diff changeset
    27
    tmpl = Template("folders.tmpl", prefs, status, status_msg)
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    28
    tmpl.set('Parents', parents)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    29
    tmpl.set('folder', folder['id'])
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    30
    tmpl.set('name', folder['name'])
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    31
    tmpl.set('total', folder['count'])
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    32
    tmpl.set('subtotal', len(bookmarks))
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    33
    bookmarks.sort(lambda l,r: cmp(l['name'],r['name']))
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    34
    tmpl.set('Bookmarks', bookmarks)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    35
    tmpl.set('Subfolders', children)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    36
    keywords.sort(sort_kw_with_removed)
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    37
    tmpl.set('Keywords', keywords)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    38
    print tmpl.process()
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    39
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 54
diff changeset
    40
def do_folders(db, prefs, form, status, status_msg):
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    41
    folder = 0
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    42
    if form:
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    43
    	if form.has_key('folder'):
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    44
    		folder = int(form['folder'].value)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    45
   	if form.has_key('action'):
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    46
		do_folder_action(db, form, folder)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    47
    parents = db.get_folder_parents_and_self(folder)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    48
    parents.reverse()
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    49
    children = db.get_subfolders(folder)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    50
    selection = map(lambda e: e['keyword'], parents)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    51
    exclusion = map(lambda e: e['keyword'], children)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    52
    folders_keywords = []
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    53
    for parent in parents:
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    54
	folders_keywords.append(parent['keyword'])
54
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    55
	parent['count'] = db.get_bookmarks_count(folders_keywords)[0][1]
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    56
    children_keywords = db.get_bookmarks_count(selection, [], exclusion)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    57
    for child in children:
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    58
	folders_keywords.append(child['keyword'])
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    59
	# This is really selection, not folders_keywords
54
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    60
	child['count'] = 0
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    61
	for kw in children_keywords:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    62
		if kw[0] == child['keyword']:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    63
			child['count'] = kw[1]
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    64
			children_keywords.remove(kw)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    65
			break
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    66
    del children_keywords
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    67
    bookmarks = map(lambda bk: bk[0], db.select_bookmarks(selection))
54
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    68
    allkw = filter(lambda kw: kw[0] not in folders_keywords,
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    69
   			 db.get_keywords(bookmarks))
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    70
    keywords = map(lambda kw: { 'id': kw[0], 'keyword': kw[1] }, allkw)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    71
    allkw = map(lambda kw: kw[0], allkw)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    72
    count = db.get_bookmarks_count(selection, [], allkw)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    73
    removed = db.get_bookmarks_count(selection, exclusion, allkw)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    74
    for kw in keywords:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    75
    	kw['count'] = 0
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    76
	kw['removed'] = 0
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    77
    	for cnt in count:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    78
		if kw['id'] == cnt[0]:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    79
			kw['count'] = cnt[1]
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    80
			count.remove(cnt)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    81
			break
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    82
    	for cnt in removed:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    83
		if kw['id'] == cnt[0]:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    84
			kw['removed'] = cnt[1]
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    85
			removed.remove(cnt)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    86
			break
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    87
    del allkw, count, removed
51
7015d65beb3d [svn r1608] Correct the keywords bugs and some other in folder view, plus mutual
fabien
parents: 49
diff changeset
    88
    bookmarks = db.select_bookmarks(selection, exclusion)
49
57932e991854 [svn r1606] Problem with zero bookmarks. Still some problem on keywords count.
fabien
parents: 47
diff changeset
    89
    if len(bookmarks) <> 0:
57932e991854 [svn r1606] Problem with zero bookmarks. Still some problem on keywords count.
fabien
parents: 47
diff changeset
    90
   	bookmarks = db.get_bookmarks(map(lambda bk: bk[0], bookmarks))
57932e991854 [svn r1606] Problem with zero bookmarks. Still some problem on keywords count.
fabien
parents: 47
diff changeset
    91
    	bookmarks = map(lambda bk: bk.dict(), bookmarks)
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 54
diff changeset
    92
    output(prefs, parents.pop(), parents, children, bookmarks, keywords, status, status_msg)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    93