def format_preprint(preprint, share_type, old_subjects=None): if old_subjects is None: old_subjects = [] from osf.models import Subject old_subjects = [Subject.objects.get(id=s) for s in old_subjects] preprint_graph = GraphNode( share_type, **{ 'title': preprint.title, 'description': preprint.description or '', 'is_deleted': ((not preprint.verified_publishable and not preprint.is_retracted) or preprint.tags.filter(name='qatest').exists()), 'date_updated': preprint.modified.isoformat(), 'date_published': preprint.date_published.isoformat() if preprint.date_published else None }) to_visit = [ preprint_graph, GraphNode('workidentifier', creative_work=preprint_graph, uri=urlparse.urljoin(settings.DOMAIN, preprint._id + '/')) ] if preprint.get_identifier('doi'): to_visit.append( GraphNode('workidentifier', creative_work=preprint_graph, uri='https://doi.org/{}'.format( preprint.get_identifier('doi').value))) if preprint.provider.domain_redirect_enabled: to_visit.append( GraphNode('workidentifier', creative_work=preprint_graph, uri=preprint.absolute_url)) if preprint.article_doi: # Article DOI refers to a clone of this preprint on another system and therefore does not qualify as an identifier for this preprint related_work = GraphNode('creativework') to_visit.append( GraphNode('workrelation', subject=preprint_graph, related=related_work)) to_visit.append( GraphNode('workidentifier', creative_work=related_work, uri='https://doi.org/{}'.format(preprint.article_doi))) preprint_graph.attrs['tags'] = [ GraphNode('throughtags', creative_work=preprint_graph, tag=GraphNode('tag', name=tag)) for tag in preprint.tags.values_list('name', flat=True) if tag ] current_subjects = [ GraphNode('throughsubjects', creative_work=preprint_graph, is_deleted=False, subject=format_subject(s)) for s in preprint.subjects.all() ] deleted_subjects = [ GraphNode('throughsubjects', creative_work=preprint_graph, is_deleted=True, subject=format_subject(s)) for s in old_subjects if not preprint.subjects.filter(id=s.id).exists() ] preprint_graph.attrs['subjects'] = current_subjects + deleted_subjects to_visit.extend( format_contributor(preprint_graph, user, preprint.get_visible(user), i) for i, user in enumerate(preprint.contributors)) visited = set() to_visit.extend(preprint_graph.get_related()) while True: if not to_visit: break n = to_visit.pop(0) if n in visited: continue visited.add(n) to_visit.extend(list(n.get_related())) return [node.serialize() for node in visited]
def format_preprint(preprint, share_type, old_subjects=None): if old_subjects is None: old_subjects = [] from osf.models import Subject old_subjects = [Subject.objects.get(id=s) for s in old_subjects] preprint_graph = GraphNode( share_type, **{ 'title': preprint.node.title, 'description': preprint.node.description or '', 'is_deleted': (not preprint.verified_publishable or preprint.node.tags.filter(name='qatest').exists()), # Note: Changing any preprint attribute that is pulled from the node, like title, will NOT bump # the preprint's date modified but will bump the node's date_modified. # We have to send the latest date to SHARE to actually get the result to be updated. # If we send a date_updated that is <= the one we previously sent, SHARE will ignore any changes # because it looks like a race condition that arose from preprints being resent to SHARE on # every step of preprint creation. 'date_updated': max(preprint.modified, preprint.node.modified).isoformat(), 'date_published': preprint.date_published.isoformat() if preprint.date_published else None }) to_visit = [ preprint_graph, GraphNode('workidentifier', creative_work=preprint_graph, uri=urlparse.urljoin(settings.DOMAIN, preprint._id + '/')) ] if preprint.get_identifier('doi'): to_visit.append( GraphNode('workidentifier', creative_work=preprint_graph, uri='http://dx.doi.org/{}'.format( preprint.get_identifier('doi').value))) if preprint.provider.domain_redirect_enabled: to_visit.append( GraphNode('workidentifier', creative_work=preprint_graph, uri=preprint.absolute_url)) if preprint.article_doi: # Article DOI refers to a clone of this preprint on another system and therefore does not qualify as an identifier for this preprint related_work = GraphNode('creativework') to_visit.append( GraphNode('workrelation', subject=preprint_graph, related=related_work)) to_visit.append( GraphNode('workidentifier', creative_work=related_work, uri='http://dx.doi.org/{}'.format(preprint.article_doi))) preprint_graph.attrs['tags'] = [ GraphNode('throughtags', creative_work=preprint_graph, tag=GraphNode('tag', name=tag)) for tag in preprint.node.tags.values_list('name', flat=True) if tag ] current_subjects = [ GraphNode('throughsubjects', creative_work=preprint_graph, is_deleted=False, subject=format_subject(s)) for s in preprint.subjects.all() ] deleted_subjects = [ GraphNode('throughsubjects', creative_work=preprint_graph, is_deleted=True, subject=format_subject(s)) for s in old_subjects if not preprint.subjects.filter(id=s.id).exists() ] preprint_graph.attrs['subjects'] = current_subjects + deleted_subjects to_visit.extend( format_contributor(preprint_graph, user, preprint.node.get_visible( user), i) for i, user in enumerate(preprint.node.contributors)) to_visit.extend( GraphNode('AgentWorkRelation', creative_work=preprint_graph, agent=GraphNode('institution', name=institution)) for institution in preprint.node.affiliated_institutions.values_list( 'name', flat=True)) visited = set() to_visit.extend(preprint_graph.get_related()) while True: if not to_visit: break n = to_visit.pop(0) if n in visited: continue visited.add(n) to_visit.extend(list(n.get_related())) return [node.serialize() for node in visited]
def format_preprint(preprint, share_type, old_subjects=None): if old_subjects is None: old_subjects = [] from osf.models import Subject old_subjects = [Subject.objects.get(id=s) for s in old_subjects] preprint_graph = GraphNode(share_type, **{ 'title': preprint.node.title, 'description': preprint.node.description or '', 'is_deleted': ( not preprint.verified_publishable or preprint.node.tags.filter(name='qatest').exists() ), # Note: Changing any preprint attribute that is pulled from the node, like title, will NOT bump # the preprint's date modified but will bump the node's date_modified. # We have to send the latest date to SHARE to actually get the result to be updated. # If we send a date_updated that is <= the one we previously sent, SHARE will ignore any changes # because it looks like a race condition that arose from preprints being resent to SHARE on # every step of preprint creation. 'date_updated': max(preprint.modified, preprint.node.modified).isoformat(), 'date_published': preprint.date_published.isoformat() if preprint.date_published else None }) to_visit = [ preprint_graph, GraphNode('workidentifier', creative_work=preprint_graph, uri=urlparse.urljoin(settings.DOMAIN, preprint._id + '/')) ] if preprint.get_identifier('doi'): to_visit.append(GraphNode('workidentifier', creative_work=preprint_graph, uri='http://dx.doi.org/{}'.format(preprint.get_identifier('doi').value))) if preprint.provider.domain_redirect_enabled: to_visit.append(GraphNode('workidentifier', creative_work=preprint_graph, uri=preprint.absolute_url)) if preprint.article_doi: # Article DOI refers to a clone of this preprint on another system and therefore does not qualify as an identifier for this preprint related_work = GraphNode('creativework') to_visit.append(GraphNode('workrelation', subject=preprint_graph, related=related_work)) to_visit.append(GraphNode('workidentifier', creative_work=related_work, uri='http://dx.doi.org/{}'.format(preprint.article_doi))) preprint_graph.attrs['tags'] = [ GraphNode('throughtags', creative_work=preprint_graph, tag=GraphNode('tag', name=tag)) for tag in preprint.node.tags.values_list('name', flat=True) if tag ] current_subjects = [ GraphNode('throughsubjects', creative_work=preprint_graph, is_deleted=False, subject=format_subject(s)) for s in preprint.subjects.all() ] deleted_subjects = [ GraphNode('throughsubjects', creative_work=preprint_graph, is_deleted=True, subject=format_subject(s)) for s in old_subjects if not preprint.subjects.filter(id=s.id).exists() ] preprint_graph.attrs['subjects'] = current_subjects + deleted_subjects to_visit.extend(format_contributor(preprint_graph, user, preprint.node.get_visible(user), i) for i, user in enumerate(preprint.node.contributors)) to_visit.extend(GraphNode('AgentWorkRelation', creative_work=preprint_graph, agent=GraphNode('institution', name=institution)) for institution in preprint.node.affiliated_institutions.values_list('name', flat=True)) visited = set() to_visit.extend(preprint_graph.get_related()) while True: if not to_visit: break n = to_visit.pop(0) if n in visited: continue visited.add(n) to_visit.extend(list(n.get_related())) return [node.serialize() for node in visited]