[MIG] document_page_tags

This commit is contained in:
Holger Brunn 2018-07-18 14:31:54 +02:00 committed by Marcel Savegnago
parent 621d5284cd
commit 2767b218bf
11 changed files with 83 additions and 210 deletions

View File

@ -1,21 +1,4 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright 2015-2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import models
from .hooks import post_init_hook

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Tags/Keywords for document page",
"version": "10.0.1.0.0",
"author": "Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Knowledge Management",
"summary": "Allows you to assign tags or keywords to pages and search for "
"them afterwards",
"depends": [
'document_page',
],
"data": [
"views/document_page_tag.xml",
"views/document_page.xml",
'security/ir.model.access.csv',
],
"installable": True,
}

View File

@ -1,37 +0,0 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
{
"name": "Tags/Keywords for document page",
"version": "8.0.1.0.0",
"author": "Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Knowledge Management",
"summary": "Allows you to assign tags or keywords to pages and search for "
"them afterwards",
"depends": [
'document_page',
],
"data": [
"views/document_page.xml",
'security/ir.model.access.csv',
],
"installable": True,
"post_init_hook": 'post_init_hook',
}

View File

@ -1,53 +0,0 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
import re
from openerp import SUPERUSER_ID
from openerp.api import Environment
def post_init_hook(cr, pool):
# in case we have an old tags column from the wiki module lying around,
# populate our tags with its content
for column in ['openupgrade_legacy_7_0_tags', 'tags']:
cr.execute('SELECT column_name FROM information_schema.columns '
'WHERE table_name=%s and column_name=%s',
(pool['document.page']._table, column))
if cr.fetchall():
populate_tags(cr, column)
def populate_tags(cr, column):
env = Environment(cr, SUPERUSER_ID, {})
document_page_tag = env['document.page.tag']
document_page = env['document.page']
# pylint: disable=E8103
cr.execute(
'SELECT %s, ARRAY_AGG(id) from %s WHERE %s IS NOT NULL GROUP BY %s' % (
column, env['document.page']._table, column, column))
for keywords, ids in cr.fetchall():
pages = document_page.browse(ids)
tag_ids = []
for keyword in re.split(r'\W+', keywords):
tag = document_page_tag.search([('name', '=ilike', keyword)])
if tag:
tag_ids.append(tag.id)
else:
tag_ids.append(document_page_tag.name_create(keyword)[0])
pages.write({'tag_ids': [(6, 0, set(tag_ids))]})

View File

@ -1,21 +1,5 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright 2015-2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import document_page
from . import document_page_tag

View File

@ -1,22 +1,6 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module copyright (C) 2015 Therp BV (<http://therp.nl>).
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright 2015-2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from openerp import models, fields

View File

@ -1,31 +1,23 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module copyright (C) 2015 Therp BV (<http://therp.nl>).
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models, fields
# Copyright 2015-2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from openerp import api, models, fields
class DocumentPageTag(models.Model):
_name = 'document.page.tag'
_description = 'A keyword for document pages'
name = fields.Char(translate=True)
name = fields.Char(required=True, translate=True)
_sql_constraints = [
('unique_name', 'unique(name)', 'Tags must me unique'),
]
@api.model
def create(self, vals):
"""Be nice when trying to create duplicates"""
existing = self.search([('name', '=ilike', vals['name'])])
if existing:
return existing
return super(DocumentPageTag, self).create(vals)

View File

@ -1,3 +1,3 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_document_page_tag_all,access_document_page_tag,model_document_page_tag,,1,0,0,0
access_document_page_tag,access_document_page_tag,model_document_page_tag,base.group_user,1,1,1,1
access_document_page_tag,access_document_page_tag,model_document_page_tag,base.group_user,1,0,1,1

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_document_page_tag_all access_document_page_tag model_document_page_tag 1 0 0 0
3 access_document_page_tag access_document_page_tag model_document_page_tag base.group_user 1 1 0 1 1

View File

@ -1,34 +1,22 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright 2015-2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from psycopg2 import IntegrityError
from openerp.tests.common import TransactionCase
from ..hooks import post_init_hook
from openerp.tools.misc import mute_logger
class TestDocumentPageTags(TransactionCase):
def test_document_page_tags(self):
# run our init hook for coverage
post_init_hook(self.cr, self.registry)
testtag = self.env['document.page.tag'].name_create('test')
# check we're charitable on duplicates
self.assertEqual(
testtag, self.env['document.page.tag'].name_create('Test'),
)
# check we can't create nonunique tags
with self.assertRaises(IntegrityError):
with mute_logger('openerp.sql_db'):
self.env['document.page.tag'].name_create('test')
self.env['document.page.tag'].name_create('test')
with mute_logger('odoo.sql_db'):
testtag2 = self.env['document.page.tag'].create({
'name': 'test2',
})
testtag2.write({'name': 'test'})

View File

@ -1,23 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="view_wiki_form" model="ir.ui.view">
<field name="model">document.page</field>
<field name="inherit_id" ref="document_page.view_wiki_form" />
<field name="arch" type="xml">
<field name="parent_id" position="after">
<field name="tag_ids" widget="many2many_tags" />
</field>
<odoo>
<record id="view_wiki_form" model="ir.ui.view">
<field name="model">document.page</field>
<field name="inherit_id" ref="document_page.view_wiki_form" />
<field name="arch" type="xml">
<field name="parent_id" position="after">
<field name="tag_ids" widget="many2many_tags" />
</field>
</record>
<record id="view_wiki_filter" model="ir.ui.view">
<field name="model">document.page</field>
<field name="inherit_id" ref="document_page.view_wiki_filter" />
<field name="arch" type="xml">
<field name="name" position="after">
<field name="tag_ids" widget="many2one" />
</field>
</field>
</record>
<record id="view_wiki_filter" model="ir.ui.view">
<field name="model">document.page</field>
<field name="inherit_id" ref="document_page.view_wiki_filter" />
<field name="arch" type="xml">
<field name="name" position="after">
<field name="tag_ids" widget="many2one" />
</field>
</record>
</data>
</openerp>
</field>
</record>
</odoo>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_document_page_tag_form" model="ir.ui.view">
<field name="model">document.page.tag</field>
<field name="arch" type="xml">
<form>
<group>
<field name="name" />
</group>
</form>
</field>
</record>
</odoo>