|
from datetime import timedelta |
|
|
|
from django.db import models |
|
from django.db.models import Max, Sum |
|
from django.utils.text import slugify |
|
|
|
from inventory.models import AdSlot |
|
|
|
|
|
# Create your models here. |
|
class Campaign(models.Model): |
|
book_title = models.CharField(max_length=255) |
|
date_created = models.DateField(auto_now_add=True) |
|
date_updated = models.DateField(auto_now=True) |
|
internal_design = models.BooleanField() |
|
order = models.ForeignKey('Order') |
|
start_date = models.DateField() |
|
|
|
@property |
|
def ad_design_price(self): |
|
""" Get the sum of all ad design costs in the products for this campaign """ |
|
print(self.product_set.all()) |
|
return self.product_set.all().aggregate(total=Sum('price'))['total'] |
|
|
|
@property |
|
def ad_deadline(self): |
|
""" Get the distance between the start date and the earliest ad deadline. """ |
|
delta = self.product_set.aggregate(Max('ad_deadline'))['ad_deadline__max'] |
|
return self.start_date - timedelta(days=delta) |
|
|
|
|
|
class Product(models.Model): |
|
campaign = models.ForeignKey('Campaign') |
|
date_created = models.DateField(auto_now_add=True) |
|
date_updated = models.DateField(auto_now=True) |
|
product_type = models.ForeignKey('ProductType') |
|
run_length = models.IntegerField() |
|
#ad_slot = models.ForeignKey('AdSlot') |
|
|
|
|
|
class ProductType(models.Model): |
|
UNKNOWN = 'UNKNOWN' |
|
WEB = 'WEB' |
|
PRINT = 'PRINT' |
|
MOBILE = 'MOBILE' |
|
EMAIL = 'EMAIL' |
|
BILLBOARD = 'BILLBOARD' |
|
|
|
MEDIUM_CHOICES = ( |
|
(UNKNOWN, 'Unknown'), |
|
(WEB, 'Web'), |
|
(PRINT, 'Print'), |
|
(EMAIL, 'Email/Newsletter'), |
|
(BILLBOARD, 'Billboard'), |
|
) |
|
|
|
RUNLENGTH_CHOICES = ( |
|
('ISSUES', 'Issues'), |
|
('WEEKS', 'Weeks'), |
|
) |
|
|
|
medium = models.CharField(max_length=50, choices=MEDIUM_CHOICES) |
|
name = models.CharField(max_length=255, ) |
|
price = models.DecimalField(max_digits=8, decimal_places=2) |
|
run_length_period = models.CharField(max_length=50, choices=RUNLENGTH_CHOICES) |
|
slug = models.SlugField(unique=True) |
|
ad_deadline = models.IntegerField() |
|
|
|
def __str__(self): |
|
return self.name |
|
|
|
def save(self, **kwargs): |
|
if not self.slug: |
|
self.slug = slugify(self.name) |
|
super().save(**kwargs) |
|
|
|
@property |
|
def price(self): |
|
return "${}".format(self.price) |
|
|
|
|
|
# Three Customer Types |
|
class Customer(models.Model): |
|
# Add demographic information, payment information |
|
pass |
|
|
|
class Order(models.Model): |
|
customer = models.ForeignKey('Customer') |
|
paid = models.BooleanField(default=False) |