diff --git a/app/API/homework.py b/app/API/homework.py deleted file mode 100644 index ed1b88a..0000000 --- a/app/API/homework.py +++ /dev/null @@ -1,90 +0,0 @@ -import requests -import json -import datetime - -def get_homework(register_id, register_r, oun, s, school_year): - if oun != 'http://uonetplus-uczen.fakelog.cf/powiatwulkanowy/123458': - cookies = { - "biezacyRokSzkolny": f"{register_r.json()['data'][0]['DziennikRokSzkolny']}", - "idBiezacyDziennik": f"{register_r.json()['data'][0]['IdDziennik']}", - "idBiezacyDziennikPrzedszkole": f"{register_r.json()['data'][0]['IdPrzedszkoleDziennik']}", - "idBiezacyDziennikWychowankowie": f"{register_r.json()['data'][0]['IdWychowankowieDziennik']}", - "idBiezacyUczen": f"{register_r.json()['data'][0]['IdUczen']}" - } - else: - cookies = { - "biezacyRokSzkolny": f"{register_r.json()['data'][0]['DziennikRokSzkolny']}", - "idBiezacyDziennik": f"{register_r.json()['data'][0]['IdDziennik']}", - "idBiezacyDziennikPrzedszkole": f"{register_r.json()['data'][0]['IdPrzedszkoleDziennik']}", - "idBiezacyUczen": f"{register_r.json()['data'][0]['IdUczen']}" - } - - now = datetime.datetime.now() - weekday = now.weekday() - - for x in range(7): - if weekday == x: - now1 = now - datetime.timedelta(days=x) - now2 = now - datetime.timedelta(days=x) + datetime.timedelta(days=7) - now3 = now - datetime.timedelta(days=x) + datetime.timedelta(days=14) - now4 = now - datetime.timedelta(days=x) + datetime.timedelta(days=21) - - day1 = str(now1.day) - month1 = str(now1.month) - year1 = str(now1.year) - date1 = year1+'-'+month1+'-'+day1 - - day2 = str(now2.day) - month2 = str(now2.month) - year2 = str(now2.year) - date2 = year2+'-'+month2+'-'+day2 - - day3 = str(now3.day) - month3 = str(now3.month) - year3 = str(now3.year) - date3 = year3+'-'+month3+'-'+day3 - - day4 = str(now4.day) - month4 = str(now4.month) - year4 = str(now4.year) - date4 = year4+'-'+month4+'-'+day4 - - headres = { - 'User-Agent': 'Wulkanowy-web :)' - } - - homework1 = s.post(oun+'/Homework.mvc/Get', headers=headres, cookies=cookies, json={'schoolYear': school_year, 'date': date1, 'statusFilter': '-1'}) - homework2 = s.post(oun+'/Homework.mvc/Get', headers=headres, cookies=cookies, json={'schoolYear': school_year, 'date': date2, 'statusFilter': '-1'}) - homework3 = s.post(oun+'/Homework.mvc/Get', headers=headres, cookies=cookies, json={'schoolYear': school_year, 'date': date3, 'statusFilter': '-1'}) - homework4 = s.post(oun+'/Homework.mvc/Get', headers=headres, cookies=cookies, json={'schoolYear': school_year, 'date': date4, 'statusFilter': '-1'}) - - return homework1.json(), homework2.json(), homework3.json(), homework4.json() - -def prepare_homework_for_display(register_id, register_r, oun, s, school_year): - homework = get_homework(register_id, register_r, oun, s, school_year) - - json_homework = {} - i = 0 - a = 0 - x = 0 - - for j in homework: - json_homework.update({x: {}}) - for i in range(5): - json_homework[x].update({i: []}) - if j['data'][i]['Homework'] != []: - while True: - json_homework[x][i].append({'Przedmiot': j['data'][i]['Homework'][a]['Subject'], - 'Nauczyciel': j['data'][i]['Homework'][a]['Teacher'], - 'Opis': j['data'][i]['Homework'][a]['Description'], - 'Data': j['data'][i]['Homework'][a]['Date']}) - if j['data'][i]['Homework'][a] == j['data'][i]['Homework'][-1]: - a = 0 - break - else: - a += 1 - else: - json_homework[x][i].append('Brak zadań domowych na ten dzień') - x += 1 - - return json_homework \ No newline at end of file diff --git a/app/API/homeworks.py b/app/API/homeworks.py new file mode 100644 index 0000000..4ace7b0 --- /dev/null +++ b/app/API/homeworks.py @@ -0,0 +1,31 @@ +import requests +import json + +def get_homeworks(register_id, register_r, oun, s, date, school_year): + cookies = s + if oun != 'http://uonetplus-uczen.fakelog.cf/powiatwulkanowy/123458': + cookies.update({ + "biezacyRokSzkolny": f"{register_r['data'][0]['DziennikRokSzkolny']}", + "idBiezacyDziennik": f"{register_r['data'][0]['IdDziennik']}", + "idBiezacyDziennikPrzedszkole": f"{register_r['data'][0]['IdPrzedszkoleDziennik']}", + "idBiezacyDziennikWychowankowie": f"{register_r['data'][0]['IdWychowankowieDziennik']}", + "idBiezacyUczen": f"{register_r['data'][0]['IdUczen']}" + }) + else: + cookies.update({ + "biezacyRokSzkolny": f"{register_r['data'][0]['DziennikRokSzkolny']}", + "idBiezacyDziennik": f"{register_r['data'][0]['IdDziennik']}", + "idBiezacyDziennikPrzedszkole": f"{register_r['data'][0]['IdPrzedszkoleDziennik']}", + "idBiezacyUczen": f"{register_r['data'][0]['IdUczen']}" + }) + + headres = { + 'Accept-Encoding': 'gzip, deflate', + 'Accept': '*/*', + 'Connection': 'keep-alive', + 'User-Agent': 'Wulkanowy-web :)' + } + + homeworks = requests.post(oun+'/Homework.mvc/Get', headers=headres, cookies=cookies, json={'schoolYear': school_year, 'date': date, 'statusFilter': '-1'}) + + return homeworks.json() \ No newline at end of file diff --git a/app/views.py b/app/views.py index b746b2e..4fb5c72 100644 --- a/app/views.py +++ b/app/views.py @@ -13,6 +13,7 @@ from .API.timetable import get_timetable from .API.notes import prepare_notes_for_display from .API.attendance import prepare_attendance_for_display from .API.messages import get_messages +from .API.homeworks import get_homeworks #views def default_view(request, *args, **kwargs): @@ -82,5 +83,19 @@ def exams(request, *args, **kwargs): school_year = data['data']['school_year'] exams = get_exams(register_id, register_r, oun, s, date, school_year) return JsonResponse(exams) + else: + return render(request, 'index.html') + +def homeworks(request, *args, **kwargs): + if request.session.has_key('is_logged'): + data = json.loads(request.body) + register_id = data['data']['register_id'] + register_r = data['data']['register_r'] + oun = data['data']['oun'] + s = data['data']['s'] + date = data['data']['date'] + school_year = data['data']['school_year'] + homeworks = get_homeworks(register_id, register_r, oun, s, date, school_year) + return JsonResponse(homeworks) else: return render(request, 'index.html') \ No newline at end of file diff --git a/files/js/homeworks.js b/files/js/homeworks.js index eaf2496..b76d244 100644 --- a/files/js/homeworks.js +++ b/files/js/homeworks.js @@ -1,7 +1,22 @@ const homeworks_ = document.querySelector('#homeworks_'); +myStorage = window.sessionStorage; + const getHomeworks = () => { document.querySelector('#content').innerHTML = 'Here is homeworks (in my imagination)'; + cookies_data = sessionStorage.getItem('cookies_data'); + csrfcookie_ = sessionStorage.getItem('csrfcookie'); + fetch(url = '../api/homeworks', { + method: 'POST', + mode: 'cors', + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': csrfcookie_ + }, + body: cookies_data + }).then(response => response.json()).then(data => { + console.log(data); + }) } homeworks_.addEventListener('click', getHomeworks); \ No newline at end of file diff --git a/wulkanowy/urls.py b/wulkanowy/urls.py index 027a11d..3e66261 100644 --- a/wulkanowy/urls.py +++ b/wulkanowy/urls.py @@ -17,7 +17,7 @@ from django.urls import path from django.contrib.staticfiles.urls import staticfiles_urlpatterns from app.views import default_view, content_view -from app.views import login, grades, timetable, exams +from app.views import login, grades, timetable, exams, homeworks urlpatterns = [ #views @@ -27,7 +27,8 @@ urlpatterns = [ path('api/login', login, name='login'), path('api/grades', grades, name='grades'), path('api/timetable', timetable, name='timetable'), - path('api/exams', exams, name='exams') + path('api/exams', exams, name='exams'), + path('api/homeworks', homeworks, name='homeworks'), ] urlpatterns += staticfiles_urlpatterns() \ No newline at end of file