File size: 1,488 Bytes
c7dfe8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import threading
from sqlalchemy import BigInteger, Column, String
from Database.sql import BASE, SESSION
class Approvals(BASE):
__tablename__ = "approval"
chat_id = Column(String(14), primary_key=True)
user_id = Column(BigInteger, primary_key=True)
def __init__(self, chat_id, user_id):
self.chat_id = str(chat_id) # ensure string
self.user_id = user_id
def __repr__(self):
return "<Approve %s>" % self.user_id
Approvals.__table__.create(checkfirst=True)
APPROVE_INSERTION_LOCK = threading.RLock()
def approve(chat_id, user_id):
with APPROVE_INSERTION_LOCK:
approve_user = Approvals(str(chat_id), user_id)
SESSION.add(approve_user)
SESSION.commit()
def is_approved(chat_id, user_id):
try:
return SESSION.query(Approvals).get((str(chat_id), user_id))
finally:
SESSION.close()
def disapprove(chat_id, user_id):
with APPROVE_INSERTION_LOCK:
disapprove_user = SESSION.query(Approvals).get((str(chat_id), user_id))
if disapprove_user:
SESSION.delete(disapprove_user)
SESSION.commit()
return True
else:
SESSION.close()
return False
def list_approved(chat_id):
try:
return (
SESSION.query(Approvals)
.filter(Approvals.chat_id == str(chat_id))
.order_by(Approvals.user_id.asc())
.all()
)
finally:
SESSION.close()
|