SciPost Code Repository

Skip to content
Snippets Groups Projects
models.py 1.78 KiB
Newer Older
Jorran de Wit's avatar
Jorran de Wit committed
from django.db import models
from django.urls import reverse
Jorran de Wit's avatar
Jorran de Wit committed

from django_countries.fields import CountryField

Jorran de Wit's avatar
Jorran de Wit committed
from scipost.models import Contributor

from .constants import INSTITUTION_TYPES, TYPE_UNIVERSITY
from .managers import AffiliationQuerySet
Jorran de Wit's avatar
Jorran de Wit committed


Jorran de Wit's avatar
Jorran de Wit committed
class Institution(models.Model):
Jorran de Wit's avatar
Jorran de Wit committed
    """
    Any (scientific) Institution in the world should ideally have a SciPost registration.
Jorran de Wit's avatar
Jorran de Wit committed
    """
    name = models.CharField(max_length=255)
    acronym = models.CharField(max_length=16, blank=True)
    country = CountryField()
    type = models.CharField(max_length=16, choices=INSTITUTION_TYPES, default=TYPE_UNIVERSITY)
Jorran de Wit's avatar
Jorran de Wit committed

        default_related_name = 'institutions'
        ordering = ['country']

Jorran de Wit's avatar
Jorran de Wit committed
    def __str__(self):
        return '{name} ({country})'.format(name=self.name, country=self.get_country_display())

    def get_absolute_url(self):
        return reverse('affiliations:institution_details', args=(self.id,))
Jorran de Wit's avatar
Jorran de Wit committed

Jorran de Wit's avatar
Jorran de Wit committed
    def contributors(self):
Jorran de Wit's avatar
Jorran de Wit committed
        return Contributor.objects.filter(affiliations__institution=self)
Jorran de Wit's avatar
Jorran de Wit committed

Jorran de Wit's avatar
Jorran de Wit committed

class Affiliation(models.Model):
    """
    An Affiliation is a (time dependent) connection between an Institution and a Contributor.
Jorran de Wit's avatar
Jorran de Wit committed
    This could thus be changed over time and history will be preserved.
    """
    institution = models.ForeignKey('affiliations.Institution')
Jorran de Wit's avatar
Jorran de Wit committed
    contributor = models.ForeignKey('scipost.Contributor')
    begin_date = models.DateField(null=True, blank=True)
    end_date = models.DateField(null=True, blank=True)

    objects = AffiliationQuerySet.as_manager()

Jorran de Wit's avatar
Jorran de Wit committed
    class Meta:
        default_related_name = 'affiliations'
        ordering = ['begin_date', 'end_date', 'institution']
Jorran de Wit's avatar
Jorran de Wit committed

    def __str__(self):
        return '{contributor} ({institution})'.format(
            contributor=self.contributor, institution=self.institution.name)