From e9292e3f6a93194862a2ba9e95c4673526b0a3ba Mon Sep 17 00:00:00 2001 From: Tomasz Date: Mon, 1 Feb 2021 23:41:18 +0100 Subject: [PATCH] Timetable --- app/views.py | 20 +++++++++++++++++++- frontend/static/frontend/js/timetable.js | 22 +++++++++++++++++----- tests/test_views.py | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/views.py b/app/views.py index 053c498..7f539ea 100644 --- a/app/views.py +++ b/app/views.py @@ -21,6 +21,7 @@ from .API.school_data import get_school_data from .API.dashboard import get_dashboard from .API.student_data import get_student_data from .decrypt import decrypt_cookies +import datetime #API def login(request, *args, **kwargs): @@ -68,13 +69,30 @@ def grades(request, *args, **kwargs): def timetable(request, *args, **kwargs): if request.session.has_key('is_logged'): data = json.loads(request.body) + week = data['week'] + data = json.loads(data['cookies']) register_id = data['data']['register_id'] students = data['data']['students'] oun = data['data']['oun'] s = data['data']['s'] key = bytes(request.session[request.session.session_key], 'utf-8') s = decrypt_cookies(s, key) - date = data['data']['date'] + now = datetime.datetime.now() + weekday = now.weekday() + + for x in range(7): + if weekday == x: + now = now - datetime.timedelta(days=x) + + now = now + datetime.timedelta(days=week*7) + + day = now.day + month = now.month + year = now.year + + date = datetime.date(year, month, day).isoformat() + + date = f'{date}T00:00:00' timetable = get_timetable(register_id, students, oun, s, date) return JsonResponse(timetable) else: diff --git a/frontend/static/frontend/js/timetable.js b/frontend/static/frontend/js/timetable.js index da0b6a9..3747689 100644 --- a/frontend/static/frontend/js/timetable.js +++ b/frontend/static/frontend/js/timetable.js @@ -1,11 +1,23 @@ const timetable_ = document.querySelector('#timetable_'); myStorage = window.sessionStorage; +week = 0 -const getTimetable = () => { - document.querySelector('#content').innerHTML = 'Here is timetable (in my imagination)'; - document.querySelector('#content').innerHTML += ''; - document.querySelector('#content').innerHTML += ''; +const getTimetable = (event) => { + if (event.target.id == 'previous') { + week -= 1 + } + else if (event.target.id == 'next') { + week += 1 + } + else { + week = week + } + document.querySelector('#content').innerHTML = 'Here is timetable (in my imagination)'; + const left_ = document.querySelector('#previous'); + const right_ = document.querySelector('#next'); + left_.addEventListener('click', getTimetable); + right_.addEventListener('click', getTimetable); cookies_data = sessionStorage.getItem('cookies_data'); csrfcookie_ = sessionStorage.getItem('csrfcookie'); fetch(url = '../api/timetable', { @@ -15,7 +27,7 @@ const getTimetable = () => { 'Content-Type': 'application/json', 'X-CSRFToken': csrfcookie_ }, - body: cookies_data + body: JSON.stringify({"cookies": cookies_data, "week": week}) }).then(response => response.json()).then(data => { console.log(data) }) diff --git a/tests/test_views.py b/tests/test_views.py index b4b2a36..43a8828 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -57,7 +57,7 @@ def get_data_test(client, cookies_data, assertEquals): assertEquals(response.status_code, 200) #TIMETABLE - response = client.post(reverse('timetable'), content_type='application/xml', data=json.dumps(cookies_data)) + response = client.post(reverse('timetable'), content_type='application/xml', data=json.dumps({'cookies': json.dumps(cookies_data), 'week': 0})) assertEquals(response.status_code, 200) #EXAMS