Skip to content

Commit

Permalink
return object ids and count, fixes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
helrond committed Sep 12, 2019
1 parent 52c1e53 commit e1a4f7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
19 changes: 10 additions & 9 deletions bagdiscovery/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def run(self):
self.log.debug("Found {} bags to process".format(len(Bag.objects.filter(bag_path__isnull=True))))
bags = Bag.objects.filter(bag_path__isnull=True)
bag_count = 0
bag_ids = []
for bag in bags:
self.bag_name = "{}.tar.gz".format(bag.bag_identifier)
self.log.bind(object=bag.bag_identifier)
Expand All @@ -53,32 +54,32 @@ def run(self):
self.log.debug("Bag unpacked")
except Exception as e:
self.log.error("Error unpacking bag: {}".format(e))
raise BagDiscoveryException("Error unpacking bag: {}".format(e))
raise BagDiscoveryException("Error unpacking bag: {}".format(e), bag.bag_identifier)
try:
self.save_bag_data(bag)
self.log.debug("Bag data saved")
except Exception as e:
self.log.error("Error saving bag data: {}".format(e))
raise BagDiscoveryException("Error saving bag data: {}".format(e))
raise BagDiscoveryException("Error saving bag data: {}".format(e), bag.bag_identifier)
try:
self.move_bag(bag)
self.log.debug("Bag moved to storage")
except Exception as e:
self.log.error("Error moving bag: {}".format(e))
raise BagDiscoveryException("Error moving bag: {}".format(e))
raise BagDiscoveryException("Error moving bag: {}".format(e), bag.bag_identifier)

if self.url:
try:
self.post_to_fornax(bag, self.url)
except Exception as e:
raise BagDiscoveryException("Error sending metadata to Fornax: {}".format(e))
raise BagDiscoveryException("Error sending metadata to Fornax: {}".format(e), bag.bag_identifier)

bag_count += 1

else:
continue

return "{} bags discovered and stored.".format(bag_count)
return ("All bags discovered and stored.", bag_ids, bag_count)

def unpack_bag(self):
tf = tarfile.open(os.path.join(self.src_dir, self.bag_name), 'r')
Expand Down Expand Up @@ -117,17 +118,17 @@ def __init__(self, identifier, dirs):
self.identifier = identifier
self.dest_dir = dirs['dest'] if dirs else settings.DEST_DIR
if not self.identifier:
raise CleanupException("No identifier submitted, unable to perform CleanupRoutine.")
raise CleanupException("No identifier submitted, unable to perform CleanupRoutine.", None)

def run(self):
try:
self.filepath = "{}.tar.gz".format(os.path.join(self.dest_dir, self.identifier))
if os.path.isfile(self.filepath):
os.remove(self.filepath)
return "Transfer {} removed.".format(self.identifier)
return "Transfer {} was not found.".format(self.identifier)
return ("Transfer removed.", self.identifier, 1)
return ("Transfer was not found.", self.identifier, 1)
except Exception as e:
raise CleanupException(e)
raise CleanupException(e, self.identifier)


class DataValidator:
Expand Down
13 changes: 7 additions & 6 deletions bagdiscovery/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import urllib
from django.db import IntegrityError
from django.template.response import SimpleTemplateResponse
from django.shortcuts import render
from jsonschema.exceptions import ValidationError
from rest_framework.views import APIView
Expand Down Expand Up @@ -110,10 +111,10 @@ def post(self, request, format=None):
url = (urllib.parse.unquote(url) if url else '')

try:
discover = BagDiscovery(url, dirs).run()
return Response({"detail": discover}, status=200)
msg, objs, count = BagDiscovery(url, dirs).run()
return Response({"detail": msg, "objects": objs, "count": count}, status=200)
except Exception as e:
return Response({"detail": str(e)}, status=500)
return Response({"detail": str(e.args[0]), "object": e.args[1]}, status=500)


class CleanupRoutineView(APIView):
Expand All @@ -124,7 +125,7 @@ def post(self, request, format=None):
identifier = request.data.get('identifier')

try:
discover = CleanupRoutine(identifier, dirs).run()
return Response({"detail": discover}, status=200)
msg, objs, count = CleanupRoutine(identifier, dirs).run()
return Response({"detail": msg, "objects": objs, "count": count}, status=200)
except Exception as e:
return Response({"detail": str(e)}, status=500)
return Response({"detail": str(e.args[0]), "object": e.args[1]}, status=500)

0 comments on commit e1a4f7b

Please sign in to comment.