lib/actions/add.py
author fabien@tzone.org
Wed, 31 Dec 2008 01:40:46 -0500
changeset 72 34fcc8b2c1f5
parent 62 56193b37f666
child 73 c078d8a04d76
permissions -rw-r--r--
Only add unique keywords and remove utils.py, which I'm not sure about the copyright.
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
import os
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     6
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     7
def get_bk_from_form(form):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     8
        bk = Bookmark()
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
     9
        bk.id = int(form['id'].value)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    10
        bk.url = form['url'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    11
        if form.has_key('name'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    12
                bk.name = form['name'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    13
        if form.has_key('desc'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    14
                bk.desc = form['desc'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    15
        return bk
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    16
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    17
def get_kw_from_form(form):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    18
        kw = form.getvalue("kw")
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    19
        if not isinstance(kw, type([])):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    20
                if kw:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    21
                        kw = [kw]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    22
                else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    23
                        kw = []
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    24
        kw = map(int, kw)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    25
        return kw
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    26
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    27
def get_new_kw_from_form(form, sep =','):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    28
        if form.has_key('newkw'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    29
                kw= map(lambda e: e.strip(),
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    30
                        form['newkw'].value.split(','))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    31
                return filter(lambda x: x != '', kw)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    32
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    33
                return []
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    34
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    35
def get_unique_keywords(form, db):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    36
      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
    37
      kw = set(map(db.get_keyword, kw))
34fcc8b2c1f5 Only add unique keywords and remove utils.py, which I'm not sure about
fabien@tzone.org
parents: 62
diff changeset
    38
      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
    39
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 57
diff changeset
    40
def edit(db, prefs, form, id):
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    41
    name = url = ""
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    42
    if form.has_key("ctitle"):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    43
        name = form["ctitle"].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    44
    if form.has_key("curl"):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    45
        url = form["curl"].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    46
    kw = db.get_all_keywords()[1:]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    47
    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
    48
    kw = map(lambda elem: {
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    49
         'id' : elem[0],
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    50
         'keyword' : elem[1],
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    51
         'checked' : 0 }, kw)
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 57
diff changeset
    52
    print_edit(id, kw, name, url, prefs)
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    53
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    54
def confirm(step, bk, kw, prefs):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    55
    tmpl = Template("add_confirm.tmpl", prefs)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    56
    confirmation = "Unknown!"
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    57
    if (step == 'delete'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    58
    	confirmation = "Delete" 
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    59
    elif (step == 'update'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    60
    	confirmation = "Update"
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    61
    tmpl.set("confirmation", confirmation)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    62
    tmpl.set("step", step)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    63
    tmpl.set("id", bk.id)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    64
    tmpl.set("name", bk.name)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    65
    tmpl.set("url", bk.url)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    66
    tmpl.set("Keywords", map(lambda x: {'keyword': x }, kw))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    67
    tmpl.set("desc", bk.desc)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    68
    print tmpl.process()
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    69
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 57
diff changeset
    70
def print_edit(id, keywords, title, url, prefs):
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    71
    tmpl = Template("add_bk.tmpl", prefs)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    72
    tmpl.set('ctitle', title)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    73
    tmpl.set('curl', url)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    74
    tmpl.set('desc', '')
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 57
diff changeset
    75
    tmpl.set('bkid', id)
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    76
    tmpl.set('Keywords', keywords)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    77
    print tmpl.process()
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    78
62
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    79
def update_bookmark(db, form):
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    80
	bk = get_bk_from_form(form)
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    81
	id = bk.id
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    82
	if (bk.id == -1):
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    83
		id = db.add_bookmark(bk)
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    84
	else:
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    85
		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
    86
	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
    87
	db.update_keywords(id, kw.values())
62
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
    88
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    89
def do_it(action, db, prefs, form):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    90
        if form.has_key('id'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    91
                id = int(form['id'].value)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    92
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    93
                id = -1;
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    94
        if form.has_key('step'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    95
                step = form['step'].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
                step = 'edit'
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    98
        if step == 'edit':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    99
                edit(db, prefs, form, id)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   100
	elif step == 'confirm':
62
56193b37f666 Correct a bug when saving keywords.
fabien@tzone.org
parents: 58
diff changeset
   101
		update_bookmark(db, form)
58
004a32370ba5 Correct some bugs and make keywords edition with the new action
fabien@tzone.org
parents: 57
diff changeset
   102
                load_index(db, prefs, form, "result", "Bookmark update")
57
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   103
        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
   104
                load_index(db, prefs, form, "err", "Operation cancel")
57
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
                if step == 'update':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   107
                        bk = get_bk_from_form(form)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   108
                        kw = get_unique_keywords(form, db)
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
                        bk = db.get_bookmarks([id])[0]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   111
                        (ids, kw) = apply(zip,db.get_keywords([id]))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   112
                        kw = kw[1:]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   113
                confirm(step, bk, kw, prefs)