Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

models: add override column #81

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions invenio_collections/alembic/3d3604b9f140_add_override_column.py
Original file line number Diff line number Diff line change
@@ -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',
)
11 changes: 9 additions & 2 deletions invenio_collections/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,7 +35,7 @@ class Collection(db.Model, BaseNestedSets):
def __repr__(self):
"""Return class representation."""
return ('Collection <id: {0.id}, name: {0.name}, '
'dbquery: {0.dbquery}>'.format(self))
'dbquery: {0.dbquery}, override: {0.override}>'.format(self))

id = db.Column(db.Integer, primary_key=True)
"""Collection identifier."""
Expand All @@ -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."""
Expand Down
5 changes: 3 additions & 2 deletions invenio_collections/receivers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
90 changes: 56 additions & 34 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -177,44 +177,63 @@ def test_tree(app):
result = runner.invoke(cmd, ['tree'], obj=script_info)
assert 0 == result.exit_code
aspected = (
"Collection <id: 1, name: a, dbquery: None>\n "
"+-- Collection <id: 2, name: b, dbquery: None>\n "
"| +-- Collection <id: 4, name: c, dbquery: title:Test0>\n "
"| +-- Collection <id: 5, name: d, dbquery: title:Test1>\n "
"Collection <id: 1, name: a, dbquery: None, override: False>\n "
"+-- Collection <id: 2, name: b, dbquery: None, "
"override: False>\n "
"| +-- Collection <id: 4, name: c, dbquery: title:Test0, "
"override: False>\n "
"| +-- Collection <id: 5, name: d, dbquery: title:Test1, "
"override: False>\n "
"+-- Collection <id: 3, name: e, dbquery: "
"title:Test2 OR title:Test3>\n "
" +-- Collection <id: 6, name: f, dbquery: title:Test2>\n "
" +-- Collection <id: 7, name: g, dbquery: None>\n "
" | +-- Collection <id: 9, name: i, dbquery: title:Test3>\n "
" +-- Collection <id: 8, name: h, dbquery: None>\n "
" +-- Collection <id: 10, name: j, dbquery: title:Test4>\n"
"title:Test2 OR title:Test3, override: False>\n "
" +-- Collection <id: 6, name: f, dbquery: title:Test2, "
"override: False>\n "
" +-- Collection <id: 7, name: g, dbquery: None, "
"override: False>\n "
" | +-- Collection <id: 9, name: i, dbquery: title:Test3, "
"override: False>\n "
" +-- Collection <id: 8, name: h, dbquery: None, "
"override: False>\n "
" +-- Collection <id: 10, name: j, dbquery: title:Test4, "
"override: False>\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's one reason I don't like PEP8's 79 characters hard limit... : )

)
assert result.output == aspected

result = runner.invoke(cmd, ['tree', 'e'], obj=script_info)
assert 0 == result.exit_code
aspected = (
"Collection <id: 3, name: e, dbquery: "
"title:Test2 OR title:Test3>\n "
"+-- Collection <id: 6, name: f, dbquery: title:Test2>\n "
"+-- Collection <id: 7, name: g, dbquery: None>\n "
"| +-- Collection <id: 9, name: i, dbquery: title:Test3>\n "
"+-- Collection <id: 8, name: h, dbquery: None>\n "
" +-- Collection <id: 10, name: j, dbquery: title:Test4>\n"
"title:Test2 OR title:Test3, override: False>\n "
"+-- Collection <id: 6, name: f, dbquery: title:Test2, "
"override: False>\n "
"+-- Collection <id: 7, name: g, dbquery: None, "
"override: False>\n "
"| +-- Collection <id: 9, name: i, dbquery: title:Test3, "
"override: False>\n "
"+-- Collection <id: 8, name: h, dbquery: None, "
"override: False>\n "
" +-- Collection <id: 10, name: j, dbquery: title:Test4, "
"override: False>\n"
)
assert result.output == aspected

result = runner.invoke(cmd, ['tree', 'e', 'd'], obj=script_info)
assert 0 == result.exit_code
aspected = (
"Collection <id: 5, name: d, dbquery: title:Test1>\n"
"Collection <id: 5, name: d, dbquery: title:Test1, "
"override: False>\n"
"Collection <id: 3, name: e, dbquery: "
"title:Test2 OR title:Test3>\n "
"+-- Collection <id: 6, name: f, dbquery: title:Test2>\n "
"+-- Collection <id: 7, name: g, dbquery: None>\n "
"| +-- Collection <id: 9, name: i, dbquery: title:Test3>\n "
"+-- Collection <id: 8, name: h, dbquery: None>\n "
" +-- Collection <id: 10, name: j, dbquery: title:Test4>\n"
"title:Test2 OR title:Test3, override: False>\n "
"+-- Collection <id: 6, name: f, dbquery: title:Test2, "
"override: False>\n "
"+-- Collection <id: 7, name: g, dbquery: None, "
"override: False>\n "
"| +-- Collection <id: 9, name: i, dbquery: title:Test3, "
"override: False>\n "
"+-- Collection <id: 8, name: h, dbquery: None, "
"override: False>\n "
" +-- Collection <id: 10, name: j, dbquery: title:Test4, "
"override: False>\n"
)
assert result.output == aspected

Expand Down Expand Up @@ -265,37 +284,40 @@ def test_path(app):
result = runner.invoke(cmd, ['path', 'a'], obj=script_info)
assert 0 == result.exit_code
aspected = (
"Collection <id: 1, name: a, dbquery: None>\n"
"Collection <id: 1, name: a, dbquery: None, override: False>\n"
)
assert result.output == aspected

result = runner.invoke(cmd, ['path', 'e'], obj=script_info)
assert 0 == result.exit_code
aspected = (
"Collection <id: 1, name: a, dbquery: None>\n "
"Collection <id: 1, name: a, dbquery: None, override: False>\n "
"+-- Collection <id: 3, name: e, dbquery: "
"title:Test2 OR title:Test3>\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 <id: 1, name: a, dbquery: None>\n "
"Collection <id: 1, name: a, dbquery: None, override: False>\n "
"+-- Collection <id: 3, name: e, dbquery: "
"title:Test2 OR title:Test3>\n "
" +-- Collection <id: 7, name: g, dbquery: None>\n"
"title:Test2 OR title:Test3, override: False>\n "
" +-- Collection <id: 7, name: g, dbquery: None, "
"override: False>\n"
)
assert result.output == aspected

result = runner.invoke(cmd, ['path', 'i'], obj=script_info)
assert 0 == result.exit_code
aspected = (
"Collection <id: 1, name: a, dbquery: None>\n "
"Collection <id: 1, name: a, dbquery: None, override: False>\n "
"+-- Collection <id: 3, name: e, dbquery: "
"title:Test2 OR title:Test3>\n "
" +-- Collection <id: 7, name: g, dbquery: None>\n "
" +-- Collection <id: 9, name: i, dbquery: title:Test3>\n"
"title:Test2 OR title:Test3, override: False>\n "
" +-- Collection <id: 7, name: g, dbquery: None, "
"override: False>\n "
" +-- Collection <id: 9, name: i, dbquery: title:Test3, "
"override: False>\n"
)
assert result.output == aspected

Expand Down
8 changes: 5 additions & 3 deletions tests/test_invenio_collections.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -93,8 +93,10 @@ def test_view(app):
db.session.add(collection)
db.session.commit()
assert 1 == collection.id
assert 'Collection <id: 1, name: Test, dbquery: None>' == repr(
collection)
assert (
'Collection <id: 1, name: Test, '
'dbquery: None, override: False>'
) == repr(collection)

with app.test_client() as client:
res = client.get(view_url)
Expand Down