-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
100 lines (79 loc) · 3.33 KB
/
app.py
File metadata and controls
100 lines (79 loc) · 3.33 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from flask import Flask, render_template, flash, request, jsonify
from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField
import sqlite3
import csv
import xlsxwriter
# -*- coding: utf-8 -*-
# App config.
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = '7d441f27d441f27567d441f2b6176a'
class VisitorDetailsForm(Form):
name = TextField('Name:', validators=[validators.required()])
email = TextField('Email:', validators=[validators.required(), validators.Length(min=6, max=35)])
phone = TextField('Phone:', validators=[validators.required()])
person = TextField('Person:', validators=[validators.required()])
purpose =TextField('Purpose:', validators=[validators.required()])
pdate = TextField('Purpose:', validators=[validators.required()])
time = TextField('Time:', validators=[validators.required()])
@app.route("/", methods=['GET', 'POST'])
def index():
form = VisitorDetailsForm(request.form)
print form.errors
if request.method == 'POST':
name=request.form['name']
email=request.form['email']
phone = request.form['phone']
person = request.form['person']
purpose = request.form['purpose']
pdate = request.form['pdate']
intime =request.form['intime']
# create DB with the data from the form.
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute("create table if not exists records (name text, email text, phone text, person text, purpose text, pdate text, intime text)")
rec = [name, email, phone, person, purpose, pdate, intime]
c.execute("insert into records values (?, ?, ?, ?, ?, ?, ?)", rec)
conn.commit()
flash('Logged the information successfully. ')
c.close()
return render_template('index.html', form=form)
@app.route("/showRecords", methods=['GET', 'POST'])
def showRecords():
conn = sqlite3.connect('test.db')
c = conn.cursor()
data = c.execute('SELECT * from records LIMIT 11')
items = data.fetchall()
return render_template('records.html', items=items)
@app.route("/export", methods=['GET', 'POST'])
def exportrecordstocsv():
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('Records.xlsx')
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'Name', bold)
worksheet.write('B1', 'Email', bold)
worksheet.write('C1', 'Phone', bold)
worksheet.write('D1', 'Met With', bold)
worksheet.write('E1', 'Purpose', bold)
worksheet.write('F1', 'Date', bold)
worksheet.write('G1', 'Time', bold)
# Write data from the db, with row/column notation.
conn = sqlite3.connect('test.db')
c = conn.cursor()
data = c.execute('SELECT * from records LIMIT 11')
items = data.fetchall()
j = 1
for record in items:
for i in range(7):
worksheet.write(j, i, record[i])
j = j+1
workbook.close()
message = 'Exported the records successfully. Please check Records.xlsx file'
return render_template('records.html', items=items, message= message)
if __name__ == "__main__":
app.run()