<commit_before># -*- encoding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2014 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __openerp__.py
#
##############################################################################
from openerp import netsvc
from openerp.osv import orm, fields
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF
from openerp.tools.translate import _

from datetime import datetime


class compassion_project(orm.Model):
    """ Add update method. """
    _inherit = 'compassion.project'

    def update(self, cr, uid, args, context=None):
        """ When we receive a notification that a project has been updated,
        we fetch the last informations. """
        project_id = args.get('object_id')
        return self.update_informations(cr, uid, project_id, context)


class compassion_child(orm.Model):
    """ Add allocation and deallocation methods on the children. """
    _inherit = 'compassion.child'

    def _get_child_states(self, cr, uid, context=None):
        """ Add new error state used when an incoming message for a child
        could not be processed. """
        res = super(compassion_child, self)._get_child_states(cr, uid, context)
        res.append(('E', _('Error')))
        return res

    _columns = {
        # New field to save previous state if child goes
        # in error state
        'previous_state': fields.char('Previous state', size=1),
        'state': fields.selection(
            _get_child_states, _("Status"), select=True, readonly=True,
            track_visibility="onchange", required=True),
    }

    def allocate(self, cr, uid, args, context=None):
        child_id = args.get('object_id')
        if child_id:
            # Child already exists, put it back to available state
            # and erase exit details
            child = self.browse(cr, uid, child_id, context)
            if child.state == 'P':
                raise orm.except_orm(
                    _("Child allocation error"),
                    _("The child that will be allocated is sponsored. "
                      "Maybe someone forgot to terminate the sponsorship ? "
                      "Please verify information of child %s.") % child.code)
            if child.state in ('F', 'X'):
                # Start a new workflow making the child available again
                wf_service = netsvc.LocalService('workflow')
                wf_service.trg_create(uid, self._name, child_id, cr)
                child.write({
                    'exit_date': False,
                    'exit_reason': False,
                    'gp_exit_reason': False})
        else:
            # Allocate new child
            child_vals = {
                'code': args.get('code'),
                'date': args.get('date')}
            args['object_id'] = self._insert_new_child(cr, uid, child_vals,
                                                       context)

        # Commit the allocation, in case update information triggers an
        # error, so that the child is still saved in database.
        cr.commit()

        # Update all information of child
        args['event'] = 'Allocate'
        return self.update(cr, uid, args, context=context)

    def deallocate(self, cr, uid, args, context=None):
        """Deallocate child.
        This happens only for unsponsored children that are no longer
        assigned to Switzerland.
        """
        return self.write(cr, uid, args.get('object_id'), {
            'state': 'X', 'exit_date': args.get('date')}, context)

    def depart(self, cr, uid, args, context=None):
        """When a depart is done automatically when processing a message:
        1. If the child is sponsored, the sponsorship is terminated with the
           'Child Departure' ending reason.
        2. The child is marked as departed (GetExitDetails API is called)
        3. GP_Exit_Reason is inferred if possible.
        """
        if context is None:
            context = dict()
        child = self.browse(cr, uid, args.get('object_id'), context)
        if child.sponsor_id:
            for contract in child.sponsorship_ids:
                if contract.state in ('waiting', 'active', 'mandate'):
                    # Terminate contract and mark the departure
                    contract.write({
                        'end_reason': '1',  # Child departure
                        'end_date': datetime.today().strftime(DF),
                        'gmc_state': 'depart'})

                    wf_service = netsvc.LocalService('workflow')
                    wf_service.trg_validate(
                        uid, 'recurring.contract', contract.id,
                        'contract_terminated', cr)

        if child.state != 'F':
            child.write({'state': 'F'})
            # Reload child to get the ExitDetails information, and infer
            # GP Exit reason
            ctx = context.copy()
            ctx['lang'] = 'en_US'
            child = self.browse(cr, uid, child.id, ctx)
            if child.exit_reason:
                for gp_exit in self.get_gp_exit_reasons(cr, uid, ctx):
                    if child.exit_reason.startswith(gp_exit[1]):
                        child.write({'gp_exit_reason': gp_exit[0]})
                        break

        return True

    def update(self, cr, uid, args, context=None):
        """ When we receive a notification that child has been updated,
        we fetch the information of child given what was updated. """
        res = True
        if not args.get('object_id'):
            raise orm.except_orm(<commit_msg>Bug fix for ending sponsorships from message_center<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2014 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __openerp__.py
#
##############################################################################
from openerp import netsvc
from openerp.osv import orm, fields
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF
from openerp.tools.translate import _

from datetime import datetime


class compassion_project(orm.Model):
    """ Add update method. """
    _inherit = 'compassion.project'

    def update(self, cr, uid, args, context=None):
        """ When we receive a notification that a project has been updated,
        we fetch the last informations. """
        project_id = args.get('object_id')
        return self.update_informations(cr, uid, project_id, context)


class compassion_child(orm.Model):
    """ Add allocation and deallocation methods on the children. """
    _inherit = 'compassion.child'

    def _get_child_states(self, cr, uid, context=None):
        """ Add new error state used when an incoming message for a child
        could not be processed. """
        res = super(compassion_child, self)._get_child_states(cr, uid, context)
        res.append(('E', _('Error')))
        return res

    _columns = {
        # New field to save previous state if child goes
        # in error state
        'previous_state': fields.char('Previous state', size=1),
        'state': fields.selection(
            _get_child_states, _("Status"), select=True, readonly=True,
            track_visibility="onchange", required=True),
    }

    def allocate(self, cr, uid, args, context=None):
        child_id = args.get('object_id')
        if child_id:
            # Child already exists, put it back to available state
            # and erase exit details
            child = self.browse(cr, uid, child_id, context)
            if child.state == 'P':
                raise orm.except_orm(
                    _("Child allocation error"),
                    _("The child that will be allocated is sponsored. "
                      "Maybe someone forgot to terminate the sponsorship ? "
                      "Please verify information of child %s.") % child.code)
            if child.state in ('F', 'X'):
                # Start a new workflow making the child available again
                wf_service = netsvc.LocalService('workflow')
                wf_service.trg_create(uid, self._name, child_id, cr)
                child.write({
                    'exit_date': False,
                    'exit_reason': False,
                    'gp_exit_reason': False})
        else:
            # Allocate new child
            child_vals = {
                'code': args.get('code'),
                'date': args.get('date')}
            args['object_id'] = self._insert_new_child(cr, uid, child_vals,
                                                       context)

        # Commit the allocation, in case update information triggers an
        # error, so that the child is still saved in database.
        cr.commit()

        # Update all information of child
        args['event'] = 'Allocate'
        return self.update(cr, uid, args, context=context)

    def deallocate(self, cr, uid, args, context=None):
        """Deallocate child.
        This happens only for unsponsored children that are no longer
        assigned to Switzerland.
        """
        return self.write(cr, uid, args.get('object_id'), {
            'state': 'X', 'exit_date': args.get('date')}, context)

    def depart(self, cr, uid, args, context=None):
        """When a depart is done automatically when processing a message:
        1. If the child is sponsored, the sponsorship is terminated with the
           'Child Departure' ending reason.
        2. The child is marked as departed (GetExitDetails API is called)
        3. GP_Exit_Reason is inferred if possible.
        """
        if context is None:
            context = dict()
        child = self.browse(cr, uid, args.get('object_id'), context)
        if child.sponsor_id:
            for contract in child.sponsorship_ids:
                if contract.state in ('waiting', 'active', 'mandate'):
                    # Terminate contract and mark the departure
                    ctx = context.copy()
                    ctx['active_model'] = 'recurring.contract'
                    ctx['active_id'] = contract.id
                    self.pool.get('recurring.contract').write(
                        cr, uid, contract.id, {
                            'end_reason': '1',  # Child departure
                            'end_date': datetime.today().strftime(DF),
                            'gmc_state': 'depart'}, ctx)

                    wf_service = netsvc.LocalService('workflow')
                    wf_service.trg_validate(
                        uid, 'recurring.contract', contract.id,
                        'contract_terminated', cr)

        if child.state != 'F':
            child.write({'state': 'F'})
            # Reload child to get the ExitDetails information, and infer
            # GP Exit reason
            ctx = context.copy()
            ctx['lang'] = 'en_US'
            child = self.browse(cr, uid, child.id, ctx)
            if child.exit_reason:
                for gp_exit in self.get_gp_exit_reasons(cr, uid, ctx):
                    if child.exit_reason.startswith(gp_exit[1]):
                        child.write({'gp_exit_reason': gp_exit[0]})
                        break

        return True

    def update(self, cr, uid, args, context=None):
        """ When we receive a notification that child has been updated,
        we fetch the information of child given what was updated. """
        res = True
        if not args.get('object_id'):
            raise orm.except_orm(
{'fraction_non_alphanumeric': 0.12358026784200712, 'fraction_numerical': 0.0020342430920494998, 'mean_word_length': 1.9977134146341464, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6714565', 'n_tokens_mistral': 3156, 'n_tokens_neox': 2973, 'n_words': 977}