跳轉到

Celery Task

celery_task.celery_main

Celery

on_failure

on_failure(**kwargs: Any) -> None

on failure

Source code in celery_task/celery_main.py
@task_failure.connect
def on_failure(**kwargs: Any) -> None:
    ''' on failure '''
    ses = AWSSES(setting.AWS_ID, setting.AWS_KEY, setting.AWS_SES_FROM)
    raw_mail = ses.raw_mail(
        to_addresses=[setting.ADMIN_To, ],
        subject=f"[COSCUP-SECRETARY] {kwargs['sender'].name} [{kwargs['sender'].request.id}]",
        body=f"""kwargs: <pre>{kwargs['kwargs']}</pre><br>
einfo: <pre>{kwargs['einfo'].traceback}</pre><br>
request: <pre>{kwargs['sender'].request}</pre>""",
    )
    ses.send_raw_email(data=raw_mail)

celery_task.task_expense

Task expense

expense_create

expense_create(
    sender: Any, **kwargs: dict[str, Any]
) -> None

Expense create

Source code in celery_task/task_expense.py
@app.task(bind=True, name='expense.create',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=2,
          routing_key='cs.expense.create', exchange='COSCUP-SECRETARY')
def expense_create(sender: Any, **kwargs: dict[str, Any]) -> None:  # pylint: disable=too-many-locals
    ''' 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'])

    project = Project.get(pid=pid)
    if project:
        uids.update(project.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()
            message = f"收到 **{users[kwargs['expense']['create_by']]['profile']['badge_name']}**" +\
                f"申請費用 - **[{kwargs['expense']['code']}] / {budget['name']}**" +\
                f",前往 [管理費用](https://volunteer.coscup.org/expense/{pid})"

            resp = mmt.posts(
                channel_id=channel_info['id'],
                message=message,
            )
            logger.info(resp.json())

celery_task.task_ipinfo

Task ipinfo

ipinfo_update_usession

ipinfo_update_usession(sender: Any) -> None

IPInfo update usession

Source code in celery_task/task_ipinfo.py
@app.task(bind=True, name='ipinfo.update.usession',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.ipinfo.update.usession', exchange='COSCUP-SECRETARY')
def ipinfo_update_usession(sender: Any) -> None:
    ''' IPInfo update usession '''
    for user in USession.get_no_ipinfo():
        ipinfo_update_usession_one.apply_async(
            kwargs={'ip': user['header']['X-Real-Ip'], 'sid': user['_id']})

ipinfo_update_usession_one

ipinfo_update_usession_one(
    sender: Any, **kwargs: str
) -> None

update session ipinfo

Source code in celery_task/task_ipinfo.py
@app.task(bind=True, name='ipinfo.update.usession.one',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.ipinfo.update.usession.one', exchange='COSCUP-SECRETARY')
def ipinfo_update_usession_one(sender: Any, **kwargs: str) -> None:
    ''' update session ipinfo '''
    logger.info(kwargs)

    resp = IPInfo(setting.IPINFO_TOKEN).get_info(ip_address=kwargs['ip'])
    USession.update_ipinfo(sid=kwargs['sid'], data=resp.json())

session_daily_clean

session_daily_clean(sender: Any) -> None

Daily clean session

Source code in celery_task/task_ipinfo.py
@app.task(bind=True, name='session.daily.clean',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.session.daily.clean', exchange='COSCUP-SECRETARY')
def session_daily_clean(sender: Any) -> None:
    ''' Daily clean session '''
    clean = USession.clean()
    logger.info('matched: %s, modified: %s, raw_result: %s',
                clean.matched_count, clean.modified_count, clean.raw_result)

celery_task.task_mail_sys

Task mail sys

mail_member_add

mail_member_add(sender: Any) -> None

mail member add

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.member.add',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.member.add', exchange='COSCUP-SECRETARY')
def mail_member_add(sender: Any) -> None:
    ''' mail member add '''
    tplenv = Environment(loader=FileSystemLoader('./templates/mail'))
    template = tplenv.get_template('./base_member_add.html')

    team_member_change_db = TeamMemberChangedDB()
    awsses = AWSSES(
        aws_access_key_id=setting.AWS_ID,
        aws_secret_access_key=setting.AWS_KEY,
        source=setting.AWS_SES_FROM)

    for raw in team_member_change_db.find(
        {'done.mail': {'$exists': False}, 'case': 'add'},
            sort=(('create_at', 1), )):
        team = Team.get(raw['pid'], raw['tid'])

        if not team:
            continue

        user = User.get_info(uids=[raw['uid'], ])[raw['uid']]

        body = template.render(
            name=user['profile']['badge_name'],
            team_name=team.name, pid=team.pid, tid=team.id, )

        raw_mail = awsses.raw_mail(
            to_addresses=(
                {'name': user['profile']['badge_name'], 'mail': user['oauth']['email']}, ),
            subject=f"申請加入 {team.name} 核准",
            body=body,
        )

        resp = mail_member_send.apply_async(
            kwargs={'raw_mail': raw_mail.as_string(), 'rid': str(raw['_id'])})
        service_sync_mattermost_add_channel.apply_async(
            kwargs={'pid': raw['pid'], 'uids': (raw['uid'], )})
        logger.info(resp)

mail_member_del

mail_member_del(sender: Any) -> None

mail member del

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.member.del',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.member.del', exchange='COSCUP-SECRETARY')
def mail_member_del(sender: Any) -> None:
    ''' mail member del '''
    tplenv = Environment(loader=FileSystemLoader('./templates/mail'))
    template = tplenv.get_template('./base_member_del.html')

    team_member_change_db = TeamMemberChangedDB()
    awsses = AWSSES(
        aws_access_key_id=setting.AWS_ID,
        aws_secret_access_key=setting.AWS_KEY,
        source=setting.AWS_SES_FROM)

    for raw in team_member_change_db.find(
        {'done.mail': {'$exists': False}, 'case': 'del'},
            sort=(('create_at', 1), )):
        team = Team.get(raw['pid'], raw['tid'])

        if not team:
            continue

        user = User.get_info(uids=[raw['uid'], ])[raw['uid']]

        body = template.render(
            name=user['profile']['badge_name'],
            team_name=team.name, )

        raw_mail = awsses.raw_mail(
            to_addresses=(
                {'name': user['profile']['badge_name'], 'mail': user['oauth']['email']}, ),
            subject=f"您已被移除 {team.name} 的組員資格!",
            body=body,
        )

        resp = mail_member_send.apply_async(
            kwargs={'raw_mail': raw_mail.as_string(), 'rid': str(raw['_id'])})
        logger.info(resp)

mail_member_deny

mail_member_deny(sender: Any) -> None

mail member deny

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.member.deny',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.member.deny', exchange='COSCUP-SECRETARY')
def mail_member_deny(sender: Any) -> None:
    ''' mail member deny '''
    tplenv = Environment(loader=FileSystemLoader('./templates/mail'))
    template = tplenv.get_template('./base_member_deny.html')

    team_member_change_db = TeamMemberChangedDB()
    awsses = AWSSES(
        aws_access_key_id=setting.AWS_ID,
        aws_secret_access_key=setting.AWS_KEY,
        source=setting.AWS_SES_FROM)

    for raw in team_member_change_db.find(
        {'done.mail': {'$exists': False}, 'case': 'deny'},
            sort=(('create_at', 1), )):
        team = Team.get(raw['pid'], raw['tid'])

        if not team:
            continue

        project = Project().get(pid=team.pid)

        if not project:
            continue

        user = User.get_info(uids=[raw['uid'], ])[raw['uid']]
        body = template.render(
            name=user['profile']['badge_name'],
            team_name=team.name,
            project_name=project.name,
            pid=team.pid, )

        raw_mail = awsses.raw_mail(
            to_addresses=(
                {'name': user['profile']['badge_name'], 'mail': user['oauth']['email']}, ),
            subject=f"申請加入 {team.name} 未核准",
            body=body,
        )

        resp = mail_member_send.apply_async(
            kwargs={'raw_mail': raw_mail.as_string(), 'rid': str(raw['_id'])})
        logger.info(resp)

mail_member_send

mail_member_send(sender: Any, **kwargs: str) -> None

mail member send

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.member.send',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.member.send', exchange='COSCUP-SECRETARY')
def mail_member_send(sender: Any, **kwargs: str) -> None:
    ''' mail member send '''
    team_member_change_db = TeamMemberChangedDB()
    awsses = AWSSES(
        aws_access_key_id=setting.AWS_ID,
        aws_secret_access_key=setting.AWS_KEY,
        source=setting.AWS_SES_FROM)

    resp = awsses.send_raw_email(data_str=kwargs['raw_mail'])
    logger.info(resp)
    if resp['ResponseMetadata']['HTTPStatusCode'] != 200:
        raise ConnectionError('HTTPStatusCode not `200`, do retry')

    team_member_change_db.find_one_and_update(
        {'_id': ObjectId(kwargs['rid'])}, {'$set': {'done.mail': True}})

mail_member_waiting

mail_member_waiting(sender: Any) -> None

mail member waiting

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.member.waiting',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.member.waiting', exchange='COSCUP-SECRETARY')
def mail_member_waiting(sender: Any) -> None:
    ''' mail member waiting '''
    template = Environment(loader=FileSystemLoader(
        './templates/mail')).get_template('./base_member_waiting.html')

    team_member_change_db = TeamMemberChangedDB()
    awsses = AWSSES(
        aws_access_key_id=setting.AWS_ID,
        aws_secret_access_key=setting.AWS_KEY,
        source=setting.AWS_SES_FROM)

    for raw in team_member_change_db.find(
        {'done.mail': {'$exists': False}, 'case': 'waiting'},
            sort=(('create_at', 1), )):
        team = Team.get(raw['pid'], raw['tid'])

        if not team:
            continue

        uids: list[str] = []
        if team.chiefs:
            uids.extend(team.chiefs)

        uids.append(raw['uid'])

        users = User.get_info(uids=uids)

        mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN,
                              base_url=setting.MATTERMOST_BASEURL)

        if team.chiefs:
            for uid in team.chiefs:
                body = template.render(
                    name=users[uid]['profile']['badge_name'],
                    uid=raw['uid'],
                    apply_name=users[raw['uid']]['profile']['badge_name'],
                    team_name=team.name, pid=team.pid, tid=team.id, )

                raw_mail = awsses.raw_mail(
                    to_addresses=({
                        'name': users[uid]['profile']['badge_name'],
                        'mail': users[uid]['oauth']['email']}, ),
                    subject=f"申請加入通知信 - {users[raw['uid']]['profile']['badge_name']}",
                    body=body,
                )

                resp = mail_member_send.apply_async(
                    kwargs={'raw_mail': raw_mail.as_string(), 'rid': str(raw['_id'])})
                logger.info(resp)

                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[raw['uid']]['profile']['badge_name']}** 申請加入 **{team.name}**,前往 [管理組員](https://volunteer.coscup.org/team/{team.pid}/{team.id}/edit_user)",  # pylint: disable=line-too-long
                    )
                    logger.info(resp.json())

mail_member_welcome

mail_member_welcome(sender: Any) -> None

mail member welcome

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.member.welcome',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.member.welcome', exchange='COSCUP-SECRETARY')
def mail_member_welcome(sender: Any) -> None:
    ''' mail member welcome '''
    uids = []
    for user in MailLetterDB().need_to_send(code='welcome'):
        uids.append(user['_id'])

    if not uids:
        return

    service_sync_mattermost_invite.apply_async(kwargs={'uids': uids})
    mail_member_welcome_send.apply_async(kwargs={'uids': uids})

mail_member_welcome_send

mail_member_welcome_send(
    sender: Any, **kwargs: list[str]
) -> None

mail member welcome to send

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.member.welcome.send',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.member.welcome.send', exchange='COSCUP-SECRETARY')
def mail_member_welcome_send(sender: Any, **kwargs: list[str]) -> None:
    ''' mail member welcome to send '''
    users = User.get_info(uids=kwargs['uids'])

    tplenv = Environment(loader=FileSystemLoader('./templates/mail'))
    template = tplenv.get_template('./welcome.html')

    awsses = AWSSES(
        aws_access_key_id=setting.AWS_ID,
        aws_secret_access_key=setting.AWS_KEY,
        source=setting.AWS_SES_FROM)

    for uid in kwargs['uids']:
        logger.info('uid: %s', uid)
        body = template.render(
            name=users[uid]['profile']['badge_name'], )

        raw_mail = awsses.raw_mail(
            to_addresses=({
                'name': users[uid]['profile']['badge_name'],
                'mail': users[uid]['oauth']['email']}, ),
            subject=f"歡迎使用志工服務系統 - {users[uid]['profile']['badge_name']}",
            body=body,
        )

        resp = awsses.send_raw_email(data=raw_mail)
        logger.info(resp)
        if resp['ResponseMetadata']['HTTPStatusCode'] != 200:
            raise ConnectionError('HTTPStatusCode not `200`, do retry')

        MailLetterDB().make_sent(uid=uid, code='welcome')

mail_sys_test

mail_sys_test(sender: Any, **kwargs: str) -> None

mail sys test

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.sys.test',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.sys.test', exchange='COSCUP-SECRETARY')
def mail_sys_test(sender: Any, **kwargs: str) -> None:
    ''' mail sys test '''
    logger.info('!!! [%s]', kwargs)
    raise NotImplementedError('Test in error and send mail.')

mail_sys_weberror

mail_sys_weberror(sender: Any, **kwargs: str) -> None

mail sys weberror

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.sys.weberror',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.sys.weberror', exchange='COSCUP-SECRETARY')
def mail_sys_weberror(sender: Any, **kwargs: str) -> None:
    ''' mail sys weberror '''
    ses = AWSSES(setting.AWS_ID, setting.AWS_KEY, setting.AWS_SES_FROM)

    raw_mail = ses.raw_mail(
        to_addresses=[setting.ADMIN_To, ],
        subject=f"[COSCUP-SECRETARY] {kwargs['title']}",
        body=kwargs['body'],
    )

    ses.send_raw_email(data=raw_mail)

mail_tasks_star

mail_tasks_star(sender: Any, **kwargs: str) -> None

mail tasks star

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.tasks.star',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.tasks.star', exchange='COSCUP-SECRETARY')
def mail_tasks_star(sender: Any, **kwargs: str) -> None:
    ''' mail tasks star '''
    pid = kwargs['pid']
    task_id = kwargs['task_id']

    task = Tasks.get_with_pid(pid=pid, _id=task_id)

    uids = []
    for user in TasksStar.get(pid=pid):
        uids.append(user['uid'])

    logger.info(uids)

    users = User.get_info(uids=uids)
    for user_info in users.values():
        user = {}
        user['name'] = user_info['profile']['badge_name']
        user['mail'] = user_info['oauth']['email']
        mail_tasks_star_one.apply_async(kwargs={
            'pid': pid, 'task_id': task_id, 'user': user, 'task': task})

mail_tasks_star_one

mail_tasks_star_one(
    sender: Any, **kwargs: dict[str, Any]
) -> None

mail tasks star one

Source code in celery_task/task_mail_sys.py
@app.task(bind=True, name='mail.tasks.star.one',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.mail.tasks.star.one', exchange='COSCUP-SECRETARY')
def mail_tasks_star_one(sender: Any, **kwargs: dict[str, Any]) -> None:
    ''' mail tasks star one '''
    logger.info(kwargs)

    tplenv = Environment(loader=FileSystemLoader('./templates/mail'))
    template = tplenv.get_template('./tasks_star.html')

    awsses = AWSSES(
        aws_access_key_id=setting.AWS_ID,
        aws_secret_access_key=setting.AWS_KEY,
        source=setting.AWS_SES_FROM)

    body = template.render(name=kwargs['user']['name'], task=kwargs['task'])

    raw_mail = awsses.raw_mail(
        to_addresses=(kwargs['user'], ),
        subject=f"有一筆新志工任務 - {kwargs['task']['title']} [{kwargs['task_id']}]",
        body=body,
    )

    resp = awsses.send_raw_email(data=raw_mail)
    logger.info(resp)

celery_task.task_sendermailer

Task sender mailer

sender_mailer_start

sender_mailer_start(
    sender: Any, **kwargs: dict[str, Any]
) -> None

campaign_data, team, uids, layout

Source code in celery_task/task_sendermailer.py
@app.task(bind=True, name='sender.mailer.start',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.sender.mailer.start', exchange='COSCUP-SECRETARY')
def sender_mailer_start(sender: Any, **kwargs: dict[str, Any]) -> None:
    '''campaign_data, team, uids, layout'''
    campaign_data = kwargs['campaign_data']
    team_name = kwargs['team_name']
    user_datas = kwargs['user_datas']
    layout = kwargs['layout']
    source = kwargs.get('source')

    for user_data in user_datas:
        sender_mailer_start_one.apply_async(
            kwargs={'campaign_data': campaign_data, 'team_name': team_name,
                    'user_data': user_data, 'layout': layout, 'source': source})

sender_mailer_start_one

sender_mailer_start_one(
    sender: Any, **kwargs: dict[str, Any]
) -> None

sender mailer start one

Source code in celery_task/task_sendermailer.py
@app.task(bind=True, name='sender.mailer.start.one',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.sender.mailer.start.one', exchange='COSCUP-SECRETARY')
def sender_mailer_start_one(sender: Any, **kwargs: dict[str, Any]) -> None:
    '''sender mailer start one '''
    campaign_data = kwargs['campaign_data']
    team_name = kwargs['team_name']
    user_data = kwargs['user_data']
    layout: str = str(kwargs['layout'])
    source = kwargs['source']

    sender_template: type
    if layout == '1':
        sender_template = SenderMailerVolunteer
    elif layout == '2':
        sender_template = SenderMailerCOSCUP

    sender_mailer = sender_template(
        subject=campaign_data['mail']['subject'],
        content={'preheader': campaign_data['mail']['preheader'],
                 'body': markdown(campaign_data['mail']['content']),
                 'text_body': campaign_data['mail']['content'],
                 'send_by': team_name, },
        source=source,
    )

    result = sender_mailer.send(
        to_list=[{'name': user_data['name'],
                  'mail': user_data['mail']}, ],
        data=user_data,
        x_coscup=campaign_data['_id'],
    )

    logger.info(result)

    SenderSESLogs.save(
        cid=campaign_data['_id'],
        name=user_data['name'],
        mail=user_data['mail'],
        result=result,
    )

    if result['ResponseMetadata']['HTTPStatusCode'] != 200:
        raise ConnectionError('Send mail error, do retry ...')

celery_task.task_service_sync

Task service sync

service_sync_gsuite_memberchange

service_sync_gsuite_memberchange(sender: Any) -> None

Sync gsuite member change

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.gsuite.memberchange',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.servicesync.gsuite.memberchange', exchange='COSCUP-SECRETARY')
def service_sync_gsuite_memberchange(sender: Any) -> None:  # pylint: disable=too-many-branches
    ''' Sync gsuite member change '''
    team_member_change_db = TeamMemberChangedDB()
    sync_gsuite = None
    for raw in team_member_change_db.find(
        {'done.gsuite_team': {'$exists': False},
            'case': {'$in': ('add', 'del')}},
            sort=(('create_at', 1), )):
        team = Team.get(raw['pid'], raw['tid'])
        if not team:
            continue

        if not team.mailling:
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {'done.gsuite_team': True}})
            continue

        if sync_gsuite is None:
            sync_gsuite = SyncGSuite(
                credentialfile=setting.GSUITE_JSON, with_subject=setting.GSUITE_ADMIN)

        user = User(uid=raw['uid']).get()
        if not user:
            continue

        if raw['case'] == 'add':
            sync_gsuite.add_users_into_group(
                group=team.mailling, users=[user['mail'], ])
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {'done.gsuite_team': True}})

        elif raw['case'] == 'del':
            sync_gsuite.del_users_from_group(
                group=team.mailling, users=[user['mail'], ])
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {'done.gsuite_team': True}})

    for raw in team_member_change_db.find(
        {'done.gsuite_staff': {'$exists': False},
            'case': {'$in': ('add', 'del')}},
            sort=(('create_at', 1), )):
        project = Project.get(raw['pid'])

        if not project:
            continue

        if not project.mailling_staff:
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {'done.gsuite_staff': True}})
            continue

        if sync_gsuite is None:
            sync_gsuite = SyncGSuite(
                credentialfile=setting.GSUITE_JSON, with_subject=setting.GSUITE_ADMIN)

        user = User(uid=raw['uid']).get()
        if not user:
            continue

        if raw['case'] == 'add':
            sync_gsuite.add_users_into_group(
                group=project.mailling_staff, users=[user['mail'], ])
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {'done.gsuite_staff': True}})

        elif raw['case'] == 'del':
            if not Team.participate_in(uid=raw['uid'], pid=raw['pid']):
                sync_gsuite.del_users_from_group(
                    group=project.mailling_staff, users=[user['mail'], ])

            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {'done.gsuite_staff': True}})

service_sync_gsuite_team_leader

service_sync_gsuite_team_leader(
    sender: Any, **kwargs: str
) -> None

Sync gsuite team leader

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.gsuite.team_leader',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.servicesync.gsuite.team_leader', exchange='COSCUP-SECRETARY')
def service_sync_gsuite_team_leader(sender: Any, **kwargs: str) -> None:
    ''' Sync gsuite team leader '''
    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'])
    if not project:
        return

    if project.mailling_leader:
        sync_gsuite = SyncGSuite(
            credentialfile=setting.GSUITE_JSON, with_subject=setting.GSUITE_ADMIN)
        sync_gsuite.add_users_into_group(group=project.mailling_leader, users=[
                                         u['oauth']['email'] for u in users_info.values()])

        logger.info('%s %s', project.mailling_leader, [
                    u['oauth']['email'] for u in users_info.values()])

service_sync_gsuite_team_members

service_sync_gsuite_team_members(
    sender: Any, **kwargs: str
) -> None

Sync gsuite team members

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.gsuite.team_members',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.servicesync.gsuite.team_members', exchange='COSCUP-SECRETARY')
def service_sync_gsuite_team_members(sender: Any, **kwargs: str) -> None:
    ''' Sync gsuite team members '''
    team = Team.get(pid=kwargs['pid'], tid=kwargs['tid'])
    if not team:
        return

    if 'to_team' in kwargs:
        to_team = Team.get(pid=kwargs['to_team'][0], tid=kwargs['to_team'][1])

        if not to_team or not to_team.mailling:
            return

        mailling = to_team.mailling

    else:
        if not team.mailling:
            return

        mailling = team.mailling

    uids = []
    if team.chiefs:
        uids.extend(team.chiefs)
    if team.members:
        uids.extend(team.members)

    users_info = User.get_info(uids=uids)

    sync_gsuite = SyncGSuite(
        credentialfile=setting.GSUITE_JSON, with_subject=setting.GSUITE_ADMIN)
    sync_gsuite.add_users_into_group(
        group=mailling, users=[u['oauth']['email'] for u in users_info.values()])

    logger.info('%s %s', mailling, [u['oauth']['email']
                for u in users_info.values()])

service_sync_mattermost_add_channel

service_sync_mattermost_add_channel(
    sender: Any, **kwargs: str | list[str]
) -> None

Sync mattermost add to channel

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.mattermost.add.channel',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.servicesync.mattermost.add.channel', exchange='COSCUP-SECRETARY')
def service_sync_mattermost_add_channel(sender: Any, **kwargs: str | list[str]) -> None:
    ''' Sync mattermost add to channel '''
    project = Project.get(pid=str(kwargs['pid']))
    if not project:
        return

    if not project.mattermost_ch_id:
        return

    mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN,
                          base_url=setting.MATTERMOST_BASEURL)
    for uid in kwargs['uids']:
        mid = mmt.find_possible_mid(uid=uid)
        if mid:
            service_sync_mattermost_add_channel_one.apply_async(
                kwargs={'ch_id': project.mattermost_ch_id, 'uid': mid}
            )

service_sync_mattermost_add_channel_one

service_sync_mattermost_add_channel_one(
    sender: Any, **kwargs: str | list[str]
) -> None

Sync mattermost add to channel one

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.mattermost.add.channel.one',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=10,
          routing_key='cs.servicesync.mattermost.add.channel.one', exchange='COSCUP-SECRETARY')
def service_sync_mattermost_add_channel_one(sender: Any, **kwargs: str | list[str]) -> None:
    ''' Sync mattermost add to channel one '''
    mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN,
                          base_url=setting.MATTERMOST_BASEURL)
    resp = mmt.post_user_to_channel(
        channel_id=str(kwargs['ch_id']), uid=str(kwargs['uid']))
    logger.info(resp.json())

service_sync_mattermost_invite

service_sync_mattermost_invite(
    sender: Any, **kwargs: list[str]
) -> None

Sync mattermost invite

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.mattermost.invite',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.servicesync.mattermost.invite', exchange='COSCUP-SECRETARY')
def service_sync_mattermost_invite(sender: Any, **kwargs: list[str]) -> None:
    ''' Sync mattermost invite '''
    mmb = MattermostBot(token=setting.MATTERMOST_BOT_TOKEN,
                        base_url=setting.MATTERMOST_BASEURL)

    users_info = User.get_info(uids=kwargs['uids'])
    resp = mmb.post_invite_by_email(
        team_id=setting.MATTERMOST_TEAM_ID,
        emails=[value['oauth']['email'] for value in users_info.values()])
    logger.info(resp.json())

service_sync_mattermost_projectuserin_channel

service_sync_mattermost_projectuserin_channel(
    sender: Any,
) -> None

Sync mattermost project user in channel

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.mattermost.projectuserin.channel',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=2,
          routing_key='cs.servicesync.mattermost.projectuserin.channel',
          exchange='COSCUP-SECRETARY')
def service_sync_mattermost_projectuserin_channel(sender: Any) -> None:
    ''' Sync mattermost project user in channel '''
    pids = {}
    for project in Project.all():
        if arrow.get(project.action_date) >= arrow.now() and project.mattermost_ch_id:
            pids[project.id] = project.mattermost_ch_id

    if not pids:
        return

    mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN,
                          base_url=setting.MATTERMOST_BASEURL)
    for pid, value in pids.items():
        uids = set()
        for team in Team.list_by_pid(pid=pid):
            if team.chiefs:
                uids.update(team.chiefs)
            if team.members:
                uids.update(team.members)

        for uid in uids:
            mid = mmt.find_possible_mid(uid=uid)
            if mid:
                service_sync_mattermost_add_channel_one.apply_async(
                    kwargs={'ch_id': value, 'uid': mid}
                )

service_sync_mattermost_users

service_sync_mattermost_users(
    sender: Any, **kwargs: str
) -> None

Sync mattermost users

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.mattermost.users',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=5,
          routing_key='cs.servicesync.mattermost.users', exchange='COSCUP-SECRETARY')
def service_sync_mattermost_users(sender: Any, **kwargs: str) -> None:
    ''' Sync mattermost users '''
    mmb = MattermostBot(token=setting.MATTERMOST_BOT_TOKEN,
                        base_url=setting.MATTERMOST_BASEURL)

    total_users_count = mmb.get_users_stats().json()['total_users_count']
    db_count = MattermostUsersDB().count_documents({})

    logger.info('total_users_count: %s, db_count: %s',
                total_users_count, db_count)

    if (db_count-3) < total_users_count or 'force' in kwargs:
        mmusers_db = MattermostUsersDB()
        num = 0
        for user in mmb.get_users_loop():
            num += 1
            mmusers_db.add(data=user)

        logger.info('Sync count: %s', num)

service_sync_mattermost_users_position

service_sync_mattermost_users_position(sender: Any) -> None

Sync mattermost users position

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.mattermost.users.position',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=2,
          routing_key='cs.servicesync.mattermost.users.position', exchange='COSCUP-SECRETARY')
def service_sync_mattermost_users_position(sender: Any) -> None:
    ''' Sync mattermost users position '''
    # pylint: disable=too-many-locals,too-many-branches
    pids = []
    for project in Project.all():
        if project.action_date >= datetime.now():
            pids.append(project.id)

    if not pids:
        return

    for pid in pids:
        users: dict[str, list[str]] = {}
        for team in Team.list_by_pid(pid=pid):
            team_name = team.name.split('-')[0].strip()

            if team.chiefs:
                for chief in team.chiefs:
                    if chief not in users:
                        users[chief] = []

                    if team.id == 'coordinator':
                        users[chief].append('🌟總召')
                    else:
                        users[chief].append(f'⭐️組長@{team_name}')

            if team.members:
                if team.chiefs:
                    members = list(set(team.members) - set(team.chiefs))

                else:
                    members = team.members

                for member in members:
                    if member not in users:
                        users[member] = []

                    users[member].append(f'{team_name}(組員)')

        mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN,
                              base_url=setting.MATTERMOST_BASEURL)
        mmb = MattermostBot(token=setting.MATTERMOST_BOT_TOKEN,
                            base_url=setting.MATTERMOST_BASEURL)

        for uid, value in users.items():
            mid = mmt.find_possible_mid(uid=uid)
            if not mid:
                continue

            position = [pid, ]
            position.extend(value)
            position.append(f'[{uid}]')
            mmb.put_users_patch(uid=mid, position=' '.join(position))

service_sync_pretalx_schedule

service_sync_pretalx_schedule(
    sender: Any, **kwargs: str
) -> None

Sync pretalx schedule

Source code in celery_task/task_service_sync.py
@app.task(bind=True, name='servicesync.pretalx.schedule',
          autoretry_for=(Exception, ), retry_backoff=True, max_retries=2,
          routing_key='cs.servicesync.pretalx.schedule', exchange='COSCUP-SECRETARY')
def service_sync_pretalx_schedule(sender: Any, **kwargs: str) -> None:
    ''' Sync pretalx schedule '''
    track = Track(pid=str(kwargs['pid']))
    track.fetch()
    track.save_raw_talks()
    track.save_raw_submissions()