Instantly share code, notes, and snippets.

@mekhami /models.py
Last active May 31, 2016

Embed
What would you like to do?
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)
from datetime import datetime, timedelta, date
from django.test import TestCase
from campaigns.models import ProductType, Product, Campaign, Customer, Order
# Create your tests here.
class CampaignTest(TestCase):
def setUp(self):
self.customer = Customer.objects.create()
self.order = Order.objects.create(customer=self.customer, paid=False)
self.half_page = ProductType.objects.create(
medium=ProductType.PRINT,
name='Half Page Ad',
price='1000',
run_length_period='ISSUES',
ad_deadline=21)
self.campaign = Campaign.objects.create(
order=self.order,
book_title='Lies of Locke',
internal_design=False,
start_date=date(2016, 5, 1) + timedelta(days=28))
self.product = Product.objects.create(campaign=self.campaign,
product_type=self.half_page,
run_length=14)
def test_ad_design_price(self):
self.assertEqual(self.campaign.ad_design_price, 1000)
def test_ad_deadline(self):
print(self.campaign.ad_deadline)
self.assertEqual(self.campaign.ad_deadline, date(2016, 5, 8))
class ProductTypeTest(TestCase):
def setUp(self):
self.half_page = ProductType.objects.create(
medium=ProductType.PRINT,
name='Half Page Ad',
price=1000,
run_length_period='ISSUES',
ad_deadline=21)
def test_slug(self):
self.assertEqual(self.half_page.slug, 'half-page-ad')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment