コード例 #1
0
def delete_snippet():
    snippet_group = request.form.get('group')
    snippet_scenario = request.form.get('scenario')
    if not snippet_group or not snippet_scenario:
        abort(404)

    scenario_id_list = Snippet.search_group({
        'name': snippet_group
    }).get('scenarios')
    for _id in scenario_id_list:
        if Snippet.search_scenario({
                '_id': ObjectId(_id)
        }).get('name') == snippet_scenario:
            Snippet.delete_doc_scenario({'_id': ObjectId(_id)})
            if len(scenario_id_list) == 1:
                Snippet.delete_doc_group({'name': snippet_group})
            else:
                scenario_id_list.remove(_id)
                Snippet.update_doc_group({'name': snippet_group},
                                         {'scenarios': scenario_id_list})
            break

    flash('Code Snippet has been deleted successfully.', category='info')
    return redirect(url_for('main.index'))
コード例 #2
0
def edit_snippet(group, scenario):
    old_group_dict = Snippet.search_group({'name': group})
    if not old_group_dict:
        try:
            old_group_dict = Snippet.search_group({'_id': ObjectId(group)})
        except:
            abort(404)

    old_scenario_id_list = old_group_dict.get('scenarios')
    old_scenario_dict = {}
    old_scenario_id = ''
    old_scenario = scenario
    for _id in old_scenario_id_list:
        old_scenario_dict = Snippet.search_scenario({'_id': ObjectId(_id)})
        if old_scenario_dict.get('name') == scenario:
            old_scenario_id = _id
            break

    if not old_scenario_dict or not old_scenario_id:
        try:
            old_scenario_dict = Snippet.search_scenario(
                {'_id': ObjectId(scenario)})
            old_scenario = old_scenario_dict.get('name')
            old_scenario_id = scenario
        except:
            abort(404)

    old_code = old_scenario_dict.get('code')
    old_group = old_group_dict.get('name')
    group_id = str(old_group_dict.get('_id'))

    if request.form:
        new_code = request.form.get('code')
        new_group = request.form.get('group')
        new_scenario = request.form.get('scenario')

        if old_group_dict.get('name') != new_group:
            # case 1: group changed
            # remove old scenario id from old group, if scenario list after
            # the operation is empty, delete the group
            old_scenario_id_list.remove(old_scenario_id)

            if not len(old_scenario_id_list):
                # if group has no scenario anymore after the removal, delete
                # the group directly
                Snippet.delete_doc_group({'_id': ObjectId(group_id)})
            else:
                # otherwise update group's scenario list
                Snippet.update_doc_group({'_id': ObjectId(group_id)},
                                         {'scenarios': old_scenario_id_list})

            new_group_dict = Snippet.search_group({'name': new_group})
            if new_group_dict:
                # if new target group already exists
                # before group change, check whether old/new scenario name is
                # already in target group, if conflict exists then do a rename
                new_scenario_id_list = Snippet.search_group({
                    '_id':
                    ObjectId(new_group_dict.get('_id'))
                }).get('scenarios')

                if old_scenario != new_scenario:
                    # if scenario name changed, check new scenario name
                    # conflict
                    for _id in new_scenario_id_list:
                        if Snippet.search_scenario({
                                '_id': ObjectId(_id)
                        }).get('name') == new_scenario:
                            new_scenario = f'{new_scenario}_{random_word(size=4)}'
                            flash(
                                'A naming conflict occurs to Scenario\'s name in current Group. Current Scenario has been renamed by a suffix.',
                                category='danger')
                            break
                else:
                    # if scenario name unchanged, check old scenario name
                    # conflict
                    for _id in new_scenario_id_list:
                        if Snippet.search_scenario({
                                '_id': ObjectId(_id)
                        }).get('name') == old_scenario:
                            new_scenario = f'{old_scenario}_{random_word(size=4)}'
                            flash(
                                'A naming conflict occurs to Snippet Scenario in current Group. Current Snippet Scenario has been renamed by a suffix.',
                                category='danger')
                            break

                # move old scenario id to new group, update group_id, update
                # scenario's group name
                Snippet.update_doc_group({'name': new_group},
                                         {'scenarios': old_scenario_id})

            else:
                # target new group does not exist
                # create new group with the old scenario id, update group_id
                document = {'name': new_group, 'scenarios': [old_scenario_id]}
                Snippet.new_group(document)
            Snippet.update_doc_scenario({'_id': ObjectId(old_scenario_id)}, {
                'name': new_scenario,
                'group': new_group
            })

        elif old_scenario != new_scenario:
            # case 2: group not changed but scenario changed
            # if group not changed but scenario changed, check new scenario
            # naming conflict under current group
            scenario_id_list = Snippet.search_group({
                '_id': ObjectId(group_id)
            }).get('scenarios')
            for _id in scenario_id_list:
                if Snippet.search_scenario({
                        '_id': ObjectId(_id)
                }).get('name') == new_scenario:
                    new_scenario = f'{new_scenario}_{random_word(size=4)}'
                    flash(
                        'A naming conflict occurs to Snippet Scenario in current Group. Current Snippet Scenario has been renamed by a suffix.',
                        category='danger')
                    break
            Snippet.update_doc_scenario({'_id': ObjectId(old_scenario_id)},
                                        {'name': new_scenario})

        if old_code != new_code:
            Snippet.update_doc_scenario({'_id': ObjectId(old_scenario_id)},
                                        {'code': new_code})

        flash('Code snippet has been updated successfully.', category='info')
        return redirect(
            url_for('main.edit_snippet',
                    group=new_group,
                    scenario=new_scenario))
    return render_template('main/edit_snippet.html',
                           snippet_group=old_group,
                           snippet_scenario=old_scenario,
                           snippet_code=old_code)