Skip to content

Commit

Permalink
Added end time to events
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko259 committed Jan 2, 2022
1 parent 0115165 commit 1629c30
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
13 changes: 8 additions & 5 deletions cogs/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ class EventsCog(commands.Cog):
DB_URL = 3
DB_DESCRIPTION = 4
DB_START = 5
DB_RECURRING = 6
DB_RECURRING_INTERVAL = 7
DB_RECURRING_END = 8
DB_PUBLISHED = 9
DB_END = 6
DB_RECURRING = 7
DB_RECURRING_INTERVAL = 8
DB_RECURRING_END = 9
DB_PUBLISHED = 10

# Embed parameters
FOOTER = {
Expand Down Expand Up @@ -205,6 +206,7 @@ async def load_data(self):
db_event[self.DB_URL],
db_event[self.DB_DESCRIPTION],
db_event[self.DB_START],
db_event[self.DB_END],
db_event[self.DB_RECURRING],
db_event[self.DB_RECURRING_INTERVAL],
db_event[self.DB_RECURRING_END],
Expand Down Expand Up @@ -322,7 +324,7 @@ async def save_data(self):
mydb = db_connection()
cursor = mydb.cursor()
cursor.execute(
"INSERT INTO events (id, name, img, url, description, start_time, recurring, recurring_interval, recurring_end, published) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE name = VALUES(name), img = VALUES(img), url = VALUES(url), description = VALUES(description), start_time = VALUES(start_time), recurring = VALUES(recurring), recurring_interval = VALUES(recurring_interval), recurring_end = VALUES(recurring_end), published = VALUES(published)",
"INSERT INTO events (id, name, img, url, description, start_time, end_time, recurring, recurring_interval, recurring_end, published) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE name = VALUES(name), img = VALUES(img), url = VALUES(url), description = VALUES(description), start_time = VALUES(start_time), end_time = VALUES(end_time), recurring = VALUES(recurring), recurring_interval = VALUES(recurring_interval), recurring_end = VALUES(recurring_end), published = VALUES(published)",
(
event.id,
event.name,
Expand All @@ -331,6 +333,7 @@ async def save_data(self):
event.desc,

event.start,
event.end,
event.recurring,
event.recurring_interval,
event.recurring_end,
Expand Down
30 changes: 25 additions & 5 deletions cogs/staffing.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,14 @@ async def book(self, ctx, position):
start_formatted = datetime.datetime.strptime(str(start[0]), '%Y-%m-%d %H:%M:%S')
start_time = start_formatted.strftime("%H:%M")

end_formatted = start_formatted + datetime.timedelta(hours=2)
end_time = end_formatted.strftime("%H:%M")
cursor.execute("SELECT end_time FROM events WHERE name = %s", (title[0],))
end = cursor.fetchone()
if end[0] is not None:
end_formatted = datetime.datetime.strptime(str(end[0]), '%Y-%m-%d %H:%M:%S')
end_time = end_formatted.strftime("%H:%M")
else:
end_formatted = start_formatted + datetime.timedelta(hours=2)
end_time = end_formatted.strftime("%H:%M")

#tag = 3

Expand Down Expand Up @@ -422,12 +428,12 @@ async def unbook(self, ctx):
if any(ctx.channel_id in channel for channel in event_channel):
if any(f'<@{usernick}>' in match for match in positions):

cid = re.findall("\d+", str(ctx.author.nick))[0]
cid = re.findall("\d+", str(ctx.author.nick))

cursor.execute("SELECT position FROM positions WHERE user = %s and title = %s", (f'<@{usernick}>', title[0]))
position = cursor.fetchone()

request = await Booking.delete_booking(self, int(cid), str(position[0]))
request = await Booking.delete_booking(self, int(cid[0]), str(position[0]))
if request == 200:
cursor.execute("UPDATE positions SET user = %s WHERE user = %s and title = %s", ("", f'<@{usernick}>', title[0]))
mydb.commit()
Expand Down Expand Up @@ -780,6 +786,20 @@ async def _updatemessage(self, title):
second_section = events[8]
third_section = events[9]

cursor.execute("SELECT start_time FROM events WHERE name = %s", (title,))
start = cursor.fetchone()
start_formatted = datetime.datetime.strptime(str(start[0]), '%Y-%m-%d %H:%M:%S')
start_time = start_formatted.strftime("%H:%M")

cursor.execute("SELECT end_time FROM events WHERE name = %s", (title,))
end = cursor.fetchone()
if end[0] is not None:
end_formatted = datetime.datetime.strptime(str(end[0]), '%Y-%m-%d %H:%M:%S')
end_time = end_formatted.strftime("%H:%M")
else:
end_formatted = start_formatted + datetime.timedelta(hours=2)
end_time = end_formatted.strftime("%H:%M")

type = 'main'
cursor.execute(
"SELECT * FROM positions WHERE title = %s and type = %s", (title, type))
Expand Down Expand Up @@ -816,7 +836,7 @@ async def _updatemessage(self, title):
format_staffing_message += "\n"

formatted_date = date.strftime("%A %d/%m/%Y")
format_staffing_message += f'{title} staffing - {formatted_date}\n\n{description}\n\n{first_section}:\n{main_position_data}\n\n{second_section}:\n{secondary_position_data}\n\n{third_section}:\n{regional_position_data}'
format_staffing_message += f'{title} staffing - {formatted_date} {start_time} - {end_time}z\n\n{description}\n\n{first_section}:\n{main_position_data}\n\n{second_section}:\n{secondary_position_data}\n\n{third_section}:\n{regional_position_data}'

channel = self.bot.get_channel(int(channel_id))
message = await channel.fetch_message(int(message_id))
Expand Down
1 change: 1 addition & 0 deletions db/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE `events` (
`url` varchar(600) NOT NULL,
`description` text NOT NULL,
`start_time` datetime NOT NULL,
`end_time` datetime NOT NULL,
`recurring` varchar(255) DEFAULT NULL,
`recurring_interval` smallint(6) DEFAULT NULL,
`recurring_end` datetime DEFAULT NULL,
Expand Down
12 changes: 6 additions & 6 deletions helpers/booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ async def delete_booking(self, cid: int, position: str):
"""
Delete a booking from the API
"""
booking = requests.get(CC_API_URL + '/bookings', headers={'Authorization': 'Bearer ' + CC_API_TOKEN, 'Accept': 'application/json'})
if booking.status_code == requests.codes.ok:
feedback = booking.json()
for booking in feedback:
if booking["cid"] == cid and booking["callsign"] + ":" == position:
bookings = requests.get(CC_API_URL + '/bookings', headers={'Authorization': 'Bearer ' + CC_API_TOKEN, 'Accept': 'application/json'})
if bookings.status_code == requests.codes.ok:
feedback = bookings.json()
for booking in feedback["data"]:
if int(booking["cid"]) == cid and str(booking["callsign"]) + ":" == position:
request = requests.delete(url=CC_API_URL + "/bookings/" + str(booking["id"]), headers={'Authorization': 'Bearer ' + CC_API_TOKEN, 'Accept': 'application/json'})
if request.status_code == requests.codes.ok:
return 200
else:
return request.status_code
return 404
else:
return booking.status_code
return bookings.status_code

return False
4 changes: 3 additions & 1 deletion helpers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Event():
# ----------------------------------
#

def __init__(self, id, name, img, url, desc, start: datetime, recurring, recurring_interval, recurring_end, published):
def __init__(self, id, name, img, url, desc, start: datetime, end: datetime, recurring, recurring_interval, recurring_end, published):
"""
Create an Event object
"""
Expand All @@ -24,6 +24,7 @@ def __init__(self, id, name, img, url, desc, start: datetime, recurring, recurri
self.desc = desc

self.start = start
self.end = end
self.recurring = recurring
self.recurring_interval = recurring_interval
self.recurring_end = recurring_end
Expand Down Expand Up @@ -134,6 +135,7 @@ async def fetch_api_updates(self):
self.desc = event_description(updated_event.get('description'))

self.start = datetime.strptime(updated_event.get('start'), "%Y-%m-%dT%H:%M:%SZ")
self.end = datetime.strptime(updated_event.get('end'), "%Y-%m-%dT%H:%M:%SZ")

self.hidden = updated_event.get('hidden')

Expand Down

0 comments on commit 1629c30

Please sign in to comment.