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)

[(4, 'bottle.route', 'route', (['"""/todo"""'], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (16, 'bottle.route', 'route', (['"""/new"""'], {'method': '"""GET"""'}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (32, 'bottle.route', 'route', (['"""/do_insert"""'], {'method': '"""GET"""'}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (38, 'bottle.route', 'route', (['"""/edit/<no:int>"""'], {'method': '"""GET"""'}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (59, 'bottle.route', 'route', (['"""/find_edit"""'], {'method': '"""GET"""'}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (65, 'bottle.route', 'route', (['"""/delete/<no:int>"""'], {'method': '"""GET"""'}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (73, 'bottle.route', 'route', (['"""/find_delete"""'], {'method': '"""GET"""'}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (79, 'bottle.debug', 'debug', (['(True)'], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (80, 'bottle.run', 'run', ([], {'reloader': '(True)'}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (6, 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), False, 'import sqlite3\n'), (11, 'bottle.template', 'template', (['"""make_table"""'], {'rows': 'result'}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (34, 'bottle.redirect', 'redirect', (['"""/new"""'], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (61, 'bottle.request.GET.editdata.strip', 'request.GET.editdata.strip', ([], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (62, 'bottle.redirect', 'redirect', (["('/edit/' + id_edit)"], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (67, 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), False, 'import sqlite3\n'), (71, 'bottle.redirect', 'redirect', (['"""/todo"""'], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (75, 'bottle.request.GET.deletedata.strip', 'request.GET.deletedata.strip', ([], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (76, 'bottle.redirect', 'redirect', (["('/delete/' + id_delete)"], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (19, 'bottle.request.GET.task.strip', 'request.GET.task.strip', ([], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (20, 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), False, 'import sqlite3\n'), (26, 'bottle.redirect', 'redirect', (['"""/todo"""'], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (29, 'bottle.template', 'template', (['"""new_task.tpl"""'], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (41, 'bottle.request.GET.task.strip', 'request.GET.task.strip', ([], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (42, 'bottle.request.GET.status.strip', 'request.GET.status.strip', ([], {}), False, 'from bottle import route, run, debug, template, request, redirect\n'), (47, 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), False, 'import sqlite3\n'), (53, 'sqlite3.connect', 'sqlite3.connect', (['"""todo.db"""'], {}), False, 'import sqlite3\n'), (57, 'bottle.template', 'template', (['"""edit_task"""'], {'old': 'cur_data', 'no': 'no'}), False, 'from bottle import route, run, debug, template, request, redirect\n')]