models.py: add catalog pdf path helpers

This commit is contained in:
Seth Ladygo
2019-04-25 16:50:50 -07:00
parent 9b3e711c28
commit 8976e791d3

View File

@ -1,7 +1,9 @@
from django.db import models
from django.conf import settings
from django.contrib.postgres.fields import JSONField
from django.db import models
import datetime
import os.path
import re
class Season(models.Model):
@ -26,6 +28,8 @@ def unix_datetime(date):
class Catalog(models.Model):
PDF_DIR = 'catalogs'
PDF_URL = 'export/catalogs'
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
season = models.ForeignKey(Season, on_delete=models.PROTECT)
@ -38,10 +42,34 @@ class Catalog(models.Model):
sections = models.PositiveIntegerField(default=0)
materials = models.PositiveIntegerField(default=0)
data = JSONField(null=True)
# build_progress = models.PositiveIntegerField(default=0)
# JSONField docs:
# https://docs.djangoproject.com/en/2.2/ref/contrib/postgres/fields/#jsonfield
def pdf_name(self):
pdf = "{}-{}-{}".format(self.season.id, self.name, self.id)
pdf = re.sub(r'\s+', r'_', pdf)
pdf = re.sub(r'[^\w\-]', r'', pdf)
pdf += '.pdf'
return pdf
def pdf_url(self):
return "http://{}/{}/{}/{}".format(settings.PUBLIC_WEB_HOST,
self.PDF_URL,
self.season.id,
self.pdf_name())
def pdf_file(self):
return os.path.join(settings.ASSET_DIR,
self.PDF_DIR,
self.season.id,
self.pdf_name())
def pdf_exists(self):
return os.path.isfile(self.pdf_file())
def summary(self):
return {
'id': self.id,
@ -57,4 +85,5 @@ class Catalog(models.Model):
'pages': self.pages,
'sections': self.sections,
'materials': self.materials,
'pdf': self.pdf_url(),
}