[REF][FIX]GOLEM Member Minor : new legal guardians

* Remove button to do default (and so browser reload) in favor of parent
constraint ;
* Ensures there is no double (two times same person) with SQL
constraint.
This commit is contained in:
Fabien BOURGEOIS 2018-10-24 07:57:15 +02:00
parent aa9eb113f1
commit da07c202f3
7 changed files with 72 additions and 73 deletions

View File

@ -18,12 +18,13 @@
{
'name': 'GOLEM members minor',
'summary': 'GOLEM non-profit members minor management',
'version': '10.0.1.1.3',
'version': '10.0.1.2.0',
'category': 'GOLEM',
'author': 'Fabien Bourgeois',
'license': 'AGPL-3',
'application': False,
'installable': True,
'depends': ['golem_member'],
'data': ['views/golem_member_views.xml']
'data': ['security/ir.model.access.csv',
'views/golem_member_views.xml']
}

View File

@ -27,3 +27,5 @@ def migrate(cursor, version):
cursor.execute('''INSERT INTO golem_legal_guardian(member_id, legal_guardian_id) VALUES
(%s, %s)''' % (member_id, legal_guardian_id))
cursor.execute('DROP TABLE golem_member_res_partner_rel')
cursor.execute('DELETE FROM ir_model_fields WHERE name=\'legal_guardian_ids\' '
'AND ttype=\'many2many\'')

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Fabien Bourgeois <fabien@yaltik.com>
# Copyright 2018 Youssef El Ouahby <youssef@yaltik.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Fabien Bourgeois <fabien@yaltik.com>
# Copyright 2018 Youssef El Ouahby <youssef@yaltik.com>
# Copyright 2018 Fabien Bourgeois <fabien@yaltik.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@ -15,58 +16,29 @@
# 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/>.
""" Golem Legal Guardian Management """
""" GOLEM Legal Guardian Management """
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
from odoo import models, fields, _
class GolemLegalGuardian(models.Model):
""" Golem Legal Guardian Management """
""" GOLEM Legal Guardian Management """
_name = 'golem.legal.guardian'
_description = 'GOLEM Legal Guardian Management'
_sql_constraints = [(
'golem_legal_guardian_uniq', 'UNIQUE (member_id, legal_guardian_id)',
_('There are doubles in your legal guardians. Please check your fills.')
)]
member_id = fields.Many2one('golem.member', required=True,
ondelete='cascade',
readonly=True)
legal_guardian_id = fields.Many2one('res.partner', required=True,
domain="[('is_company', '=', False)]",
ondelete='cascade')
name = fields.Char(related="legal_guardian_id.name")
contact_address = fields.Char(related="legal_guardian_id.contact_address")
phone = fields.Char(related="legal_guardian_id.phone")
mobile = fields.Char(related="legal_guardian_id.mobile")
email = fields.Char(related="legal_guardian_id.email")
index=True, auto_join=True,
ondelete='cascade')
legal_guardian_id = fields.Many2one(
'res.partner', required=True, index=True, auto_join=True,
string='Legal guardian', ondelete='cascade',
domain="[('is_company', '=', False)]"
)
contact_address = fields.Char(related='legal_guardian_id.contact_address')
phone = fields.Char(related='legal_guardian_id.phone')
mobile = fields.Char(related='legal_guardian_id.mobile')
email = fields.Char(related='legal_guardian_id.email')
is_default_guardian = fields.Boolean()
def do_default_guardian(self):
""" Make current only default guardian """
self.ensure_one()
self.write({'is_default_guardian': True})
legal_list = self.member_id.legal_guardian_ids.filtered(
lambda a: a.legal_guardian_id not in self.legal_guardian_id)
legal_list.write({'is_default_guardian': False})
return {
'type': 'ir.actions.client',
'tag': 'reload'
}
@api.model
def create(self, values):
""" Make the current guardian is default if the only, and the only if default """
if values['is_default_guardian']:
self.env['golem.member'].browse(values['member_id']).legal_guardian_ids.write(
{'is_default_guardian': False})
if not self.env['golem.member'].browse(values['member_id']).legal_guardian_ids:
values['is_default_guardian'] = True
return super(GolemLegalGuardian, self).create(values)
@api.multi
def unlink(self):
""" Forbids default legal guardian removal """
for guardian in self:
if guardian.is_default_guardian and len(guardian.member_id.legal_guardian_ids) > 1:
emsg = _('You can\'t delete the default legal guardian')
raise ValidationError(emsg)
else:
return super(GolemLegalGuardian, self).unlink()

View File

@ -18,7 +18,8 @@
""" GOLEM Member Minor management """
from datetime import date, timedelta
from odoo import models, fields, api
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
ADULT_DURATION = timedelta(days=365.25*18)
@ -51,3 +52,16 @@ class GolemMember(models.Model):
else:
operator = '<=' if value else '>'
return [('birthdate_date', operator, adult_date)]
@api.constrains('legal_guardian_ids')
def check_default_guardian(self):
""" Ensures there is one and only one default guardian, no double """
for member in self:
if len(member.legal_guardian_ids):
default_guardians_count = len(member.legal_guardian_ids.filtered(
'is_default_guardian'
))
if not default_guardians_count or default_guardians_count > 1:
verr = _('You must have one and only one default legal '
'guardian. Please check your fills.')
raise ValidationError(verr)

View File

@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_golem_legal_guardian_user,Access GOLEM Legal Guardian User,model_golem_legal_guardian,golem_base.group_golem_user,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_golem_legal_guardian_user Access GOLEM Legal Guardian User model_golem_legal_guardian golem_base.group_golem_user 1 1 1 1

View File

@ -2,6 +2,7 @@
<!--
Copyright 2017-2018 Fabien Bourgeois <fabien@yaltik.com>
Copyright 2018 Youssef El Ouahby <youssef@yaltik.com>
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
@ -20,7 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<odoo>
<data>
<!-- Form -->
<!-- Forms -->
<record model="ir.ui.view" id="golem_member_form_inherit_minor">
<field name="name">Add Minor Page/Tab</field>
<field name="model">golem.member</field>
@ -34,39 +35,45 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<field name="leave_alone"/>
<field name="partner_id" invisible="True" required="False" />
<field name="is_minor" invisible="True" />
<field name="legal_guardian_ids" context="{'default_member_id': active_id}">
<tree>
<field name="name"/>
<field name="contact_address" />
<field name="phone" />
<field name="mobile" />
<field name="email" widget="email" />
<field name="is_default_guardian"/>
<button name="do_default_guardian" type="object"
icon="fa-bookmark"
attrs="{'invisible': [('is_default_guardian', '=', True)]}"/>
</tree>
</field>
<field name="legal_guardian_ids" context="{'default_member_id': active_id}" />
</group>
</page>
</page>
</field>
</record>
<!-- Forms -->
<record id="golem_legal_guardian_form" model="ir.ui.view">
<record id="golem_legal_guardian_view_form" model="ir.ui.view">
<field name="name">GOLEM Legal Guardian Form</field>
<field name="model">golem.legal.guardian</field>
<field name="arch" type="xml">
<form>
<group>
<field name="member_id" invisible="1"/>
<field name="legal_guardian_id"/>
<field name="is_default_guardian"/>
</group>
<sheet>
<group>
<field name="member_id" />
<field name="legal_guardian_id" />
<field name="is_default_guardian" />
</group>
</sheet>
</form>
</field>
</record>
<!-- Trees -->
<record id="golem_legal_guardian_view_tree" model="ir.ui.view">
<field name="name">GOLEM Legal Guardian Tree</field>
<field name="model">golem.legal.guardian</field>
<field name="arch" type="xml">
<tree editable="bottom">
<field name="legal_guardian_id" />
<field name="contact_address" />
<field name="phone" attrs="{'readonly': [('legal_guardian_id', '=', False)]}" />
<field name="mobile" attrs="{'readonly': [('legal_guardian_id', '=', False)]}" />
<field name="email" widget="email" attrs="{'readonly': [('legal_guardian_id', '=', False)]}" />
<field name="is_default_guardian" />
</tree>
</field>
</record>
<!-- Search -->
<record model="ir.ui.view" id="golem_member_search_inherit_minor">
<field name="name">Add Minor/Adult filters</field>