mirror of
https://github.com/Code-For-Groningen/temmies.git
synced 2025-05-16 00:33:47 +02:00
Prettified submission code
This commit is contained in:
parent
2fa3bfbad8
commit
f6e6bc28d2
@ -1,5 +1,7 @@
|
||||
# submission.py
|
||||
|
||||
"""
|
||||
File to define the submission class
|
||||
File to define the Submission class
|
||||
"""
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
@ -7,11 +9,11 @@ from bs4 import BeautifulSoup
|
||||
class Submission:
|
||||
"""
|
||||
Submission class
|
||||
|
||||
|
||||
Methods:
|
||||
test_cases: Get a dict of test cases status
|
||||
info: Submission information (in details)
|
||||
files: Get a list of uploaded files(as names)
|
||||
get_test_cases: Get a dict of test cases status
|
||||
get_info: Submission information (in details)
|
||||
get_files: Get a list of uploaded files (as names)
|
||||
"""
|
||||
def __init__(self, url: str, session):
|
||||
self.url = "https://themis.housing.rug.nl" + url
|
||||
@ -24,23 +26,17 @@ class Submission:
|
||||
"""Clean text"""
|
||||
clean = text.replace("\t", "").replace("\n", "")
|
||||
if value:
|
||||
return clean
|
||||
return clean.replace(" ", "_").replace(":", "").lower()
|
||||
return clean.strip()
|
||||
return clean.replace(" ", "_").replace(":", "").lower().strip()
|
||||
|
||||
def get_test_cases(self) -> dict[str, str]:
|
||||
"""Get a dict of test cases status"""
|
||||
# In the submission page, the test cases are in a div with class "sub-cases subsec round shade"
|
||||
# print(self.__raw.prettify())
|
||||
cases = self.__raw.find("div", class_=lambda x: x and "sub-cases" in x.split())
|
||||
if not cases:
|
||||
return {}
|
||||
|
||||
# The test cases are in a table in a div with class "cfg-container"
|
||||
cases = cases.find("div", class_="cfg-container")
|
||||
cases = cases.find("table")
|
||||
# For each test case, there is a tr with class sub-casetop, which contains 2 tds:
|
||||
# * a td with class "sub-case name" which is a name
|
||||
# * a td with a variable class, which is the status text
|
||||
|
||||
results = {}
|
||||
for entry in cases.find_all("tr", class_="sub-casetop"):
|
||||
@ -48,52 +44,51 @@ class Submission:
|
||||
status = entry.find(
|
||||
"td", class_=lambda x: x and "status-icon" in x.split()
|
||||
).text
|
||||
results[name] = self.__clean(status)
|
||||
results[name.strip()] = self.__clean(status)
|
||||
|
||||
return results
|
||||
|
||||
def get_info(self) -> dict[str, str] | None:
|
||||
"""Submission information (in details)"""
|
||||
# in div with class subsec round shade where there is an h4 with class info
|
||||
# The info is in a div with class "cfg-container"
|
||||
if self.__info:
|
||||
return self.__info
|
||||
|
||||
for div in self.__raw.find_all("div", class_="subsec round shade"):
|
||||
if h4 := div.find("h4", class_=lambda x: x and "info" in x.split()):
|
||||
if "Details" in h4.text:
|
||||
# The information is in divs with class "cfg-line"
|
||||
# With key in span with class "cfg-key" and value in span with class "cfg-value"
|
||||
info = div.find("div", class_="cfg-container")
|
||||
info = info.find_all("div", class_="cfg-line")
|
||||
return {
|
||||
self.__clean(
|
||||
key := line.find("span", class_="cfg-key").text
|
||||
):
|
||||
self.__clean(line.find("span", class_="cfg-val").text, value=True) if "Files" not in key else
|
||||
([(self.__clean(x.text), x["href"]) for x in line.find("span", class_="cfg-val").find_all("a")])
|
||||
for line in info
|
||||
}
|
||||
h4 = div.find("h4", class_=lambda x: x and "info" in x.split())
|
||||
if h4 and "Details" in h4.text:
|
||||
info = div.find("div", class_="cfg-container")
|
||||
info_lines = info.find_all("div", class_="cfg-line")
|
||||
self.__info = {
|
||||
self.__clean(
|
||||
key := line.find("span", class_="cfg-key").text
|
||||
): (
|
||||
self.__clean(line.find("span", class_="cfg-val").text, value=True)
|
||||
if "Files" not in key
|
||||
else [
|
||||
(self.__clean(a.text), a["href"])
|
||||
for a in line.find("span", class_="cfg-val").find_all("a")
|
||||
]
|
||||
)
|
||||
for line in info_lines
|
||||
}
|
||||
return self.__info
|
||||
return None
|
||||
|
||||
def get_files(self) -> list[str] | None:
|
||||
"""Get a list of uploaded files in the format [(name, url)]"""
|
||||
if not self.__info:
|
||||
self.__info = self.get_info()
|
||||
|
||||
return self.__info.get("files", None)
|
||||
|
||||
# TODO: Remove deprecated methods
|
||||
# Deprecated methods
|
||||
def info(self):
|
||||
print("This method is deprecated and will be deleted in v1.1.3. Use get_info instead.")
|
||||
return self.get_info()
|
||||
|
||||
|
||||
def test_cases(self):
|
||||
print("This method is deprecated and will be deleted in v1.1.3. Use get_test_cases instead.")
|
||||
return self.get_test_cases()
|
||||
|
||||
|
||||
def files(self):
|
||||
print("This method is deprecated and will be deleted in v1.1.3. Use get_files instead.")
|
||||
return self.get_files()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user