Skip to content

Commit

Permalink
created interactive prompt that query django model db to fill out inf…
Browse files Browse the repository at this point in the history
…ormation for each event
  • Loading branch information
pinwheeeel committed Aug 24, 2024
1 parent 7ff606b commit 859651a
Showing 1 changed file with 210 additions and 16 deletions.
226 changes: 210 additions & 16 deletions core/management/commands/add_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.core.management.base import BaseCommand
from django.db import IntegrityError

from core.models import Event
from core.models import Event, Organization, Term

# TODO: consider adding this to metropolis/settings.py in the "Event calender Settings" section (?)
SECRET_CAL_ADDRESS = "Change me"
Expand All @@ -25,7 +25,7 @@ def handle(self, *args, **options):
rq = requests.get(SECRET_CAL_ADDRESS)

if rq.status_code != 200:
raise AssertionError("Could not get calendar data. Are you sure the calendar link is set correctly? See the instructions in core/management/commands/add_events.py")
raise AssertionError(f"Error {rq.status_code}, {rq.reason}. Could not get calendar data. Are you sure the calendar link is set correctly? See the instructions in core/management/commands/add_events.py")

lines = rq.text.splitlines()

Expand All @@ -37,20 +37,205 @@ def handle(self, *args, **options):
l = l.strip()

if l == "BEGIN:VEVENT":
# Default values
event_data = {
"name": "Unnamed Event",
"description": "",
"start_date": None,
"end_date": None,
"term": None,
"organization": None,
"schedule_format": "default",
"is_instructional": True,
"is_public": True,
"should_announce": False,
# "tags": [],
}
is_in_event = True

elif l == "END:VEVENT":
is_in_event = False

# TODO: because past events are not deleted from the calendar,
# we have to somehow make sure we don't add the same event twice
# => maybe discard any events that have end before the script was run?
self.stdout.write(f"New event discovered:\n")
self.stdout.write(f"\tData: {event_data}\n")
# Because past events are not deleted from the calendar, we don't
# add any events that have already passed to prevent duplication.
if datetime.now() > event_data["end_date"]:
self.stdout.write(
self.style.ERROR(f"Event '{event_data["name"]}' skipped because it has already passed.")
)
continue

self.stdout.write(
self.style.SUCCESS(f"\nNew event created:")
)
# self.stdout.write(f"\tData: {event_data}\n")
for k, v in event_data.items():
self.stdout.write(f"\t{k}: {v}\n")

term = None
self.stdout.write(
"\n\tPlease enter the name of the event's affiliated term (case insensitive):"
)
while True:
try:
print("\t", end="")
term_name = input()
term = Term.objects.get(name__iexact=term_name)

self.stdout.write(
self.style.SUCCESS(f"\tTerm found: {term}")
)
break
except Term.DoesNotExist:
self.stdout.write(
self.style.ERROR(
"\tTerm not found. Did you make a typo?"
)
)
self.stdout.write("\tPlease re-enter term name:")
event_data["term"] = term

organization = None
self.stdout.write(
"\n\tPlease enter the name OR slug of the event's affiliated organization (case insensitive):"
)
while True:
try:
print("\t", end="")
org_name = input()
organization = Organization.objects.get(name__iexact=org_name)
self.stdout.write(
self.style.SUCCESS(
f"\tOrganization found: {organization}"
)
)
break

except Organization.DoesNotExist:
try:
organization = Organization.objects.get(slug__iexact=org_name)
self.stdout.write(
self.style.SUCCESS(
f"\tOrganization found: {organization}"
)
)
break

except Organization.DoesNotExist:
self.stdout.write(
self.style.ERROR(
"\tOrganization not found. Did you make a typo?"
)
)
self.stdout.write("\tPlease re-enter organization name:")
event_data["organization"] = organization

schedule_format = "default"
while True:
self.stdout.write(
f"\n\tChange schedule format from its default value ({event_data["schedule_format"]})? (y/N):"
)
print("\t", end="")

i = input()

if i.lower() == "y":
options = [
"early-dismissal",
"one-hour-lunch",
"early-early-dismissal",
"default",
"half-day",
"late-start",
"sac-election",
]

for i, option in enumerate(options):
self.stdout.write(f"\t{i+1}: {option}")

while True:
self.stdout.write("\tSelect a new format by number:")

print("\t", end="")
num = input()
try:
if int(num) > 0 and int(num) <= len(options):
schedule_format = options[int(num) - 1]
self.stdout.write(
self.style.SUCCESS(
f"\tNew value: {event_data["schedule_format"]}"
)
)
break
else:
raise ValueError

except ValueError:
self.stdout.write(
self.style.ERROR(
"\tInvalid input. Please enter a number within the list."
)
)
break

elif i.lower() == "n" or i.lower() == "":
self.stdout.write(
self.style.SUCCESS(f"\tNo change")
)
break

else:
self.stdout.write(
self.style.ERROR(
"\tInvalid input. Please enter 'y' or 'n' (leave blank for 'n')."
)
)
event_data["schedule_format"] = schedule_format

for s in ["is_instructional", "is_public", "should_announce"]:
boolean = event_data[s]
while True:
self.stdout.write(
f"\n\tChange {s} from its default value ({boolean})? (y/N):"
)
print("\t", end="")

i = input()

if i.lower() == "y":
boolean = not boolean
self.stdout.write(
self.style.SUCCESS(f"\tNew value: {boolean}")
)
break

elif i.lower() == "n" or len(i) == 0:
self.stdout.write(
self.style.SUCCESS(f"\tNo change")
)
break

else:
self.stdout.write(
self.style.ERROR(
"\tInvalid input. Please enter 'y' or 'n' (leave blank for 'n')."
)
)

event_data = {}
event = Event(
name=event_data["name"],
description=event_data["description"],
start_date=event_data["start_date"],
end_date=event_data["end_date"],
term=event_data["term"],
organization=event_data["organization"],
schedule_format=event_data["schedule_format"],
is_instructional=event_data["is_instructional"],
is_public=event_data["is_public"],
should_announce=event_data["should_announce"],
)
event.save()

self.stdout.write(self.style.SUCCESS(f"\nEvent saved: {event}"))

elif is_in_event:
if l.startswith("SUMMARY:"):
Expand All @@ -59,12 +244,21 @@ def handle(self, *args, **options):
elif l.startswith("DESCRIPTION:"):
event_data["description"] = l[len("DESCRIPTION:"):]

elif l.startswith("DTSTART:"):
dtstart = l[len("DTSTART:"):]
event_data["start"] = datetime.strptime(dtstart, "%Y%m%dT%H%M%SZ") # TODO: check if timezone is correct
# TODO: all-day events are stored as DTSTART;VALUE=DATE:%Y%m%d for some weird reason
elif l.startswith("DTSTART"):
if l.startswith("DTSTART;VALUE=DATE:"):
dtstart = l[len("DTSTART;VALUE=DATE:"):]
event_data["start_date"] = datetime.strptime(dtstart, "%Y%m%d") # TODO: fix timezone

elif l.startswith("DTSTART:"):
dtstart = l[len("DTSTART:"):]
event_data["start_date"] = datetime.strptime(dtstart, "%Y%m%dT%H%M%SZ") # TODO: fix timezone

elif l.startswith("DTEND"):
if l.startswith("DTEND;VALUE=DATE:"):
dtend = l[len("DTEND;VALUE=DATE:"):]
event_data["end_date"] = datetime.strptime(dtend, "%Y%m%d") # TODO: fix timezone

elif l.startswith("DTEND:"):
dtend = l[len("DTEND:"):]
event_data["end_date"] = datetime.strptime(dtend, "%Y%m%dT%H%M%SZ") # TODO: fix timezone

elif l.startswith("DTEND:"):
dtend = l[len("DTEND:"):]
event_data["end"] = datetime.strptime(dtend, "%Y%m%dT%H%M%SZ") # TODO: check if timezone is correct
# TODO: all-day events are stored as DTSTART;VALUE=DATE:%Y%m%d for some weird reason

0 comments on commit 859651a

Please sign in to comment.