lib/actions/add.py
author fabien@tzone.org
Wed, 31 Dec 2008 00:07:25 -0500
changeset 69 d79722064d8d
parent 62 56193b37f666
child 72 34fcc8b2c1f5
permissions -rw-r--r--
Merge previous changeset and ensure everything seems to work.
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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     5
from utils import unique
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)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    38
      kw = map(db.get_keyword, kw)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    39
      kw = kw + get_new_kw_from_form(form)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    40
      kw = unique(kw) 
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    41
      return kw
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    44
    name = url = ""
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    45
    if form.has_key("ctitle"):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    46
        name = form["ctitle"].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    47
    if form.has_key("curl"):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    48
        url = form["curl"].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    49
    kw = db.get_all_keywords()[1:]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    50
    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
    51
    kw = map(lambda elem: {
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    52
         'id' : elem[0],
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    53
         'keyword' : elem[1],
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    56
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    57
def confirm(step, bk, kw, prefs):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    58
    tmpl = Template("add_confirm.tmpl", prefs)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    59
    confirmation = "Unknown!"
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    60
    if (step == 'delete'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    61
    	confirmation = "Delete" 
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    62
    elif (step == 'update'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    63
    	confirmation = "Update"
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    64
    tmpl.set("confirmation", confirmation)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    65
    tmpl.set("step", step)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    66
    tmpl.set("id", bk.id)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    67
    tmpl.set("name", bk.name)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    68
    tmpl.set("url", bk.url)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    69
    tmpl.set("Keywords", map(lambda x: {'keyword': x }, kw))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    70
    tmpl.set("desc", bk.desc)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    71
    print tmpl.process()
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    74
    tmpl = Template("add_bk.tmpl", prefs)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    75
    tmpl.set('ctitle', title)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    76
    tmpl.set('curl', url)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    79
    tmpl.set('Keywords', keywords)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    80
    print tmpl.process()
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    81
62
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    82
def update_bookmark(db, form):
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    83
	bk = get_bk_from_form(form)
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    84
	id = bk.id
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    85
	if (bk.id == -1):
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    86
		id = db.add_bookmark(bk)
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    87
	else:
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    88
		db.update_bookmark(bk)
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    89
	kw = map(db.add_keyword, get_new_kw_from_form(form))
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    90
      	kw += map(db.get_keyword, get_kw_from_form(form))
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    91
	db.update_keywords(id, kw)
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    92
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    93
def do_it(action, db, prefs, form):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    94
        if form.has_key('id'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    95
                id = int(form['id'].value)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    96
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    97
                id = -1;
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    98
        if form.has_key('step'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    99
                step = form['step'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   100
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   101
                step = 'edit'
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   102
        if step == 'edit':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   103
                edit(db, prefs, form, id)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   104
	elif step == 'confirm':
62
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
   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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   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
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
                if step == 'update':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   111
                        bk = get_bk_from_form(form)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   112
                        kw = get_unique_keywords(form, db)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   113
                else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   114
                        bk = db.get_bookmarks([id])[0]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   115
                        (ids, kw) = apply(zip,db.get_keywords([id]))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   116
                        kw = kw[1:]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   117
                confirm(step, bk, kw, prefs)