From 30d0db93d6f4a32a6f49396bc93984dba36c74b0 Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Sun, 10 Feb 2019 13:06:09 -0500 Subject: [PATCH] fix reservation deletion --- penn/wharton.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/penn/wharton.py b/penn/wharton.py index d1bd91f..412bdba 100644 --- a/penn/wharton.py +++ b/penn/wharton.py @@ -1,5 +1,4 @@ import requests -import datetime from bs4 import BeautifulSoup from .base import APIError @@ -47,11 +46,11 @@ def get_reservations(self, sessionid): def delete_booking(self, sessionid, booking_id): """Returns a list of location IDs and names.""" - url = "{}{}{}".format(BASE_URL, "/delete/", booking_id) + url = "{}{}{}/".format(BASE_URL, "/delete/", booking_id) cookies = dict(sessionid=sessionid) - + try: - resp = requests.get(url, cookies=cookies) + resp = requests.get(url, cookies=cookies, headers={'Referer': '{}{}'.format(BASE_URL, "/reservations/")}) except resp.exceptions.HTTPError as error: raise APIError("Server Error: {}".format(error)) @@ -62,26 +61,19 @@ def delete_booking(self, sessionid, booking_id): if "https://weblogin.pennkey.upenn.edu" in html: raise APIError("Wharton Auth Failed. Session ID is not valid.") + resp.raise_for_status() + soup = BeautifulSoup(html, "html5lib") middleware_token = soup.find("input", {'name': "csrfmiddlewaretoken"}).get('value') csrftoken = resp.cookies['csrftoken'] cookies2 = {'sessionid': sessionid, 'csrftoken': csrftoken} - - reservationsUrl = "{}{}".format(BASE_URL, "/reservations") - headers = {'Referer': reservationsUrl} - + headers = {'Referer': url} payload = {'csrfmiddlewaretoken': middleware_token} - print(csrftoken) - print(middleware_token) - print(headers) - print(payload) try: - resp2 = requests.post(url, cookies=cookies2, data=payload, headers = headers) - except resp.exceptions.HTTPError as error: + resp2 = requests.post(url, cookies=cookies2, data=payload, headers=headers) + except resp2.exceptions.HTTPError as error: raise APIError("Server Error: {}".format(error)) - print(resp2.content.decode("utf8")) - return "success"