Esempio n. 1
0
def delete(objective_id,
           so1_id=None, so2_id=None, so3_id=None, so4_id=None):
    myargs = ['objective_id', 'so1_id', 'so2_id', 'so3_id', 'so4_id']
    parents = [(i, locals()[i]) for i in myargs if locals()[i] is not None]
    objective = mongo.db.objectives.find_one_or_404({'id': objective_id})

    from pymongo.errors import OperationFailure

    if len(parents) == 1:
        subobj_list = sugar.generate_objectives()[objective_id]
        # clean up entire object
        try:
            mongo.db.objectives.remove(spec_or_id=objective['_id'], safe=True)
            flask.flash(_("Objective deleted."), "success")
        except OperationFailure:
            flask.flash(_("Errors found when deleting objective."), "errors")
            pass
    else:
        mask = str(objective['id'])
        father = objective
        parent = objective
        for i in range(1, len(parents)):
            son_id = parents[i][1]
            try:
                son = [s for s in father['subobjs'] if s['id'] == son_id][0]
            except IndexError:
                flask.abort(404)
            parent = father
            father = son
            mask = ".".join([mask, str(son['id'])])

        subobj_list = sugar.get_subobjs_of_subobj(father, mask)
        # clean up entire subobjective from its parent list
        parent['subobjs'] = [s for s in parent['subobjs'] if s['id'] !=
                             father['id']]
        try:
            mongo.db.objectives.save(objective, safe=True)
            flask.flash(_("Objective deleted."), "success")
        except OperationFailure:
            flask.flash(_("Errors found when deleting objective."), "errors")
            pass

    # clean up all mappings for subobjectives
    for subobj_id in subobj_list:
        mappings = mongo.db.mapping.find()
        related_mappings_ids = [m['_id'] for m in mappings
                                if m['objective'] == subobj_id]
        for mapping_id in related_mappings_ids:
            try:
                mongo.db.mapping.remove(spec_or_id=mapping_id)
            except OperationFailure:
                flask.flash(_("Errors found when deleting"
                              "corresponding mappings."), "errors")
                pass

    return flask.jsonify({'status': 'success'})
Esempio n. 2
0
def mapping_edit(mapping_id=None):
    # check if valid objectives exist
    objs = mongo.db.objectives.find()
    valid_mapping = [True for obj in objs if obj['subobjs']]
    if not valid_mapping:
        return flask.redirect(flask.url_for('goals.default_mapping_routing'))

    import bson
    app = flask.current_app
    objectives = sugar.generate_objectives()
    eu_display = flask.current_app.config['DISPLAY_EU_STRATEGY']

    if mapping_id:
        objectid = bson.objectid.ObjectId(oid=mapping_id)
        mapping = mongo.db.mapping.find_one_or_404({'_id': objectid})

        ms = get_mapping_schema(show_eu=eu_display,
                                based_on=mapping)
        ms.set_objectives(objectives)
        ms['objective'].set(mapping['objective'])
        ms['main_target'].valid_values = map(str,
                                             ms['main_target'].valid_values)
        ms['main_target'].set(mapping['main_target'])
    else:
        ms = get_mapping_schema(show_eu=eu_display)
        ms.set_objectives(objectives)

    if flask.request.method == "POST":
        initial_form = flask.request.form
        form_data = initial_form.to_dict()
        targets_list = initial_form.getlist('other_targets')

        if app.config['DISPLAY_EU_STRATEGY']:
            eu_actions_list = initial_form.getlist('eu_actions')
            eu_targets_list = initial_form.getlist('eu_targets')

        try:
            targets_list.remove(form_data['main_target'])
        except ValueError:
            pass
        if mapping_id:
            ms['goal'].set(form_data['goal'])
        else:
            ms = get_mapping_schema(show_eu=eu_display,
                                    based_on=form_data)
            ms.set_objectives(objectives)

        ms['objective'].set(form_data['objective'])
        ms['other_targets'].set(targets_list)

        if app.config['DISPLAY_EU_STRATEGY']:
            ms['eu_actions'].set(eu_actions_list)
            ms['eu_targets'].set(eu_targets_list)

        ms['main_target'].valid_values = map(str,
                                             ms['main_target'].valid_values)
        ms['main_target'].set(form_data['main_target'])

        if ms.validate():
            if mapping_id:
                mapping.update(ms.flatten())
                mapping['_id'] = bson.objectid.ObjectId(oid=mapping_id)
            else:
                mapping = ms.flatten()
                mapping['_id'] = bson.objectid.ObjectId()
            mongo.db.mapping.save(mapping)
            flask.flash(_("Mapping saved"), "success")
            return flask.redirect(flask.url_for('goals.mapping'))

        else:
            flask.flash(_("Errors in mapping information"), "error")

    return {
        "mk": sugar.MarkupGenerator(app.jinja_env.get_template
                                    ("widgets/widgets_edit_data.html")),
        "mapping_schema": ms,
        "mapping_id": mapping_id
    }