<commit_before># -*- coding: utf-8 -*-


# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <<EMAIL>>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""Controllers for simulations"""


from biryani1 import strings

from .. import contexts, conv, model, urls, wsgihelpers


@wsgihelpers.wsgify
def delete(req):
    ctx = contexts.Ctx(req)
    assert req.method == 'POST'

    session = ctx.session
    if session is None or session.user is None:
        return wsgihelpers.forbidden(ctx,
            explanation = ctx._("User simulations forbidden"),
            message = ctx._("You can not view this simulation."),
            title = ctx._('Operation denied'),
            )

    id_or_slug = req.urlvars.get('id_or_slug')
    simulation = conv.check(
        model.Simulation.make_id_or_slug_or_words_to_instance(user_id = session.user._id)(id_or_slug, state = ctx)
        )
    if simulation is None or simulation._id not in session.user.simulations_id:
        return wsgihelpers.not_found(ctx, explanation = ctx._(u'Simulation {} not found').format(id_or_slug))

    session.user.simulations_id.remove(simulation._id)
    if session.user.current_simulation_id == simulation._id:
        session.user.current_simulation_id = session.user.simulations_id[0] \
            if len(session.user.simulations_id) > 0 else None
    session.user.save(safe = True)
    simulation.delete(safe = True)
    return wsgihelpers.redirect(ctx, location = session.user.get_user_url(ctx))


@wsgihelpers.wsgify
def duplicate(req):
    ctx = contexts.Ctx(req)

    session = ctx.session
    if session is None or session.user is None:
        return wsgihelpers.forbidden(ctx,
            explanation = ctx._("View forbidden"),
            message = ctx._("You can not view an account."),<commit_msg>Fix error 500 when deleting last user's simulation.<commit_after># -*- coding: utf-8 -*-


# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <<EMAIL>>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""Controllers for simulations"""


import datetime

from biryani1 import strings

from .. import contexts, conv, model, urls, wsgihelpers


@wsgihelpers.wsgify
def delete(req):
    ctx = contexts.Ctx(req)
    assert req.method == 'POST'

    session = ctx.session
    if session is None or session.user is None:
        return wsgihelpers.forbidden(ctx,
            explanation = ctx._("User simulations forbidden"),
            message = ctx._("You can not view this simulation."),
            title = ctx._('Operation denied'),
            )

    id_or_slug = req.urlvars.get('id_or_slug')
    simulation = conv.check(
        model.Simulation.make_id_or_slug_or_words_to_instance(user_id = session.user._id)(id_or_slug, state = ctx)
        )
    if simulation is None or simulation._id not in session.user.simulations_id:
        return wsgihelpers.not_found(ctx, explanation = ctx._(u'Simulation {} not found').format(id_or_slug))

    session.user.simulations_id.remove(simulation._id)
    if len(session.user.simulations_id) == 0:
        simulation.delete(safe = True)
        simulation_date = datetime.datetime.utcnow()
        simulation_title = u'Ma simulation du {} à {}'.format(
            datetime.datetime.strftime(simulation_date, u'%d/%m/%Y'),
            datetime.datetime.strftime(simulation_date, u'%H:%M'),
            )
        simulation = model.Simulation(
            author_id = session.user._id,
            title = simulation_title,
            slug = strings.slugify(simulation_title),
            )
        simulation.save(safe = True)
        session.user.current_simulation = simulation
        session.user.simulations_id = [simulation._id]
    elif session.user.current_simulation_id == simulation._id:
        session.user.current_simulation = simulation
        session.user.current_simulation = session.user.simulations[0]
        simulation.delete(safe = True)
    session.user.save(safe = True)
    return wsgihelpers.redirect(ctx, location = session.user.get_user_url(ctx))


@wsgihelpers.wsgify
def duplicate(req):
    ctx = contexts.Ctx(req)

    session = ctx.session
    if session is None or session.user is None:
        return wsgihelpers.forbidden(ctx,
            explanation = ctx._("View forbidden"),
            message = ctx._("You can not view an account."),
{'fraction_non_alphanumeric': 0.0973802137194071, 'fraction_numerical': 0.0077559462254395035, 'mean_word_length': 3.2234352256186316, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2391060', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1700, 'n_words': 524}