Create PublicationYear on API request

Closes #3
This commit is contained in:
Théophile Bastian 2017-09-24 18:41:31 +02:00
parent 6d2ab4c0ca
commit 673ce754f6
2 changed files with 32 additions and 0 deletions

View file

@ -78,4 +78,6 @@ def publishApiView(request, data):
"Invalid data: {}".format(e))
pub.save()
pub.createPubYear()
return response.HttpResponse("OK")

View file

@ -47,6 +47,36 @@ class Publication(models.Model):
max_length=128,
blank=True)
class NoPublicationYear(Exception):
def __str__(self):
return "No matching publication year."
@property
def numericPublicationYear(self):
startYear = self.date.year
if self.date.month < 8:
return startYear - 1
return startYear
def publicationYear(self):
''' Fetch corresponding publication year
Raise `NoPublicationYear` if there is no such entry '''
startYear = self.numericPublicationYear
try:
return PublicationYear.objects.get(startYear=startYear)
except PublicationYear.DoesNotExist:
raise self.NoPublicationYear
def createPubYear(self):
''' Creates the corresponding publication year if needed. '''
if (PublicationYear.objects
.filter(startYear=self.numericPublicationYear).count()) == 0:
pubYear = PublicationYear(startYear=self.numericPublicationYear,
descr='')
pubYear.save()
return True
return False
def __str__(self):
if self.custom_name:
return self.custom_name