2018-10-09 10:01:01 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
2018-10-23 17:10:40 +02:00
|
|
|
|
# Copyright 2018 Abdulkadir Furkan Şanlı
|
|
|
|
|
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
2018-10-09 10:01:01 +02:00
|
|
|
|
import sqlite3
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def connect_database(file):
|
|
|
|
|
"""
|
2018-11-04 11:33:43 +01:00
|
|
|
|
Creates and returns Connection object.
|
2018-10-09 10:01:01 +02:00
|
|
|
|
:param file: database file
|
|
|
|
|
"""
|
|
|
|
|
conn = sqlite3.connect(file)
|
|
|
|
|
return conn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_lower_weekday():
|
|
|
|
|
"""
|
2018-11-04 11:33:43 +01:00
|
|
|
|
Returns current weekday's name as lowercase string.
|
2018-10-09 10:01:01 +02:00
|
|
|
|
"""
|
|
|
|
|
day = time.strftime("%A")
|
|
|
|
|
lower_day = day.lower()
|
|
|
|
|
return lower_day
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_id(conn, id):
|
|
|
|
|
"""
|
2018-11-04 11:33:43 +01:00
|
|
|
|
Returns True if existing ID, False if not.
|
2018-10-09 10:01:01 +02:00
|
|
|
|
:param conn: Connection object
|
|
|
|
|
:param id: ID of student, int
|
|
|
|
|
"""
|
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
cur.execute("SELECT id FROM students")
|
|
|
|
|
ids = cur.fetchall()
|
|
|
|
|
|
|
|
|
|
for x in ids:
|
|
|
|
|
if id == x[0]:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def select_final_time(conn, day, id):
|
|
|
|
|
"""
|
2018-11-04 11:33:43 +01:00
|
|
|
|
Returns ending time of final lesson student must attend, or None.
|
2018-10-09 10:01:01 +02:00
|
|
|
|
:param conn: Connection object
|
|
|
|
|
:param day: current weekday, lowercase str
|
|
|
|
|
:param id: ID of student, int
|
|
|
|
|
"""
|
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
cur.execute("""
|
|
|
|
|
SELECT end_time
|
|
|
|
|
FROM timetable
|
|
|
|
|
INNER JOIN students ON g1 = lesson_id
|
|
|
|
|
OR g2 = lesson_id
|
|
|
|
|
OR g3 = lesson_id
|
|
|
|
|
OR g4 = lesson_id
|
|
|
|
|
OR g5 = lesson_id
|
|
|
|
|
OR g6 = lesson_id
|
2018-10-13 07:56:48 +02:00
|
|
|
|
OR lesson_id = 'gu'
|
2018-12-01 08:35:07 +01:00
|
|
|
|
OR lesson_id = 'tok'
|
2018-10-09 10:01:01 +02:00
|
|
|
|
WHERE id = ? AND day = ?
|
|
|
|
|
ORDER BY end_time DESC
|
|
|
|
|
LIMIT 1
|
|
|
|
|
""", (id, day))
|
|
|
|
|
finish = cur.fetchone()
|
|
|
|
|
if str(type(finish)) == "<class 'tuple'>":
|
|
|
|
|
return finish[0]
|
|
|
|
|
else:
|
|
|
|
|
return finish
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_time_string(timestring):
|
|
|
|
|
"""
|
2018-11-04 11:33:43 +01:00
|
|
|
|
Parses given 24h time string of format "HH:MM" into time_struct.
|
2018-10-09 10:06:48 +02:00
|
|
|
|
:param timestring: time string "HH:MM"
|
2018-10-09 10:01:01 +02:00
|
|
|
|
"""
|
|
|
|
|
timestruct = time.strptime(timestring, "%H:%M")
|
|
|
|
|
return timestruct
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
db = input(
|
2018-11-04 11:33:43 +01:00
|
|
|
|
"Please enter name of database (in same folder as program): ")
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
|
|
|
|
y = "y"
|
|
|
|
|
while y == "y":
|
|
|
|
|
conn = connect_database(db)
|
|
|
|
|
day = get_lower_weekday()
|
|
|
|
|
id = int(input("Please input the student ID number: "))
|
|
|
|
|
|
|
|
|
|
if check_id(conn, id):
|
2018-11-04 11:33:43 +01:00
|
|
|
|
raw_time = select_final_time(conn, day, id)
|
|
|
|
|
final_time = parse_time_string(
|
|
|
|
|
raw_time) if raw_time != None else None
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
2018-11-04 11:33:43 +01:00
|
|
|
|
if final_time != None:
|
2018-10-09 10:01:01 +02:00
|
|
|
|
current_time = parse_time_string(time.strftime("%H:%M"))
|
|
|
|
|
|
2018-11-04 11:33:43 +01:00
|
|
|
|
if current_time > final_time:
|
2018-10-09 10:01:01 +02:00
|
|
|
|
print("Clear to leave.")
|
|
|
|
|
else:
|
|
|
|
|
print("Student does not have clearance.")
|
2018-11-04 11:33:43 +01:00
|
|
|
|
|
|
|
|
|
elif final_time == None:
|
2018-10-09 10:01:01 +02:00
|
|
|
|
print("Clear to leave.")
|
2018-11-03 09:30:35 +01:00
|
|
|
|
else:
|
|
|
|
|
print("Student does not exist in the database. Please try again.")
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
2018-11-04 11:33:43 +01:00
|
|
|
|
y = input("Press y key followed by enter to run again.")
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|