lib/actions/bookmark.py
author fabien@tzone.org
Wed, 31 Dec 2008 16:26:22 -0500
changeset 75 4f6b7b48322f
parent 74 6784c4350b41
permissions -rw-r--r--
A lot of fix again, for folder view, bookmark editing and removal and some error handling.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     1
from templates import Template
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     2
from bkmark import Bookmark
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     3
from webutils import load_index
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     6
import os
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     7
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     8
def get_bk_from_form(form):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     9
        bk = Bookmark()
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    10
        bk.id = int(form['id'].value)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    11
        bk.url = form['url'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    12
        if form.has_key('name'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    13
                bk.name = form['name'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    14
        if form.has_key('desc'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    15
                bk.desc = form['desc'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    16
        return bk
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    17
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    18
def get_kw_from_form(form):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    19
        kw = form.getvalue("kw")
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    20
        if not isinstance(kw, type([])):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    21
                if kw:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    22
                        kw = [kw]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    23
                else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    24
                        kw = []
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    25
        kw = map(int, kw)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    26
        return kw
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    27
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    28
def get_new_kw_from_form(form, sep =','):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    29
        if form.has_key('newkw'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    30
                kw= map(lambda e: e.strip(),
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    31
                        form['newkw'].value.split(','))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    32
                return filter(lambda x: x != '', kw)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    33
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    34
                return []
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    35
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    36
def get_unique_keywords(form, db):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    42
    name = url = ""
75
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    43
    bk = Bookmark()
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    44
    selected_kws = []
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    45
    if (id != -1):
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    46
    	bks = db.get_bookmarks([id])
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    47
	if len(bks) > 0:
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    48
		bk = bks[0]
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    49
		(selected_kws, kwnames) = apply(zip,db.get_keywords([id]))
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    50
    if form.has_key("name"):
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    51
        bk.name = form["name"].value
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    52
    if form.has_key("url"):
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    53
        bk.url = form["url"].value
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    54
    kw = db.get_all_keywords()[1:]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    55
    kw.sort(lambda l,r: cmp(l[1],r[1]))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    56
    kw = map(lambda elem: {
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    57
         'id' : elem[0],
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    58
         'keyword' : elem[1],
75
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    59
         'checked' : elem[0] in selected_kws }, kw)
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    60
    print_edit(prefs, bk, kw)
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    61
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    62
def confirm(step, bk, kw, prefs):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    63
    tmpl = Template("add_confirm.tmpl", prefs)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    64
    confirmation = "Unknown!"
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    65
    if (step == 'delete'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    66
    	confirmation = "Delete" 
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    67
    elif (step == 'update'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    68
    	confirmation = "Update"
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    69
    tmpl.set("confirmation", confirmation)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    70
    tmpl.set("step", step)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    71
    tmpl.set("id", bk.id)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    72
    tmpl.set("name", bk.name)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    73
    tmpl.set("url", bk.url)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    74
    tmpl.set("Keywords", map(lambda x: {'keyword': x }, kw))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    75
    tmpl.set("desc", bk.desc)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    76
    print tmpl.process()
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    77
75
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    78
def print_edit(prefs, bk, keywords):
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    79
    tmpl = Template("add_bk.tmpl", prefs)
75
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    80
    tmpl.set('name', bk.name)
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    81
    tmpl.set('url', bk.url)
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    82
    tmpl.set('desc', bk.desc)
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    83
    tmpl.set('bkid', bk.id)
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    84
    tmpl.set('Keywords', keywords)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    85
    print tmpl.process()
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    86
75
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    87
def execute(db, form):
62
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    88
	bk = get_bk_from_form(form)
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    89
	id = bk.id
75
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    90
	execution = form['execute'].value
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    91
	if execution == 'delete':
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    92
		if id != -1:
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    93
			db.remove_bookmark(id)
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    94
	elif execution == 'update':
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    95
		if (bk.id == -1):
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    96
			id = db.add_bookmark(bk)
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    97
		else:
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    98
			db.update_bookmark(bk)
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
    99
		kw = add_unique_keywords(db, get_unique_keywords(form, db))
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
   100
		db.update_keywords(id, kw.values())
62
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
   101
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   102
def do_it(action, db, prefs, form):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   103
        if form.has_key('id'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   104
                id = int(form['id'].value)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   105
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   106
                id = -1;
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   107
        if form.has_key('step'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   108
                step = form['step'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   109
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   110
                step = 'edit'
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   111
        if step == 'edit':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   112
                edit(db, prefs, form, id)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   113
	elif step == 'confirm':
75
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
   114
		execute(db, form)
4f6b7b48322f A lot of fix again, for folder view, bookmark editing and removal
fabien@tzone.org
parents: 74
diff changeset
   115
                load_index(db, prefs, form, "result", "Bookmark " + form['execute'].value)
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   116
        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
   117
                load_index(db, prefs, form, "err", "Operation cancel")
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   118
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   119
                if step == 'update':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   120
                        bk = get_bk_from_form(form)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   121
                        kw = get_unique_keywords(form, db)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   122
                else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   123
                        bk = db.get_bookmarks([id])[0]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   124
                        (ids, kw) = apply(zip,db.get_keywords([id]))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   125
                        kw = kw[1:]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   126
                confirm(step, bk, kw, prefs)