-
Notifications
You must be signed in to change notification settings - Fork 0
/
attendance.py
249 lines (219 loc) · 7.87 KB
/
attendance.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from flask import Flask,render_template, redirect, request, session, flash, url_for, send_file
from datetime import timedelta, datetime, date
import pandas as pd
import io
from flask_sqlalchemy import SQLAlchemy
username = 'JayantaDas' #Username For Login
passw = 'smartEd@5500' #Password For Login
today = date.today()
day_of_week = today.strftime("%A")
app = Flask(__name__)
app.secret_key = 'hello'
app.permanent_session_lifetime = timedelta(hours= 4)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///entry.db'
app.config['SQLALCHEMY_BINDS'] = {'two' : 'sqlite:///view.db'}
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
app.app_context().push()
#Entry Database Schema Start
class EntryData(db.Model):
sno = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(200), nullable = False)
entry_time = db.Column(db.String(200), default= datetime.now().strftime("%H:%M:%S"))
exit_time = db.Column(db.String(200))
def __repr__(self) -> str:
return f'{self.sno} - {self.name}'
#Entry Database Schema End
#View Database Schema Start
class PreviousSession(db.Model):
__bind_key__ = 'two'
sno = db.Column(db.Integer, primary_key = True)
class_desc = db.Column(db.String(200), default = 'Normal Class')
date = db.Column(db.DateTime, default = datetime.now)
day = db.Column(db.String(200))
def __repr__(self) -> str:
return f'{self.sno} - {self.date}'
#View Database Schema End
with app.app_context():
db.create_all()
#App Login Page Start
@app.route("/", methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['name']
password = request.form['passwd']
checked = request.form.get('Remember me')
if user == username and passw == password:
if checked=='Remember me':
session.permanent = True
session['user'] = user
session['password'] = password
flash(f'You Have Logged In Successfully!', 'success')
return redirect(url_for('home'))
else:
flash(f'Username Or Password Is Not Correct! Try Again', 'error')
return redirect(url_for('login'))
else:
if 'user' in session:
flash('Already Logged In!', 'info')
return redirect(url_for('home'))
return render_template('login.html')
@app.route('/home', methods = ['POST', 'GET'])
def home():
if 'user' in session:
allEntry = EntryData.query.all()
return render_template('home.html', allEntry = allEntry)
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#App Login Page End
#App Logout Start
@app.route('/logout')
def logout():
if 'user' in session:
flash(f'You Have Been Logged Out Successfully', 'info')
session.pop('user', None)
session.pop('password', None)
return redirect(url_for('login'))
#App Logout End
#App Entry Data Page Start
@app.route('/entry', methods = ['GET', 'POST'])
def data_entry():
if 'user' in session:
if request.method == 'POST':
name = request.form.get('name')
entry = EntryData(name = name)
db.session.add(entry)
db.session.commit()
allEntry = EntryData.query.all()
if len(allEntry) == 0:
start_session = PreviousSession(day = day_of_week)
db.session.add(start_session)
db.session.commit()
return render_template('data_create.html', allEntry = allEntry)
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#App Entry Data Page End
#App Change Start
@app.route('/change/<int:sno>', methods = ['POST', 'GET'])
def change(sno):
if 'user' in session:
if request.method == 'POST':
name = request.form.get('name')
allEntry = EntryData.query.filter_by(sno = sno).first()
allEntry.name = name
db.session.add(allEntry)
db.session.commit()
return redirect('/entry')
allEntry = EntryData.query.filter_by(sno = sno).first()
return render_template('change.html', allEntry = allEntry)
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#App Change End
#App Update Start
@app.route('/update/<int:sno>')
def update(sno):
if 'user' in session:
allEntry = EntryData.query.filter_by(sno = sno).first()
allEntry.exit_time = datetime.now().strftime("%H:%M:%S")
db.session.add(allEntry)
db.session.commit()
return redirect('/entry')
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#App Update End
#End Session And Save Button Start
@app.route('/end_session')
def end_session():
if 'user' in session:
allEntry = EntryData.query.all()
for item in allEntry:
db.session.delete(item)
db.session.commit()
return redirect('/home')
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#End Session And Save Buttion End
#Delete Entry Start
@app.route('/delete/<int:sno>')
def delete(sno):
if 'user' in session:
allEntry = EntryData.query.filter_by(sno = sno).all()
for entry in allEntry:
db.session.delete(entry)
db.session.commit()
return redirect('/entry')
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#Delete Entry End
#Save File Start
@app.route('/export')
def export():
if 'user' in session:
all_entries = EntryData.query.all()
data = []
for entry in all_entries:
data.append({
'Sno': entry.sno,
'Name': entry.name,
'Entry Time': entry.entry_time,
'Exit Time': entry.exit_time
})
df = pd.DataFrame(data)
output = io.BytesIO()
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
df.to_excel(writer, index=False, sheet_name= 'Attendance' )
writer.close()
output.seek(0)
return send_file(output, download_name= f"Attendance_{today}.xlsx", as_attachment=True)
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#Save File End
#Session View Start
@app.route('/view_session')
def view_session():
if 'user' in session:
allEntry = PreviousSession.query.all()
return render_template('session_view.html', allEntry = allEntry)
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#Session View End
#Session View Entry Update Start
@app.route('/session_change/<int:sno>', methods = ['POST', 'GET'])
def session_view_change(sno):
if 'user' in session:
if request.method == 'POST':
desc = request.form.get('desc')
Entry = PreviousSession.query.filter_by(sno = sno).first()
Entry.class_desc = desc
db.session.add(Entry)
db.session.commit()
return redirect('/view_session')
Entry = PreviousSession.query.filter_by(sno = sno).first()
return render_template('view_session_change.html', Entry = Entry)
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#Session View Entry Update End
#Session View Entry Delete Start
@app.route('/session_delete/<int:sno>')
def session_view_delete(sno):
if 'user' in session:
allEntry = PreviousSession.query.filter_by(sno = sno).all()
for entry in allEntry:
db.session.delete(entry)
db.session.commit()
return redirect('/view_session')
else:
flash('You Are Not Logged In', 'info')
return redirect(url_for('login'))
#Session View Entry delete End
if __name__ == '__main__':
app.run(debug=True)