def get(sid): ''' Get usession data :param str sid: usession id ''' return USessionDB(token=sid).get()
def get_no_ipinfo(): ''' Get no ipinfo ''' for raw in USessionDB().find({'ipinfo': { '$exists': False }}, { 'header.X-Real-Ip': 1, 'header.X-Forwarded-For': 1 }): yield raw
def index() -> Text: ''' Index page ''' if request.method == 'GET': if 'tc' not in session: session['tc'] = 0 session['tc'] += 1 return render_template('./dev_index.html', count=session['tc']) if request.method == 'POST': data = request.get_json() if data: if 'casename' in data and data['casename'] == 'get': accounts = [] for user in UsersDB().find(): accounts.append(user) sessions = [] for usession in USessionDB().find(): sessions.append(usession) return jsonify({ 'sid': session['sid'], 'accounts': accounts, 'sessions': sessions, 'projects': list(Project.all()), }) if 'casename' in data and data['casename'] == 'create_project': usession = USessionDB().find_one({'_id': session['sid']}) if usession: project_data = data['project'] Project.create( pid=project_data['pid'], name=project_data['name'], owners=[ usession['uid'], ], action_date=project_data['action_date'], ) return jsonify({})
def get_recently(uid, limit=25): ''' Get recently record :param str uid: uid ''' for raw in USessionDB(token='').find({'uid': uid}, sort=(('created_at', -1), ), limit=limit): yield raw
def update_ipinfo(sid, data): ''' Update session ipinfo :param str sid: usession id ''' USessionDB().find_one_and_update({'_id': sid}, {'$set': { 'ipinfo': data }})
def get(sid: Optional[str]) -> Optional[dict[str, Any]]: ''' Get usession data Args: sid (str): usession id. Returns: Return the usession data. ''' return USessionDB(token=sid).get()
def update_ipinfo(sid: str, data: dict[str, Any]) -> None: ''' Update session ipinfo Args: sid (str): usession id. data (dict): The ipinfo response data. ''' USessionDB().find_one_and_update({'_id': sid}, {'$set': { 'ipinfo': data }})
def get_alive(uid): ''' Get alive session :param str uid: uid ''' for raw in USessionDB(token='').find({ 'uid': uid, 'alive': True }, sort=(('created_at', -1), )): yield raw
def get_no_ipinfo() -> Generator[dict[str, Any], None, None]: ''' Get no ipinfo Yields: Return the data, `ipinfo` is not exist. ''' for raw in USessionDB().find({'ipinfo': { '$exists': False }}, { 'header.X-Real-Ip': 1, 'header.X-Forwarded-For': 1 }): yield raw
def make_new(uid, header): ''' make new session record :param str uid: uid :param dict header: headers ''' doc = { 'uid': uid, 'header': header, 'created_at': time(), 'alive': True } return USessionDB().save(doc)
def make_dead(sid, uid=None): ''' Make session to dead :param str sid: sid :param str uid: uid ''' query = {'_id': sid} if uid: query['uid'] = uid USessionDB(token='').find_one_and_update(query, {'$set': { 'alive': False }}) MC.get_client().delete('sid:%s' % sid)
def get_alive(uid: str) -> Generator[dict[str, Any], None, None]: ''' Get alive session Args: uid (str): User id. Yields: Return the datas. ''' for raw in USessionDB(token='').find({ 'uid': uid, 'alive': True }, sort=(('created_at', -1), )): yield raw
def get_recently(uid: str, limit: int = 25) -> Generator[dict[str, Any], None, None]: ''' Get recently record Args: uid (str): User id. limit (int): Limit. Yields: Return the recently datas. ''' for raw in USessionDB(token='').find({'uid': uid}, sort=(('created_at', -1), ), limit=limit): yield raw
def make_dead(sid: str, uid: Optional[str] = None) -> None: ''' Make session to dead Args: sid (str): usession id. uid (str): User id. ''' query = {'_id': sid} if uid: query['uid'] = uid USessionDB(token='').find_one_and_update(query, {'$set': { 'alive': False }}) MC.get_client().delete(f'sid:{sid}')
def clean(days=3): ''' Make expired :param int days: days ''' target = time() - 86400 * days return USessionDB(token='').update_many( { 'alive': True, 'created_at': { '$lte': target } }, {'$set': { 'alive': False }}, )
def make_new(uid: str, header: dict[str, Any]) -> InsertOneResult: ''' make new session record Args: uid (str): User id. header (dict): User's request header. Returns: Return the [pymongo.results.InsertOneResult][] object. ''' doc = { 'uid': uid, 'header': header, 'created_at': time(), 'alive': True } return USessionDB().add(doc)
def make_index() -> None: ''' Make index for the collection with `index()` ''' BudgetDB().index() ExpenseDB().index() FormDB().index() MailLetterDB().index() MattermostLinkDB().index() MattermostUsersDB().index() OAuthDB().index() ProjectDB(pid='').index() SenderReceiverDB().index() TeamDB(pid='', tid='').index() TeamMemberChangedDB().index() TeamMemberTagsDB().index() TeamPlanDB().index() TelegramDB().index() USessionDB().index() UsersDB().index() WaitListDB().index()
def clean(days: int = 3) -> UpdateResult: ''' Make expired :param int days: days Args: days (int): The long days to clean. Returns: Return the [pymongo.results.UpdateResult][] object. ''' target = time() - 86400 * days return USessionDB(token='').update_many( { 'alive': True, 'created_at': { '$lte': target } }, {'$set': { 'alive': False }}, )
from models.formdb import FormDB from models.mailletterdb import MailLetterDB from models.mattermost_link_db import MattermostLinkDB from models.mattermostdb import MattermostUsersDB from models.oauth_db import OAuthDB from models.projectdb import ProjectDB from models.senderdb import SenderReceiverDB from models.teamdb import TeamDB from models.teamdb import TeamMemberChangedDB from models.teamdb import TeamMemberTagsDB from models.teamdb import TeamPlanDB from models.users_db import UsersDB from models.usessiondb import USessionDB from models.waitlistdb import WaitListDB if __name__ == '__main__': FormDB().index() MailLetterDB().index() MattermostLinkDB().index() MattermostUsersDB().index() OAuthDB().index() ProjectDB(pid=None).index() SenderReceiverDB().index() TeamDB(pid=None, tid=None).index() TeamMemberChangedDB().index() TeamMemberTagsDB().index() TeamPlanDB().index() USessionDB().index() UsersDB().index() WaitListDB().index()