Skip to content

Commit

Permalink
[CHG] improve create/update thesaurus
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Nemry (ACSONE) committed Nov 4, 2015
1 parent f1be5a8 commit 2c226f6
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 19 deletions.
4 changes: 2 additions & 2 deletions mozaik_thesaurus/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
'images': [
],
'data': [
'security/ir.model.access.csv',
'data/thesaurus_data.xml',
'views/thesaurus_view.xml',
'wizard/thesaurus_terms_loader_view.xml',
# 'security/ir.model.access.csv',
# 'data/thesaurus_data.xml',
],
'sequence': 150,
'installable': True,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

from openerp.tools import SUPERUSER_ID
from openerp.modules.registry import RegistryManager

__name__ = "Truncate Table thesaurus and thesaurus_term"
Expand All @@ -10,9 +10,6 @@ def migrate(cr, version):
return

registry = RegistryManager.get(cr.dbname)
thesaurus_obj = registry['thesaurus']
thesaurus_term_obj = registry['thesaurus.term']
thesaurus_term_ids = thesaurus_term_obj.search(cr, SUPERUSER_ID, [])
thesaurus_term_obj.unlink(cr, SUPERUSER_ID, thesaurus_term_ids)
thesaurus_ids = thesaurus_obj.search(cr, SUPERUSER_ID, [])
thesaurus_term_obj.unlink(cr, SUPERUSER_ID, thesaurus_ids)
28 changes: 21 additions & 7 deletions mozaik_thesaurus/models/thesaurus.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def update_notification_term(self, newid=False):
if not newid:
# context: notrack when resetting the new term id to False to avoid
# a notification
ctx['mail_notrack']
ctx['mail_notrack'] = True
vals = {'new_thesaurus_term_id': newid}
return self.with_context(ctx).write(vals)

Expand All @@ -83,7 +83,7 @@ class ThesaurusTerm(models.Model):
_inherit = ['mozaik.abstract.model']
_description = 'Thesaurus Term'
_order = 'name'
_unicity_keys = 'ext_identifier'
_unicity_keys = 'technical_name'

@api.model
def _get_default_thesaurus_id(self):
Expand All @@ -99,18 +99,32 @@ def _check_ext_identifier(self):
raise Warning(
_('Missing External Identifier for a validated term'))

@api.one
@api.depends('name', 'state', 'expire_date')
def _compute_technical_name(self):
elts = [
'%s' % self.thesaurus_id.id,
self.state,
self.state == 'draft' and self.name or
self.state == 'confirm' and
self.ext_identifier or self.expire_date
]
self.technical_name = '#'.join([el for el in elts if el])

name = fields.Char(
string='Term', required=True, select=True, track_visibility='onchange')
string='Term', required=True, index=True, track_visibility='onchange')
thesaurus_id = fields.Many2one(
string='thesaurus', 'Thesaurus', readonly=True, required=True,
default=_get_default_thesaurus_id)
comodel_name='thesaurus', string='Thesaurus', readonly=True,
required=True, default=_get_default_thesaurus_id)
ext_identifier = fields.Char(
string='External Identifier', required=False, select=True,
string='External Identifier', required=False, index=True,
track_visibility='onchange', states={'confirm': [('required', True)]})
state = fields.Selection(
selection=TERM_AVAILABLE_STATES, string='Status', readonly=True,
required=True, track_visibility='onchange',
default=TERM_AVAILABLE_STATES[0][0])
technical_name = fields.Char(
compute='_compute_technical_name', index=True, store=True)

@api.model
@api.returns('self', lambda value: value.id)
Expand All @@ -128,7 +142,7 @@ def create(self, vals):
# Reset notification term on the thesaurus
self.thesaurus_id.update_notification_term()
# Set notification term on the thesaurus
self.thesaurus_id.update_notification_term(new_id=new_id)
self.thesaurus_id.update_notification_term(newid=new_id)
return new_id

@api.one
Expand Down
192 changes: 192 additions & 0 deletions mozaik_thesaurus/views/thesaurus_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,200 @@
<openerp>
<data noupdate="0">

<!-- Thesaurus -->

<record id="thesaurus_tree_view" model="ir.ui.view">
<field name="name">thesaurus.tree</field>
<field name="model">thesaurus</field>
<field name="arch" type="xml">
<tree string="Thesaurus" version="7.0">
<field name="name" />
</tree>
</field>
</record>

<record id="thesaurus_action" model="ir.actions.act_window">
<field name="name">Thesaurus</field>
<field name="res_model">thesaurus</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="thesaurus_tree_view" />
</record>

<record id="thesaurus_form_view" model="ir.ui.view">
<field name="name">thesaurus.form</field>
<field name="model">thesaurus</field>
<field name="arch" type="xml">
<form string="Thesaurus" version="7.0">
<header>
<div attrs="{'invisible': ['|',('id','=',False),('active','=',False)]}"
invisible="context.get('popup')"
groups="mozaik_base.mozaik_res_groups_configurator">
<button name="action_invalidate" string="Invalidate" type="object"
confirm="Invalidate thesaurus: proceed anyway?" />
</div>
</header>
<sheet>
<div class="oe_title" name="title">
<label for="name" class="oe_edit_only"/>
<h1><field name="name" /></h1>
</div>
<div class="oe_right oe_button_box" name="buttons" invisible="context.get('popup')">
</div>

<group name="status" attrs="{'invisible': [('id','=',False)]}">
<group name="validity">
<field name="create_date" />
<field name="expire_date" attrs="{'invisible': [('active','=',True)]}" />
</group>
<group name="other">
</group>
</group>

</sheet>
<div name="dev" class="oe_form_sheetbg" groups="mozaik_base.res_groups_developper">
<div class="oe_formview oe_form_sheet_width">
<h3>More Info</h3>
<group name="moreinfo">
<field name="id"/>
<field name="active"/>
<field name="new_thesaurus_term_id"/>
</group>
</div>
</div>
<div name="chat" class="oe_chatter" invisible="context.get('popup')">
<field name="message_follower_ids" widget="mail_followers" groups="mozaik_base.mozaik_res_groups_configurator"/>
<field name="message_ids" widget="mail_thread" />
</div>
</form>
</field>
</record>

<!-- Thesaurus Terms -->

<record id="thesaurus_term_search_view" model="ir.ui.view">
<field name="name">thesaurus.term.search</field>
<field name="model">thesaurus.term</field>
<field name="arch" type="xml">
<search string="Thesaurus Term" version="7.0">
<field name="name" />
<field name="state" />

<filter name="draft" string="To confirm" domain="[('state','in',['draft'])]" />
<filter name="confirm" string="Confirmed" domain="[('state','in',['confirmed'])]" />
<separator />
<filter name="expired" string="Expired"
context="{'invisible_expire_date': False}"
domain="[('active','=',False)]" help="Expired Terms" />
<filter name="all" string="All"
context="{'invisible_expire_date': False}"
domain="[('active', '&lt;=', True)]" />

<group expand="1" string="Group By..." name="group_by">
<filter name="status" string="Status" domain="[]" context="{'group_by':'state'}" />
</group>
</search>
</field>
</record>

<record id="thesaurus_term_tree_view" model="ir.ui.view">
<field name="name">thesaurus.term.tree</field>
<field name="model">thesaurus.term</field>
<field name="arch" type="xml">
<tree string="Thesaurus Terms" version="7.0"
colors="grey:active==False;red:state=='draft'">
<field name="name" />

<field name="expire_date"
invisible="context.get('invisible_expire_date', True)" />

<field name="state" invisible="1" />
<field name="active" invisible="1" />
</tree>
</field>
</record>

<!--delete id="thesaurus_term_action" model="ir.actions.act_window"/-->
<record id="thesaurus_term_action" model="ir.actions.act_window">
<field name="name">Thesaurus Terms</field>
<field name="res_model">thesaurus.term</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="thesaurus_term_tree_view" />
<field name="search_view_id" ref="thesaurus_term_search_view" />
<field name="context">{'search_default_draft': True}</field>
</record>

<record id="thesaurus_term_form_view" model="ir.ui.view">
<field name="name">thesaurus.term.form</field>
<field name="model">thesaurus.term</field>
<field name="arch" type="xml">
<form string="Thesaurus Term" version="7.0">
<header>
<field name="state" widget="statusbar" statusbar_visible="draft,confirm"/>
<div attrs="{'invisible': ['|',('id','=',False),('active','=',False)]}"
invisible="context.get('popup')"
groups="mozaik_base.mozaik_res_groups_configurator">
<button name="button_confirm" string="Confirm Term" type="object" states="draft" />
<button name="button_cancel" string="Cancel Term" type="object" states="confirm" />
<button name="button_reset" string="Reset Term" type="object" states="cancel" />
</div>
</header>
<sheet>
<div class="oe_title" name="title">
<label for="name" class="oe_edit_only"/>
<h1><field name="name" /></h1>
</div>
<div class="oe_right oe_button_box" name="buttons" invisible="context.get('popup')">
</div>

<group name="data">
<group name="main">
<field name="ext_identifier" />
</group>
<group name="other">
<field name="thesaurus_id" groups="mozaik_base.mozaik_res_groups_configurator" />
</group>
</group>

<group name="state" attrs="{'invisible': [('id','=',False)]}">
<group name="validity">
<field name="create_date" />
<field name="expire_date" attrs="{'invisible': [('active','=',True)]}" />
</group>
<group name="other">
</group>
</group>

</sheet>
<div name="dev" class="oe_form_sheetbg" groups="mozaik_base.res_groups_developper">
<div class="oe_formview oe_form_sheet_width">
<h3>More Info</h3>
<group name="moreinfo">
<field name="id"/>
<field name="technical_name"/>
<field name="active"/>
</group>
</div>
</div>
<div name="chat" class="oe_chatter" invisible="context.get('popup')">
<field name="message_follower_ids" widget="mail_followers" groups="mozaik_base.mozaik_res_groups_configurator"/>
<field name="message_ids" widget="mail_thread" />
</div>
</form>
</field>
</record>

<!-- Menu -->

<menuitem id="thesaurus_submenu" name="Thesaurus"
sequence="55" parent="mozaik_base.persons_configuration_submenu" />

<menuitem id="thesaurus_term_menu" action="thesaurus_term_action"
sequence="10" parent="thesaurus_submenu" />

<menuitem id="thesaurus_menu" action="thesaurus_action"
sequence="20" parent="thesaurus_submenu" />

</data>
</openerp>
16 changes: 10 additions & 6 deletions mozaik_thesaurus/wizard/thesaurus_terms_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _update_terms(
for identifier in to_update_identifiers:
t_term_id = thesaurus_term_ids.filtered(
lambda t: t.ext_identifier == identifier)
data_name = datas[identifier]['name']
data_name = datas[identifier]['name'].decode('UTF-8')
if data_name != t_term_id.name:
t_term_id.name = data_name

Expand All @@ -106,7 +106,8 @@ def _create_terms(
created
"""
for identifier in to_create_identifiers:
self.env['thesaurus.term'].create(datas[identifier])
vals = dict(datas[identifier], state='confirm')
self.env['thesaurus.term'].create(vals)

@api.one
def cu_terms(self, datas_file):
Expand All @@ -115,22 +116,25 @@ def cu_terms(self, datas_file):
:param datas_file: data structure of the csv
"""
keys = ['ext_identifier', 'name']
dict_datas = [dict(zip(keys, values)) for values in datas_file][0]
ext_identifiers = dict_datas.keys()
dict_datas = [dict(zip(keys, values)) for values in datas_file]
identifier_datas = {}
for dict_data in dict_datas:
identifier_datas[dict_data[keys[0]]] = dict_data
ext_identifiers = identifier_datas.keys()
thesaurus_term_ids = self.env['thesaurus.term'].sudo().search([])
existing_identifiers = [t.ext_identifier for t in thesaurus_term_ids]

to_update_identifiers =\
list(set(ext_identifiers) & set(existing_identifiers))
_logger.info('Start Updating Existing Terms')
self._update_terms(
dict_datas, to_update_identifiers, thesaurus_term_ids._ids)
identifier_datas, to_update_identifiers, thesaurus_term_ids._ids)
_logger.info('Existing Terms Updated')

to_create_identifiers =\
list(set(ext_identifiers) - set(existing_identifiers))
_logger.info('Start Creating New Terms')
self._create_terms(dict_datas, to_create_identifiers)
self._create_terms(identifier_datas, to_create_identifiers)
_logger.info('New Terms Created')

@api.one
Expand Down

0 comments on commit 2c226f6

Please sign in to comment.