def amend_datacite_doi(publication): """Amend a citation on DataCite Use the published project to update the DOI on Datacite Note: This assumes that the DOI being updated is related to the project. We may want to change this to accept dict objects which are being amended. :param elasticsearch publication: Publication to amend """ # Only amend doi while in prod if getattr(settings, 'DESIGNSAFE_ENVIRONMENT', 'dev') != 'default': return pub_dict = publication.to_dict() project_type = pub_dict['project']['value']['projectType'] if project_type == 'other': prj_class = lookup_model(pub_dict['project']) project = prj_class(value=pub_dict['project']['value'], uuid=pub_dict['project']['uuid']) prj_datacite_json = project.to_datacite_json() prj_doi = project.dois[0] DataciteManager.create_or_update_doi(prj_datacite_json, prj_doi) else: for field in publication.entity_keys(publishable=True): for ent in pub_dict[field]: ent_class = lookup_model(ent) entity = ent_class(value=ent['value'], uuid=ent['uuid']) entity.authors = ent['authors'] # swap author formats before creating json ent_datacite_json = entity.to_datacite_json() ent_doi = entity.dois[0] DataciteManager.create_or_update_doi(ent_datacite_json, ent_doi)
def related_entities(self, offset=0, limit=100): from designsafe.apps.projects.models.utils import lookup_model relattrs = self._meta._reverse_fields rel_names = [getattr(self, attrname).related_obj_name for attrname in relattrs \ if getattr(self, attrname).related_obj_name != 'designsafe.file'] resp = self.manager().agave_client.meta.listMetadata( q=json.dumps({'name': {'$in': rel_names}, 'associationIds': self.uuid}), offset=offset, limit=limit) ents = [lookup_model(rsp)(**rsp) for rsp in resp] return ents
def handle(self, *args, **options): project_id = options.get('project_id', '').upper() prj = self.get_project(project_id) #self.stdout.write('getting entities') rel_names = self.get_related_entities_names() #self.stdout.write('rel_names: %s' % rel_names) entities = self.client.meta.listMetadata(q=json.dumps( {'name': {'$in': rel_names}, 'associationIds': prj.uuid})) self.stdout.write('entities length: %d' % len(entities)) for entity in entities: cls = lookup_model(entity) ent = cls(**entity) self.stdout.write('Entity: %s' % ent.uuid) self._check_related_uuids(ent)
def get_project(self, project_id): client = self.get_client() res = [] if project_id.startswith('PRJ-'): res = client.meta.listMetadata(q=json.dumps( {"value.projectId": project_id})) #prj = Project._meta.model_manager.get(client, project_id=project_id) else: #prj = Project._meta.model_manager.get(client, uuid=options.get('project_id')) res = client.meta.getMetadata(uuid=project_id) if len(res): cls = lookup_model(res[0]) prj = cls(**res[0]) self.prj = prj return prj else: raise ValueError('No project found')