Skip to content

Commit a666f51

Browse files
committed
First Commit
1 parent 56fe126 commit a666f51

22 files changed

+1579
-0
lines changed

database/crud_flask.sql

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 4.4.15.7
3+
-- http://www.phpmyadmin.net
4+
--
5+
-- Host: localhost
6+
-- Generation Time: Jan 30, 2017 at 10:34 AM
7+
-- Server version: 5.7.17-0ubuntu0.16.04.1
8+
-- PHP Version: 7.0.13-0ubuntu0.16.04.1
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
SET time_zone = "+00:00";
12+
13+
14+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
15+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
16+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
17+
/*!40101 SET NAMES utf8mb4 */;
18+
19+
--
20+
-- Database: `crud_flask`
21+
--
22+
23+
-- --------------------------------------------------------
24+
25+
--
26+
-- Table structure for table `phone_book`
27+
--
28+
29+
CREATE TABLE IF NOT EXISTS `phone_book` (
30+
`id` int(5) NOT NULL,
31+
`name` varchar(255) NOT NULL,
32+
`phone` varchar(50) NOT NULL,
33+
`address` varchar(255) NOT NULL
34+
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
35+
36+
--
37+
-- Dumping data for table `phone_book`
38+
--
39+
40+
INSERT INTO `phone_book` (`id`, `name`, `phone`, `address`) VALUES
41+
(16, 'Muhammad Hanif', '085733492411', 'Lamongan');
42+
43+
--
44+
-- Indexes for dumped tables
45+
--
46+
47+
--
48+
-- Indexes for table `phone_book`
49+
--
50+
ALTER TABLE `phone_book`
51+
ADD PRIMARY KEY (`id`);
52+
53+
--
54+
-- AUTO_INCREMENT for dumped tables
55+
--
56+
57+
--
58+
-- AUTO_INCREMENT for table `phone_book`
59+
--
60+
ALTER TABLE `phone_book`
61+
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=21;
62+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
63+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
64+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

source_code/module/database.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
Created on Jan 10, 2017
3+
4+
@author: hanif
5+
'''
6+
7+
import pymysql
8+
9+
class Database:
10+
def connect(self):
11+
return pymysql.connect("localhost","dev","dev","crud_flask" )
12+
13+
def read(self, id):
14+
con = Database.connect(self)
15+
cursor = con.cursor()
16+
17+
try:
18+
if id == None:
19+
cursor.execute("SELECT * FROM phone_book order by name asc")
20+
else:
21+
cursor.execute("SELECT * FROM phone_book where id = %s order by name asc", (id,))
22+
23+
con.close()
24+
25+
return cursor.fetchall()
26+
except:
27+
return ()
28+
29+
30+
def insert(self,data):
31+
con = Database.connect(self)
32+
cursor = con.cursor()
33+
34+
try:
35+
cursor.execute("INSERT INTO phone_book(name,phone,address) VALUES(%s, %s, %s)", (data['name'],data['phone'],data['address'],))
36+
con.commit()
37+
38+
return True
39+
except:
40+
con.rollback()
41+
42+
return False
43+
44+
con.close()
45+
46+
def update(self, id, data):
47+
con = Database.connect(self)
48+
cursor = con.cursor()
49+
50+
try:
51+
cursor.execute("UPDATE phone_book set name = %s, phone = %s, address = %s where id = %s", (data['name'],data['phone'],data['address'],id,))
52+
con.commit()
53+
54+
return True
55+
except:
56+
con.rollback()
57+
58+
return False
59+
60+
con.close()
61+
62+
def delete(self, id):
63+
con = Database.connect(self)
64+
cursor = con.cursor()
65+
66+
try:
67+
cursor.execute("DELETE FROM phone_book where id = %s", (id,))
68+
con.commit()
69+
70+
return True
71+
except:
72+
con.rollback()
73+
74+
return False
75+
76+
con.close()

source_code/server.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'''
2+
Created on Jan 10, 2017
3+
4+
@author: hanif
5+
'''
6+
7+
from flask import Flask, flash, render_template, redirect, url_for, request, session
8+
from module.database import Database
9+
10+
11+
app = Flask(__name__)
12+
app.secret_key = "mys3cr3tk3y"
13+
db = Database()
14+
15+
@app.route('/')
16+
def index():
17+
data = db.read(None)
18+
19+
return render_template('index.html', data = data)
20+
21+
@app.route('/add/')
22+
def add():
23+
return render_template('add.html')
24+
25+
@app.route('/addphone', methods = ['POST', 'GET'])
26+
def addphone():
27+
if request.method == 'POST' and request.form['save']:
28+
if db.insert(request.form):
29+
flash("A new phone number has been added")
30+
else:
31+
flash("A new phone number can not be added")
32+
33+
return redirect(url_for('index'))
34+
else:
35+
return redirect(url_for('index'))
36+
37+
@app.route('/update/<int:id>/')
38+
def update(id):
39+
data = db.read(id);
40+
41+
if len(data) == 0:
42+
return redirect(url_for('index'))
43+
else:
44+
session['update'] = id
45+
return render_template('update.html', data = data)
46+
47+
@app.route('/updatephone', methods = ['POST'])
48+
def updatephone():
49+
if request.method == 'POST' and request.form['update']:
50+
51+
if db.update(session['update'], request.form):
52+
flash('A phone number has been updated')
53+
54+
else:
55+
flash('A phone number can not be updated')
56+
57+
session.pop('update', None)
58+
59+
return redirect(url_for('index'))
60+
else:
61+
return redirect(url_for('index'))
62+
63+
@app.route('/delete/<int:id>/')
64+
def delete(id):
65+
data = db.read(id);
66+
67+
if len(data) == 0:
68+
return redirect(url_for('index'))
69+
else:
70+
session['delete'] = id
71+
return render_template('delete.html', data = data)
72+
73+
@app.route('/deletephone', methods = ['POST'])
74+
def deletephone():
75+
if request.method == 'POST' and request.form['delete']:
76+
77+
if db.delete(session['delete']):
78+
flash('A phone number has been deleted')
79+
80+
else:
81+
flash('A phone number can not be deleted')
82+
83+
session.pop('delete', None)
84+
85+
return redirect(url_for('index'))
86+
else:
87+
return redirect(url_for('index'))
88+
89+
@app.errorhandler(404)
90+
def page_not_found(error):
91+
return render_template('error.html')
92+
93+
if __name__ == '__main__':
94+
app.run(debug = True, port=8181, host="0.0.0.0")

source_code/static/css/AdminLTE.min.css

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source_code/static/css/_all-skins.min.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source_code/static/css/bootstrap.min.css

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)