Additional work to Add function.
It is currently in a broken state and needs work aligning INSERT sqlite instructions with schema of events db
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import sqlite3 # Make sure sqlite3 is imported
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import typer
|
||||
|
||||
from cs50_final_project.db import db_init
|
||||
from cs50_final_project.helpers import create_cursor
|
||||
|
||||
app = typer.Typer(help="Calendar Clash")
|
||||
|
||||
@@ -42,14 +44,33 @@ def add():
|
||||
error_datetime = "Incompatible Syntax. Please use DD-MM-YY and HH:MM for date and time, respectively, as well as a whole number (in minutes) for length."
|
||||
typer.echo(typer.style(error_datetime, fg=typer.colors.RED), err=True)
|
||||
raise typer.Exit(code=1)
|
||||
#Query events.db for existing events matching iso_date
|
||||
conn = sqlite3.
|
||||
|
||||
print(name)
|
||||
print(date)
|
||||
print(time)
|
||||
print(length)
|
||||
print(description)
|
||||
conn = sqlite3.connect("events.db")
|
||||
cursor = create_cursor(conn)
|
||||
|
||||
cursor.execute("SELECT name, time, length FROM events WHERE date = ?", (iso_date,))
|
||||
existing_events = cursor.fetchall()
|
||||
|
||||
for existing_name, existing_time, existing_length in existing_events:
|
||||
ex_start = datetime.strptime(f"{iso_date} {existing_time}", "%Y-%m-%d %H:%M")
|
||||
ex_end = ex_start + timedelta(minutes=int(existing_length))
|
||||
|
||||
if start_datetime < ex_end and ex_start < end_datetime:
|
||||
print(f"\n Clash detected! Conflicts with existing event: '{existing_name}' ({existing_time} - {ex_end.strftime('%H:%M')})")
|
||||
conn.close()
|
||||
return
|
||||
|
||||
# Close connection if no clash found
|
||||
conn.close()
|
||||
|
||||
cursor.execute(
|
||||
"INSERT INTO events (name, start_time, length, description) VALUES (?, ?, ?, ?, ?)",
|
||||
(name, iso_date, time, duration, description)
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
typer.secho("\nEvent successfully added without clashes!", fg=typer.colors.GREEN)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app()
|
||||
|
||||
Reference in New Issue
Block a user