wulkanowy-web/app/views.py

445 lines
17 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 rest_framework.decorators import api_view
2021-01-23 12:23:24 +01:00
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
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
def login(request, *args, **kwargs):
data = json.loads(request.body)
loginName = data['loginName']
Password = data['Password']
symbol = data['Symbol']
diary_url = data['diaryUrl']
2021-06-08 19:23:31 +02:00
if diary_url != 'http://cufs.fakelog.tk/':
2021-01-23 12:23:24 +01:00
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:
2021-06-08 19:23:31 +02:00
link = 'http://cufs.fakelog.tk/powiatwulkanowy/FS/LS?wa=wsignin1.0&wtrealm=http://uonetplus.fakelog.localhost:300/powiatwulkanowy/LoginEndpoint.aspx&wctx=http://uonetplus.fakelog.localhost:300/powiatwulkanowy/LoginEndpoint.aspx'
2021-01-23 12:23:24 +01:00
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)
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
grades = get_grades(register_id, students, school_url, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(grades)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
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'
timetable = get_timetable(register_id, students, school_url, s, date)
2021-01-23 12:23:24 +01:00
return JsonResponse(timetable)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
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']
exams = get_exams(register_id, students, school_url, s, date, school_year)
2021-01-23 12:23:24 +01:00
return JsonResponse(exams)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
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']
homeworks = get_homeworks(register_id, students, school_url, s, date, school_year)
2021-01-23 12:23:24 +01:00
return JsonResponse(homeworks)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
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'
attendance = get_attendance(register_id, students, school_url, s, date)
2021-01-23 12:23:24 +01:00
return JsonResponse(attendance, safe=False)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
notes = get_notes(register_id, students, school_url, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(notes)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
registered = get_registered_devices(register_id, students, school_url, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(registered)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
register_data = register_device(register_id, students, school_url, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(register_data)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
date = data['data']['date']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
received_messages = get_received_messages(register_id, students, school_url, s, date, school_year, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(received_messages)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
date = data['data']['date']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
sent_messages = get_sent_messages(register_id, students, school_url, s, date, school_year, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(sent_messages)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
date = data['data']['date']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
deleted_messages = get_deleted_messages(register_id, students, school_url, s, date, school_year, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(deleted_messages)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
date = data['data']['date']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
recipients = get_recipients(register_id, students, school_url, s, date, school_year, symbol)
2021-01-23 12:23:24 +01:00
return JsonResponse(recipients)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = data['data']['school_url']
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)
school_data = get_school_data(register_id, students, school_url, s)
2021-01-23 12:23:24 +01:00
return JsonResponse(school_data)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = cookies_data['data']['school_url']
2021-01-23 12:23:24 +01:00
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']}
send = send_message(register_id, students, school_url, s, date, school_year, symbol, send_data)
2021-01-23 12:23:24 +01:00
return JsonResponse(send, safe=False)
else:
return redirect('../')
@api_view(['POST'])
2021-01-23 12:23:24 +01:00
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']
school_url = cookies_data['data']['school_url']
2021-01-23 12:23:24 +01:00
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']
content = get_message_content(register_id, students, school_url, 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('../')
@api_view(['POST'])
2021-01-30 21:09:11 +01:00
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']
school_url = data['data']['school_url']
2021-01-30 21:09:11 +01:00
s = data['data']['s']
key = bytes(request.session[request.session.session_key], 'utf-8')
s = decrypt_cookies(s, key)
data = get_student_data(register_id, students, school_url, 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
@api_view(['POST'])
2021-02-02 22:19:29 +01:00
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']
school_url = data['data']['school_url']
2021-02-02 22:19:29 +01:00
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, school_url, s)
2021-02-02 22:19:29 +01:00
return JsonResponse(partial_stats)
else:
return redirect('../')
@api_view(['POST'])
2021-02-02 22:19:29 +01:00
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']
school_url = data['data']['school_url']
2021-02-02 22:19:29 +01:00
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, school_url, s)
2021-02-02 22:19:29 +01:00
return JsonResponse(year_stats)
else:
return redirect('../')
@api_view(['GET'])
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})