<commit_before>from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from papers.utils import nstr, iunaccent
from django.utils.translation import ugettext_lazy as _

OA_STATUS_CHOICES = (
        ('OA', _('Open Access')),
        ('OK', _('Allows pre/post prints')),
        ('NOK', _('Forbids pre/post prints')),
        ('UNK', _('Policy unclear')),
   )

PDF_STATUS_CHOICES = [('OK', _('Available')),
                      ('NOK', _('Unavailable'))]

VISIBILITY_CHOICES = [('VISIBLE', _('Visible')),
                      ('CANDIDATE', _('Candidate')),
                      ('DELETED', _('Deleted')),
                      ]

# Information about the researchers and their groups
class Department(models.Model):
    name = models.CharField(max_length=300)

    @property
    def sorted_researchers(self):
        return self.researcher_set.order_by('name')

    def __unicode__(self):
        return self.name

class ResearchGroup(models.Model):
    name = models.CharField(max_length=300)

    def __unicode__(self):
        return self.name

class Researcher(models.Model):
    department = models.ForeignKey(Department)
    groups = models.ManyToManyField(ResearchGroup,blank=True,null=True)
    email = models.EmailField(blank=True,null=True)
    homepage = models.URLField(blank=True, null=True)
    role = models.CharField(max_length=128, null=True, blank=True)

    # DOI search
    last_doi_search = models.DateTimeField(null=True,blank=True)
    status = models.CharField(max_length=512, blank=True, null=True)
    last_status_update = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        first_name = Name.objects.filter(researcher_id=self.id).order_by('id').first()
        if first_name:
            return unicode(first_name)
        return _("Anonymous researcher")

    @property
    def authors_by_year(self):
        return Author.objects.filter(name__researcher_id=self.id).order_by('-paper__year')
    @property
    def names(self):
        return self.name_set.order_by('id')
    @property
    def name(self):
        name = self.names[0]
        if name:
            return name
        else:
            return _("Anonymous researcher")
    @property
    def aka(self):
        return self.names[:2]

class Name(models.Model):
    researcher = models.ForeignKey(Researcher, blank=True, null=True, on_delete=models.SET_NULL)
    first = models.CharField(max_length=256)
    last = models.CharField(max_length=256)
    full = models.CharField(max_length=513, db_index=True)

    unique_together = ('first','last')# TODO Two researchers with the same name is not supported
    
    class Meta:
        ordering = ['last','first']

    @classmethod
    def create(cls, first, last):
        full = iunaccent(first+' '+last)
        return cls(first=first,last=last,full=full)
    def __unicode__(self):
        return '%s %s' % (self.first,self.last)
    @property
    def is_known(self):
        return self.researcher != None

# Papers matching one or more researchers
class Paper(models.Model):
    title = models.CharField(max_length=1024)
    fingerprint = models.CharField(max_length=64)
    year = models.IntegerField()
    last_modified = models.DateField(auto_now=True)
    visibility = models.CharField(max_length=32, default='VISIBLE')

    def __unicode__(self):
        return self.title

    # The two following fields need to be updated after the relevant changes
    # using the methods below.
    oa_status = models.CharField(max_length=32, null=True, blank=True)
    pdf_url = models.URLField(max_length=2048, null=True, blank=True)

    def update_oa_status(self):
        # Look for publications
        qs = list(self.publication_set.all()[:1])
        if qs:
            self.oa_status = qs[0].oa_status()
        else:
            self.oa_status = 'UNK'
        # Look for automatic OA OAI sources
        records = list(self.oairecord_set.filter(source__oa=True)[:1])
        if records:
            self.oa_status = 'OA'
        self.save()

    # TODO merge these two functions !

    def update_pdf_url(self):
        # TODO: create an oa_status field in each publication so that we optimize queries
        # and can deal with hybrid OA

        # If it is an open access publisher, keep the publisher version
        if self.oa_status == 'OA':
            publications = Publication.objects.filter(journal__publisher__oa_status='OA')[:1]
            if publications:
                self.pdf_url = publications[0].splash_url()
                self.save()
                return
        # otherwise try the OAI sources
        matches = OaiRecord.objects.filter(about=self.id,pdf_url__isnull=False)[:1]
        if matches:
            self.pdf_url = matches[0].pdf_url
        self.save()

# Researcher / Paper binary relation
# TODO: it could be a ManyToMany field...
class Author(models.Model):
    paper = models.ForeignKey(Paper)
    name = models.ForeignKey(Name)
    def __unicode__(self):
        return unicode(self.name)

# Publisher associated with a journal
class Publisher(models.Model):
    romeo_id = models.CharField(max_length=64)
    name = models.CharField(max_length=256)
    alias = models.CharField(max_length=256,null=True,blank=True)
    url = models.URLField(null=True,blank=True)
    preprint = models.CharField(max_length=32)
    postprint = models.CharField(max_length=32)
    pdfversion = models.CharField(max_length=32)
    oa_status = models.CharField(max_length=32, choices=OA_STATUS_CHOICES)<commit_msg>Fixed a big bug in PDF url generation<commit_after>from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from papers.utils import nstr, iunaccent
from django.utils.translation import ugettext_lazy as _

OA_STATUS_CHOICES = (
        ('OA', _('Open Access')),
        ('OK', _('Allows pre/post prints')),
        ('NOK', _('Forbids pre/post prints')),
        ('UNK', _('Policy unclear')),
   )

PDF_STATUS_CHOICES = [('OK', _('Available')),
                      ('NOK', _('Unavailable'))]

VISIBILITY_CHOICES = [('VISIBLE', _('Visible')),
                      ('CANDIDATE', _('Candidate')),
                      ('DELETED', _('Deleted')),
                      ]

# Information about the researchers and their groups
class Department(models.Model):
    name = models.CharField(max_length=300)

    @property
    def sorted_researchers(self):
        return self.researcher_set.order_by('name')

    def __unicode__(self):
        return self.name

class ResearchGroup(models.Model):
    name = models.CharField(max_length=300)

    def __unicode__(self):
        return self.name

class Researcher(models.Model):
    department = models.ForeignKey(Department)
    groups = models.ManyToManyField(ResearchGroup,blank=True,null=True)
    email = models.EmailField(blank=True,null=True)
    homepage = models.URLField(blank=True, null=True)
    role = models.CharField(max_length=128, null=True, blank=True)

    # DOI search
    last_doi_search = models.DateTimeField(null=True,blank=True)
    status = models.CharField(max_length=512, blank=True, null=True)
    last_status_update = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        first_name = Name.objects.filter(researcher_id=self.id).order_by('id').first()
        if first_name:
            return unicode(first_name)
        return _("Anonymous researcher")

    @property
    def authors_by_year(self):
        return Author.objects.filter(name__researcher_id=self.id).order_by('-paper__year')
    @property
    def names(self):
        return self.name_set.order_by('id')
    @property
    def name(self):
        name = self.names[0]
        if name:
            return name
        else:
            return _("Anonymous researcher")
    @property
    def aka(self):
        return self.names[:2]

class Name(models.Model):
    researcher = models.ForeignKey(Researcher, blank=True, null=True, on_delete=models.SET_NULL)
    first = models.CharField(max_length=256)
    last = models.CharField(max_length=256)
    full = models.CharField(max_length=513, db_index=True)

    unique_together = ('first','last')# TODO Two researchers with the same name is not supported
    
    class Meta:
        ordering = ['last','first']

    @classmethod
    def create(cls, first, last):
        full = iunaccent(first+' '+last)
        return cls(first=first,last=last,full=full)
    def __unicode__(self):
        return '%s %s' % (self.first,self.last)
    @property
    def is_known(self):
        return self.researcher != None

# Papers matching one or more researchers
class Paper(models.Model):
    title = models.CharField(max_length=1024)
    fingerprint = models.CharField(max_length=64)
    year = models.IntegerField()
    last_modified = models.DateField(auto_now=True)
    visibility = models.CharField(max_length=32, default='VISIBLE')

    def __unicode__(self):
        return self.title

    # The two following fields need to be updated after the relevant changes
    # using the methods below.
    oa_status = models.CharField(max_length=32, null=True, blank=True)
    pdf_url = models.URLField(max_length=2048, null=True, blank=True)

    def update_oa_status(self):
        # Look for publications
        qs = list(self.publication_set.all()[:1])
        if qs:
            self.oa_status = qs[0].oa_status()
        else:
            self.oa_status = 'UNK'
        # Look for automatic OA OAI sources
        records = list(self.oairecord_set.filter(source__oa=True)[:1])
        if records:
            self.oa_status = 'OA'
        self.save()

    # TODO merge these two functions !

    def update_pdf_url(self):
        # TODO: create an oa_status field in each publication so that we optimize queries
        # and can deal with hybrid OA

        # If it is an open access publisher, keep the publisher version
        if self.oa_status == 'OA':
            publications = self.publication_set.filter(journal__publisher__oa_status='OA')[:1]
            if publications:
                self.pdf_url = publications[0].splash_url()
                self.save()
                return
        # otherwise try the OAI sources
        matches = OaiRecord.objects.filter(about=self.id,pdf_url__isnull=False)[:1]
        if matches:
            self.pdf_url = matches[0].pdf_url
        self.save()

# Researcher / Paper binary relation
# TODO: it could be a ManyToMany field...
class Author(models.Model):
    paper = models.ForeignKey(Paper)
    name = models.ForeignKey(Name)
    def __unicode__(self):
        return unicode(self.name)

# Publisher associated with a journal
class Publisher(models.Model):
    romeo_id = models.CharField(max_length=64)
    name = models.CharField(max_length=256)
    alias = models.CharField(max_length=256,null=True,blank=True)
    url = models.URLField(null=True,blank=True)
    preprint = models.CharField(max_length=32)
    postprint = models.CharField(max_length=32)
    pdfversion = models.CharField(max_length=32)
    oa_status = models.CharField(max_length=32, choices=OA_STATUS_CHOICES)
{'fraction_non_alphanumeric': 0.11072040393111532, 'fraction_numerical': 0.010819583446037328, 'mean_word_length': 3.0689655172413794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1825678', 'n_tokens_mistral': 3500, 'n_tokens_neox': 3331, 'n_words': 742}