First Commit

This commit is contained in:
2023-09-12 22:52:05 +02:00
commit 1cf7f82eda
3 changed files with 97 additions and 0 deletions

5
pyvenv.cfg Normal file
View File

@@ -0,0 +1,5 @@
home = /usr/lib/python-exec/python3.11
include-system-site-packages = false
version = 3.11.4
executable = /usr/bin/python3.11
command = /usr/lib/python-exec/python3.11/python -m venv /stuffs/src/helloasso

75
serv.py Executable file
View File

@@ -0,0 +1,75 @@
#!/bin/env python3
from flask import Flask, make_response, render_template,request,jsonify
from flask_sock import Sock
app = Flask(__name__)
sock = Sock(app)
class Payement:
amount: float
name : str
message : str
def __init__(self,amount,name,message) -> None:
self.amount = amount
self.name = name
self.message = message
def save():
return
def __repr__(self) -> str:
return '{} - {}€- {}'.format(self.name,self.amount/100,self.message)
class Client:
def __init__(self,sock) -> None:
self.sock = sock
def send_event(self, data):
print(self.sock)
self.sock.send(data)
clients_list = []
payements_list : list[Payement] = []
@app.route('/')
def index():
return render_template('index.html')
@app.route('/last')
def last():
print(len(clients_list))
if(len(payements_list) > 0):
p = payements_list[0]
for c in clients_list:
try:
c.send_event(repr(p))
except:
clients_list.remove(c)
return make_response('last',200)
@sock.route('/notify')
def notify(sock):
clients_list.append(Client(sock))
while True:
data = sock.receive()
sock.send(data)
@app.route('/notifications',methods=['POST'])
def notifications():
if request.json is not None:
print(request.json)
if request.json['eventType'] == 'Order':
p = Payement(request.json['data']['amount']['total'],
request.json['data']['items'][0]['customFields'][0]['answer'],
request.json['data']['payer']['firstName'])
payements_list.append(p)
for c in clients_list:
try:
c.send_event(repr(p))
except:
clients_list.remove(c)
return make_response('OK',200)
return make_response('Not Handled',400)

17
templates/index.html Normal file
View File

@@ -0,0 +1,17 @@
<html>
<head>
<title>Hello Asso</title>
</head>
<body>
<div id="p">TEST</div>
<script>
const websocket = new WebSocket('ws://'+ location.host + '/notify');
websocket.addEventListener('message', ev => {
console.log(ev.data);
let div = document.createTextNode(ev.data);
document.body.append(div);
});
</script>
</body>
</html>