forked from inspirehep/invenio-records
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* FIX Uses nested transactions instead of sub-transactions to persist record modifications. (addresses inveniosoftware#22) * NEW Adds new celery task to save a new record to the database. * Moves in record related tasks from `invenio-collection` and Elasticsearch extension. Signed-off-by: Esteban J. G. Gabancho <[email protected]>
- Loading branch information
Showing
10 changed files
with
232 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
|
||
from .version import __version__ | ||
|
||
from .receivers import record_modification | ||
|
||
__all__ = ( | ||
'__version__', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2015 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. | ||
|
||
from flask import current_app | ||
from flask_sqlalchemy import models_committed | ||
|
||
|
||
# FIXME add after_delete | ||
@models_committed.connect | ||
def record_modification(sender, changes): | ||
from .models import RecordMetadata | ||
from .tasks.index import index_record | ||
for obj, change in changes: | ||
if isinstance(obj, RecordMetadata) and change in ('insert', 'update'): | ||
index_record.delay(obj.id, obj.json) | ||
|
||
|
||
def new_collection(mapper, connection, target): | ||
if target.dbquery is not None: | ||
index_collection_percolator.delay(target.name, target.dbquery) |
72 changes: 72 additions & 0 deletions
72
invenio_records/recordext/functions/get_record_collections.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2015 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. | ||
|
||
"""Record field function.""" | ||
|
||
from invenio_records.signals import ( | ||
before_record_insert, | ||
before_record_update, | ||
) | ||
from six import iteritems | ||
|
||
from invenio_utils.datastructures import LazyDict | ||
from invenio_search.api import Query | ||
|
||
COLLECTIONS_DELETED_RECORDS = '{dbquery} AND NOT collection:"DELETED"' | ||
|
||
|
||
def _queries(): | ||
"""Preprocess collection queries.""" | ||
from invenio_ext.sqlalchemy import db | ||
from invenio_collections.models import Collection | ||
return dict( | ||
(collection.name, dict( | ||
query=Query(COLLECTIONS_DELETED_RECORDS.format( | ||
dbquery=collection.dbquery) | ||
), | ||
ancestors=set(c.name for c in collection.ancestors | ||
if c.dbquery is None) | ||
)) | ||
for collection in Collection.query.filter( | ||
Collection.dbquery.isnot(None), | ||
db.not_(Collection.dbquery.like('hostedcollection:%')) | ||
).all() | ||
) | ||
|
||
queries = LazyDict(_queries) | ||
|
||
|
||
def get_record_collections(record): | ||
"""Return list of collections to which record belongs to. | ||
:record: Record instance | ||
:returns: list of collection names | ||
""" | ||
output = set() | ||
for name, data in iteritems(queries): | ||
if data['query'].match(record): | ||
output.add(name) | ||
output |= data['ancestors'] | ||
return list(output) | ||
|
||
|
||
@before_record_insert.connect | ||
@before_record_update.connect | ||
def update_collections(sender, *args, **kwargs): | ||
sender['_collections'] = get_record_collections(sender) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2015 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. | ||
|
||
from __future__ import absolute_import | ||
|
||
|
||
from celery.utils.log import get_task_logger | ||
|
||
from invenio_celery import celery | ||
|
||
from ..api import Record | ||
|
||
logger = get_task_logger(__name__) | ||
|
||
|
||
@celery.task | ||
def create_record(json, force=False): | ||
from invenio_ext.sqlalchemy import db | ||
try: | ||
return Record.create(json).get('recid') | ||
except exc.IntegrityError: | ||
if force: | ||
current_app.logger.warning( | ||
"Trying to force insert: {0}".format(json)) | ||
return Record(json).commit().get('recid') | ||
finally: | ||
db.session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2015 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. | ||
|
||
from invenio_celery import celery | ||
from invenio_ext.es import es | ||
|
||
|
||
@celery.task | ||
def index_record(recid, json): | ||
"""Index a record in elasticsearch.""" | ||
es.index( | ||
index='records', | ||
doc_type='record', | ||
body=json, | ||
id=recid | ||
) | ||
|
||
|
||
@celery.task | ||
def index_collection_percolator(name, dbquery): | ||
"""Create an elasticsearch percolator for a given query.""" | ||
from invenio_search.api import Query | ||
from invenio_search.walkers.elasticsearch import ElasticSearchDSL | ||
es.index( | ||
index='records', | ||
doc_type='.percolator', | ||
body={'query': Query(dbquery).query.accept(ElasticSearchDSL())}, | ||
id=name | ||
) |