def get_context_data(self, **kwargs): """Update context data to add publication.""" context = super(DataDepotPublishedView, self).get_context_data(**kwargs) logger.info('Get context Data') pub = Publication(project_id=kwargs['project_id'].strip('/')) logger.debug('pub: %s', pub.to_dict()) context['citation_title'] = pub.project.value.title context['citation_date'] = pub.created context['doi'] = pub.project.doi context['keywords'] = pub.project.value.keywords.split(',') context['authors'] = [{ 'full_name': '{last_name}, {first_name}'.format(last_name=user['last_name'], first_name=user['first_name']), 'institution': getattr(getattr(user, 'profile'), 'institution', '') } for user in pub.users] context['publication'] = pub if self.request.user.is_authenticated: context['angular_init'] = json.dumps({ 'authenticated': True, }) else: context['angular_init'] = json.dumps({ 'authenticated': False, }) return context
def get(self, request, project_id): pub = Publication(project_id=project_id) if pub is not None and hasattr(pub, 'project'): return JsonResponse(pub.to_dict()) else: return JsonResponse({'status': 404, 'message': 'Not found'}, status=404)
def zip_publication_files(self, project_id): from designsafe.apps.api.agave.filemanager.public_search_index import Publication try: pub = Publication(project_id=project_id) pub.archive() except Exception as exc: logger.error('Zip Proj Id: %s. %s', project_id, exc, exc_info=True) raise self.retry(exc=exc)
def save_publication(self, project_id): from designsafe.apps.api.agave.filemanager.public_search_index import Publication from designsafe.apps.api.projects.managers import publication as PublicationManager try: pub = Publication(project_id=project_id) publication = PublicationManager.reserve_publication(pub.to_dict()) pub.update(**publication) except Exception as exc: logger.error('Proj Id: %s. %s', project_id, exc, exc_info=True) raise self.retry(exc=exc)
def save_to_fedora(self, project_id): import requests import magic from designsafe.apps.api.agave.filemanager.public_search_index import Publication try: pub = Publication(project_id=project_id) pub.update(status='published') _root = os.path.join('/corral-repl/tacc/NHERI/published', project_id) fedora_base = 'http://fedoraweb01.tacc.utexas.edu:8080/fcrepo/rest/publications_01' res = requests.get(fedora_base) if res.status_code == 404 or res.status_code == 410: requests.put(fedora_base) fedora_project_base = ''.join([fedora_base, '/', project_id]) res = requests.get(fedora_project_base) if res.status_code == 404 or res.status_code == 410: requests.put(fedora_project_base) headers = {'Content-Type': 'text/plain'} #logger.debug('walking: %s', _root) for root, dirs, files in os.walk(_root): for name in files: mime = magic.Magic(mime=True) headers['Content-Type'] = mime.from_file( os.path.join(root, name)) #files full_path = os.path.join(root, name) _path = full_path.replace(_root, '', 1) _path = _path.replace('[', '-') _path = _path.replace(']', '-') url = ''.join([fedora_project_base, urllib.quote(_path)]) #logger.debug('uploading: %s', url) with open(os.path.join(root, name), 'rb') as _file: requests.put(url, data=_file, headers=headers) for name in dirs: #dirs full_path = os.path.join(root, name) _path = full_path.replace(_root, '', 1) url = ''.join([fedora_project_base, _path]) #logger.debug('creating: %s', _path) requests.put(url) except Exception as exc: logger.error('Proj Id: %s. %s', project_id, exc) raise self.retry(exc=exc)
def copy_publication_files_to_corral(self, project_id): from designsafe.apps.api.agave.filemanager.public_search_index import Publication import shutil publication = Publication(project_id=project_id) filepaths = publication.related_file_paths() if not len(filepaths): res = get_service_account_client().files.list( systemId='project-{project_uuid}'.format( project_uuid=publication.project.uuid ), filePath='/' ) filepaths = [ _file.path.strip('/') for _file in res if ( _file.name != '.' and _file.name != 'Trash' ) ] filepaths = list(set(filepaths)) filepaths = sorted(filepaths) base_path = ''.join(['/', publication.projectId]) prefix_dest = '/corral-repl/tacc/NHERI/published/{}'.format(project_id) if not os.path.isdir(prefix_dest): os.mkdir(prefix_dest) prefix_src = '/corral-repl/tacc/NHERI/projects/{}'.format(publication.project['uuid']) for filepath in filepaths: local_src_path = '{}/{}'.format(prefix_src, filepath) local_dst_path = '{}/{}'.format(prefix_dest, filepath) logger.info('Trying to copy: %s to %s', local_src_path, local_dst_path) if os.path.isdir(local_src_path): try: #os.mkdir(local_dst_path) shutil.copytree(local_src_path, local_dst_path) except OSError as exc: logger.info(exc) except IOError as exc: logger.info(exc) else: try: if not os.path.isdir(os.path.dirname(local_dst_path)): os.mkdir(os.path.dirname(local_dst_path)) shutil.copy(local_src_path, local_dst_path) except OSError as exc: logger.info(exc) except IOError as exc: logger.info(exc) #for filepath in filepaths: # filepath = filepath.strip('/') # logger.info('Copying: {}'.format(filepath)) # path_comps = filepath.split('/') # parent_path = os.path.join(*path_comps[:-1]) # try: # file_obj = BaseFileResource.\ # listing(system=proj_system, # path=filepath, # agave_client=service) # if file_obj.type == 'dir': # logger.info('path is a directory, ensuring path exists') # base_obj = BaseFileResource.\ # ensure_path(service, # settings.PUBLISHED_SYSTEM, # os.path.join(base_path, parent_path)) # else: # logger.info('path is a file, ensuring parent path exists') # base_obj = BaseFileResource.\ # ensure_path(service, # settings.PUBLISHED_SYSTEM, # os.path.join(base_path, parent_path)) # base_obj.import_data(file_obj.system, file_obj.path) # except Exception as err: # logger.error('Error when copying data to published: %s. %s', filepath, err) # local_src_path = '{}/{}'.format(prefix_src, file_obj.path) # local_dst_path = '{}/{}'.format(prefix_dest, file_obj.path) # logger.info('Trying to copy: %s to %s', local_src_path, local_dst_path) # if os.path.isdir(local_src_path): # shutil.copytree(local_src_path, local_dst_path) # else: # shutil.copy(local_src_path, local_dst_path) # #self.retry(exc=err) #try: # image = BaseFileResource.\ # listing(system=proj_system, # path='projectimage.jpg', # agave_client=service) # base_dir.import_data(image.system, image.path) #except HTTPError as err: # logger.debug('No project image') save_to_fedora.apply_async(args=[project_id])
def copy_publication_files_to_corral(self, project_id): from designsafe.apps.api.agave.filemanager.public_search_index import Publication import shutil publication = Publication(project_id=project_id) filepaths = publication.related_file_paths() if not len(filepaths): res = get_service_account_client().files.list( systemId='project-{project_uuid}'.format( project_uuid=publication.project.uuid), filePath='/') filepaths = [ _file.path.strip('/') for _file in res if (_file.name != '.' and _file.name != 'Trash') ] filepaths = list(set(filepaths)) filepaths = sorted(filepaths) base_path = ''.join(['/', publication.projectId]) os.chmod('/corral-repl/tacc/NHERI/published', 0755) prefix_dest = '/corral-repl/tacc/NHERI/published/{}'.format(project_id) if not os.path.isdir(prefix_dest): os.mkdir(prefix_dest) prefix_src = '/corral-repl/tacc/NHERI/projects/{}'.format( publication.project['uuid']) for filepath in filepaths: local_src_path = '{}/{}'.format(prefix_src, filepath) local_dst_path = '{}/{}'.format(prefix_dest, filepath) logger.info('Trying to copy: %s to %s', local_src_path, local_dst_path) if os.path.isdir(local_src_path): try: #os.mkdir(local_dst_path) if not os.path.isdir(os.path.dirname(local_dst_path)): os.makedirs(os.path.dirname(local_dst_path)) shutil.copytree(local_src_path, local_dst_path) for root, dirs, files in os.walk(local_dst_path): for d in dirs: os.chmod(os.path.join(root, d), 0555) for f in files: os.chmod(os.path.join(root, f), 0444) os.chmod(local_dst_path, 0555) except OSError as exc: logger.info(exc) except IOError as exc: logger.info(exc) else: try: if not os.path.isdir(os.path.dirname(local_dst_path)): os.makedirs(os.path.dirname(local_dst_path)) for root, dirs, files in os.walk( os.path.dirname(local_dst_path)): for d in dirs: os.chmod(os.path.join(root, d), 0555) for f in files: os.chmod(os.path.join(root, f), 0444) shutil.copy(local_src_path, local_dst_path) os.chmod(local_dst_path, 0444) except OSError as exc: logger.info(exc) except IOError as exc: logger.info(exc) os.chmod(prefix_dest, 0555) os.chmod('/corral-repl/tacc/NHERI/published', 0555) save_to_fedora.apply_async(args=[project_id]) agave_indexer.apply_async(kwargs={ 'username': '******', 'systemId': 'designsafe.storage.published', 'filePath': '/' + project_id, 'recurse': True }, queue='indexing')
def set_publish_status(self, project_id): from designsafe.apps.api.agave.filemanager.public_search_index import Publication pub = Publication(project_id=project_id) pub.update(status='published')