跳轉到

models/usessiondb.py

models.usessiondb

USessionDB

USessionDB

Bases: DBBase

USessionDB Collection

Parameters:

Name Type Description Default
token str

If is None, it will generate an random sha256 code.

None

Attributes:

Name Type Description
token str

An session token.

Source code in models/usessiondb.py
class USessionDB(DBBase):
    ''' USessionDB Collection

    Args:
        token (str): If is `None`, it will generate an random sha256 code.

    Attributes:
        token (str): An session token.

    '''

    def __init__(self, token: Optional[str] = None) -> None:
        super().__init__('usession')

        if token is None:
            message = hashlib.sha256()
            message.update((f'{uuid4().hex}{time()}').encode('utf8'))
            token = message.hexdigest()

        self.token = token

    def index(self) -> None:
        ''' To make collection's index

        Indexs:
            - `created_at`
            - `ipinfo`
            - `uid`
            - `alive`

        '''
        self.create_index([('created_at', 1), ])
        self.create_index([('ipinfo', 1), ])
        self.create_index([('uid', 1), ])
        self.create_index([('alive', 1), ])

    def add(self, data: dict[str, Any]) -> InsertOneResult:
        ''' save

        Args:
            data (dict): The data to insert.

        Returns:
            Return the inserted data.

        '''
        doc = {}
        doc.update(data)
        doc['_id'] = self.token

        return self.insert_one(doc)

    def get(self) -> Optional[dict[str, Any]]:
        ''' Get data

        Returns:
            Return the data.

        '''
        return self.find_one({'_id': self.token, 'alive': True})

add

add(data: dict[str, Any]) -> InsertOneResult

save

Parameters:

Name Type Description Default
data dict

The data to insert.

required

Returns:

Type Description
InsertOneResult

Return the inserted data.

Source code in models/usessiondb.py
def add(self, data: dict[str, Any]) -> InsertOneResult:
    ''' save

    Args:
        data (dict): The data to insert.

    Returns:
        Return the inserted data.

    '''
    doc = {}
    doc.update(data)
    doc['_id'] = self.token

    return self.insert_one(doc)

get

get() -> Optional[dict[str, Any]]

Get data

Returns:

Type Description
Optional[dict[str, Any]]

Return the data.

Source code in models/usessiondb.py
def get(self) -> Optional[dict[str, Any]]:
    ''' Get data

    Returns:
        Return the data.

    '''
    return self.find_one({'_id': self.token, 'alive': True})

index

index() -> None

To make collection's index

Indexs
  • created_at
  • ipinfo
  • uid
  • alive
Source code in models/usessiondb.py
def index(self) -> None:
    ''' To make collection's index

    Indexs:
        - `created_at`
        - `ipinfo`
        - `uid`
        - `alive`

    '''
    self.create_index([('created_at', 1), ])
    self.create_index([('ipinfo', 1), ])
    self.create_index([('uid', 1), ])
    self.create_index([('alive', 1), ])