Beispiel #1
0
def mail_member_del(sender):
    ''' 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'])

        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=(
                dict(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)
Beispiel #2
0
def mail_member_waiting(sender):
    ''' 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'])

        uids = []
        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)

        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['tid'], )

            raw_mail = awsses.raw_mail(
                to_addresses=(dict(
                    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['tid']}/edit_user)",  # pylint: disable=line-too-long
                )
                logger.info(resp.json())
Beispiel #3
0
def mail_member_add(sender, **kwargs):
    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'])

        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['tid'],
        )

        raw_mail = awsses.raw_mail(
            to_addresses=(dict(name=user['profile']['badge_name'],
                               mail=user['oauth']['email']), ),
            subject=u'申請加入 %s 核准' % team['name'],
            body=body,
        )

        r = 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(r)
def service_sync_gsuite_memberchange(sender, **kwargs):
    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 'mailling' not in team or 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 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 'mailling_staff' not in project or 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 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':
            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
                }})