<commit_before># Copyright 2018 <NAME>/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from datetime import datetime

from dateutil.relativedelta import relativedelta

from odoo import _, api, fields, models
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT

SELECTION_COMMITTEE_AVAILABLE_STATES = [
    ("draft", "In Progress"),
    ("done", "Closed"),
]


class AbstractSelectionCommittee(models.Model):
    _name = "abstract.selection.committee"
    _description = "Abstract Selection Committee"
    _inherit = ["mozaik.abstract.model"]
    _unicity_keys = "N/A"

    _candidature_model = "abstract.candidature"
    _assembly_model = "abstract.assembly"
    _assembly_category_model = "abstract.assembly.category"
    _parameters_key = False

    state = fields.Selection(
        selection=SELECTION_COMMITTEE_AVAILABLE_STATES,
        string="Status",
        tracking=True,
        default=SELECTION_COMMITTEE_AVAILABLE_STATES[0][0],
    )
    mandate_category_id = fields.Many2one(
        comodel_name="mandate.category",
        string="Mandate Category",
        required=True,
        tracking=True,
    )
    assembly_id = fields.Many2one(
        comodel_name=_assembly_model,
        string="Abstract Assembly",
        tracking=True,
    )
    candidature_ids = fields.One2many(
        comodel_name=_candidature_model,
        inverse_name="selection_committee_id",
        string="Abstract Candidatures",
        domain=[("active", "<=", True)],
        copy=False,
    )
    assembly_category_id = fields.Many2one(
        comodel_name=_assembly_category_model,
        string="Abstract Assembly Category",
    )
    designation_int_assembly_id = fields.Many2one(
        comodel_name="int.assembly",
        string="Designation Assembly",
        required=True,
        tracking=True,
        domain=[("is_designation_assembly", "=", True)],
    )
    decision_date = fields.Date(
        string="Designation Date",
        tracking=True,
        copy=False,
    )
    mandate_start_date = fields.Date(
        string="Mandates Start Date",
        required=True,
        tracking=True,
    )
    mandate_deadline_date = fields.Date(
        string="Mandates Deadline Date",
        required=True,
        tracking=True,
    )
    name = fields.Char(
        string="Name",
        size=128,
        index=True,
        required=True,
        tracking=True,
    )
    note = fields.Text(string="Notes", tracking=True, copy=False)
    int_instance_id = fields.Many2one(
        comodel_name="int.instance",
        string="Internal Instance",
        tracking=True,
    )

    # constraints

    _sql_constraints = [
        (
            "date_check",
            "CHECK(mandate_start_date <= mandate_deadline_date)",
            "The start date must be anterior to the deadline date.",
        ),
    ]

    # orm methods

    def name_get(self):
        res = []
        for committee in self:
            display_name = u"{assembly}/{start} ({name})".format(
                assembly=committee.assembly_id.name,
                start=committee.mandate_start_date or False,
                name=committee.name,
            )
            res.append((committee["id"], display_name))
        return res

    @api.model
    def _name_search(self, name, args=None, operator="ilike", limit=100):
        if not args:
            args = []
        if name:
            assembly_ids = self.env[self._assembly_model].search(
                [("name", operator, name)]
            )
            records = self.search(
                ["|", ("name", operator, name), ("assembly_id", "in", assembly_ids)]
                + args,
                limit=limit,
            )
        else:
            records = self.search(args, limit=limit)
        return records.ids

    def copy_data(self, default=None):
        if default is None:
            default = {}
        default.update(
            {
                "active": True,
                "state": SELECTION_COMMITTEE_AVAILABLE_STATES[0][0],
            }
        )
        res = super(AbstractSelectionCommittee, self).copy_data(default=default)

        res.update(
            {
                "name": _("%s (copy)") % res.get("name"),
            }
        )
        return res

    # view methods: onchange, button

    def button_designate_candidatures(self):
        """
        ==========================
        button_designate_candidatures
        ==========================
        This method calls the candidature workflow for each candidature_id
        in order to update their state
        :rparam: True
        :rtype: boolean
        """
        for candidature in self.candidature_ids:
            if candidature.state == "declared":
                candidature.button_designate()
        return True

    def button_non_elect_candidatures(self):
        for candidature in self.candidature_ids:
            if candidature.state != "elected":
                candidature.button_non_elected_candidature()
        self.action_invalidate({"state": "done"})
        return True

    @api.onchange("assembly_id")
    def onchange_assembly_id(self):
        self.ensure_one()
        if self.assembly_id:
            self.mandate_category_id = self.env["mandate.category"].search(
                [
                    (
                        self._mandate_catefory_foreign_key,
                        "=",
                        self.assembly_id.assembly_category_id.id,
                    )
                ],
                limit=1,
            )
            if "designation_int_assembly_id" in self.env[self._assembly_model]._fields:
                self.designation_int_assembly_id = self.assembly_id.id<commit_msg>[FIX] mozaik_committee typo<commit_after># Copyright 2018 <NAME>/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from datetime import datetime

from dateutil.relativedelta import relativedelta

from odoo import _, api, fields, models
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT

SELECTION_COMMITTEE_AVAILABLE_STATES = [
    ("draft", "In Progress"),
    ("done", "Closed"),
]


class AbstractSelectionCommittee(models.Model):
    _name = "abstract.selection.committee"
    _description = "Abstract Selection Committee"
    _inherit = ["mozaik.abstract.model"]
    _unicity_keys = "N/A"

    _candidature_model = "abstract.candidature"
    _assembly_model = "abstract.assembly"
    _assembly_category_model = "abstract.assembly.category"
    _parameters_key = False

    state = fields.Selection(
        selection=SELECTION_COMMITTEE_AVAILABLE_STATES,
        string="Status",
        tracking=True,
        default=SELECTION_COMMITTEE_AVAILABLE_STATES[0][0],
    )
    mandate_category_id = fields.Many2one(
        comodel_name="mandate.category",
        string="Mandate Category",
        required=True,
        tracking=True,
    )
    assembly_id = fields.Many2one(
        comodel_name=_assembly_model,
        string="Abstract Assembly",
        tracking=True,
    )
    candidature_ids = fields.One2many(
        comodel_name=_candidature_model,
        inverse_name="selection_committee_id",
        string="Abstract Candidatures",
        domain=[("active", "<=", True)],
        copy=False,
    )
    assembly_category_id = fields.Many2one(
        comodel_name=_assembly_category_model,
        string="Abstract Assembly Category",
    )
    designation_int_assembly_id = fields.Many2one(
        comodel_name="int.assembly",
        string="Designation Assembly",
        required=True,
        tracking=True,
        domain=[("is_designation_assembly", "=", True)],
    )
    decision_date = fields.Date(
        string="Designation Date",
        tracking=True,
        copy=False,
    )
    mandate_start_date = fields.Date(
        string="Mandates Start Date",
        required=True,
        tracking=True,
    )
    mandate_deadline_date = fields.Date(
        string="Mandates Deadline Date",
        required=True,
        tracking=True,
    )
    name = fields.Char(
        string="Name",
        size=128,
        index=True,
        required=True,
        tracking=True,
    )
    note = fields.Text(string="Notes", tracking=True, copy=False)
    int_instance_id = fields.Many2one(
        comodel_name="int.instance",
        string="Internal Instance",
        tracking=True,
    )

    # constraints

    _sql_constraints = [
        (
            "date_check",
            "CHECK(mandate_start_date <= mandate_deadline_date)",
            "The start date must be anterior to the deadline date.",
        ),
    ]

    # orm methods

    def name_get(self):
        res = []
        for committee in self:
            display_name = u"{assembly}/{start} ({name})".format(
                assembly=committee.assembly_id.name,
                start=committee.mandate_start_date or False,
                name=committee.name,
            )
            res.append((committee["id"], display_name))
        return res

    @api.model
    def _name_search(self, name, args=None, operator="ilike", limit=100):
        if not args:
            args = []
        if name:
            assembly_ids = self.env[self._assembly_model].search(
                [("name", operator, name)]
            )
            records = self.search(
                ["|", ("name", operator, name), ("assembly_id", "in", assembly_ids)]
                + args,
                limit=limit,
            )
        else:
            records = self.search(args, limit=limit)
        return records.ids

    def copy_data(self, default=None):
        if default is None:
            default = {}
        default.update(
            {
                "active": True,
                "state": SELECTION_COMMITTEE_AVAILABLE_STATES[0][0],
            }
        )
        res = super(AbstractSelectionCommittee, self).copy_data(default=default)

        res.update(
            {
                "name": _("%s (copy)") % res.get("name"),
            }
        )
        return res

    # view methods: onchange, button

    def button_designate_candidatures(self):
        """
        ==========================
        button_designate_candidatures
        ==========================
        This method calls the candidature workflow for each candidature_id
        in order to update their state
        :rparam: True
        :rtype: boolean
        """
        for candidature in self.candidature_ids:
            if candidature.state == "declared":
                candidature.button_designate()
        return True

    def button_non_elect_candidatures(self):
        for candidature in self.candidature_ids:
            if candidature.state != "elected":
                candidature.button_non_elected_candidature()
        self.action_invalidate({"state": "done"})
        return True

    @api.onchange("assembly_id")
    def onchange_assembly_id(self):
        self.ensure_one()
        if self.assembly_id:
            self.mandate_category_id = self.env["mandate.category"].search(
                [
                    (
                        self._mandate_category_foreign_key,
                        "=",
                        self.assembly_id.assembly_category_id.id,
                    )
                ],
                limit=1,
            )
            if "designation_int_assembly_id" in self.env[self._assembly_model]._fields:
                self.designation_int_assembly_id = self.assembly_id.id
{'fraction_non_alphanumeric': 0.1090972464273266, 'fraction_numerical': 0.004008365284071105, 'mean_word_length': 2.0711800909820712, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '7483713', 'n_tokens_mistral': 3340, 'n_tokens_neox': 3209, 'n_words': 587}