From c989018027756c04d66c625a0dd250613824d218 Mon Sep 17 00:00:00 2001 From: Jacopo Notarstefano Date: Mon, 21 Aug 2017 15:42:31 +0200 Subject: [PATCH] models: add override column Signed-off-by: Jacopo Notarstefano --- .../3d3604b9f140_add_override_column.py | 55 ++++++++++++ invenio_collections/models.py | 11 ++- invenio_collections/receivers.py | 5 +- tests/test_cli.py | 90 ++++++++++++------- tests/test_invenio_collections.py | 8 +- 5 files changed, 128 insertions(+), 41 deletions(-) create mode 100644 invenio_collections/alembic/3d3604b9f140_add_override_column.py diff --git a/invenio_collections/alembic/3d3604b9f140_add_override_column.py b/invenio_collections/alembic/3d3604b9f140_add_override_column.py new file mode 100644 index 0000000..d66e95b --- /dev/null +++ b/invenio_collections/alembic/3d3604b9f140_add_override_column.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Invenio. +# Copyright (C) 2017 CERN. +# +# Invenio is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# Invenio 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 +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Invenio; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307, USA. +# +# In applying this license, CERN does not +# waive the privileges and immunities granted to it by virtue of its status +# as an Intergovernmental Organization or submit itself to any jurisdiction. + +"""Add override column.""" + +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic. +revision = '3d3604b9f140' +down_revision = '97faa437d867' +branch_labels = None +depends_on = None + + +def upgrade(): + """Upgrade database.""" + op.add_column( + 'collection', + sa.Column( + 'override', + sa.Boolean(create_constraint=False), + default=False, + nullable=False, + ), + ) + + +def downgrade(): + """Downgrade database.""" + op.drop_column( + 'collection', + 'override', + ) diff --git a/invenio_collections/models.py b/invenio_collections/models.py index 79d71a2..f2346e6 100644 --- a/invenio_collections/models.py +++ b/invenio_collections/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 CERN. +# Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017 CERN. # # Invenio is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -35,7 +35,7 @@ class Collection(db.Model, BaseNestedSets): def __repr__(self): """Return class representation.""" return ('Collection '.format(self)) + 'dbquery: {0.dbquery}, override: {0.override}>'.format(self)) id = db.Column(db.Integer, primary_key=True) """Collection identifier.""" @@ -46,6 +46,13 @@ def __repr__(self): dbquery = db.Column(db.Text, nullable=True) """Query that returns all records that belong to this collection..""" + override = db.Column( + db.Boolean(create_constraint=False), + default=False, + nullable=False, + ) + """Whether to override `dbquery` and return `True` or not.""" + @validates('parent_id') def validate_parent_id(self, key, parent_id): """Parent has to be different from itself.""" diff --git a/invenio_collections/receivers.py b/invenio_collections/receivers.py index ce7a58b..5f1588c 100644 --- a/invenio_collections/receivers.py +++ b/invenio_collections/receivers.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2015, 2016 CERN. +# Copyright (C) 2015, 2016, 2017 CERN. # # Invenio is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -55,6 +55,7 @@ def _build_cache(): yield collection.name, dict( query=query.format(dbquery=collection.dbquery), ancestors=set(_ancestors(collection)), + override=collection.override, ) raise StopIteration @@ -66,7 +67,7 @@ def _find_matching_collections_internally(collections, record): :param record: record to match """ for name, data in iteritems(collections): - if _build_query(data['query']).match(record): + if data['override'] or _build_query(data['query']).match(record): yield data['ancestors'] raise StopIteration diff --git a/tests/test_cli.py b/tests/test_cli.py index 319feff..e4b31fa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2015 CERN. +# Copyright (C) 2015, 2017 CERN. # # Invenio is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License as @@ -177,17 +177,25 @@ def test_tree(app): result = runner.invoke(cmd, ['tree'], obj=script_info) assert 0 == result.exit_code aspected = ( - "Collection \n " - "+-- Collection \n " - "| +-- Collection \n " - "| +-- Collection \n " + "Collection \n " + "+-- Collection \n " + "| +-- Collection \n " + "| +-- Collection \n " "+-- Collection \n " - " +-- Collection \n " - " +-- Collection \n " - " | +-- Collection \n " - " +-- Collection \n " - " +-- Collection \n" + "title:Test2 OR title:Test3, override: False>\n " + " +-- Collection \n " + " +-- Collection \n " + " | +-- Collection \n " + " +-- Collection \n " + " +-- Collection \n" ) assert result.output == aspected @@ -195,26 +203,37 @@ def test_tree(app): assert 0 == result.exit_code aspected = ( "Collection \n " - "+-- Collection \n " - "+-- Collection \n " - "| +-- Collection \n " - "+-- Collection \n " - " +-- Collection \n" + "title:Test2 OR title:Test3, override: False>\n " + "+-- Collection \n " + "+-- Collection \n " + "| +-- Collection \n " + "+-- Collection \n " + " +-- Collection \n" ) assert result.output == aspected result = runner.invoke(cmd, ['tree', 'e', 'd'], obj=script_info) assert 0 == result.exit_code aspected = ( - "Collection \n" + "Collection \n" "Collection \n " - "+-- Collection \n " - "+-- Collection \n " - "| +-- Collection \n " - "+-- Collection \n " - " +-- Collection \n" + "title:Test2 OR title:Test3, override: False>\n " + "+-- Collection \n " + "+-- Collection \n " + "| +-- Collection \n " + "+-- Collection \n " + " +-- Collection \n" ) assert result.output == aspected @@ -265,37 +284,40 @@ def test_path(app): result = runner.invoke(cmd, ['path', 'a'], obj=script_info) assert 0 == result.exit_code aspected = ( - "Collection \n" + "Collection \n" ) assert result.output == aspected result = runner.invoke(cmd, ['path', 'e'], obj=script_info) assert 0 == result.exit_code aspected = ( - "Collection \n " + "Collection \n " "+-- Collection \n" + "title:Test2 OR title:Test3, override: False>\n" ) assert result.output == aspected result = runner.invoke(cmd, ['path', 'g'], obj=script_info) assert 0 == result.exit_code aspected = ( - "Collection \n " + "Collection \n " "+-- Collection \n " - " +-- Collection \n" + "title:Test2 OR title:Test3, override: False>\n " + " +-- Collection \n" ) assert result.output == aspected result = runner.invoke(cmd, ['path', 'i'], obj=script_info) assert 0 == result.exit_code aspected = ( - "Collection \n " + "Collection \n " "+-- Collection \n " - " +-- Collection \n " - " +-- Collection \n" + "title:Test2 OR title:Test3, override: False>\n " + " +-- Collection \n " + " +-- Collection \n" ) assert result.output == aspected diff --git a/tests/test_invenio_collections.py b/tests/test_invenio_collections.py index a0fa3cc..25091b8 100644 --- a/tests/test_invenio_collections.py +++ b/tests/test_invenio_collections.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2015, 2016 CERN. +# Copyright (C) 2015, 2016, 2017 CERN. # # Invenio is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License as @@ -93,8 +93,10 @@ def test_view(app): db.session.add(collection) db.session.commit() assert 1 == collection.id - assert 'Collection ' == repr( - collection) + assert ( + 'Collection ' + ) == repr(collection) with app.test_client() as client: res = client.get(view_url)