import sqlite3
from bottle import route, run,debug,template,request,redirect

@route('/todo')
def todo_list():
    conn = sqlite3.connect('todo.db')
    c = conn.cursor()
    c.execute("SELECT id, task FROM todo WHERE status LIKE '1'")
    result = c.fetchall()
    c.close()
    output = template('make_table', rows=result)
    return output



@route('/new', method='GET')
def new_item():
    if request.GET.save:
        new = request.GET.task.strip()
        conn = sqlite3.connect('todo.db')
        c = conn.cursor()
        c.execute("INSERT INTO todo (task,status) VALUES (?,?)", (new,1))
        new_id = c.lastrowid
        conn.commit()
        c.close()
        redirect('/todo')
        #return '<p>The new task was inserted into the database, the ID is %s</p>' % new_id
    else:
        return template('new_task.tpl')


@route('/do_insert' , method='GET')
def get_id():
    redirect('/new')      



@route('/edit/<no:int>', method='GET')
def edit_item(no):
    if request.GET.save:
        edit = request.GET.task.strip()
        status = request.GET.status.strip()
        if status == 'open':
            status = 1
        else:
            status = 0
        conn = sqlite3.connect('todo.db')
        c = conn.cursor()
        c.execute("UPDATE todo SET task = ?, status = ? WHERE id LIKE ?", (edit, status, no))
        conn.commit()
        return '<p>The item number %s was successfully updated</p>' % no
    else:
        conn = sqlite3.connect('todo.db')
        c = conn.cursor()
        c.execute("SELECT task FROM todo WHERE id LIKE ?", (str(no)))
        cur_data = c.fetchone()
        return template('edit_task', old=cur_data, no=no)

@route('/find_edit' , method='GET')
def get_id():
    id_edit = request.GET.editdata.strip()
    redirect('/edit/' + id_edit)   
    

@route('/delete/<no:int>', method='GET')
def delete_item(no):
       conn = sqlite3.connect('todo.db')
       c = conn.cursor()
       c.execute("DELETE FROM todo WHERE id LIKE ?", (str(no)))
       conn.commit()
       redirect('/todo')

@route('/find_delete' , method='GET')
def get_id():
    id_delete = request.GET.deletedata.strip()
    redirect('/delete/' + id_delete)   


debug(True)
run(reloader=True)

[((79, 93), 'bottle.route', 'route', (['"""/todo"""'], {}), "('/todo')\n", (84, 93), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((347, 374), 'bottle.route', 'route', (['"""/new"""'], {'method': '"""GET"""'}), "('/new', method='GET')\n", (352, 374), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((837, 870), 'bottle.route', 'route', (['"""/do_insert"""'], {'method': '"""GET"""'}), "('/do_insert', method='GET')\n", (842, 870), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((917, 954), 'bottle.route', 'route', (['"""/edit/<no:int>"""'], {'method': '"""GET"""'}), "('/edit/<no:int>', method='GET')\n", (922, 954), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((1669, 1702), 'bottle.route', 'route', (['"""/find_edit"""'], {'method': '"""GET"""'}), "('/find_edit', method='GET')\n", (1674, 1702), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((1804, 1843), 'bottle.route', 'route', (['"""/delete/<no:int>"""'], {'method': '"""GET"""'}), "('/delete/<no:int>', method='GET')\n", (1809, 1843), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((2043, 2078), 'bottle.route', 'route', (['"""/find_delete"""'], {'method': '"""GET"""'}), "('/find_delete', method='GET')\n", (2048, 2078), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((2183, 2194), 'bottle.debug', 'debug', (['(True)'], {}), '(True)\n', (2188, 2194), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((2195, 2213), 'bottle.run', 'run', ([], {'reloader': '(True)'}), '(reloader=True)\n', (2198, 2213), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((122, 148), 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), "('todo.db')\n", (137, 148), False, 'import sqlite3\n'), ((289, 324), 'bottle.template', 'template', (['"""make_table"""'], {'rows': 'result'}), "('make_table', rows=result)\n", (297, 324), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((890, 906), 'bottle.redirect', 'redirect', (['"""/new"""'], {}), "('/new')\n", (898, 906), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((1732, 1760), 'bottle.request.GET.editdata.strip', 'request.GET.editdata.strip', ([], {}), '()\n', (1758, 1760), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((1765, 1793), 'bottle.redirect', 'redirect', (["('/edit/' + id_edit)"], {}), "('/edit/' + id_edit)\n", (1773, 1793), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((1879, 1905), 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), "('todo.db')\n", (1894, 1905), False, 'import sqlite3\n'), ((2023, 2040), 'bottle.redirect', 'redirect', (['"""/todo"""'], {}), "('/todo')\n", (2031, 2040), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((2110, 2140), 'bottle.request.GET.deletedata.strip', 'request.GET.deletedata.strip', ([], {}), '()\n', (2138, 2140), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((2145, 2177), 'bottle.redirect', 'redirect', (["('/delete/' + id_delete)"], {}), "('/delete/' + id_delete)\n", (2153, 2177), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((430, 454), 'bottle.request.GET.task.strip', 'request.GET.task.strip', ([], {}), '()\n', (452, 454), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((470, 496), 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), "('todo.db')\n", (485, 496), False, 'import sqlite3\n'), ((674, 691), 'bottle.redirect', 'redirect', (['"""/todo"""'], {}), "('/todo')\n", (682, 691), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((809, 833), 'bottle.template', 'template', (['"""new_task.tpl"""'], {}), "('new_task.tpl')\n", (817, 833), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((1014, 1038), 'bottle.request.GET.task.strip', 'request.GET.task.strip', ([], {}), '()\n', (1036, 1038), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((1056, 1082), 'bottle.request.GET.status.strip', 'request.GET.status.strip', ([], {}), '()\n', (1080, 1082), False, 'from bottle import route, run, debug, template, request, redirect\n'), ((1187, 1213), 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), "('todo.db')\n", (1202, 1213), False, 'import sqlite3\n'), ((1454, 1480), 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), "('todo.db')\n", (1469, 1480), False, 'import sqlite3\n'), ((1624, 1666), 'bottle.template', 'template', (['"""edit_task"""'], {'old': 'cur_data', 'no': 'no'}), "('edit_task', old=cur_data, no=no)\n", (1632, 1666), False, 'from bottle import route, run, debug, template, request, redirect\n')]