Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ async def get_calendar() -> JSONResponse:
JSONResponse: A JSON response containing the calendar data.
"""

events: dict[str, str] = {}
events: list[dict[str, str]] = []

try:
get_future_events_ical: tuple[
get_future_events_ical: list[
cshcalendar.CalendarInfo
] = await cshcalendar.get_future_events()
events = cshcalendar.format_events(get_future_events_ical)
except Exception as e:
logger.error(f"Error fetching calendar events: {e}")
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)

return JSONResponse(events)
return JSONResponse({"data": events})


@router.get("/announcement")
Expand Down
53 changes: 13 additions & 40 deletions src/core/cshcalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,69 +136,42 @@ def repl(match: re.Match[str]) -> str:

return TIME_PATTERN.sub(repl, unformatted_string)


def calendar_to_html(seg_header: str, seg_content: str) -> str:
"""
Formats a header and content into the HTML for the calendar front end

Args:
seg_header (str): The header of the calendar segment
seg_content (str): The content in the calendar segment

Returns:
str:
"""

ret_string: str = (
"""<div class='calendar-event-container-lvl2'><span class='calendar-text-date'> """
+ seg_header
+ """ </span><br>"""
)
ret_string += (
"<span class='calendar-text' id='calendar'>" + seg_content + "</span></div>"
)
return ret_string


def format_events(events: tuple[CalendarInfo]) -> dict[str, str]:
def format_events(events: list[CalendarInfo]) -> list[dict[str, str]]:
"""
Formats a parsed list of CalendarInfos, and returns the HTML required for front end

Args:
events: The list of CalendarInfos to be formatted

Returns:
dict: Returns a dictionary with the "data" key mapping to the HTML data.
list[dict[str, str]]: Returns a dictionary with the "data" key mapping to a list of dictionarys of each event.
"""

current_date: date = datetime.now(ZoneInfo(CALENDAR_TIMEZONE))
final_events: str = "<br>"

if not events:
final_events += BORDER_STRING
return {"data": [{"header": ":(", "content": "No Events on the Calendar"}]}

final_events += calendar_to_html(":(", "No Events on the Calendar")

final_events += BORDER_STRING

return {"data": final_events}
formatted_list: list[dict[str, str]] = []

for event in events:
content_dict: dict[str, str] = {}

event_cur_happening: bool = event.date < current_date
if event_cur_happening:
formatted: str = (
f"Happening in {event.location}!"
if event.location
else "Happening Now!"
)
final_events += calendar_to_html(formatted, event.name)
content_dict["header"] = formatted
content_dict["content"] = str(event.name)
else:
final_events += calendar_to_html(
time_humanizer(current_date, event.date), event.name
)
content_dict["header"] = time_humanizer(current_date, event.date)
content_dict["content"] = str(event.name)

final_events += BORDER_STRING
return {"data": final_events}
formatted_list.append(content_dict)
return formatted_list


async def rebuild_calendar() -> None:
Expand Down Expand Up @@ -261,7 +234,7 @@ async def get_future_events() -> list[CalendarInfo]:
custom object has name, date and the location

Returns:
list: A list of CalendarInfo objects
list[CalendarInfo]: A list of CalendarInfo objects
"""

global \
Expand Down
10 changes: 0 additions & 10 deletions src/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,6 @@ body{
white-space: pre-line;
}

.wikithoughts-page-name-header{
color: var(--panel-header-text-color);
font-family: 'Courier New', Courier, monospace;
font-size: 20px;
text-align: left;
white-space: pre-line;
display: block;
font-style: italic;
}

.wikithoughts-text-body{
color:var(--panel-body-text-color);
font-family: 'Courier New', Courier, monospace;
Expand Down
39 changes: 37 additions & 2 deletions src/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ function setNewPageTheme(newTheme) {
document.body.classList.toggle(newTheme);
}

function createCalendarEvent(headerText, contentText) {
const container = document.createElement("div");
container.className = "calendar-event-container-lvl2";

const headerLabel = document.createElement("span");
headerLabel.className = "calendar-text-date";
headerLabel.textContent = headerText;
container.appendChild(headerLabel);

container.appendChild(document.createElement("br"));

const contentLabel = document.createElement("span");
contentLabel.className = "calendar-text";
contentLabel.textContent = contentText;
container.appendChild(contentLabel);

return container;
}

async function generateCalendar(calData) {

const calendar = document.getElementById('calendar');
calendar.replaceChildren();
calendar.appendChild(document.createElement("br"));

for (const event of calData) {
const newEvent = createCalendarEvent(event.header, event.content);
calendar.appendChild(newEvent);

const bracket = document.createElement("hr");
bracket.className = "calendar-border";
calendar.appendChild(bracket);
}
}

async function longUpdate() {
const date = new Date();
const hour = date.getHours();
Expand Down Expand Up @@ -107,7 +142,7 @@ async function longUpdate() {

const res = await fetch('/api/calendar', { method: 'GET', mode: 'cors' });
const data = await res.json();
$("#calendar").html(data.data);
await generateCalendar(data.data);
} catch (err) {
console.log(err);
}
Expand All @@ -121,7 +156,7 @@ async function mediumUpdate() {
]);
const wikiData = await wikiRes.json();
const announcementData = await announcementRes.json();
$("#wikipageheader").text(wikiData.page)
$("#wikipageheader").text("csh/Wikithoughts - " + wikiData.page)
$("#wikipagetext").text(wikiData.content);
$("#announcement").text(announcementData.content.substring(0, 910));
$("#announcement-stamp").text(announcementData.user + " - " + announcementData.timestamp)
Expand Down
3 changes: 1 addition & 2 deletions src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
<div class="wikithoughts">
<div class="panel panel-primary">
<div class="panel-heading">
<b><span class="wikithoughts-text-header" id="wikilabelheader">csh/Wikithoughts:</span></b>
<b><span class="wikithoughts-page-name-header" id="wikipageheader">Page Name</span></b>
<b><span class="wikithoughts-text-header" id="wikipageheader">csh/Wikithoughts:</span></b>
</div>
<div class="panel-body">
<div class="plug-body">
Expand Down
Loading