Ejemplo n.º 1
0
def project_edit_create_team_api(pid):
    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':
        _team = Team.get(pid, request.args['tid'].strip())
        team = {}
        for k in ('name', 'chiefs', 'members', 'owners', 'tid', 'headcount',
                  'mailling', 'disabled'):
            if k in _team:
                team[k] = _team[k]

        if 'headcount' not in team:
            team['headcount'] = 0
        else:
            team['headcount'] = max([0, int(team['headcount'])])

        return jsonify(team)

    elif request.method == 'POST':
        data = request.json
        if data['submittype'] == 'update':
            Team.update_setting(pid=pid, tid=data['tid'], data=data)
            return u'%s' % data
        elif data['submittype'] == 'create':
            Team.create(pid=pid,
                        tid=data['tid'],
                        name=data['name'],
                        owners=project['owners'])
            return u'%s' % data
Ejemplo n.º 2
0
def project_edit_create_team_api(pid):
    ''' Project edit create team API '''
    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':
        _team = Team.get(pid, request.args['tid'].strip())
        team = {}
        for k in ('name', 'chiefs', 'members', 'owners', 'tid', 'headcount',
                  'mailling', 'disabled'):
            if k in _team:
                team[k] = _team[k]

        if 'headcount' not in team:
            team['headcount'] = 0
        else:
            team['headcount'] = max([0, int(team['headcount'])])

        return jsonify(team)

    if request.method == 'POST':
        data = request.json
        if data['submittype'] == 'update':
            chiefs = data['chiefs']
            members = data['members']
            if isinstance(data['chiefs'], str):
                chiefs = [
                    _uid.strip() for _uid in data['chiefs'].split(',')
                    if _uid.strip()
                ]

            if isinstance(data['members'], str):
                members = [
                    _uid.strip() for _uid in data['members'].split(',')
                    if _uid.strip()
                ]

            new_members = set(chiefs + members)
            old_members = set(
                Team.get_users(pid=pid, tids=(data['tid'], ))[data['tid']])

            TeamMemberChangedDB().make_record(pid=pid,
                                              tid=data['tid'],
                                              action={
                                                  'add':
                                                  new_members - old_members,
                                                  'del':
                                                  old_members - new_members
                                              })

            Team.update_setting(pid=pid, tid=data['tid'], data=data)
            service_sync_mattermost_add_channel.apply_async(
                kwargs={
                    'pid': pid,
                    'uids': list(new_members)
                })

            return f'{data}'

        if data['submittype'] == 'create':
            Team.create(pid=pid,
                        tid=data['tid'],
                        name=data['name'],
                        owners=project['owners'])
            return f'{data}'

    return '', 404