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 | #\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 ( '/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 : 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 | #\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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!--\views\customer.tpl--> %include("./partials/header.tpl") < div class = "main" id = "main" > < div class = "content" id = "content" > < div class = "top-widget" > < span >CUSTOMERS</ span >< input onclick = "location.href='/custform'" type = "button" value = "Add Customer" > </ div > < div id = "table" ></ div > < script >bicycle.showCustomer({{!data['customers']}})</ script > </ div > <!--content--> </ div > <!--main--> %include("./partials/footer") |
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 | //\public\js\main.js class Bicycle{ customerForm(formId){ var name = document.forms[formId][ 'fname' ].value; var phone = document.forms[formId][ 'fphone' ].value; if ((name == "" ) || (phone == "" )){ alert( "Please fill this form with your name and phone number!" ); return false ; } else { var phoneRegex = /^(\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/; var numberResult = phoneRegex.test(phone); if (!numberResult){ alert( "Please fill this form with the right phone number." ); return false ; } } } showCustomer(customers){ var html = '' ; if (customers.length > 0){ html += "<table>" ; html += "<tr>" ; for ( var k in {id:0, name:0, phone:0}){ html += "<th>" + k.toUpperCase() + "</th>" ; } html += "<th>OPTION</th>" ; html += "</tr>" ; for ( var i=0; i<customers.length; i++){ html += "<tr>" ; html += "<td>" + (i+1) + "</td>" ; for ( var j in customers[i]){ if (j > 0) html += "<td>" + customers[i][j] + "</td>" ; } html += `<td class= "option" ><a href= "/custform/edit/${customers[i][0]}" class= "edit" >Edit</a>|<a href= "/customer/delete/${customers[i][0]}" class= "delete" >Delete</a></td>`; html += "</tr>" ; } html += "</table>" ; } document.getElementById( "table" ).innerHTML = html; } } //end of class var bicycle = new Bicycle(); |
GitHub: https://github.com/Sokhavuth/Rental-Bicycle-App
Heroku: https://khmerweb-rba.herokuapp.com/