wulkanowy-web/app/views.py

423 lines
16 KiB
Python
Raw Normal View History

2021-01-23 12:23:24 +01:00
from requests import get
from cryptography.fernet import Fernet
from django.contrib.sessions.backends.db import SessionStore
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
import json
import requests
from django.core import serializers
from django.shortcuts import redirect
from django.contrib.sessions.models import Session
from .login import sender
from .API.grades import get_grades
from .API.exams import get_exams
from .API.timetable import get_timetable
from .API.notes import get_notes
from .API.attendance import get_attendance
from .API.messages import get_received_messages, get_sent_messages, get_deleted_messages, get_recipients, send_message, get_message_content
from .API.homeworks import get_homeworks
from .API.mobile_access import get_registered_devices, register_device
from .API.school_data import get_school_data
from .API.dashboard import get_dashboard
2021-01-30 21:09:11 +01:00
from .API.student_data import get_student_data
2021-02-02 22:19:29 +01:00
from .API.stats import get_partial, get_year
2021-01-23 12:23:24 +01:00
from .decrypt import decrypt_cookies
2021-02-01 23:41:18 +01:00
import datetime
2021-01-23 12:23:24 +01:00
#API
def login(request, *args, **kwargs):
data = json.loads(request.body)
loginName = data['loginName']
Password = data['Password']
symbol = data['Symbol']
diary_url = data['diaryUrl']
if diary_url != 'http://cufs.fakelog.cf/':
link = f'{diary_url}{symbol}/Account/LogOn?ReturnUrl=%2F{symbol}%2FFS%2FLS%3Fwa%3Dwsignin1.0%26wtrealm%3Dhttps%253a%252f%252fuonetplus.vulcan.net.pl%252f{symbol}%252fLoginEndpoint.aspx%26wctx%3Dhttps%253a%252f%252fuonetplus.vulcan.net.pl%252f{symbol}%252fLoginEndpoint.aspx'
else:
link = 'http://cufs.fakelog.cf/powiatwulkanowy/FS/LS?wa=wsignin1.0&wtrealm=http://uonetplus.fakelog.localhost:300/powiatwulkanowy/LoginEndpoint.aspx&wctx=http://uonetplus.fakelog.localhost:300/powiatwulkanowy/LoginEndpoint.aspx'
s = requests.Session()
sender_return = sender(link, loginName, Password, ('loginName', 'Password'), 'Zła nazwa użytkownika lub hasło', symbol, diary_url, s)
if sender_return == {'success': False}:
data_response = {
'success': False
}
else:
2021-01-28 11:06:57 +01:00
request.session['is_logged'] = True
2021-01-29 23:30:36 +01:00
request.session[request.session.session_key] = Fernet.generate_key().decode('utf-8')
rkey = Fernet(bytes(request.session[request.session.session_key], 'utf-8'))
2021-01-28 12:28:16 +01:00
2021-01-23 12:23:24 +01:00
sender_return['s'] = json.dumps(sender_return['s'])
sender_return['s'] = sender_return['s'].encode()
sender_return['s'] = rkey.encrypt(sender_return['s'])
sender_return['s'] = sender_return['s'].decode('utf-8')
data_response = {'success': True, 'data': sender_return}
return JsonResponse(data_response)
def grades(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-01-31 22:23:03 +01:00
grades = get_grades(register_id, students, oun, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(grades)
else:
return redirect('../')
def timetable(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
2021-02-01 23:41:18 +01:00
week = data['week']
data = json.loads(data['cookies'])
2021-01-23 12:23:24 +01:00
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-02-01 23:41:18 +01:00
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'
2021-01-31 22:23:03 +01:00
timetable = get_timetable(register_id, students, oun, s, date)
2021-01-23 12:23:24 +01:00
return JsonResponse(timetable)
else:
return redirect('../')
def exams(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
2021-02-02 11:10:24 +01:00
week = data['week']
data = json.loads(data['cookies'])
2021-01-23 12:23:24 +01:00
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-02-02 11:10:24 +01:00
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'
2021-01-23 12:23:24 +01:00
school_year = data['data']['school_year']
2021-01-31 22:23:03 +01:00
exams = get_exams(register_id, students, oun, s, date, school_year)
2021-01-23 12:23:24 +01:00
return JsonResponse(exams)
else:
return redirect('../')
def homeworks(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
2021-02-02 11:41:38 +01:00
week = data['week']
data = json.loads(data['cookies'])
2021-01-23 12:23:24 +01:00
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-02-02 11:41:38 +01:00
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'
2021-01-23 12:23:24 +01:00
school_year = data['data']['school_year']
2021-01-31 22:23:03 +01:00
homeworks = get_homeworks(register_id, students, oun, s, date, school_year)
2021-01-23 12:23:24 +01:00
return JsonResponse(homeworks)
else:
return redirect('../')
def attendance(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
2021-02-02 12:03:42 +01:00
week = data['week']
data = json.loads(data['cookies'])
2021-01-23 12:23:24 +01:00
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-02-02 12:03:42 +01:00
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'
2021-01-31 22:23:03 +01:00
attendance = get_attendance(register_id, students, oun, s, date)
2021-01-23 12:23:24 +01:00
return JsonResponse(attendance, safe=False)
else:
return redirect('../')
def notes(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-01-31 22:23:03 +01:00
notes = get_notes(register_id, students, oun, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(notes)
else:
return redirect('../')
def registered_devices(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-01-31 22:23:03 +01:00
registered = get_registered_devices(register_id, students, oun, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(registered)
else:
return redirect('../')
def register_device_(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-01-31 22:23:03 +01:00
register_data = register_device(register_id, students, oun, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(register_data)
else:
return redirect('../')
def received_messages(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
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']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
2021-01-31 22:23:03 +01:00
received_messages = get_received_messages(register_id, students, oun, s, date, school_year, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(received_messages)
else:
return redirect('../')
def sent_messages(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
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']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
2021-01-31 22:23:03 +01:00
sent_messages = get_sent_messages(register_id, students, oun, s, date, school_year, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(sent_messages)
else:
return redirect('../')
def deleted_messages(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
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']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
2021-01-31 22:23:03 +01:00
deleted_messages = get_deleted_messages(register_id, students, oun, s, date, school_year, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(deleted_messages)
else:
return redirect('../')
def recipients(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
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']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
2021-01-31 22:23:03 +01:00
recipients = get_recipients(register_id, students, oun, s, date, school_year, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(recipients)
else:
return redirect('../')
def school_data(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-01-31 22:23:03 +01:00
school_data = get_school_data(register_id, students, oun, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(school_data)
else:
return redirect('../')
def dashboard(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-23 12:23:24 +01:00
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
diary_url = data['data']['diary_url']
symbol = data['data']['symbol']
2021-01-31 22:23:03 +01:00
dashboard = get_dashboard(register_id, students, s, diary_url, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(dashboard)
else:
return redirect('../')
def send(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
cookies_data = json.loads(data['cookies_data'])
register_id = cookies_data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = cookies_data['data']['students']
2021-01-23 12:23:24 +01:00
oun = cookies_data['data']['oun']
s = cookies_data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
date = cookies_data['data']['date']
school_year = cookies_data['data']['school_year']
symbol = cookies_data['data']['symbol']
send_data = {'data': data['data'], 'subject': data['subject'], 'content': data['content']}
2021-01-31 22:23:03 +01:00
send = send_message(register_id, students, oun, s, date, school_year, symbol, send_data)
2021-01-23 12:23:24 +01:00
return JsonResponse(send, safe=False)
else:
return redirect('../')
def message_content(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
cookies_data = json.loads(data['cookies_data'])
register_id = cookies_data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = cookies_data['data']['students']
2021-01-23 12:23:24 +01:00
oun = cookies_data['data']['oun']
s = cookies_data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
date = cookies_data['data']['date']
school_year = cookies_data['data']['school_year']
symbol = cookies_data['data']['symbol']
message_id = data['message_id']
2021-01-31 22:23:03 +01:00
content = get_message_content(register_id, students, oun, s, date, school_year, symbol, message_id)
2021-01-23 12:23:24 +01:00
return JsonResponse(content, safe=False)
2021-01-30 21:09:11 +01:00
else:
return redirect('../')
def student_data(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
register_id = data['data']['register_id']
2021-01-31 22:23:03 +01:00
students = data['data']['students']
2021-01-30 21:09:11 +01:00
oun = data['data']['oun']
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
2021-01-31 22:23:03 +01:00
data = get_student_data(register_id, students, oun, s)
2021-01-30 21:09:11 +01:00
return JsonResponse(data)
2021-01-30 22:09:37 +01:00
else:
return redirect('../')
2021-02-02 22:19:29 +01:00
#STATS
def partial(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
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)
partial_stats = get_partial(register_id, students, oun, s)
return JsonResponse(partial_stats)
else:
return redirect('../')
def year(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
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)
year_stats = get_year(register_id, students, oun, s)
return JsonResponse(year_stats)
else:
return redirect('../')
2021-01-30 22:09:37 +01:00
def log_out(request, *args, **kwargs):
2021-01-30 22:20:51 +01:00
del request.session[request.session.session_key]
del request.session['is_logged']
return JsonResponse({'logOut': True})