Beispiel #1
0
def team_edit_user_api(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':
        user = User(uid=request.args['uid']).get()
        user_waitting = WaitList.list_by_team(pid=pid, tid=tid, uid=user['_id'])
        if not user_waitting:
            return jsonify({})

        users_info = User.get_info([user['_id'], ])

        user_data = {
            'badge_name': users_info[user['_id']]['profile']['badge_name'],
            'picture': users_info[user['_id']]['oauth']['picture'],
            'uid': user['_id'],
            'note': user_waitting['note'],
            'wid': u'%(_id)s' % user_waitting,
        }

        return jsonify(user_data)

    elif request.method == 'POST':
        all_members = len(team['members']) + len(team['chiefs'])
        if 'headcount' in team and team['headcount'] and all_members >= team['headcount']:
            return jsonify({'status': 'fail', 'message': 'over headcount.'}), 406

        data = request.json
        w = WaitList.make_result(wid=data['wid'], pid=pid, uid=data['uid'], result=data['result'])
        if w and 'result' in w:
            if w['result'] == 'approval':
                Team.update_members(pid=pid, tid=tid, add_uids=[data['uid'], ])
            elif w['result'] == 'deny':
                TeamMemberChangedDB().make_record(pid=pid, tid=tid, deny_uids=(data['uid'], ))

        return jsonify({'status': 'ok'})
Beispiel #2
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)
Beispiel #3
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({})