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 = BaseESPublication(project_id=kwargs['project_id'].strip('/')) logger.debug('pub: %s', pub.to_dict()) context['projectId'] = pub.projectId context['citation_title'] = pub.project.value.title context['citation_date'] = pub.created if pub.project.value.to_dict().get('dois') != None: #This is for newer publications context['doi'] = pub.project.value.dois[0] elif pub.project.to_dict().get('doi') != None: #This is for older publications context['doi'] = pub.project.doi context['keywords'] = pub.project.value.keywords.split(',') if 'users' in pub.to_dict(): 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 getattr(pub, 'users', [])] elif 'authors' in pub.to_dict(): context['authors'] = [{ 'full_name': '{last_name}, {first_name}'.format( last_name=author['lname'], first_name=author['fname'] ), 'institution': getattr(author, 'inst', '') } for author in getattr(pub, 'authors',[])] else: context['authors'] = [{ 'full_name': '{last_name}, {first_name}'.format( last_name=author['lname'], first_name=author['fname'] ), 'institution': getattr(author, 'inst', '') } for author in getattr(pub.project.value, 'teamOrder', [])] context['publication'] = pub context['description'] = pub.project.value.description context['experiments'] = getattr(pub, 'experimentsList', []) context['missions'] = getattr(pub, 'missions', []) context['reports'] = getattr(pub, 'reports', []) context['simulations'] = getattr(pub, 'simulations', []) context['hybrid_simulations'] = getattr(pub, 'hybrid_simulations',[]) proj = ProjectsManager(service_account()).get_project_by_id(pub.projectId) context['dc_json'] = json.dumps(proj.to_dataset_json()) 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, revision=None): """ Get a publication. If a revision is not supplied, return the "Original" publication. Include the latest version if it is not being queried. """ es_client = new_es_client() pub = BaseESPublication(project_id=project_id, revision=revision, using=es_client) latest_revision = IndexedPublication.max_revision( project_id=project_id, using=es_client) latest_pub_dict = None if latest_revision > 0 and latest_revision != revision: latest_pub = BaseESPublication(project_id=project_id, revision=latest_revision, using=es_client) if latest_pub is not None and hasattr(latest_pub, 'project'): latest_pub_dict = latest_pub.to_dict() if pub is not None and hasattr(pub, 'project'): pub_dict = pub.to_dict() if pub_dict['project']['value']['projectType'] != 'other': metrics.info('Data Depot', extra={ 'user': request.user.username, 'sessionId': getattr(request.session, 'session_key', ''), 'operation': 'listing', 'agent': request.META.get('HTTP_USER_AGENT'), 'ip': get_client_ip(request), 'info': { 'api': 'agave', 'systemId': 'designsafe.storage.published', 'filePath': project_id, 'query': {} } }) if latest_pub_dict: pub_dict['latestRevision'] = latest_pub_dict return JsonResponse(pub_dict) else: return JsonResponse({ 'status': 404, 'message': 'Not found' }, status=404)
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 = BaseESPublication(project_id=kwargs['project_id'].strip('/')) logger.debug('pub: %s', pub.to_dict()) context['projectId'] = pub.projectId 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 getattr(pub, 'users', [])] context['publication'] = pub context['description'] = pub.project.value.description if self.request.user.is_authenticated: context['angular_init'] = json.dumps({ 'authenticated': True, }) else: context['angular_init'] = json.dumps({ 'authenticated': False, }) return context
def freeze_project_and_entity_metadata(project_id, entity_uuids=None): """Freeze project and entity metadata. Given a project id and an entity uuid (should be a main entity) this function retrieves all metadata related to these entities and stores it into Elasticsearch as :class:`~designafe.libs.elasticsearch.docs.publications.BaseESPublication` :param str project_id: Project id. :param list of entity_uuid strings: Entity uuids. """ mgr = ProjectsManager(service_account()) prj = mgr.get_project_by_id(project_id) pub_doc = BaseESPublication(project_id=project_id) publication = pub_doc.to_dict() if entity_uuids: # clear any existing entities in publication entity = mgr.get_entity_by_uuid(entity_uuids[0]) pub_entities_field_name = FIELD_MAP[entity.name] publication[pub_entities_field_name] = [] for ent_uuid in entity_uuids: entity = None entity = mgr.get_entity_by_uuid(ent_uuid) entity_json = entity.to_body_dict() if entity: pub_entities_field_name = FIELD_MAP[entity.name] publication['authors'] = entity_json['value']['authors'][:] entity_json['authors'] = [] _populate_entities_in_publication(entity, publication) _transform_authors(entity_json, publication) if entity_json['value']['dois']: entity_json['doi'] = entity_json['value']['dois'][-1] _delete_unused_fields(entity_json) publication[pub_entities_field_name].append(entity_json) prj_json = prj.to_body_dict() _delete_unused_fields(prj_json) award_number = publication.get('project', {}).get('value', {}).pop( 'awardNumber', []) or [] if not isinstance(award_number, list): award_number = [] prj_json['value']['awardNumbers'] = award_number prj_json['value'].pop('awardNumber', None) if publication.get('project'): publication['project'].update(prj_json) else: publication['project'] = prj_json pub_doc.update(**publication) return pub_doc
def amend_publication(project_id, amendments=None, authors=None, revision=None): """Amend a Publication Update Amendable fields on a publication and the corrosponding DataCite records. These changes do not produce a new version of a publication, but they do allow for limited changes to a published project. This is currently configured to support "Other" publications only. :param str project_id: Project uuid to amend :param int revision: Revision number to amend """ es_client = new_es_client() mgr = ProjectsManager(service_account()) prj = mgr.get_project_by_id(project_id) pub = BaseESPublication(project_id=project_id, revision=revision, using=es_client) prj_dict = prj.to_body_dict() pub_dict = pub.to_dict() _delete_unused_fields(prj_dict) if pub.project.value.projectType != 'other': pub_entity_uuids = pub.entities() for uuid in pub_entity_uuids: if uuid in amendments: entity = amendments[uuid] else: entity = mgr.get_entity_by_uuid(uuid) entity = entity.to_body_dict() _delete_unused_fields(entity) for pub_ent in pub_dict[FIELD_MAP[entity['name']]]: if pub_ent['uuid'] == entity['uuid']: for key in entity['value']: ent_type = 'entity' if 'dois' in entity['value'] else 'subentity' if key not in UNAMENDABLE_FIELDS[ent_type]: pub_ent['value'][key] = entity['value'][key] if 'authors' in entity['value']: pub_ent['value']['authors'] = authors[entity['uuid']] _set_authors(pub_ent, pub_dict) # weird key swap for old issues with awardnumber(s) award_number = prj.award_number or [] if not isinstance(award_number, list): award_number = [] prj_dict['value']['awardNumbers'] = award_number prj_dict['value'].pop('awardNumber', None) for key in prj_dict['value']: if key not in UNAMENDABLE_FIELDS['project']: pub_dict['project']['value'][key] = prj_dict['value'][key] if authors and prj_dict['value']['projectType'] == 'other': pub_dict['project']['value']['teamOrder'] = authors pub.update(**pub_dict) IndexedPublication._index.refresh(using=es_client) return pub
def get(self, request, project_id): pub = BaseESPublication(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 freeze_project_and_entity_metadata(project_id, entity_uuids=None): """Freeze project and entity metadata. Given a project id and an entity uuid (should be a main entity) this function retrieves all metadata related to these entities and stores it into Elasticsearch as :class:`~designafe.libs.elasticsearch.docs.publications.BaseESPublication` When publishing for the first time or publishing over an existing publication. We will clear any existing entities (if any) from the published metadata. We'll use entity_uuids (the entities getting DOIs) to rebuild the rest of the publication. These entities usually do not have files associated to them (except published reports/documents). :param str project_id: Project id. :param list of entity_uuid strings: Entity uuids. """ mgr = ProjectsManager(service_account()) prj = mgr.get_project_by_id(project_id) pub_doc = BaseESPublication(project_id=project_id) publication = pub_doc.to_dict() if entity_uuids: # clear any existing sub entities in publication and keep updated fileObjs fields_to_clear = [] entities_with_files = [] for key in list(FIELD_MAP.keys()): if FIELD_MAP[key] in list(publication.keys()): fields_to_clear.append(FIELD_MAP[key]) fields_to_clear = set(fields_to_clear) for field in fields_to_clear: for ent in publication[field]: if 'fileObjs' in ent: entities_with_files.append(ent) if ent['uuid'] in entity_uuids: publication[field] = [] for ent_uuid in entity_uuids: entity = None entity = mgr.get_entity_by_uuid(ent_uuid) if entity: entity_json = entity.to_body_dict() pub_entities_field_name = FIELD_MAP[entity.name] for e in entities_with_files: if e['uuid'] == entity_json['uuid']: entity_json['fileObjs'] = e['fileObjs'] publication['authors'] = list(entity_json['value']['authors']) entity_json['authors'] = [] _populate_entities_in_publication(entity, publication) _transform_authors(entity_json, publication) if entity_json['value']['dois']: entity_json['doi'] = entity_json['value']['dois'][-1] _delete_unused_fields(entity_json) publication[pub_entities_field_name].append(entity_json) prj_json = prj.to_body_dict() _delete_unused_fields(prj_json) award_number = publication.get('project', {}).get('value', {}).pop( 'awardNumber', [] ) or [] if not isinstance(award_number, list): award_number = [] prj_json['value']['awardNumbers'] = award_number prj_json['value'].pop('awardNumber', None) if publication.get('project'): publication['project'].update(prj_json) else: publication['project'] = prj_json pub_doc.update(**publication) return pub_doc
def fix_file_tags(project_id): pub = BaseESPublication(project_id=project_id) pub_dict = pub.to_dict() entities_to_check = list(set(pub_dict.keys()).intersection(list(FIELD_MAP.values()))) entities_to_check.append('project') def check_complete_tags(tags): for tag in tags: if 'path' not in tag: return False return True def fix_tags_path(entity): for tag in entity['value']['fileTags']: try: pub_file = BaseFileResource.listing( service_account(), system="designsafe.storage.published", path="{}{}".format(project_id, tag['path']) ) tag['fileUuid'] = pub_file.uuid except Exception as err: LOG.info('error: {}'.format(err)) continue def fix_tags_no_path(entity): if entity['name'] == 'designsafe.project': proj_other = BaseFileResource.listing(service_account(), system="project-{}".format(entity['uuid']), path="") for child in proj_other.children: try: pub_file = BaseFileResource.listing(service_account(), system="designsafe.storage.published", path="{}{}".format(project_id, child.path)) proj_file = BaseFileResource.listing(service_account(), system="project-{}".format(entity['uuid']), path=child.path) for tag in entity['value']['fileTags']: if tag['fileUuid'] == proj_file.uuid: tag['fileUuid'] = pub_file.uuid except Exception as err: LOG.info('error: {}'.format(err)) continue else: for fobj in entity['fileObjs']: try: pub_file = BaseFileResource.listing(service_account(), system="designsafe.storage.published", path="{}{}".format(project_id, fobj['path'])) proj_file = BaseFileResource.listing(service_account(), system="project-{}".format(pub_dict['project']['uuid']), path=fobj['path']) for tag in entity['value']['fileTags']: if tag['fileUuid'] == proj_file.uuid: tag['fileUuid'] = pub_file.uuid except Exception as err: LOG.info('error: {}'.format(err)) continue for entname in entities_to_check: if type(pub_dict[entname]) == list: for entity in pub_dict[entname]: if 'value' in entity and 'fileTags' in entity['value'] and check_complete_tags(entity['value']['fileTags']): fix_tags_path(entity) elif 'value' in entity and 'fileTags' in entity['value']: fix_tags_no_path(entity) else: if 'value' in pub_dict[entname] and 'fileTags' in pub_dict[entname]['value'] and check_complete_tags(pub_dict[entname]['value']['fileTags']): fix_tags_path(pub_dict[entname]) elif 'value' in pub_dict[entname] and 'fileTags' in pub_dict[entname]['value']: fix_tags_no_path(pub_dict[entname]) pub.update(**pub_dict)