#\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')
#\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
%include("./partials/header.tpl")%include("./partials/footer")
//\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/