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
|
|
|
|
|
|
|
|
|
|
|
2019-01-05 10:49:18 +01:00
|
|
|
|
def get_data(conn):
|
2018-10-09 10:01:01 +02:00
|
|
|
|
"""
|
2019-01-05 10:49:18 +01:00
|
|
|
|
Requests student ID and returns integer tuple (id, class) with valid
|
2019-01-06 10:04:44 +01:00
|
|
|
|
student id, corresponding class no. (1 or 2) and extra subject IDs.
|
2018-10-09 10:01:01 +02:00
|
|
|
|
:param conn: Connection object
|
|
|
|
|
"""
|
2018-12-30 09:28:16 +01:00
|
|
|
|
try:
|
|
|
|
|
id = int(input("Please enter student ID: "))
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
2018-12-30 09:28:16 +01:00
|
|
|
|
cur = conn.cursor()
|
2019-01-06 10:04:44 +01:00
|
|
|
|
cur.execute("SELECT id, student_class, other FROM students")
|
2018-12-30 09:28:16 +01:00
|
|
|
|
ids = cur.fetchall()
|
|
|
|
|
|
2019-01-06 10:04:44 +01:00
|
|
|
|
for tup in ids:
|
|
|
|
|
# ids is list of tuples
|
|
|
|
|
if id == tup[0]:
|
|
|
|
|
# return tuple x with valid id, class and extra class IDs
|
|
|
|
|
lis = list(tup)
|
|
|
|
|
lis[2] = lis[2].split()
|
|
|
|
|
return lis
|
2018-12-30 09:28:16 +01:00
|
|
|
|
else:
|
|
|
|
|
raise ValueError
|
|
|
|
|
except ValueError:
|
2019-01-06 10:04:44 +01:00
|
|
|
|
print("Invalid ID, try again.")
|
2019-01-05 10:49:18 +01:00
|
|
|
|
return get_data(conn)
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
|
|
|
|
|
2019-01-06 10:04:44 +01:00
|
|
|
|
def select_final_time(conn, day, stuple):
|
2018-10-09 10:01:01 +02:00
|
|
|
|
"""
|
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
|
2019-01-06 10:04:44 +01:00
|
|
|
|
:param stuple: tuple with student ID no. and class no., int
|
2018-10-09 10:01:01 +02:00
|
|
|
|
"""
|
|
|
|
|
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
|
2019-01-05 10:49:18 +01:00
|
|
|
|
WHERE id = ?
|
|
|
|
|
AND day = ?
|
|
|
|
|
AND lesson_class = ?
|
2018-10-09 10:01:01 +02:00
|
|
|
|
ORDER BY end_time DESC
|
|
|
|
|
LIMIT 1
|
2019-01-06 10:04:44 +01:00
|
|
|
|
""", (stuple[0], day, stuple[1]))
|
|
|
|
|
ib_finish = cur.fetchone()
|
2019-01-11 11:54:57 +01:00
|
|
|
|
if ib_finish is not None:
|
2019-01-06 10:04:44 +01:00
|
|
|
|
ib_finish = parse_time_string(ib_finish[0])
|
|
|
|
|
|
|
|
|
|
others = []
|
|
|
|
|
for sub in stuple[2]:
|
|
|
|
|
cur.execute("""
|
|
|
|
|
SELECT end_time
|
|
|
|
|
FROM timetable
|
|
|
|
|
INNER JOIN students ON ? = lesson_id
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
AND day = ?
|
|
|
|
|
AND lesson_class = ?
|
|
|
|
|
ORDER BY end_time DESC
|
|
|
|
|
LIMIT 1
|
|
|
|
|
""", (sub, stuple[0], day, stuple[1]))
|
2019-01-11 11:54:57 +01:00
|
|
|
|
other_finish = cur.fetchone()
|
|
|
|
|
if other_finish is not None:
|
|
|
|
|
others.append(parse_time_string(other_finish[0]))
|
2019-01-06 10:04:44 +01:00
|
|
|
|
if not others:
|
2019-01-11 11:54:57 +01:00
|
|
|
|
others_finish = None
|
2018-10-09 10:01:01 +02:00
|
|
|
|
else:
|
2019-01-11 11:54:57 +01:00
|
|
|
|
others_finish = max(others)
|
2019-01-06 10:04:44 +01:00
|
|
|
|
|
2019-01-11 11:54:57 +01:00
|
|
|
|
if ib_finish is not None and others_finish is not None:
|
|
|
|
|
if ib_finish > others_finish:
|
2019-01-06 10:04:44 +01:00
|
|
|
|
finish = ib_finish
|
|
|
|
|
else:
|
2019-01-11 11:54:57 +01:00
|
|
|
|
finish = others_finish
|
|
|
|
|
elif ib_finish is not None:
|
|
|
|
|
finish = ib_finish
|
|
|
|
|
elif others_finish is not None:
|
|
|
|
|
finish = others_finish
|
2019-01-06 10:04:44 +01:00
|
|
|
|
else:
|
|
|
|
|
finish = None
|
|
|
|
|
|
|
|
|
|
return finish
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_time_string(timestring):
|
|
|
|
|
"""
|
2018-12-30 09:28:16 +01:00
|
|
|
|
Parses given 24h time string of format "HH:MM" into struct_time.
|
2018-10-09 10:06:48 +02:00
|
|
|
|
:param timestring: time string "HH:MM"
|
2018-10-09 10:01:01 +02:00
|
|
|
|
"""
|
2019-01-05 10:49:18 +01:00
|
|
|
|
structtime = time.strptime(timestring, "%H:%M")
|
|
|
|
|
return structtime
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2018-12-10 16:08:14 +01:00
|
|
|
|
print("ib-clearance")
|
|
|
|
|
print("============")
|
2018-12-30 09:28:16 +01:00
|
|
|
|
|
|
|
|
|
conn = connect_database("database.db")
|
|
|
|
|
|
|
|
|
|
while True:
|
2019-01-06 10:04:44 +01:00
|
|
|
|
student_data = get_data(conn)
|
2018-12-30 09:28:16 +01:00
|
|
|
|
day = time.strftime("%A").lower()
|
2019-01-06 10:04:44 +01:00
|
|
|
|
finish_time = select_final_time(conn, day, student_data)
|
|
|
|
|
current_time = parse_time_string(time.strftime("%H:%M"))
|
2018-12-30 09:28:16 +01:00
|
|
|
|
|
|
|
|
|
if finish_time == None:
|
|
|
|
|
print("Student has no lessons today, clear to leave.")
|
2019-01-06 10:04:44 +01:00
|
|
|
|
elif finish_time < current_time:
|
2018-12-30 09:28:16 +01:00
|
|
|
|
print("Student has finished for today, clear to leave.")
|
|
|
|
|
else:
|
2019-01-11 11:54:57 +01:00
|
|
|
|
print("\nStudent still has lessons, clearance not granted.")
|
2018-12-30 09:28:16 +01:00
|
|
|
|
|
2019-01-06 10:04:44 +01:00
|
|
|
|
if input("Enter y to run again.") not in ["y", "Y"]:
|
2018-12-30 09:28:16 +01:00
|
|
|
|
break
|
2018-10-09 10:01:01 +02:00
|
|
|
|
|
|
|
|
|
|
2018-12-30 09:28:16 +01:00
|
|
|
|
# Run main() if script is run standalone.
|
2018-10-09 10:01:01 +02:00
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|