diff --git a/sok/management/commands/citations.py b/sok/management/commands/citations.py index 9a981a5..dafe2f1 100644 --- a/sok/management/commands/citations.py +++ b/sok/management/commands/citations.py @@ -3,13 +3,13 @@ from typing import Set, Tuple from django.core.management.base import BaseCommand, CommandParser from django.db.models import Count, Q -from sok.models import Publication +from sok.models import Publication, PublicationReference class Command(BaseCommand): - def echo(self, msg: str): - self.stdout.write(msg) + def echo(self, msg: str, nl: bool = True): + self.stdout.write(msg, ending='\n' if nl else '') # BaseCommand @@ -27,7 +27,7 @@ class Command(BaseCommand): ).filter(citation_count__gte=min_citations) self.echo("digraph G {") - self.echo("\trankdir = RL;") + self.echo("\trankdir = BT;") graph: Set[Tuple[int, int]] = set() @@ -48,9 +48,16 @@ class Command(BaseCommand): )) if pk: - self.echo(f'\t"{publication.pk}" -> "{reference.pk}";') + self.echo(f'\t"{publication.pk}" -> "{reference.pk}"',nl=False) else: - self.echo(f'\t"{publication.cite_key}" -> "{reference.cite_key}";') + self.echo(f'\t"{publication.cite_key}" -> "{reference.cite_key}"', nl=False) + + rel = PublicationReference.objects.get(publication=publication, reference=reference) + + if rel.is_self_cite: + self.echo(" [style=dashed]", nl=False) + + self.echo(";") self.echo("}") diff --git a/sok/models.py b/sok/models.py index 4e1347a..ac36d1e 100644 --- a/sok/models.py +++ b/sok/models.py @@ -179,5 +179,11 @@ class PublicationReference(models.Model): reference = models.ForeignKey(Publication, on_delete=models.CASCADE, related_name='cited_by') identifier = models.CharField(max_length=255, blank=True, null=True, default=None) + @property + def is_self_cite(self) -> bool: + lhs: Set[int] = set(self.publication.authors.values_list('pk', flat=True)) + rhs: Set[int] = set(self.reference.authors.values_list('pk', flat=True)) + return not lhs.isdisjoint(rhs) + class Meta: unique_together = (('publication', 'reference'), ('publication', 'identifier'))