lib/actions/editkw.py
author fabien@tzone.org
Sun, 28 Dec 2008 12:24:02 -0500
changeset 58 004a32370ba5
parent 47 edit_kw.py@2781ac85b807
child 74 6784c4350b41
permissions -rwxr-xr-x
Correct some bugs and make keywords edition with the new action mechanic.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
     1
from keywords import get_keywords
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
     2
from webutils import load_index
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
     3
from templates import Template
15
574631f841c3 [svn r1549] Add update/merge/delete keywords.
fabien
parents:
diff changeset
     4
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
     5
def print_kw(keywords, prefs):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
     6
    tmpl = Template("edit_kw.tmpl", prefs, "", "")
47
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents: 43
diff changeset
     7
    tmpl.set('Keywords', keywords)
2781ac85b807 [svn r1604] Implement default_view preferences, which move things around a lot.
fabien
parents: 43
diff changeset
     8
    print tmpl.process()
15
574631f841c3 [svn r1549] Add update/merge/delete keywords.
fabien
parents:
diff changeset
     9
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    10
def print_confirm(step, sel_keywords, keywords, prefs):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    11
    if step == 'delete':
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    12
    	tmpl = Template("kw_delete.tmpl", prefs)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    13
    elif step == 'merge':
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    14
    	tmpl = Template("kw_merge.tmpl", prefs)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    15
    elif step == 'rename':
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    16
    	tmpl = Template("kw_rename.tmpl", prefs)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    17
    tmpl.set("Selected", sel_keywords)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    18
    tmpl.set("Keywords", keywords)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    19
    print tmpl.process()
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    20
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    21
def list_kw(db, prefs):
15
574631f841c3 [svn r1549] Add update/merge/delete keywords.
fabien
parents:
diff changeset
    22
    kw = db.get_all_keywords()[1:]
574631f841c3 [svn r1549] Add update/merge/delete keywords.
fabien
parents:
diff changeset
    23
    kw = map(lambda elem: {
574631f841c3 [svn r1549] Add update/merge/delete keywords.
fabien
parents:
diff changeset
    24
   	 'id' : elem[0],
574631f841c3 [svn r1549] Add update/merge/delete keywords.
fabien
parents:
diff changeset
    25
	 'keyword' : elem[1] }, kw)
30
dbf4027d82fc [svn r1574] Keywords edition now in a separate page.
fabien
parents: 21
diff changeset
    26
    kw.sort(lambda l,r: cmp(l['keyword'],r['keyword']))
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    27
    print_kw(kw, prefs)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    28
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    29
def confirm_kw(step, db, prefs, form):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    30
    ids = get_keywords(form, 'kw')
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    31
    keywords = map(lambda e: { 'id': e[0], 'keyword': e[1]},
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    32
   	 db.get_all_keywords()[1:])
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    33
    keywords.sort(lambda x, y: cmp(x['keyword'],y['keyword']))
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    34
    sel_keywords = filter(lambda e: e['id'] in ids, keywords)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    35
    keywords = filter(lambda e: e['id'] not in ids, keywords)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    36
    print_confirm(step, sel_keywords, keywords, prefs)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    37
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    38
def do_merge(db, prefs, form):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    39
	fromids = get_keywords(form, 'id')
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    40
	toids = get_keywords(form, 'kw')
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    41
	remove_them = form.has_key('remove')
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    42
	if remove_them:
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    43
		lastid = toids.pop()
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    44
	status = "result"
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    45
	status_msg = []
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    46
	for id in fromids:
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    47
		fromkw = db.get_keyword(id)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    48
		for dest in toids:
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    49
			tokw = db.get_keyword(dest)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    50
			status_msg.append("Adding '%s' into '%s'." % (fromkw, tokw))
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    51
			db.add_keywords(id, dest) 
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    52
		if remove_them:
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    53
			tokw = db.get_keyword(lastid)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    54
			status_msg.append("Merging '%s' into '%s'." % (fromkw, tokw))
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    55
			db.merge_keywords(id, lastid)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    56
	load_index(db, prefs, form, status, "<br/>".join(status_msg))
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    57
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    58
def do_rename(db, prefs, form):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    59
	ids = get_keywords(form, 'id')
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    60
	status = "result"
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    61
	status_msg = []
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    62
	for id in ids:
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    63
		oldname = db.get_keyword(id)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    64
		newname = form['name%d' % id].value
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    65
		status_msg.append("Renaming '%s' into '%s'." % (oldname, newname))
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    66
		db.update_keyword(id, newname)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    67
	load_index(db, prefs, form, status, "<br/>".join(status_msg))
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    68
 
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    69
def do_delete(db, prefs, form):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    70
	ids = get_keywords(form, 'id')
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    71
	status = "result"
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    72
	status_msg = []
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    73
	for id in ids:
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    74
		kw = db.get_keyword(id)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    75
		status_msg.append("Removing '%s'." % kw)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    76
		db.remove_keyword(id)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    77
	load_index(db, prefs, form, status, "<br/>".join(status_msg))
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    78
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    79
def do_it(action, db, prefs, form):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    80
	step = 'list'
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    81
	if form.has_key('step'):
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    82
		step = form['step'].value
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    83
	if step == 'list':
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    84
		list_kw(db, prefs)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    85
	elif step == 'cancel':
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    86
		load_index(db, prefs, form, "err", "Operation cancel")
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    87
	elif step == 'do_delete':
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    88
		do_delete(db, prefs, form)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    89
	elif step == 'do_merge':
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    90
		do_merge(db, prefs, form)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    91
	elif step == 'do_rename':
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    92
		do_rename(db, prefs, form)
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    93
	else:
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 47
diff changeset
    94
		confirm_kw(step, db, prefs, form)