File size: 1,199 Bytes
44aada1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# (c) @SpEcHIDe

from sqlalchemy import (
    Column,
    String
)
from . import (
    SESSION,
    BASE
)


class blacklist(BASE):
    __tablename__ = "blacklist"
    chat_id = Column(String(14), primary_key=True)
    # reason = Column(UnicodeText)

    def __init__(self, chat_id):
        self.chat_id = int(chat_id)
        # self.reason = reason

    def __repr__(self):
        return "<BL %s>" % self.chat_id


blacklist.__table__.create(checkfirst=True)


def add_user_to_bl(chat_id: int):
    """Adding the user to the blacklist"""
    __user = blacklist(str(chat_id))
    SESSION.add(__user)
    SESSION.commit()


def check_is_black_list(chat_id):
    """check if blacklisted"""
    try:
        return SESSION.query(blacklist).filter(
            blacklist.chat_id == str(chat_id)).one()
    except BaseException:
        return None
    finally:
        SESSION.close()


def rem_user_from_bl(chat_id):
    """remove from bl"""
    __user = SESSION.query(blacklist).get(str(chat_id))
    if __user:
        SESSION.delete(__user)
        SESSION.commit()


def all_bl_users():
    """get all bl users"""
    __user = SESSION.query(blacklist).all()
    SESSION.close()
    return __user