Merge pull request #9 from wulkanowy/feature/sending_messages

Sending messages
This commit is contained in:
Marcin Kowalicki 2021-01-18 23:12:56 +00:00 committed by GitHub
commit 3b964815d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 233 additions and 8 deletions

View file

@ -2,6 +2,7 @@ import requests
import json
import calendar
import time
import re
def get_received_messages(register_id, register_r, oun, s, date, school_year, symbol):
headers = {
@ -53,3 +54,105 @@ def get_deleted_messages(register_id, register_r, oun, s, date, school_year, sym
deleted_messages = requests.get(f'https://uonetplus-uzytkownik.vulcan.net.pl/{symbol}/Wiadomosc.mvc/GetOutboxMessages?_dc={now}&dataOd=&dataDo=&page=1&start=0&limit=25', headers=headers, cookies=s)
return deleted_messages.json()
def get_recipients(register_id, register_r, oun, s, date, school_year, symbol):
headers = {
'Accept-Encoding': 'gzip, deflate, br',
'Accept': '*/*',
'Connection': 'keep-alive',
"User-Agent": "Wulkanowy-web :)"
}
if oun == 'http://uonetplus-uczen.fakelog.cf/powiatwulkanowy/123458':
link = f'http://uonetplus-uzytkownik.fakelog.cf/{symbol}'
else:
link = f'https://uonetplus-uzytkownik.vulcan.net.pl/{symbol}'
get_jednostki = requests.get(f'{link}/NowaWiadomosc.mvc/GetJednostkiUzytkownika', headers=headers, cookies=s)
id_jednostka = get_jednostki.json()['data'][0]['IdJednostkaSprawozdawcza']
data = {
"paramsVo":{"IdJednostkaSprawozdawcza":id_jednostka, 'Rola': 2}
}
get_addressee = requests.post(f'{link}/Adresaci.mvc/GetAddressee', headers=headers, cookies=s, json=data)
return {'addressee': get_addressee.json(), 'unitId': id_jednostka}
def send_message(register_id, register_r, oun, s, date, school_year, symbol, send_data):
headers = {
'Accept-Encoding': 'gzip, deflate, br',
'Accet': '*/*',
'Connection': 'keep-alive',
"User-Agent": "Wulkanowy-web :)",
'Content-Type': 'application/json',
'TE': "Trailers"
}
if oun == 'http://uonetplus-uczen.fakelog.cf/powiatwulkanowy/123458':
link = f'http://uonetplus-uzytkownik.fakelog.cf/{symbol}'
else:
link = f'https://uonetplus-uzytkownik.vulcan.net.pl/{symbol}'
student_data = register_r['data'][0]['UczenNazwisko']+' '+register_r['data'][0]['UczenImie']
sess = requests.Session()
sess.cookies.update(s)
sess.headers.update(headers)
index = sess.get(link)
antiForgeryToken = re.search("antiForgeryToken: '(.)*'", index.text)
antiForgeryToken = antiForgeryToken.group()
antiForgeryToken = antiForgeryToken.replace('antiForgeryToken: ', '').replace("'", "")
appGuid = re.search("appGuid: '(.)*'", index.text)
appGuid = appGuid.group()
appGuid = appGuid.replace('appGuid: ', '').replace("'", "")
sess.headers.update({
'X-V-RequestVerificationToken': antiForgeryToken,
'X-V-AppGuid': appGuid
})
payload = {
"incomming": {
"Id": 0,
"Nieprzeczytane": 0,
"Przeczytane": 0,
"Nieprzeczytana": False,
"FolderWiadomosci": 0,
"WiadomoscPowitalna": False,
"Data": None,
"Tresc": send_data['content'],
"Temat": send_data['subject'],
"IdWiadomosci": 0,
"HasZalaczniki": False,
"Zalaczniki": "",
"Adresaci": [
{
"Id": send_data['data']['Id'],
"IdReceiver": "",
"Name": send_data['data']['Name'],
"Role": send_data['data']['Role'],
"UnitId": send_data['data']['UnitId'],
"IdLogin": send_data['data']['IdLogin'],
"PushWiadomosc": False,
"Hash": send_data['data']['Hash'],
"Date": None,
"IsMarked": False
}
],
"WyslijJako": student_data,
"WiadomoscAdresatLogin": "",
"IdWiadomoscAdresatLogin": None,
"RolaNadawcy": 0,
"NieprzeczytanePrzeczytane": "0/0",
"NadawcaNazwa": "Brak nadawcy",
"IdNadawca": 0,
"AdresaciNazwa": "Brak adresata"
}
}
send = sess.post(f'{link}/NowaWiadomosc.mvc/InsertWiadomosc', data=json.dumps(payload))
return send.json()

View file

@ -12,7 +12,7 @@ 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
from .API.messages import get_received_messages, get_sent_messages, get_deleted_messages, get_recipients, send_message
from .API.homeworks import get_homeworks
from .API.mobile_access import get_registered_devices, register_device
from .API.school_data import get_school_data
@ -197,6 +197,21 @@ def deleted_messages(request, *args, **kwargs):
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']
register_r = data['data']['register_r']
oun = data['data']['oun']
s = data['data']['s']
date = data['data']['date']
school_year = data['data']['school_year']
symbol = data['data']['symbol']
recipients = get_recipients(register_id, register_r, oun, s, date, school_year, symbol)
return JsonResponse(recipients)
else:
return redirect('../')
def school_data(request, *args, **kwargs):
if request.session.has_key('is_logged'):
data = json.loads(request.body)
@ -220,4 +235,21 @@ def dashboard(request, *args, **kwargs):
dashboard = get_dashboard(register_id, register_r, s, diary_url, symbol)
return JsonResponse(dashboard)
else:
return redirect('../')
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']
register_r = cookies_data['data']['register_r']
oun = cookies_data['data']['oun']
s = cookies_data['data']['s']
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, register_r, oun, s, date, school_year, symbol, send_data)
return JsonResponse(send, safe=False)
else:
return redirect('../')

View file

@ -1,7 +1,9 @@
const received_ = document.querySelector('#received_');
const sent_ = document.querySelector('#sent_');
const deleted_ = document.querySelector('#deleted_');
const content = document.getElementById("content")
const content = document.getElementById("content");
const send_ = document.querySelector('#send_')
var hash = require('object-hash');
const getReceivedMessages = () => {
content.innerHTML = ""
@ -156,7 +158,74 @@ const getDeletedMessages = () => {
})
}
const getAddressee = () => {
cookies_data = sessionStorage.getItem('cookies_data');
csrfcookie_ = sessionStorage.getItem('csrfcookie');
fetch(url = '../api/messages/recipients', {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfcookie_
},
body: cookies_data
}).then(response => response.json()).then(data => {
console.log(data);
document.querySelector('#content').innerHTML = `
<form action="javascript:void(0);">
<div class="input-field">
<select id='recipients' style='display: inline !important;'>
</select>
</div>
<div class="input-field">
<input required="" placeholder="Temat" id="subject" class="input-field">
</div>
<div class="input-field">
<input required="" placeholder="Treść" id="message-content" class="input-field">
</div>
<button id="button" class="waves-light waves-effect btn red darken-1">WYŚLIJ</button>
</form>`
data.addressee.data.forEach((recipient) => {
const recipient_hash = `${hash.MD5(recipient)}`
document.querySelector('#recipients').insertAdjacentHTML('beforeend', '<option value="'+recipient_hash+'">'+recipient.Name+'</option>');
sessionStorage.setItem(recipient_hash, JSON.stringify(recipient));
const button_ = document.querySelector('#button');
button_.addEventListener('click', sendMessage);
})
})
}
const sendMessage = () => {
const recipient_hash_ = document.querySelector('#recipients').value;
const subject_ = document.querySelector('#subject').value;
const content_ = document.querySelector('#message-content').value;
const recipient_ = JSON.parse(sessionStorage.getItem(recipient_hash_));
if(subject_ != '' && content_ != ''){
cookies_data = sessionStorage.getItem('cookies_data');
csrfcookie_ = sessionStorage.getItem('csrfcookie');
send_data = {
'cookies_data': cookies_data,
'data': recipient_,
'subject': subject_,
'content': content_
}
fetch(url = '../api/messages/send', {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfcookie_
},
body: JSON.stringify(send_data)
}).then(response => response.json()).then(data => {
console.log(data)
})
}
}
received_.addEventListener('click', getReceivedMessages);
sent_.addEventListener('click', getSentMessages);
deleted_.addEventListener('click', getDeletedMessages);
send_.addEventListener('click', getAddressee);

19
package-lock.json generated
View file

@ -1939,6 +1939,11 @@
}
}
},
"md5-jkmyers": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/md5-jkmyers/-/md5-jkmyers-0.0.1.tgz",
"integrity": "sha1-t9PfKAe5HV7H8zc7V/JNYLD6krs="
},
"merge-stream": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
@ -2198,6 +2203,15 @@
"integrity": "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==",
"dev": true
},
"reverse-md5": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/reverse-md5/-/reverse-md5-0.0.5.tgz",
"integrity": "sha512-nSzYU/we3UHObW0rHdf/680AxlAuaVH9fmV3l/1/fnyzbphPPpTFZ684g4wQ2Ru+2lCOdmu8rgWJTTmg/JoXfA==",
"requires": {
"md5-jkmyers": "^0.0.1",
"xtend": "^4.0.0"
}
},
"safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
@ -2480,6 +2494,11 @@
"isexe": "^2.0.0"
}
},
"xtend": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="
},
"yocto-queue": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",

View file

@ -26,7 +26,8 @@
},
"homepage": "https://github.com/wulkanowy/wulkanowy-web#readme",
"dependencies": {
"object-hash": "^2.1.1"
"object-hash": "^2.1.1",
"reverse-md5": "0.0.5"
},
"devDependencies": {
"@babel/core": "^7.12.10",

View file

@ -14,7 +14,7 @@
<script src="{% static 'js/notes.js' %}" type="text/javascript" defer></script>
<script src="{% static 'js/attendance.js' %}" type="text/javascript" defer></script>
<script src="{% static 'js/homeworks.js' %}" type="text/javascript" defer></script>
<script src="{% static 'js/messages.js' %}" type="text/javascript" defer></script>
<script src="{% static 'js/dist/out-messages.js' %}" type="text/javascript" defer></script>
<script src="{% static 'js/mobile_access.js' %}" type="text/javascript" defer></script>
<script src="{% static 'js/school_data.js' %}" type="text/javascript" defer></script>
<link rel="shortcut icon" href="{% static 'images/27146352.png' %}">
@ -59,14 +59,13 @@
<ul id='dropdown1' class='dropdown-content'>
<li><a id='received_' class='option'>Odebrane</a></li>
<li><a id='sent_' class='option'>Wysłane</a></li>
<li><a href="#!" class='option'>Napisz Wiadomość</a></li>
<li><a id='send_' class='option'>Napisz Wiadomość</a></li>
<li><a id='deleted_' class='option'>Usunięte</a></li>
</ul>
</ul>
<a href="#" data-target="slide-out" class="sidenav-trigger"><i class="material-icons">menu</i></a>
<div id="content">
</div>
</div>
<script>

View file

@ -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, homeworks, attendance, notes, registered_devices, register_device_, received_messages, sent_messages, deleted_messages, school_data, dashboard
from app.views import login, grades, timetable, exams, homeworks, attendance, notes, registered_devices, register_device_, received_messages, sent_messages, deleted_messages, school_data, dashboard, recipients, send
urlpatterns = [
#views
@ -40,6 +40,8 @@ urlpatterns = [
path('api/messages/received', received_messages, name='received_messages'),
path('api/messages/sent', sent_messages, name='sent_messages'),
path('api/messages/deleted', deleted_messages, name='deleted_messages'),
path('api/messages/recipients', recipients, name='recipients'),
path('api/messages/send', send, name='send_message'),
]
urlpatterns += staticfiles_urlpatterns()