Exemplo n.º 1
0
def members(pid, tid):
    team, project, _redirect = check_the_team_and_project_are_existed(pid=pid, tid=tid)
    if _redirect:
        return _redirect

    is_admin = (g.user['account']['_id'] in team['chiefs'] or \
                g.user['account']['_id'] in team['owners'] or \
                g.user['account']['_id'] in project['owners'])

    uids = []
    uids.extend(team['chiefs'])
    uids.extend(team['members'])

    uids = list(set(uids))
    users_info = User.get_info(uids=uids)

    members = []
    for uid in uids:
        if uid in users_info:
            user = users_info[uid]

            user['chat'] = {}
            mid = MattermostTools.find_possible_mid(uid=uid)
            if mid:
                user['chat'] = {'mid': mid, 'name': MattermostTools.find_user_name(mid=mid)}

            members.append(user)

    members = sorted(members, key=lambda u: u['profile']['badge_name'].lower())

    return render_template('./team_members.html', project=project, team=team, is_admin=is_admin, members=members)
Exemplo n.º 2
0
def user_page(uid, nickname=None):
    user = User(uid=uid).get()

    if not user:
        return u'', 200

    oauth = OAuth(user['mail']).get()

    if 'profile' in user and 'badge_name' in user['profile']:
        _nickname = user['profile']['badge_name']
    else:
        _nickname = oauth['data']['name']

    _nickname = quote_plus(_nickname)

    if nickname is None or nickname != _nickname:
        return redirect(url_for('user.user_page', uid=uid, nickname=_nickname))

    if 'profile' not in user:
        badge_name = ''
        intro = ''
    else:
        badge_name = user['profile']['badge_name']
        intro = re.sub('<a href="javascript:.*"', '<a href="/"',
                       markdown(html.escape(user['profile']['intro'])))

    participate_in = []
    for p in Team.participate_in(uid):
        p['_project'] = Project.get(p['pid'])
        p['_title'] = '???'
        if uid in p['chiefs']:
            p['_title'] = 'chief'
        elif uid in p['members']:
            p['_title'] = 'member'

        p['_action_date'] = arrow.get(
            p['_project']['action_date']).format('YYYY/MM')

        participate_in.append(p)

    participate_in = sorted(participate_in,
                            key=lambda p: p['_project']['action_date'],
                            reverse=True)

    mattermost_data = {}
    mid = MattermostTools.find_possible_mid(uid=uid)
    if mid:
        mattermost_data['mid'] = mid
        mattermost_data['username'] = MattermostTools.find_user_name(mid=mid)

    return render_template(
        './user.html',
        badge_name=badge_name,
        intro=intro,
        oauth=oauth,
        user=user,
        mattermost_data=mattermost_data,
        participate_in=participate_in,
    )
Exemplo n.º 3
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())
def service_sync_mattermost_add_channel(sender, **kwargs):
    project = Project.get(pid=kwargs['pid'])
    if not ('mattermost_ch_id' in project and 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:
            r = mmt.post_user_to_channel(
                channel_id=project['mattermost_ch_id'], uid=mid)
            logger.info(r.json())
Exemplo n.º 5
0
def read(pid, task_id):
    ''' Read '''
    project_info = Project.get(pid=pid)
    if not project_info:
        return '404', 404

    task = Tasks.get_with_pid(pid=pid, _id=task_id)
    if not task:
        return redirect(
            url_for('tasks.project', pid=pid, _scheme='https', _external=True))

    task['starttime'] = arrow.get(
        task['starttime']).to('Asia/Taipei').format('YYYY-MM-DD HH:mm')
    if task['endtime']:
        task['endtime'] = arrow.get(
            task['endtime']).to('Asia/Taipei').format('YYYY-MM-DD HH:mm')

    uid = g.get('user', {}).get('account', {}).get('_id')
    if request.method == 'GET':
        creator = {}
        user_info = User.get_info(uids=[
            task['created_by'],
        ])
        creator['name'] = user_info[
            task['created_by']]['profile']['badge_name']
        creator['uid'] = task['created_by']

        mid = MattermostTools.find_possible_mid(uid=task['created_by'])
        if mid:
            creator['mattermost_uid'] = MattermostTools.find_user_name(mid=mid)

        return render_template('./tasks_detail.html',
                               task=task,
                               creator=creator,
                               uid=uid)

    if request.method == 'POST':
        if not uid:
            return jsonify({'info': 'Need login'}), 401

        post_data = request.get_json()
        if post_data['casename'] == 'join':
            Tasks.join(pid=pid, task_id=task['_id'], uid=uid)

        elif post_data['casename'] == 'cancel':
            Tasks.cancel(pid=pid, task_id=task['_id'], uid=uid)

        return jsonify({})
    return jsonify({})
Exemplo n.º 6
0
def service_sync_mattermost_users_position(sender, **kwargs):
    ''' Sync mattermost users position '''
    # pylint: disable=too-many-locals,too-many-branches
    pids = []
    for project in Project.all():
        if project['action_date'] >= time():
            pids.append(project['_id'])

    if not pids:
        return

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

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

                if team['tid'] == 'coordinator':
                    users[chief].append('­ЪїЪуИйтЈг')
                else:
                    users[chief].append(f'РГљ№ИЈухёжЋи@{team_name}')

            team['members'] = set(team['members']) - set(team['chiefs'])
            for member in team['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))
Exemplo n.º 7
0
def project_contact_book(pid):
    ''' Project contact book '''
    project = Project.get(pid)
    if g.user['account']['_id'] not in project['owners']:
        return redirect(
            url_for('project.team_page',
                    pid=pid,
                    _scheme='https',
                    _external=True))

    if request.method == 'GET':
        return render_template('./project_contact_book.html', project=project)

    if request.method == 'POST':
        post_data = request.get_json()

        if post_data['casename'] == 'get':
            all_users = {}
            for team in Team.list_by_pid(pid=pid):
                for uid in team['chiefs'] + team['members']:
                    all_users[uid] = {'tid': team['tid']}

            user_infos = User.get_info(uids=list(all_users.keys()),
                                       need_sensitive=True)

            mmt = MattermostTools(token=setting.MATTERMOST_BOT_TOKEN,
                                  base_url=setting.MATTERMOST_BASEURL)
            datas = []
            for uid, value in all_users.items():
                user_info = user_infos[uid]
                data = {
                    'uid': uid,
                    'name': user_info['profile']['badge_name'],
                    'picture': user_info['oauth']['picture'],
                    'tid': value['tid'],
                    'email': user_info['oauth']['email'],
                }

                if 'profile_real' in user_info:
                    data['phone'] = user_info['profile_real'].get('phone', '')

                data['user_name'] = mmt.find_user_name(
                    mmt.find_possible_mid(uid=uid))
                datas.append(data)

            return jsonify({'datas': datas})

    return jsonify({}), 404
def service_sync_mattermost_users_position(sender, **kwargs):
    pids = []
    for project in Project.all():
        if project['action_date'] >= time():
            pids.append(project['_id'])

    if not pids:
        return

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

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

                if team['tid'] == 'coordinator':
                    users[chief].append('­ЪїЪуИйтЈг')
                else:
                    users[chief].append('РГљ№ИЈухёжЋи@%s' % team_name)

            team['members'] = set(team['members']) - set(team['chiefs'])
            for member in team['members']:
                if member not in users:
                    users[member] = []

                users[member].append('%s(ухётЊА)' % 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 in users:
            mid = mmt.find_possible_mid(uid=uid)
            if not mid:
                continue

            position = [
                pid,
            ]
            position.extend(users[uid])
            position.append('[%s]' % uid)
            mmb.put_users_patch(uid=mid, position=' '.join(position))
Exemplo n.º 9
0
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())
Exemplo n.º 10
0
def index():
    if 'user' not in g:
        return render_template('index.html')

    check = {
        'profile': False,
        'participate_in': False,
        'mattermost': False,
    }

    if 'profile' in g.user['account'] and 'intro' in g.user['account'][
            'profile']:
        if len(g.user['account']['profile']['intro']) > 100:
            check['profile'] = True

    if list(Team.participate_in(uid=g.user['account']['_id'])):
        check['participate_in'] = True

    if MattermostTools.find_possible_mid(uid=g.user['account']['_id']):
        check['mattermost'] = True

    return render_template('index_guide.html', check=check)
Exemplo n.º 11
0
def service_sync_mattermost_projectuserin_channel(sender, **kwargs):
    pids = {}
    for project in Project.all():
        if project['action_date'] >= time(
        ) and 'mattermost_ch_id' in project 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 in pids:
        uids = set()
        for team in Team.list_by_pid(pid=pid):
            uids.update(team['chiefs'])
            uids.update(team['members'])

        for uid in uids:
            mid = mmt.find_possible_mid(uid=uid)
            if mid:
                r = mmt.post_user_to_channel(channel_id=pids[pid], uid=mid)
                logger.info(r.json())
Exemplo n.º 12
0
def project(pid):
    ''' Project '''
    # pylint: disable=too-many-locals,too-many-return-statements,too-many-branches,too-many-statements
    uid = g.get('user', {}).get('account', {}).get('_id')

    project_info = Project.get(pid=pid)
    if not project_info:
        return '404', 404

    is_in_project = False
    if uid:
        is_in_project = bool(Team.participate_in(uid, pid))

    if request.method == 'GET':
        return render_template('./tasks_project.html', project=project_info)

    if request.method == 'POST':
        post_data = request.get_json()

        def page_args(data, uid):
            data['_uid'] = uid
            data['_joined'] = False
            data['_login'] = False
            data['_is_in_project'] = is_in_project

            if uid:
                data['_login'] = True

                if uid in data['people']:
                    data['_joined'] = True

        if post_data['casename'] == 'get':
            datas = []

            for data in Tasks.get_by_pid(pid=pid):
                page_args(data=data, uid=uid)
                datas.append(data)

            is_star = False
            if uid:
                is_star = TasksStar.status(pid, uid)['add']

            return jsonify({
                'datas': datas,
                'is_in_project': is_in_project,
                'is_star': is_star
            })

        if post_data['casename'] == 'star':
            if not uid:
                return jsonify({'info': 'Need login'}), 401

            result = TasksStar.toggle(pid=pid, uid=uid)

            return jsonify({'is_star': result['add']})

        if post_data['casename'] == 'join':
            if not uid:
                return jsonify({'info': 'Need login'}), 401

            data = Tasks.join(pid=pid, task_id=post_data['task_id'], uid=uid)
            page_args(data=data, uid=uid)

            return jsonify({'data': data})

        if post_data['casename'] == 'cancel':
            if not uid:
                return jsonify({'info': 'Need login'}), 401

            data = Tasks.cancel(pid=pid, task_id=post_data['task_id'], uid=uid)
            page_args(data=data, uid=uid)

            return jsonify({'data': data})

        if post_data['casename'] == 'cancel_user':
            if not uid:
                return jsonify({'info': 'Need login'}), 401

            if not is_in_project:
                return jsonify({'info': 'Need as staff'}), 401

            data = Tasks.cancel(pid=pid,
                                task_id=post_data['task_id'],
                                uid=post_data['uid'])
            page_args(data=data, uid=uid)

            return jsonify({'data': data})

        if post_data['casename'] == 'peoples':
            task_data = Tasks.get_with_pid(pid=pid, _id=post_data['task_id'])
            if not task_data:
                return jsonify({}), 404

            creator = {}
            if task_data:
                user_info = User.get_info(uids=[
                    task_data['created_by'],
                ])
                creator['name'] = user_info[
                    task_data['created_by']]['profile']['badge_name']
                creator['uid'] = task_data['created_by']

                mid = MattermostTools.find_possible_mid(
                    uid=task_data['created_by'])
                if mid:
                    creator['mattermost_uid'] = MattermostTools.find_user_name(
                        mid=mid)

            if not is_in_project:
                return jsonify({'peoples': {}, 'creator': creator})

            users_info = Tasks.get_peoples_info(pid=pid,
                                                task_id=post_data['task_id'])
            peoples = {}
            for uid, user in users_info.items():
                peoples[uid] = {
                    'name': user['profile']['badge_name'],
                    'mail': user['oauth']['email'],
                    'picture': user['oauth']['picture'],
                    'mattermost_uid': None,
                }

                mid = MattermostTools.find_possible_mid(uid=uid)
                if mid:
                    peoples[uid][
                        'mattermost_uid'] = MattermostTools.find_user_name(
                            mid=mid)

            return jsonify({'peoples': peoples, 'creator': creator})

    return jsonify({}), 404
Exemplo n.º 13
0
def team_edit_user(pid, tid):
    team, project, _redirect = check_the_team_and_project_are_existed(pid=pid, tid=tid)
    if _redirect:
        return _redirect

    is_admin = (g.user['account']['_id'] in team['chiefs'] or \
                g.user['account']['_id'] in team['owners'] or \
                g.user['account']['_id'] in project['owners'])

    if not is_admin:
        return redirect('/')

    if request.method == 'GET':
        waitting_list = list(WaitList.list_by_team(pid=pid, tid=tid))
        uids = [u['uid'] for u in waitting_list]
        users_info = User.get_info(uids)

        for u in waitting_list:
            u['_info'] = users_info[u['uid']]
            u['_history'] = []
            for w in WaitList.find_history(pid=pid, uid=u['uid']):
                if 'result' not in w:
                    w['result'] = 'waitting'

                u['_history'].append(w)

            u['_mail'] = User(uid=u['uid']).get()['mail']

        members = []
        if team['members'] or team['chiefs']:
            _all_uids = set(team['chiefs']) | set(team['members'])
            users_info = User.get_info(list(_all_uids))
            for uid in _all_uids:
                members.append(users_info[uid])

            for u in members:
                u['chat'] = {}
                mid = MattermostTools.find_possible_mid(uid=u['_id'])
                if mid:
                    u['chat'] = {'mid': mid, 'name': MattermostTools.find_user_name(mid=mid)}

                u['phone'] = {'country_code': '', 'phone': ''}
                if 'phone' in u['profile_real'] and u['profile_real']['phone']:
                    phone = phonenumbers.parse(u['profile_real']['phone'])
                    u['phone']['country_code'] = phonenumbers.COUNTRY_CODE_TO_REGION_CODE[phone.country_code][0]
                    u['phone']['phone'] = phonenumbers.format_number(phone, phonenumbers.PhoneNumberFormat.NATIONAL)

            members = sorted(members, key=lambda u: u['profile']['badge_name'])

        return render_template('./team_edit_user.html',
                project=project, team=team, waitting_list=waitting_list, members=members)

    elif request.method == 'POST':
        data = request.json

        if data['case'] == 'deluser':
            Team.update_members(pid=pid, tid=tid, del_uids=[data['uid'], ])
        elif data['case'] == 'history':
            history = []
            for raw in WaitList.find_history_in_team(uid=data['uid'], pid=pid, tid=tid):
                raw['_id'] = str(raw['_id'])
                history.append(raw)

            return jsonify({'history': history})

        return jsonify(data)
Exemplo n.º 14
0
def members(pid, tid):
    ''' members '''
    # pylint: disable=too-many-locals
    team, project, _redirect = check_the_team_and_project_are_existed(pid=pid,
                                                                      tid=tid)
    if _redirect:
        return _redirect

    is_admin = (g.user['account']['_id'] in team['chiefs']
                or g.user['account']['_id'] in team['owners']
                or g.user['account']['_id'] in project['owners'])

    if request.method == 'GET':
        return render_template('./team_members.html',
                               project=project,
                               team=team,
                               is_admin=is_admin)

    if request.method == 'POST':
        post_data = request.get_json()

        if post_data['casename'] == 'get':
            list_teams = []
            if 'tid' in post_data and post_data['tid'] != tid:
                team = Team.get(pid=pid, tid=post_data['tid'])

            else:
                for lteam in Team.list_by_pid(pid=pid):
                    list_teams.append({
                        '_id': lteam['tid'],
                        'name': lteam['name']
                    })

            uids = []
            uids.extend(team['chiefs'])
            uids.extend(team['members'])

            uids = list(set(uids))
            users_info = User.get_info(uids=uids)

            result_members = []
            for uid in uids:
                if uid in users_info:
                    user = {
                        '_id': uid,
                        'profile': {
                            'badge_name':
                            users_info[uid]['profile']['badge_name']
                        },
                        'oauth': {
                            'picture': users_info[uid]['oauth']['picture']
                        }
                    }

                    user['is_chief'] = False
                    if uid in team['chiefs']:
                        user['is_chief'] = True

                    user['chat'] = {}
                    mid = MattermostTools.find_possible_mid(uid=uid)
                    if mid:
                        user['chat'] = {
                            'mid': mid,
                            'name': MattermostTools.find_user_name(mid=mid)
                        }

                    result_members.append(user)

            result_members = sorted(
                result_members,
                key=lambda u: u['profile']['badge_name'].lower())

            tags = []
            if 'tag_members' in team and team['tag_members']:
                tags = team['tag_members']

            members_tags = Team.get_members_tags(pid=team['pid'],
                                                 tid=team['tid'])

        return jsonify({
            'members': result_members,
            'teams': list_teams,
            'tags': tags,
            'members_tags': members_tags
        })

    return jsonify({}), 404
Exemplo n.º 15
0
def team_edit_user(pid, tid):
    ''' Team edit user '''
    # pylint: disable=too-many-locals,too-many-return-statements,too-many-branches,too-many-statements
    team, project, _redirect = check_the_team_and_project_are_existed(pid=pid,
                                                                      tid=tid)
    if _redirect:
        return _redirect

    is_admin = (g.user['account']['_id'] in team['chiefs']
                or g.user['account']['_id'] in team['owners']
                or g.user['account']['_id'] in project['owners'])

    if not is_admin:
        return redirect('/')

    if request.method == 'GET':
        waitting_list = list(WaitList.list_by_team(pid=pid, tid=tid))
        uids = [u['uid'] for u in waitting_list]
        users_info = User.get_info(uids)

        for user in waitting_list:
            user['_info'] = users_info[user['uid']]
            user['_history'] = []
            for wait_info in WaitList.find_history(pid=pid, uid=user['uid']):
                if 'result' not in wait_info:
                    wait_info['result'] = 'waitting'

                user['_history'].append(wait_info)

            user['_mail'] = User(uid=user['uid']).get()['mail']

        return render_template('./team_edit_user.html',
                               project=project,
                               team=team,
                               waitting_list=waitting_list)

    if request.method == 'POST':
        data = request.json

        if data['case'] == 'deluser':
            Team.update_members(pid=pid, tid=tid, del_uids=[
                data['uid'],
            ])
        elif data['case'] == 'history':
            history = []
            for raw in WaitList.find_history_in_team(uid=data['uid'],
                                                     pid=pid,
                                                     tid=tid):
                raw['_id'] = str(raw['_id'])
                history.append(raw)

            return jsonify({'history': history})
        elif data['case'] == 'members':
            result_members = []
            if team['members'] or team['chiefs']:
                _all_uids = set(team['chiefs']) | set(team['members'])
                users_info = User.get_info(list(_all_uids))
                for uid in _all_uids:
                    result_members.append(users_info[uid])

                for user in result_members:
                    user['chat'] = {}
                    mid = MattermostTools.find_possible_mid(uid=user['_id'])
                    if mid:
                        user['chat'] = {
                            'mid': mid,
                            'name': MattermostTools.find_user_name(mid=mid)
                        }

                    user['phone'] = {'country_code': '', 'phone': ''}
                    if 'phone' in user['profile_real'] and user[
                            'profile_real']['phone']:
                        phone = phonenumbers.parse(
                            user['profile_real']['phone'])
                        user['phone']['country_code'] = phonenumbers.COUNTRY_CODE_TO_REGION_CODE[phone.country_code][0]  # pylint: disable=line-too-long
                        user['phone']['phone'] = phonenumbers.format_number(
                            phone, phonenumbers.PhoneNumberFormat.NATIONAL)

                result_members = sorted(
                    result_members, key=lambda u: u['profile']['badge_name'])

                return jsonify({
                    'members':
                    result_members,
                    'tags':
                    team.get('tag_members', []),
                    'members_tags':
                    Team.get_members_tags(pid=pid, tid=tid),
                })

        elif data['case'] == 'add_tag':
            result = Team.add_tag_member(pid=pid,
                                         tid=tid,
                                         tag_name=data['tag_name'])
            return jsonify({'tag': result})

        elif data['case'] == 'update_member_tags':
            team_tags = [i['id'] for i in team.get('tag_members', [])]
            team_members = set(team['members'] + team['chiefs'])

            tag_datas = {}
            for uid in team_members:
                if uid in data['data']:
                    tag_datas[uid] = {
                        'tags': list(set(team_tags) & set(data['data'][uid]))
                    }

            if tag_datas:
                Team.add_tags_to_members(pid=pid, tid=tid, data=tag_datas)

            return jsonify({'data': tag_datas})

        elif data['case'] == 'del_tag':
            Team.del_tag(pid=pid, tid=tid, tag_id=data['tag']['id'])

            return jsonify({})

        return jsonify(data)

    return jsonify({})