ivancrneto
3/10/2015 - 6:57 PM

models.py

# -*- coding: utf-8 -*-
from django.contrib.sites.models import Site
from django.db import models


class AdChannel(models.Model):
    id = models.CharField(max_length=100, primary_key=True)
    name = models.CharField(max_length=50)
    CHANNEL_TYPE_CHOICES = (
        ('page', 'Page'), ('unit', 'Ad Unit'), ('template', 'Template'),
        ('length', 'Content length'), ('property', 'Property'))
    channel_type = models.CharField(max_length=50, default='unit',
                                    choices=CHANNEL_TYPE_CHOICES)
    description = models.TextField(blank=True)
    in_use = models.BooleanField(default=False)
    parent = models.ForeignKey('self', blank=True, null=True,
                               related_name='children')
    site = models.ForeignKey(Site, blank=True, null=True)

    # Deprecated fields
    min_length = models.IntegerField(default=0)
    max_length = models.IntegerField(default=0)

    def __unicode__(self):
        return u'{}'.format(self.name)


class AdUnit(models.Model):
    channels = models.ManyToManyField(AdChannel, blank=True)
    name = models.CharField(max_length=50, unique=True)
    description = models.TextField(blank=True)
    network = models.CharField(max_length=50, default='g',
                               choices=(('g', 'google'), ('ch', 'chitika'),
                                        ('asterpix', 'asterpix'),
                                        ('aol', 'aol'),
                                        ('bills', 'bills.com')))
    in_use = models.BooleanField(default=False)
    template_name = models.CharField(max_length=50, blank=True)
    priority = models.IntegerField(default=10)
    min_offset = models.IntegerField(default=0)
    ad_count = models.IntegerField(default=0)
    content_selector = models.CharField(max_length=100, blank=True)
    title = models.CharField(default='Ads by Google', max_length=100)

    # Deprecated fields
    script = models.TextField(blank=True)
    js_placement = models.BooleanField(default=False)
    js_placement_override = models.BooleanField(default=False)
    min_content_length = models.IntegerField(default=0)
    min_char = models.IntegerField(default=0)
    max_char = models.IntegerField(default=0)
    max_offset = models.IntegerField(default=0)

    def __unicode__(self):
        return u'{}'.format(self.name)


class AdGroup(models.Model):
    '''
    AdGroup can have mutliple custom unit ads that are all square boxes
    for sidebars / topbars / top links at different sizes. Also knows if one
    is mobile for scaling down. This allows for different ads to be able to
    show up in one area on a page based on different criteria
    '''
    name = models.CharField(max_length=100)
    ads = models.ManyToManyField('ads.CustomUnit', related_name='groups')
    active = models.BooleanField()

    def __unicode__(self):
        return '{} - {}'.format('AdGroup', self.name)


class AdPage(models.Model):
    topbar = models.ForeignKey(AdGroup, related_name='topbar_adpages')
    sidebar = models.ForeignKey(AdGroup, related_name='sidebar_adpages')
    top_link_unit = models.ForeignKey(
        AdGroup, related_name='top_link_unit_adpages')

    category = models.OneToOneField(
        'categories.Category', related_name='ad_page')

    include_price_comparison = models.BooleanField(
        help_text='Check this if you want to include "Compare price" links'
        'to comparison tables and guides')
    price_comparison_link = models.URLField(blank=True, null=True)

    def __unicode__(self):
        return '{} - {}'.format('AdPage', self.category)


class CustomUnit(models.Model):
    image_local = models.ImageField(upload_to='ads-custom-units')
    name = models.CharField(max_length=100)
    mobile = models.BooleanField()
    link = models.URLField(
        help_text='Link the custom unit will be pointing to', blank=True,
        null=True)

    def __unicode__(self):
        return '{} - {}'.format('CustomUnit', self.name)