Skip to content

Commit 47dd3a7

Browse files
authored
Merge pull request #108 from filips123/updater-and-website-fixes
Updater and website fixes
2 parents 8a5405c + 27d8611 commit 47dd3a7

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

API/gimvicurnik/updaters/base.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,22 @@ def handle_document(self, document: DocumentInfo, span: Span) -> None:
163163
span.set_tag("document.modified", document.modified)
164164
span.set_tag("document.action", "crashed")
165165

166+
# == DOCUMENT EFFECTIVE
167+
168+
# Get the document's effective date using the subclassed method
169+
# This may return none for documents without an effective date
170+
# If this fails, we can't do anything other than to skip the document
171+
effective = self.get_document_effective(document)
172+
166173
# == DOCUMENT RECORD (GET)
167174

168175
# Try to find an existing document record
169-
record = self.retrieve_document(document)
176+
record = self.retrieve_document(document, effective)
170177

171178
# == DOCUMENT PROCESSING
172179

173180
# Get the modified time if it is set, otherwise use the current time
174-
created = document.created or datetime.datetime.utcnow()
181+
created = document.created or datetime.datetime.now(datetime.timezone.utc)
175182
modified = document.modified or created
176183

177184
# Check if the document has changed without downloading it and comparing hashes
@@ -193,8 +200,8 @@ def handle_document(self, document: DocumentInfo, span: Span) -> None:
193200
# If this fails, we can't do anything other than to skip the document
194201
stream, new_hash = self.download_document(document)
195202

196-
# Check if the document hash has changed
197-
if record and record.parsed and record.hash == new_hash:
203+
# Check if the document hash or document URL have changed
204+
if record and record.parsed and record.hash == new_hash and record.url == document.url:
198205
changed = False
199206
else:
200207
action = "updated"
@@ -233,11 +240,6 @@ def handle_document(self, document: DocumentInfo, span: Span) -> None:
233240

234241
return
235242

236-
# Get the document's effective date using the subclassed method
237-
# This may return none for documents without an effective date
238-
# If this fails, we can't do anything other than to skip the document
239-
effective = self.get_document_effective(document)
240-
241243
if parsable:
242244
# If there is no date, we can't do anything other than to skip the document
243245
if not effective:
@@ -320,14 +322,17 @@ def handle_document(self, document: DocumentInfo, span: Span) -> None:
320322
self.logger.info("Skipped because the %s document for %s is already stored", document.type.value, effective)
321323
# fmt: on
322324

323-
def retrieve_document(self, document: DocumentInfo) -> Document | None:
325+
def retrieve_document(self, document: DocumentInfo, effective: datetime.date | None) -> Document | None:
324326
"""Get a document record from the database. May be set by subclasses."""
325327

326-
return (
327-
self.session.query(Document)
328-
.filter(Document.type == document.type, Document.url == document.url)
329-
.first()
330-
)
328+
# Normally, the document URL should match
329+
criterion = Document.url == document.url
330+
331+
if effective:
332+
# If effective date is set, it may also match instead of the URL
333+
criterion |= Document.effective == effective
334+
335+
return self.session.query(Document).filter(Document.type == document.type, criterion).first()
331336

332337
@with_span(op="download")
333338
def download_document(self, document: DocumentInfo) -> tuple[BytesIO, str]:

API/gimvicurnik/updaters/menu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def get_document_effective(self, document: DocumentInfo) -> datetime.date:
9090
# jedilnik-kosilo-YYYY-MM-DD(-popravek).pdf
9191
# jedilnik-malica-YYYY-MM-DD(-popravek).pdf
9292
date = re.search(
93-
r"jedilnik-(?:kosilo|malica)-(\d+)-(\d+)-(\d+)(?:-[\w-]*)?\.(?:pdf|xlsx)", document.url
93+
r"jedilnik-(?:kosilo|malica|K|M)-(\d+)-(\d+)-(\d+)(?:-[\w-]*)?\.(?:pdf|xlsx)", document.url
9494
)
9595

9696
# The specified date is commonly Monday of the effective week

API/gimvicurnik/updaters/timetable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
import typing
66
from collections import defaultdict
7-
from datetime import datetime
7+
from datetime import datetime, timezone
88
from hashlib import sha256
99

1010
import requests
@@ -167,7 +167,7 @@ def _parse(self, document: Document | None, raw_data: str, new_hash: str, span:
167167
created = False
168168

169169
document.type = DocumentType.TIMETABLE
170-
document.modified = datetime.utcnow()
170+
document.modified = datetime.now(timezone.utc)
171171
document.url = self.config.url
172172
document.hash = new_hash
173173
self.session.add(document)

website/src/components/TimetableDisplay.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<script setup lang="ts">
2+
import { useIntervalFn } from '@vueuse/core'
23
import { storeToRefs } from 'pinia'
3-
import { computed } from 'vue'
4+
import { computed, ref } from 'vue'
45
56
import TimetableEmptyClassrooms from '@/components/TimetableEmptyClassrooms.vue'
67
import TimetableLesson from '@/components/TimetableLesson.vue'
78
import { useSessionStore } from '@/stores/session'
89
import { EntityType, useSettingsStore } from '@/stores/settings'
910
import { type MergedLesson, useTimetableStore } from '@/stores/timetable'
10-
import { getCurrentDay } from '@/utils/days'
11+
import { getCurrentDay, getIsWeekend } from '@/utils/days'
1112
import { localizedWeekdays } from '@/utils/localization'
1213
import { getCurrentTime, lessonTimes } from '@/utils/times'
1314
@@ -25,9 +26,15 @@ const { lessons } = storeToRefs(useTimetableStore())
2526
const { showHoursInTimetable, highlightCurrentTime, enableLessonDetails } =
2627
storeToRefs(useSettingsStore())
2728
28-
const isWeekend = [0, 6].includes(new Date().getDay())
29-
const currentDay = getCurrentDay()
30-
const currentTime = getCurrentTime()
29+
const isWeekend = ref(getIsWeekend())
30+
const currentDay = ref(getCurrentDay())
31+
const currentTime = ref(getCurrentTime())
32+
33+
useIntervalFn(() => {
34+
isWeekend.value = getIsWeekend()
35+
currentDay.value = getCurrentDay()
36+
currentTime.value = getCurrentTime()
37+
}, 30000)
3138
3239
/**
3340
* Determines the range of lesson times that need to be displayed in the timetable.
@@ -48,7 +55,7 @@ function lessonStyles(dayIndex: number, timeIndex: number) {
4855
// prettier-ignore
4956
return {
5057
'bg-surface-highlighted': lessons.value[timeIndex][dayIndex]?.find(lesson => lesson.isSubstitution),
51-
'current-time': highlightCurrentTime.value && !isWeekend && dayIndex === currentDay && timeIndex === currentTime,
58+
'current-time': highlightCurrentTime.value && !isWeekend.value && dayIndex === currentDay.value && timeIndex === currentTime.value,
5259
}
5360
}
5461

website/src/utils/days.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export function getCurrentDay(): number {
2222
return currentDay - 1
2323
}
2424

25+
/**
26+
* Returns whether it is currently a weekend.
27+
*/
28+
export function getIsWeekend(): boolean {
29+
return [0, 6].includes(new Date().getDay())
30+
}
31+
2532
/**
2633
* Returns the weekdays of the week of the given date.
2734
*
@@ -54,5 +61,9 @@ export function getWeekdays(date: Date): Date[] {
5461
* Returns the ISO date string of the given date.
5562
*/
5663
export function getISODate(date: Date): string {
64+
// Make sure the date is UTC midnight, otherwise, conversion to ISO may be wrong
65+
date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
66+
67+
// Convert the date into ISO format and return the date part
5768
return date.toISOString().split('T')[0]
5869
}

0 commit comments

Comments
 (0)