def update_members(pid: str, tid: str, add_uids: Optional[list[str]] = None, del_uids: Optional[list[str]] = None, make_record: bool = True) -> None: ''' update chiefs :param list add_uids: add uids :param list del_uids: del uids :param bool make_record: make user update record .. note:: also make user changed record ''' teamdb = TeamDB(pid, tid) teamdb.update_users(field='members', add_uids=add_uids, del_uids=del_uids) if make_record: TeamMemberChangedDB().make_record(pid=pid, tid=tid, action={ 'add': add_uids, 'del': del_uids })
def update_setting(pid, tid, data): ''' update setting :param str pid: project id :param str tid: team id :param dict data: data ''' teamdb = TeamDB(pid=pid, tid=tid) _data = {} for k in ('name', 'public_desc', 'desc', 'chiefs', 'members', 'owners', 'headcount', 'mailling', 'disabled'): if k in data: _data[k] = data[k] if isinstance(_data[k], str): _data[k] = _data[k].strip() if 'headcount' in _data: _data['headcount'] = int(_data['headcount']) for k in ('chiefs', 'members', 'owners'): if k in _data: if not _data[k]: _data[k] = [] continue if isinstance(_data[k], str): _data[k] = [i.strip() for i in _data[k].split(',')] if _data: return teamdb.update_setting(_data)
def update_chiefs(pid, tid, add_uids=None, del_uids=None): ''' update chiefs :param list add_uids: add uids :param list del_uids: del uids ''' teamdb = TeamDB(pid, tid) teamdb.update_users(field='chiefs', add_uids=add_uids, del_uids=del_uids)
def update_chiefs(pid: str, tid: str, add_uids: Optional[list[str]] = None, del_uids: Optional[list[str]] = None) -> None: ''' update chiefs :param list add_uids: add uids :param list del_uids: del uids ''' teamdb = TeamDB(pid, tid) teamdb.update_users(field='chiefs', add_uids=add_uids, del_uids=del_uids)
def participate_in(uid, pid=None): ''' participate in :param str uid: uid ''' query = { '$or': [ { 'members': uid, '$or': [{ 'disabled': { '$exists': False } }, { 'disabled': False }] }, { 'chiefs': uid, '$or': [{ 'disabled': { '$exists': False } }, { 'disabled': False }] }, ], } if pid: query['pid'] = {'$in': pid} return TeamDB(None, None).find(query)
def is_admin(pid: str, uid: str) -> bool: ''' check user is admin Args: pid (str): Project id. uid (str): User id. Returns: What cases will return `true`: - Project owner - Finace team chiefs and members - coordinator chiefs ''' if TeamDB(pid='', tid='').count_documents({ 'pid': pid, 'tid': {'$in': ['finance', 'coordinator']}, '$or': [{'chiefs': uid}, {'members': uid}], }): return True if ProjectDB(pid='').count_documents({'_id': pid, 'owners': uid}): return True return False
def get(pid: str, tid: str) -> Optional[dict[str, Any]]: ''' Get team data :param str pid: project id :param str tid: team id ''' return TeamDB(pid=pid, tid=tid).get()
def get(pid, tid): ''' Get team data :param str pid: project id :param str tid: team id ''' return TeamDB(pid=pid, tid=tid).get()
def create(pid, tid, name, owners): ''' Create team :param str pid: project id :param str tid: team id :param str name: team name :param list owners: owners ''' if not tid or not pid or not owners: raise Exception('lost required') teamdb = TeamDB(pid, tid) data = teamdb.default() data['name'] = name data['owners'].extend(owners) return teamdb.add(data)
def list_by_pid(pid, show_all=False): ''' List all team in project :param str pid: project id ''' if show_all: return TeamDB(None, None).find({'pid': pid}) return TeamDB(None, None).find({ 'pid': pid, '$or': [{ 'disabled': { '$exists': False } }, { 'disabled': False }] })
def list_by_pid(pid: str, show_all: bool = False) -> Cursor[dict[str, None]]: ''' List all team in project :param str pid: project id ''' if show_all: return TeamDB('', '').find({'pid': pid}) return TeamDB('', '').find({ 'pid': pid, '$or': [{ 'disabled': { '$exists': False } }, { 'disabled': False }] })
def get_users(pid, tids): ''' Get all users by team :param str pid: project id :param list tid: team id ''' users = {} for tid in tids: team = TeamDB(pid=pid, tid=tid).get() users[tid] = team['chiefs'] + team['members'] return users
def update_members(pid, tid, add_uids=None, del_uids=None, make_record=True): ''' update chiefs :param list add_uids: add uids :param list del_uids: del uids :param bool make_record: make user update record .. note:: also make user changed record ''' teamdb = TeamDB(pid, tid) teamdb.update_users(field='members', add_uids=add_uids, del_uids=del_uids) if make_record: TeamMemberChangedDB().make_record(pid=pid, tid=tid, add_uids=add_uids, del_uids=del_uids)
def get_users(pid: str, tids: list[str]) -> dict[str, Any]: ''' Get all users by team :param str pid: project id :param list tid: team id ''' users = {} for tid in tids: team = TeamDB(pid=pid, tid=tid).get() if not team: raise Exception(f"no team: {tid}") users[tid] = team['chiefs'] + team['members'] return users
def del_tag(pid: str, tid: str, tag_id: str) -> None: ''' Delete tag ''' TeamDB(pid=pid, tid=tid).update_one({ 'pid': pid, 'tid': tid }, {'$pull': { 'tag_members': { 'id': tag_id } }}) TeamMemberTagsDB().update_many({ 'pid': pid, 'tid': tid }, {'$pull': { 'tags.tags': tag_id }})
def service_sync_gsuite_team_leader(sender, **kwargs): chiefs = [] # note: sync all, include `disabled` team for team in TeamDB(pid=None, tid=None).find({'pid': kwargs['pid']}): chiefs.extend(team['chiefs']) users_info = User.get_info(uids=chiefs) project = Project.get(pid=kwargs['pid']) sync_gsuite = SyncGSuite(credentialfile=setting.GSUITE_JSON, with_subject=setting.GSUITE_ADMIN) sync_gsuite.add_users_into_group( group=project['mailling_staff'], users=[u['oauth']['email'] for u in users_info.values()]) logger.info('%s %s', project['mailling_staff'], [u['oauth']['email'] for u in users_info.values()])
def expense_create(sender, **kwargs): ''' Expense create ''' pid = kwargs['expense']['pid'] buid = kwargs['expense']['request']['buid'] budget = None for data in Budget.get(buids=[ buid, ], pid=pid): budget = data if not budget: return uids = set() for team in TeamDB(pid=None, tid=None).find({ 'pid': pid, 'tid': 'finance' }): uids.update(team['chiefs']) uids.update(team['members']) uids.update(Project.get(pid=pid)['owners']) uids.add(kwargs['expense']['create_by']) logger.info(uids) users = User.get_info(uids=list(uids)) mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN, base_url=setting.MATTERMOST_BASEURL) for uid in uids: mid = mmt.find_possible_mid(uid=uid) if mid: channel_info = mmt.create_a_direct_message( users=(mid, setting.MATTERMOST_BOT_ID)).json() resp = mmt.posts( channel_id=channel_info['id'], message= f"""收到 **{users[kwargs['expense']['create_by']]['profile']['badge_name']}** 申請費用 - **[{kwargs['expense']['code']}] / {budget['name']}**,前往 [管理費用](https://volunteer.coscup.org/expense/{pid})""", ) logger.info(resp.json())
def add_tag_member(pid: str, tid: str, tag_name: str, tag_id: Optional[str] = None) -> dict[str, Any]: ''' Add tag member :param str pid: project id :param str tid: team id :param str tag_id: tag id :param str tag_name: tag name ''' if tag_id is None: tag_id = f'{uuid4().fields[0]:08x}' data = {'id': tag_id, 'name': tag_name.strip()} TeamDB(pid=pid, tid=tid).add_tag_member(tag_data=data) return data
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 participate_in(uid: str, pid: Optional[str] = None) -> Cursor[dict[str, None]]: ''' participate in :param str uid: uid ''' query: dict[str, Any] = { '$or': [ { 'members': uid, '$or': [{ 'disabled': { '$exists': False } }, { 'disabled': False }] }, { 'chiefs': uid, '$or': [{ 'disabled': { '$exists': False } }, { 'disabled': False }] }, ], } if pid: query['pid'] = {'$in': pid} return TeamDB('', '').find(query)
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()