Skip to content

Commit

Permalink
api: process files customization
Browse files Browse the repository at this point in the history
* Adds support for customizing file processing during deposit
  publishing.

Signed-off-by: Lars Holm Nielsen <lars.holm.nielsen@cern.ch>
  • Loading branch information
lnielsen committed Aug 1, 2016
1 parent 180b50f commit 352336b
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions invenio_deposit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,21 @@ def create(cls, data, id_=None):

return super(Deposit, cls).create(data, id_=id_)

@contextmanager
def _process_files(self, record_id, data):
"""Snapshot bucket and add files in record during first publishing."""
if self.files:
assert not self.files.bucket.locked
self.files.bucket.locked = True
snapshot = self.files.bucket.snapshot(lock=True)
data['_files'] = self.files.dumps(bucket=snapshot.id)
yield data
db.session.add(RecordsBuckets(
record_id=record_id, bucket_id=snapshot.id
))
else:
yield data

def _publish_new(self, id_=None):
"""Publish new deposit.
Expand All @@ -263,26 +278,9 @@ def _publish_new(self, id_=None):
data = dict(self.dumps())
data['$schema'] = self.record_schema

# During first publishing create snapshot the bucket.
@contextmanager
def process_files(data):
"""Process deposit files."""
if RecordsBuckets.query.filter_by(
record_id=self.model.id).count():
assert not self.files.bucket.locked
self.files.bucket.locked = True
snapshot = self.files.bucket.snapshot(lock=True)
data['_files'] = self.files.dumps(bucket=snapshot.id)
yield data
db.session.add(RecordsBuckets(
record_id=id_, bucket_id=snapshot.id
))
else:
yield data
with self._process_files(id_, data):
record = self.published_record_class.create(data, id_=id_)

with process_files(data) as data:
record = self.published_record_class.create(data,
id_=id_)
return record

def _publish_edited(self):
Expand Down

0 comments on commit 352336b

Please sign in to comment.