author | fabien@tzone.org |
Wed, 31 Dec 2008 15:41:32 -0500 | |
changeset 74 | 6784c4350b41 |
parent 73 | lib/actions/add.py@c078d8a04d76 |
child 75 | 4f6b7b48322f |
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 |
|
73
c078d8a04d76
Now only add unique keywords, either they are selected or write down.
fabien@tzone.org
parents:
72
diff
changeset
|
5 |
from keywords import add_unique_keywords |
57 | 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) |
|
72
34fcc8b2c1f5
Only add unique keywords and remove utils.py, which I'm not sure about
fabien@tzone.org
parents:
62
diff
changeset
|
38 |
kw = set(map(db.get_keyword, kw)) |
73
c078d8a04d76
Now only add unique keywords, either they are selected or write down.
fabien@tzone.org
parents:
72
diff
changeset
|
39 |
return list(kw.union(get_new_kw_from_form(form))) |
57 | 40 |
|
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
41 |
def edit(db, prefs, form, id): |
57 | 42 |
name = url = "" |
43 |
if form.has_key("ctitle"): |
|
44 |
name = form["ctitle"].value |
|
45 |
if form.has_key("curl"): |
|
46 |
url = form["curl"].value |
|
47 |
kw = db.get_all_keywords()[1:] |
|
48 |
kw.sort(lambda l,r: cmp(l[1],r[1])) |
|
49 |
kw = map(lambda elem: { |
|
50 |
'id' : elem[0], |
|
51 |
'keyword' : elem[1], |
|
52 |
'checked' : 0 }, kw) |
|
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
53 |
print_edit(id, kw, name, url, prefs) |
57 | 54 |
|
55 |
def confirm(step, bk, kw, prefs): |
|
56 |
tmpl = Template("add_confirm.tmpl", prefs) |
|
57 |
confirmation = "Unknown!" |
|
58 |
if (step == 'delete'): |
|
59 |
confirmation = "Delete" |
|
60 |
elif (step == 'update'): |
|
61 |
confirmation = "Update" |
|
62 |
tmpl.set("confirmation", confirmation) |
|
63 |
tmpl.set("step", step) |
|
64 |
tmpl.set("id", bk.id) |
|
65 |
tmpl.set("name", bk.name) |
|
66 |
tmpl.set("url", bk.url) |
|
67 |
tmpl.set("Keywords", map(lambda x: {'keyword': x }, kw)) |
|
68 |
tmpl.set("desc", bk.desc) |
|
69 |
print tmpl.process() |
|
70 |
||
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
71 |
def print_edit(id, keywords, title, url, prefs): |
57 | 72 |
tmpl = Template("add_bk.tmpl", prefs) |
73 |
tmpl.set('ctitle', title) |
|
74 |
tmpl.set('curl', url) |
|
75 |
tmpl.set('desc', '') |
|
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
76 |
tmpl.set('bkid', id) |
57 | 77 |
tmpl.set('Keywords', keywords) |
78 |
print tmpl.process() |
|
79 |
||
62 | 80 |
def update_bookmark(db, form): |
81 |
bk = get_bk_from_form(form) |
|
82 |
id = bk.id |
|
83 |
if (bk.id == -1): |
|
84 |
id = db.add_bookmark(bk) |
|
85 |
else: |
|
86 |
db.update_bookmark(bk) |
|
72
34fcc8b2c1f5
Only add unique keywords and remove utils.py, which I'm not sure about
fabien@tzone.org
parents:
62
diff
changeset
|
87 |
kw = add_unique_keywords(db, get_unique_keywords(form, db)) |
34fcc8b2c1f5
Only add unique keywords and remove utils.py, which I'm not sure about
fabien@tzone.org
parents:
62
diff
changeset
|
88 |
db.update_keywords(id, kw.values()) |
62 | 89 |
|
57 | 90 |
def do_it(action, db, prefs, form): |
91 |
if form.has_key('id'): |
|
92 |
id = int(form['id'].value) |
|
93 |
else: |
|
94 |
id = -1; |
|
95 |
if form.has_key('step'): |
|
96 |
step = form['step'].value |
|
97 |
else: |
|
98 |
step = 'edit' |
|
99 |
if step == 'edit': |
|
100 |
edit(db, prefs, form, id) |
|
101 |
elif step == 'confirm': |
|
62 | 102 |
update_bookmark(db, form) |
58
004a32370ba5
Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents:
57
diff
changeset
|
103 |
load_index(db, prefs, form, "result", "Bookmark update") |
57 | 104 |
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
|
105 |
load_index(db, prefs, form, "err", "Operation cancel") |
57 | 106 |
else: |
107 |
if step == 'update': |
|
108 |
bk = get_bk_from_form(form) |
|
109 |
kw = get_unique_keywords(form, db) |
|
110 |
else: |
|
111 |
bk = db.get_bookmarks([id])[0] |
|
112 |
(ids, kw) = apply(zip,db.get_keywords([id])) |
|
113 |
kw = kw[1:] |
|
114 |
confirm(step, bk, kw, prefs) |