Databases now contain all classes' data

This commit is contained in:
Abdulkadir Furkan Şanlı 2019-01-05 10:49:18 +01:00
parent b7611be5ee
commit e8f51079d7
No known key found for this signature in database
GPG Key ID: 7823BD18E6F95D73
2 changed files with 18 additions and 14 deletions

View File

@ -3,7 +3,7 @@ To-Do List
## v0.2 ## v0.2
- [x] Error handling (when entering invalid data types) - [x] Error handling (when entering invalid data types)
- [ ] Make database contain data for all classes - [x] Make database contain data for all classes
- [ ] Flexible mandatory subjects - [ ] Flexible mandatory subjects
## v0.3 ## v0.3

View File

@ -28,35 +28,37 @@ def connect_database(file):
return conn return conn
def request_id(conn): def get_data(conn):
""" """
Request student ID, validate, and return. Requests student ID and returns integer tuple (id, class) with valid
student id and corresponding class no.
:param conn: Connection object :param conn: Connection object
""" """
try: try:
id = int(input("Please enter student ID: ")) id = int(input("Please enter student ID: "))
cur = conn.cursor() cur = conn.cursor()
cur.execute("SELECT id FROM students") cur.execute("SELECT id, student_class FROM students")
ids = cur.fetchall() ids = cur.fetchall()
for x in ids: for x in ids:
# ids are single value tuples # ids is list with single value tuples
if id == x[0]: if id == x[0]:
return id # return tuple x with valid id and corresponding class no.
return x
else: else:
raise ValueError raise ValueError
except ValueError: except ValueError:
print("Incorrect ID, try again.") print("Incorrect ID, try again.")
request_id(conn) return get_data(conn)
def select_final_time(conn, day, id): def select_final_time(conn, day, stud):
""" """
Returns ending time of final lesson student must attend, or None. Returns ending time of final lesson student must attend, or None.
:param conn: Connection object :param conn: Connection object
:param day: current weekday, lowercase str :param day: current weekday, lowercase str
:param id: ID of student, int :param stud: tuple with student ID no. and class no., int
""" """
cur = conn.cursor() cur = conn.cursor()
cur.execute(""" cur.execute("""
@ -70,10 +72,12 @@ def select_final_time(conn, day, id):
OR g6 = lesson_id OR g6 = lesson_id
OR lesson_id = 'gu' OR lesson_id = 'gu'
OR lesson_id = 'tok' OR lesson_id = 'tok'
WHERE id = ? AND day = ? WHERE id = ?
AND day = ?
AND lesson_class = ?
ORDER BY end_time DESC ORDER BY end_time DESC
LIMIT 1 LIMIT 1
""", (id, day)) """, (stud[0], day, stud[1]))
finish = cur.fetchone() finish = cur.fetchone()
if str(type(finish)) == "<class 'tuple'>": if str(type(finish)) == "<class 'tuple'>":
return finish[0] return finish[0]
@ -86,8 +90,8 @@ def parse_time_string(timestring):
Parses given 24h time string of format "HH:MM" into struct_time. Parses given 24h time string of format "HH:MM" into struct_time.
:param timestring: time string "HH:MM" :param timestring: time string "HH:MM"
""" """
timestruct = time.strptime(timestring, "%H:%M") structtime = time.strptime(timestring, "%H:%M")
return timestruct return structtime
def main(): def main():
@ -97,7 +101,7 @@ def main():
conn = connect_database("database.db") conn = connect_database("database.db")
while True: while True:
id = request_id(conn) id = get_data(conn)
day = time.strftime("%A").lower() day = time.strftime("%A").lower()
finish_time = select_final_time(conn, day, id) finish_time = select_final_time(conn, day, id)
current_time = time.strftime("%H:%M") current_time = time.strftime("%H:%M")