def sync_es_aip_status_with_storage_service(uuid, es_status): """Update AIP's status in ES indices to match Storage Service. This is a bit of a kludge that is made necessary by the fact that the Storage Service does not update ElasticSearch directly when a package's status has changed. Updates to ES are visible in Archival Storage after running a new search or refreshing the page. :param uuid: AIP UUID. :param es_status: Current package status in ES. :returns: Boolean indicating whether AIP should be kept in search results (i.e. has not been deleted from Storage Service). """ keep_in_results = True amclient = setup_amclient() amclient.package_uuid = uuid api_results = amclient.get_package_details() if api_results in AMCLIENT_ERROR_CODES: logger.warning( "Package {} not found in Storage Service. AMClient error code: {}".format( uuid, api_results ) ) return keep_in_results aip_status = api_results.get("status") if not aip_status: logger.warning( "Status for package {} could not be retrived from Storage Service." ) return keep_in_results if ( aip_status == es.STATUS_DELETE_REQUESTED and es_status != es.STATUS_DELETE_REQUESTED ): es_client = es.get_client() es.mark_aip_deletion_requested(es_client, uuid) elif aip_status == es.STATUS_UPLOADED and es_status != es.STATUS_UPLOADED: es_client = es.get_client() es.revert_aip_deletion_request(es_client, uuid) elif aip_status == es.STATUS_DELETED: keep_in_results = False es_client = es.get_client() es.delete_aip(es_client, uuid) es.delete_aip_files(es_client, uuid) return keep_in_results
def view_aip(request, uuid): es_client = es.get_client() try: es_aip_doc = es.get_aip_data( es_client, uuid, fields="name,size,created,status,filePath,encrypted" ) except IndexError: raise Http404 source = es_aip_doc["_source"] name = source.get("name") active_tab = None form_upload = forms.UploadMetadataOnlyAtomForm(prefix="upload") form_reingest = forms.ReingestAIPForm(prefix="reingest") form_delete = forms.DeleteAIPForm(prefix="delete", uuid=uuid) # Process metadata-only DIP upload form if request.POST and "submit-upload-form" in request.POST: form_upload = forms.UploadMetadataOnlyAtomForm(request.POST, prefix="upload") active_tab = "upload" if form_upload.is_valid(): try: file_slug = upload_dip_metadata_to_atom( name, uuid, form_upload.cleaned_data["slug"] ) except AtomMetadataUploadError: messages.error( request, _( "Metadata-only DIP upload failed, check the logs for more details" ), ) logger.error( "Unexepected error during metadata-only DIP upload (UUID: %s)", uuid, exc_info=True, ) else: messages.success( request, _( "Metadata-only DIP upload has been completed successfully. New resource has slug: %(slug)s" ) % {"slug": file_slug}, ) form_upload = forms.UploadMetadataOnlyAtomForm( prefix="upload" ) # Reset form # Process reingest form if request.POST and "submit-reingest-form" in request.POST: form_reingest = forms.ReingestAIPForm(request.POST, prefix="reingest") active_tab = "reingest" if form_reingest.is_valid(): response = storage_service.request_reingest( uuid, form_reingest.cleaned_data["reingest_type"], form_reingest.cleaned_data["processing_config"], ) error = response.get("error", True) message = response.get("message", "An unknown error occurred.") if error: messages.error( request, _("Error re-ingesting package: %(message)s") % {"message": message}, ) else: messages.success(request, message) return redirect("archival_storage:archival_storage_index") # Process delete form if request.POST and "submit-delete-form" in request.POST: form_delete = forms.DeleteAIPForm(request.POST, prefix="delete", uuid=uuid) active_tab = "delete" if form_delete.is_valid(): response = storage_service.request_file_deletion( uuid, request.user.id, request.user.email, form_delete.cleaned_data["reason"], ) messages.info(request, response["message"]) es_client = es.get_client() es.mark_aip_deletion_requested(es_client, uuid) return redirect("archival_storage:archival_storage_index") context = { "uuid": uuid, "name": name, "created": source.get("created"), "status": AIP_STATUS_DESCRIPTIONS[source.get("status", es.STATUS_UPLOADED)], "encrypted": source.get("encrypted", False), "size": "{0:.2f} MB".format(source.get("size", 0)), "location_basename": os.path.basename(source.get("filePath")), "active_tab": active_tab, "forms": { "upload": form_upload, "reingest": form_reingest, "delete": form_delete, }, } return render(request, "archival_storage/view.html", context)
def view_aip(request, uuid): es_client = elasticSearchFunctions.get_client() try: es_aip_doc = elasticSearchFunctions.get_aip_data(es_client, uuid, fields='name,size,created,status,filePath,encrypted') except IndexError: raise Http404 name = _get_es_field(es_aip_doc['fields'], 'name') active_tab = None form_upload = forms.UploadMetadataOnlyAtomForm(prefix='upload') form_reingest = forms.ReingestAIPForm(prefix='reingest') form_delete = forms.DeleteAIPForm(prefix='delete', uuid=uuid) # Process metadata-only DIP upload form if request.POST and 'submit-upload-form' in request.POST: form_upload = forms.UploadMetadataOnlyAtomForm(request.POST, prefix='upload') active_tab = 'upload' if form_upload.is_valid(): try: file_slug = upload_dip_metadata_to_atom(name, uuid, form_upload.cleaned_data['slug']) except AtomMetadataUploadError: messages.error(request, _('Metadata-only DIP upload failed, check the logs for more details')) logger.error('Unexepected error during metadata-only DIP upload (UUID: %s)', uuid, exc_info=True) else: messages.success(request, _('Metadata-only DIP upload has been completed successfully. New resource has slug: %(slug)s') % {'slug': file_slug}) form_upload = forms.UploadMetadataOnlyAtomForm(prefix='upload') # Reset form # Process reingest form if request.POST and 'submit-reingest-form' in request.POST: form_reingest = forms.ReingestAIPForm(request.POST, prefix='reingest') active_tab = 'reingest' if form_reingest.is_valid(): response = storage_service.request_reingest(uuid, form_reingest.cleaned_data['reingest_type'], form_reingest.cleaned_data['processing_config']) error = response.get('error', True) message = response.get('message', 'An unknown error occurred.') if error: messages.error(request, _('Error re-ingesting package: %(message)s') % {'message': message}) else: messages.success(request, message) return redirect('archival_storage_index') # Process delete form if request.POST and 'submit-delete-form' in request.POST: form_delete = forms.DeleteAIPForm(request.POST, prefix='delete', uuid=uuid) active_tab = 'delete' if form_delete.is_valid(): response = storage_service.request_file_deletion(uuid, request.user.id, request.user.email, form_delete.cleaned_data['reason']) messages.info(request, response['message']) es_client = elasticSearchFunctions.get_client() elasticSearchFunctions.mark_aip_deletion_requested(es_client, uuid) return redirect('archival_storage_index') context = { 'uuid': uuid, 'name': name, 'created': _get_es_field(es_aip_doc['fields'], 'created'), 'status': AIP_STATUS_DESCRIPTIONS[_get_es_field(es_aip_doc['fields'], 'status', 'UPLOADED')], 'encrypted': _get_es_field(es_aip_doc['fields'], 'encrypted', False), 'size': '{0:.2f} MB'.format(_get_es_field(es_aip_doc['fields'], 'size', 0)), 'location_basename': os.path.basename(_get_es_field(es_aip_doc['fields'], 'filePath')), 'active_tab': active_tab, 'forms': { 'upload': form_upload, 'reingest': form_reingest, 'delete': form_delete, }, } return render(request, 'archival_storage/view.html', context)