[Improvement] Clean up arXiv works as well
@@ -10,7 +10,8 @@ from share.models import WorkIdentifier, Preprint, ShareUser
class Command(BaseCommand):
- OSF_PP = ['OSF', 'engrXiv', 'PsyArXiv', 'BITSS', 'LIS Scholarship Archive', 'SocArXiv', 'LawArXiv', 'AgriXiv', 'MindRxiv', 'Open Science Framework']
+ LIMIT = 250
+ OSF_PP = ['OSF', 'engrXiv', 'PsyArXiv', 'BITSS', 'LIS Scholarship Archive', 'SocArXiv', 'LawArXiv', 'AgriXiv', 'MindRxiv', 'Open Science Framework', 'arXiv']
AGGREGATORS = ['DataCite MDS', 'CrossRef']
def add_arguments(self, parser):
@@ -33,10 +34,17 @@ class Command(BaseCommand):
id__in=Preprint.objects.filter(sources__in=OSF_PP_USER_IDS)
)
- for work in pps:
+ i = 0
+ for work in pps.iterator():
+ if i > self.LIMIT:
+ self.stdout.write(self.style.SUCCESS('Fixed {} works, stopping...'.format(self.LIMIT)))
+ break
+
dupes = {}
- for agent in work.related_agents.filter(type='share.person'):
+ for agent in work.related_agents.filter(type='share.person').include('identifiers', 'sources__source', 'work_relations'):
+ if not (agent.given_name and agent.family_name):
+ continue
dupes.setdefault((agent.given_name, agent.family_name), []).append(agent)
# Filter down to just duplicated agents
@@ -48,17 +56,24 @@ class Command(BaseCommand):
if not dupes:
continue
+ i += 1
self.stdout.write('=== Processing Work "{}" Modified On {} ==='.format(work.id, work.date_modified))
for agents in dupes.values():
# Order by # of identifiers and a preference towards OSF sources
- core_agent = list(sorted(agents, key=lambda x: (agent.sources.filter(source__long_title__in=self.OSF_PP).exists(), x.identifiers.count()), reverse=True))[0]
+ core_agent = list(sorted(agents, key=lambda x: (
+ set(self.OSF_PP).intersection({user.source.long_title for user in agent.sources.all()}),
+ len(x.identifiers.all()),
+ len(x.work_relations.all()),
+ ), reverse=True))[0]
self.stdout.write('\tSmashing {} into {} from {} identified by {}'.format(
[a for a in agents if a != core_agent],
core_agent,
- core_agent.sources.values_list('source__long_title', flat=True),
- core_agent.identifiers.values_list('uri', flat=True),
+ # core_agent.sources.values_list('source__long_title', flat=True),
+ # core_agent.identifiers.values_list('uri', flat=True),
+ [user.source.long_title for user in core_agent.sources.all()],
+ [identifier.uri for identifier in core_agent.identifiers.all()]
))
for agent in agents:
@@ -70,6 +85,8 @@ class Command(BaseCommand):
identifier.administrative_change(agent=core_agent)
for rel in agent.work_relations.all():
+ if rel.creative_work_id != work.id:
+ continue
self.stdout.write('\t\tReassigning {}: {} -> {}'.format(rel, rel.creative_work.id, graveyard.id))
if not options.get('dry'):
rel.administrative_change(creative_work=graveyard, type=''.join(random.sample(string.ascii_letters, 5)))
