# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError, ObjectDoesNotExist

from opps.core.models.published import Published



class Channel(Published):

    name = models.CharField(_(u"Name"), max_length=60, unique=True)
    slug = models.SlugField(u"URL", max_length=150, unique=True,
            db_index=True)
    description = models.CharField(_(u"Description"),
            max_length=255, null=True, blank=True)
    position = models.IntegerField(_(u"Position"), default=1)
    site = models.ForeignKey(Site, related_name='site')
    channel = models.ForeignKey('self', related_name='subchannel',
            null=True, blank=True)

    class Meta:
        app_label = 'opps'

    def __unicode__(self):
        if self.channel:
            return "%s/%s" % (self.channel, self.slug)
        return "%s/%s" % (self.site.domain, self.slug)

    def clean(self):

        try:
            channel_exist_domain = Channel.objects.filter(slug=self.slug,
                    site__domain=self.site.domain)
            channel_home = Channel.objects.filter(site__domain=self.site.domain,
                    channel=None, published=True)
        except ObjectDoesNotExist:
            return False

        if len(channel_exist_domain) >= 1 and not self.pk:
            raise ValidationError('Slug exist in domain!')

        if not self.channel and len(channel_home) >= 1 and not self.pk:
            raise ValidationError('Exist home channel published!')

# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError, ObjectDoesNotExist

from opps.core.models.published import Published
from opps.core.models.date import Date



class Channel(Published, Date):

    name = models.CharField(_(u"Name"), max_length=60, unique=True)
    slug = models.SlugField(u"URL", max_length=150, unique=True,
            db_index=True)
    description = models.CharField(_(u"Description"),
            max_length=255, null=True, blank=True)
    position = models.IntegerField(_(u"Position"), default=1)
    site = models.ForeignKey(Site, related_name='site')
    channel = models.ForeignKey('self', related_name='subchannel',
            null=True, blank=True)

    class Meta:
        app_label = 'opps'

    def __unicode__(self):
        if self.channel:
            return "%s/%s" % (self.channel, self.slug)
        return "%s/%s" % (self.site.domain, self.slug)

    def clean(self):

        try:
            channel_exist_domain = Channel.objects.filter(slug=self.slug,
                    site__domain=self.site.domain)
            channel_home = Channel.objects.filter(site__domain=self.site.domain,
                    channel=None, published=True)
        except ObjectDoesNotExist:
            return False

        if len(channel_exist_domain) >= 1 and not self.pk:
            raise ValidationError('Slug exist in domain!')

        if not self.channel and len(channel_home) >= 1 and not self.pk:
            raise ValidationError('Exist home channel published!')

Add core date models on channel models