From 97f5b25534c652215d42a778a86a46bca5a291f7 Mon Sep 17 00:00:00 2001 From: youssef Date: Thu, 18 Oct 2018 17:05:15 +0100 Subject: [PATCH] Add constrains to member to save history --- golem_member_history/models/golem_member.py | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 golem_member_history/models/golem_member.py diff --git a/golem_member_history/models/golem_member.py b/golem_member_history/models/golem_member.py new file mode 100644 index 0000000..9f43bee --- /dev/null +++ b/golem_member_history/models/golem_member.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2018 Youssef El Ouahby +# Copyright 2018 Fabien Bourgeois +# +# This program 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. +# +# This program 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 . + +""" GOLEM Member adaptations """ + +from odoo import models, fields, api + + +class GolemMember(models.Model): + """ GOLEM Member adaptations """ + _inherit = 'golem.member' + + member_history_ids = fields.One2many('golem.member.history', 'member_id') + + @api.constrains('gender', 'area_id', 'city', 'family_quotient', 'pcs_id', 'nationality_id') + def save_history(self): + default_season = self.env['golem.season'].search([('is_default', '=', True)]) + for member in self: + history = self.env['golem.member.history'].search([('member_id', '=', member.id), + ('season_id', '=', default_season.id)]) + if history: + history[0].write({'gender': member.gender, + 'nationality_id': member.nationality_id.id, + 'city': member.city, + 'family_quotient': member.family_quotient, + 'pcs_id': member.pcs_id.id, + 'area_id': member.area_id.id}) + else: + self.env['golem.member.history'].create({'member_id': member.id, + 'season_id': default_season.id, + 'gender': member.gender, + 'nationality_id': member.nationality_id.id, + 'city': member.city, + 'family_quotient': member.family_quotient, + 'pcs_id': member.pcs_id.id, + 'area_id': member.area_id.id + })