lib/folders.py
author fabien@tzone.org
Wed, 31 Dec 2008 15:41:32 -0500
changeset 74 6784c4350b41
parent 58 004a32370ba5
permissions -rw-r--r--
Cleanup of old code, fixes some errors with folders view and make it a standard action.
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
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
     3
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
     4
def sort_kw_with_removed(l, r):
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
     5
	result = l['removed'] - r['removed']
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
     6
	if result == 0:
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
     7
		result = l['count'] - r['count']
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
     8
	return -int(result)
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
     9
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 54
diff changeset
    10
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
    11
    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
    12
    tmpl.set('Parents', parents)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    13
    tmpl.set('folder', folder['id'])
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    14
    tmpl.set('name', folder['name'])
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    15
    tmpl.set('total', folder['count'])
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    16
    tmpl.set('subtotal', len(bookmarks))
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    17
    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
    18
    tmpl.set('Bookmarks', bookmarks)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    19
    tmpl.set('Subfolders', children)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    20
    keywords.sort(sort_kw_with_removed)
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    21
    tmpl.set('Keywords', keywords)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    22
    print tmpl.process()
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    23
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 54
diff changeset
    24
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
    25
    folder = 0
74
6784c4350b41 Cleanup of old code, fixes some errors with folders view and make it a
fabien@tzone.org
parents: 58
diff changeset
    26
    if form.has_key('folder'):
6784c4350b41 Cleanup of old code, fixes some errors with folders view and make it a
fabien@tzone.org
parents: 58
diff changeset
    27
    	folder = int(form['folder'].value)
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    28
    parents = db.get_folder_parents_and_self(folder)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    29
    parents.reverse()
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    30
    children = db.get_subfolders(folder)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    31
    selection = map(lambda e: e['keyword'], parents)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents:
diff changeset
    32
    exclusion = map(lambda e: e['keyword'], children)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    33
    folders_keywords = []
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    34
    for parent in parents:
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    35
	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
    36
	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
    37
    children_keywords = db.get_bookmarks_count(selection, [], exclusion)
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    38
    for child in children:
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    39
	folders_keywords.append(child['keyword'])
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    40
	# 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
    41
	child['count'] = 0
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    42
	for kw in children_keywords:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    43
		if kw[0] == child['keyword']:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    44
			child['count'] = kw[1]
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    45
			children_keywords.remove(kw)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    46
			break
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    47
    del children_keywords
53
e80160a19653 [svn r1610] Lots of "small" bugs corrected.
fabien
parents: 52
diff changeset
    48
    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
    49
    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
    50
   			 db.get_keywords(bookmarks))
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    51
    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
    52
    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
    53
    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
    54
    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
    55
    for kw in keywords:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    56
    	kw['count'] = 0
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    57
	kw['removed'] = 0
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    58
    	for cnt in count:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    59
		if kw['id'] == cnt[0]:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    60
			kw['count'] = cnt[1]
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    61
			count.remove(cnt)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    62
			break
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    63
    	for cnt in removed:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    64
		if kw['id'] == cnt[0]:
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    65
			kw['removed'] = cnt[1]
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    66
			removed.remove(cnt)
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    67
			break
f74bad856bce [svn r1611] Optimization of db.get_bookmarks_count which was call too often.
fabien
parents: 53
diff changeset
    68
    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
    69
    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
    70
    if len(bookmarks) <> 0:
57932e991854 [svn r1606] Problem with zero bookmarks. Still some problem on keywords count.
fabien
parents: 47
diff changeset
    71
   	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
    72
    	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
    73
    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
    74