author | fabien@tzone.org |
Wed, 31 Dec 2008 00:00:09 -0500 | |
changeset 68 | c1f1491f098c |
parent 62 | 56193b37f666 |
child 72 | 34fcc8b2c1f5 |
permissions | -rw-r--r-- |
57 | 1 |
from templates import Template |
2 |
from bkmark import Bookmark |
|
3 |
from webutils import load_index |
|
4 |
from templates import Template |
|
5 |
from utils import unique |
|
6 |
import os |
|
7 |
||
8 |
def get_bk_from_form(form): |
|
9 |
bk = Bookmark() |
|
10 |
bk.id = int(form['id'].value) |
|
11 |
bk.url = form['url'].value |
|
12 |
if form.has_key('name'): |
|
13 |
bk.name = form['name'].value |
|
14 |
if form.has_key('desc'): |
|
15 |
bk.desc = form['desc'].value |
|
16 |
return bk |
|
17 |
||
18 |
def get_kw_from_form(form): |
|
19 |
kw = form.getvalue("kw") |
|
20 |
if not isinstance(kw, type([])): |
|
21 |
if kw: |
|
22 |
kw = [kw] |
|
23 |
else: |
|
24 |
kw = [] |
|
25 |
kw = map(int, kw) |
|
26 |
return kw |
|
27 |
||
28 |
def get_new_kw_from_form(form, sep =','): |
|
29 |
if form.has_key('newkw'): |
|
30 |
kw= map(lambda e: e.strip(), |
|
31 |
form['newkw'].value.split(',')) |
|
32 |
return filter(lambda x: x != '', kw) |
|
33 |
else: |
|
34 |
return [] |
|
35 |
||
36 |
def get_unique_keywords(form, db): |
|
37 |
kw = get_kw_from_form(form) |
|
38 |
kw = map(db.get_keyword, kw) |
|
39 |
kw = kw + get_new_kw_from_form(form) |
|
40 |
kw = unique(kw) |
|
41 |
return kw |
|
42 |
||
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
43 |
def edit(db, prefs, form, id): |
57 | 44 |
name = url = "" |
45 |
if form.has_key("ctitle"): |
|
46 |
name = form["ctitle"].value |
|
47 |
if form.has_key("curl"): |
|
48 |
url = form["curl"].value |
|
49 |
kw = db.get_all_keywords()[1:] |
|
50 |
kw.sort(lambda l,r: cmp(l[1],r[1])) |
|
51 |
kw = map(lambda elem: { |
|
52 |
'id' : elem[0], |
|
53 |
'keyword' : elem[1], |
|
54 |
'checked' : 0 }, kw) |
|
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
55 |
print_edit(id, kw, name, url, prefs) |
57 | 56 |
|
57 |
def confirm(step, bk, kw, prefs): |
|
58 |
tmpl = Template("add_confirm.tmpl", prefs) |
|
59 |
confirmation = "Unknown!" |
|
60 |
if (step == 'delete'): |
|
61 |
confirmation = "Delete" |
|
62 |
elif (step == 'update'): |
|
63 |
confirmation = "Update" |
|
64 |
tmpl.set("confirmation", confirmation) |
|
65 |
tmpl.set("step", step) |
|
66 |
tmpl.set("id", bk.id) |
|
67 |
tmpl.set("name", bk.name) |
|
68 |
tmpl.set("url", bk.url) |
|
69 |
tmpl.set("Keywords", map(lambda x: {'keyword': x }, kw)) |
|
70 |
tmpl.set("desc", bk.desc) |
|
71 |
print tmpl.process() |
|
72 |
||
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
73 |
def print_edit(id, keywords, title, url, prefs): |
57 | 74 |
tmpl = Template("add_bk.tmpl", prefs) |
75 |
tmpl.set('ctitle', title) |
|
76 |
tmpl.set('curl', url) |
|
77 |
tmpl.set('desc', '') |
|
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
78 |
tmpl.set('bkid', id) |
57 | 79 |
tmpl.set('Keywords', keywords) |
80 |
print tmpl.process() |
|
81 |
||
62 | 82 |
def update_bookmark(db, form): |
83 |
bk = get_bk_from_form(form) |
|
84 |
id = bk.id |
|
85 |
if (bk.id == -1): |
|
86 |
id = db.add_bookmark(bk) |
|
87 |
else: |
|
88 |
db.update_bookmark(bk) |
|
89 |
kw = map(db.add_keyword, get_new_kw_from_form(form)) |
|
90 |
kw += map(db.get_keyword, get_kw_from_form(form)) |
|
91 |
db.update_keywords(id, kw) |
|
92 |
||
57 | 93 |
def do_it(action, db, prefs, form): |
94 |
if form.has_key('id'): |
|
95 |
id = int(form['id'].value) |
|
96 |
else: |
|
97 |
id = -1; |
|
98 |
if form.has_key('step'): |
|
99 |
step = form['step'].value |
|
100 |
else: |
|
101 |
step = 'edit' |
|
102 |
if step == 'edit': |
|
103 |
edit(db, prefs, form, id) |
|
104 |
elif step == 'confirm': |
|
62 | 105 |
update_bookmark(db, form) |
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
106 |
load_index(db, prefs, form, "result", "Bookmark update") |
57 | 107 |
elif step == 'cancel' or (action == 'delete' and id == -1): |
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
108 |
load_index(db, prefs, form, "err", "Operation cancel") |
57 | 109 |
else: |
110 |
if step == 'update': |
|
111 |
bk = get_bk_from_form(form) |
|
112 |
kw = get_unique_keywords(form, db) |
|
113 |
else: |
|
114 |
bk = db.get_bookmarks([id])[0] |
|
115 |
(ids, kw) = apply(zip,db.get_keywords([id])) |
|
116 |
kw = kw[1:] |
|
117 |
confirm(step, bk, kw, prefs) |