[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 ;
* Add SQL on migration script to handle old column name inherited from
m2m ;
* Add base security on new model ;
This commit is contained in:
Fabien BOURGEOIS 2018-10-24 07:57:15 +02:00
parent aa9eb113f1
commit 0a035ce504
7 changed files with 72 additions and 73 deletions

View File

@ -18,12 +18,13 @@
{ {
'name': 'GOLEM members minor', 'name': 'GOLEM members minor',
'summary': 'GOLEM non-profit members minor management', 'summary': 'GOLEM non-profit members minor management',
'version': '10.0.1.1.3', 'version': '10.0.1.2.0',
'category': 'GOLEM', 'category': 'GOLEM',
'author': 'Fabien Bourgeois', 'author': 'Fabien Bourgeois',
'license': 'AGPL-3', 'license': 'AGPL-3',
'application': False, 'application': False,
'installable': True, 'installable': True,
'depends': ['golem_member'], '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 cursor.execute('''INSERT INTO golem_legal_guardian(member_id, legal_guardian_id) VALUES
(%s, %s)''' % (member_id, legal_guardian_id)) (%s, %s)''' % (member_id, legal_guardian_id))
cursor.execute('DROP TABLE golem_member_res_partner_rel') 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 -*- # -*- coding: utf-8 -*-
# Copyright 2017 Fabien Bourgeois <fabien@yaltik.com> # 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 # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- 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 # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # 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 # 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/>. # 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 import models, fields, _
from odoo.exceptions import ValidationError
class GolemLegalGuardian(models.Model): class GolemLegalGuardian(models.Model):
""" Golem Legal Guardian Management """ """ GOLEM Legal Guardian Management """
_name = 'golem.legal.guardian' _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, member_id = fields.Many2one('golem.member', required=True,
ondelete='cascade', index=True, auto_join=True,
readonly=True) ondelete='cascade')
legal_guardian_id = fields.Many2one('res.partner', required=True, legal_guardian_id = fields.Many2one(
domain="[('is_company', '=', False)]", 'res.partner', required=True, index=True, auto_join=True,
ondelete='cascade') string='Legal guardian', ondelete='cascade',
name = fields.Char(related="legal_guardian_id.name") domain="[('is_company', '=', False)]"
contact_address = fields.Char(related="legal_guardian_id.contact_address") )
phone = fields.Char(related="legal_guardian_id.phone") contact_address = fields.Char(related='legal_guardian_id.contact_address')
mobile = fields.Char(related="legal_guardian_id.mobile") phone = fields.Char(related='legal_guardian_id.phone')
email = fields.Char(related="legal_guardian_id.email") mobile = fields.Char(related='legal_guardian_id.mobile')
email = fields.Char(related='legal_guardian_id.email')
is_default_guardian = fields.Boolean() 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 """ """ GOLEM Member Minor management """
from datetime import date, timedelta 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) ADULT_DURATION = timedelta(days=365.25*18)
@ -51,3 +52,16 @@ class GolemMember(models.Model):
else: else:
operator = '<=' if value else '>' operator = '<=' if value else '>'
return [('birthdate_date', operator, adult_date)] 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 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 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 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> <odoo>
<data> <data>
<!-- Form --> <!-- Forms -->
<record model="ir.ui.view" id="golem_member_form_inherit_minor"> <record model="ir.ui.view" id="golem_member_form_inherit_minor">
<field name="name">Add Minor Page/Tab</field> <field name="name">Add Minor Page/Tab</field>
<field name="model">golem.member</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="leave_alone"/>
<field name="partner_id" invisible="True" required="False" /> <field name="partner_id" invisible="True" required="False" />
<field name="is_minor" invisible="True" /> <field name="is_minor" invisible="True" />
<field name="legal_guardian_ids" context="{'default_member_id': active_id}"> <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>
</group> </group>
</page> </page>
</page> </page>
</field> </field>
</record> </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="name">GOLEM Legal Guardian Form</field>
<field name="model">golem.legal.guardian</field> <field name="model">golem.legal.guardian</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form> <form>
<group> <sheet>
<field name="member_id" invisible="1"/> <group>
<field name="legal_guardian_id"/> <field name="member_id" />
<field name="is_default_guardian"/> <field name="legal_guardian_id" />
</group> <field name="is_default_guardian" />
</group>
</sheet>
</form> </form>
</field> </field>
</record> </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 --> <!-- Search -->
<record model="ir.ui.view" id="golem_member_search_inherit_minor"> <record model="ir.ui.view" id="golem_member_search_inherit_minor">
<field name="name">Add Minor/Adult filters</field> <field name="name">Add Minor/Adult filters</field>