Files
helloasso/api/classes.py

100 lines
2.7 KiB
Python
Raw Permalink Normal View History

2023-09-13 00:14:14 +02:00
from sqlite3 import Connection
2023-09-14 00:30:02 +02:00
import json
from flask.json.provider import DefaultJSONProvider
2023-09-13 00:14:14 +02:00
2023-09-14 00:30:02 +02:00
class CustomJSONProvider(DefaultJSONProvider):
@staticmethod
def default(obj) -> dict :
if isinstance(obj,Payment):
return obj.to_json()
2023-11-18 14:30:34 +01:00
if isinstance(obj,Donator):
return obj.to_json()
2023-09-14 00:30:02 +02:00
return DefaultJSONProvider.default(obj)
2023-11-18 14:30:34 +01:00
class Donator(json.JSONEncoder):
name : str
amount : float
@staticmethod
def top_donator(conn: Connection):
cur = conn.cursor()
cur.execute("""select name, sum(amount) from orders
2024-01-21 12:05:02 +01:00
group by name
order by sum(amount) desc
2023-11-18 14:30:34 +01:00
LIMIT 5
""")
data = cur.fetchall();
print(data);
if data is not None :
return [Donator(p[0],p[1]) for p in data]
else:
return None
def __init__(self,name,amount) -> None:
self.name = name
self.amount = float(amount/100)
def to_json(self) -> dict :
return { "name" : self.name,
"amount": self.amount
}
2023-09-14 00:30:02 +02:00
class Payment(json.JSONEncoder):
2023-09-13 00:14:14 +02:00
id : int
2023-11-19 13:16:17 +01:00
id_hello : int
2023-09-13 00:14:14 +02:00
amount: float
name : str
message : str
def __init__(self,id,amount,message,name) -> None:
2023-11-19 13:16:17 +01:00
self.id_hello = id
2023-09-13 00:14:14 +02:00
self.amount = amount
self.name = name
self.message = message
def save(self, conn : Connection):
try:
conn.cursor()
2023-11-19 13:16:17 +01:00
conn.execute("insert into orders (id_hello,amount,message,name) values (:id_hello,:amount,:message,:name)",
2023-09-13 00:14:14 +02:00
{
2023-11-19 13:16:17 +01:00
"id_hello" : self.id_hello,
2023-09-13 00:14:14 +02:00
"amount" : self.amount,
"message" : self.message,
"name" : self.name
})
conn.commit()
except:
print("Can't save?")
return
2023-11-14 22:08:05 +01:00
@staticmethod
def get_all(conn: Connection):
cur = conn.cursor()
2023-11-19 13:16:17 +01:00
cur.execute("select id,id_hello,amount,message,name from orders")
2023-11-14 22:08:05 +01:00
data = cur.fetchall()
if data is not None:
2023-11-19 13:16:17 +01:00
return [Payment(p[1],p[2],p[3],p[4]) for p in data]
2023-11-14 22:08:05 +01:00
return None
2023-11-18 14:30:34 +01:00
2023-09-13 00:14:14 +02:00
def __repr__(self) -> str:
return '{} - {}€- {}'.format(self.name,self.amount/100,self.message)
2023-09-14 00:30:02 +02:00
def to_json(self) -> dict:
return {
"name":self.name,
"amount": self.amount/100,
"message": self.message
}
2023-09-13 00:14:14 +02:00
class Client:
def __init__(self,sock) -> None:
self.sock = sock
def send_event(self, data):
print(self.sock)
self.sock.send(data)