lib/actions/add.py
author fabien@tzone.org
Sat, 27 Dec 2008 20:23:39 -0500
changeset 57 31271426f879
child 58 004a32370ba5
permissions -rw-r--r--
First change to use a common entry point.
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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    43
def edit(db, prefs, form):
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)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    55
    print_edit(keywords, title, url, prefs)
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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    73
def print_edit(keywords, title, url, prefs):
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', '')
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    78
    tmpl.set('bkid', -1)
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
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    82
def do_it(action, db, prefs, form):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    83
        if form.has_key('id'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    84
                id = int(form['id'].value)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    85
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    86
                id = -1;
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    87
        if form.has_key('step'):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    88
                step = form['step'].value
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    89
        else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    90
                step = 'edit'
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    91
        if step == 'edit':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    92
                edit(db, prefs, form, id)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    93
	elif step == 'confirm':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    94
		bk = get_bk_from_form(form)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    95
		kw = get_unique_keywords(form, db)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    96
		db.update_bookmark(bk)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    97
                load_index(db, prefs)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    98
        elif step == 'cancel' or (action == 'delete' and id == -1):
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
    99
                load_index(db, prefs)
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
                if step == 'update':
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   102
                        bk = get_bk_from_form(form)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   103
                        kw = get_unique_keywords(form, db)
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   104
                else:
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   105
                        bk = db.get_bookmarks([id])[0]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   106
                        (ids, kw) = apply(zip,db.get_keywords([id]))
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   107
                        kw = kw[1:]
31271426f879 First change to use a common entry point.
fabien@tzone.org
parents:
diff changeset
   108
                confirm(step, bk, kw, prefs)