Update/Delete Data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#\controllers\customer.py
import config, re, json
from bottle import template, route, request, redirect
from models import customerdb
 
@route('/customer')
def displayCustomer():
  config.kargs['customers'] = json.dumps(customerdb.select())
  return template('customer', data=config.kargs)
 
@route('/custform')
def getCustomerForm():
  return template('custform', data=config.kargs)
 
@route('/custform/edit/<id>')
def editCustomerForm(id):
  config.kargs['rowedit'] = customerdb.edit(id)
  config.kargs['id'] = id
  return template('custform', data=config.kargs)
 
@route("/customer/delete/<id>")
def deleteCustomer(id):
  config.kargs['id'] = id
  customerdb.delete(id)
  redirect('/customer')
 
@route('/customer', method="POST")
def getFormData():
  name = request.forms.get("fname")
  phone = request.forms.get("fphone")
 
  if not re.match(r"^(\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$", phone):
    config.kargs['message'] = "You need to enter a right phone number."
    return template('custform', data=config.kargs)
 
  else:
    if 'rowedit' in config.kargs:
      customerdb.update(name, phone, config.kargs['id'])
      del config.kargs['rowedit']
      redirect('/customer')
 
    else:
      customerdb.insert(name, phone)
      redirect('/customer')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#\models\customerdb.py
import sqlite3
 
def insert(*customer):
  conn = sqlite3.connect('sqlite.db')
  cursor = conn.cursor()
 
  sql ='''CREATE TABLE IF NOT EXISTS CUSTOMER(
    NAME TEXT,
    PHONE TEXT
  )
  '''
 
  cursor.execute(sql)
 
  cursor.execute("INSERT INTO CUSTOMER VALUES (?, ?)", customer)
   
  conn.commit()
  conn.close()
 
 
def select():
  conn = sqlite3.connect('sqlite.db')
  cursor = conn.cursor()
 
  sql ='''CREATE TABLE IF NOT EXISTS CUSTOMER(
    NAME TEXT,
    PHONE TEXT
  )
  '''
 
  cursor.execute(sql)
 
  cursor.execute("SELECT ROWID, * from CUSTOMER")
 
  customers = cursor.fetchall()
   
  conn.commit()
  conn.close()
 
  return customers
 
def edit(id):
  conn = sqlite3.connect('sqlite.db')
  cursor = conn.cursor()
 
  cursor.execute("SELECT ROWID, * from CUSTOMER WHERE ROWID = " + id)
 
  customer = cursor.fetchone()
   
  conn.commit()
  conn.close()
   
  return customer
 
 
def update(*args):
  conn = sqlite3.connect('sqlite.db')
  cursor = conn.cursor()
 
  sql = "UPDATE CUSTOMER SET NAME=?, PHONE=? WHERE ROWID=?"
   
  cursor.execute(sql, args)
   
  conn.commit()
  conn.close()
 
def delete(id):
  conn = sqlite3.connect('sqlite.db')
  cursor = conn.cursor()
 
  cursor.execute("DELETE FROM CUSTOMER WHERE ROWID=?", (id,))
 
  conn.commit()
  conn.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!--\views\custform.tpl-->
 
%include("./partials/header.tpl")
 
<style>
#custform{
  margin-top: 30px;
  width: 30%;
  display: grid;
  grid-template-columns: auto calc(85% - 5px);
  grid-gap: 5px;
}
#custform a{
  text-align: right;
}
</style>
 
<div class="main" id="main">
  <div class="content" id="content">
    <span>CUSTOMER ENTRY FORM</span>
     
    <form id="custform" method="POST" action="/customer" onsubmit="return bicycle.customerForm('custform')">
      %if 'rowedit' in data:
      <a>Name:</a><input name="fname" value="{{data['rowedit'][1]}}" type="text" required />
      <a>Phone:</a><input name="fphone" value="{{data['rowedit'][2]}}" type="text" required />
      %else:
      <a>Name:</a><input name="fname" type="text" required />
      <a>Phone:</a><input name="fphone" type="text" required />
      %end
      <a></a><input type="submit" />
    </form>
     
    <p>{{data['message']}}</p>
    %data['message'] = ""
  </div><!--content-->
 
</div><!--main-->
 
%include("./partials/footer")

GitHub: https://github.com/Sokhavuth/Rental-Bicycle-App
Heroku: https://khmerweb-rba.herokuapp.com/