# Path: micro_blog/models.py
# class Category(models.Model):
#     name = models.CharField(max_length=50, unique=True)
#     slug = models.CharField(max_length=50, unique=True)
#     description = models.CharField(max_length=500)
#     is_display = models.BooleanField(default=False)
#     min_published_blogs = models.IntegerField()
#     max_published_blogs = models.IntegerField()
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Category, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def get_url(self):
#         return settings.SITE_BLOG_URL + "category/" + self.slug
# 
#     def get_blog_posts(self):
#         return Post.objects.filter(category=self, status='P')
# 
#     def no_of_blog_posts(self):
#         return Post.objects.filter(category=self).count()
# 
# class Country(models.Model):
#     name = models.CharField(max_length=50, unique=True)
#     code = models.CharField(max_length=50)
#     slug = models.CharField(max_length=50, unique=True)
# 
#     def __str__(self):
#         return self.name


from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from micro_blog.models import Category, Country
from django.contrib.postgres.fields import ArrayField, JSONField
import json

class Page(models.Model):
    title = models.CharField(max_length=500)
    country = models.ForeignKey(Country, blank=True, null=True)
    is_default = models.BooleanField(default=False)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
    content = models.TextField()
    slug = models.SlugField()
    is_active = models.BooleanField(default=False)
    meta_data = models.TextField()
    category = models.ManyToManyField(Category)
    contact_info = JSONField(default={})

    # def save(self, *args, **kwargs):
    #     tempslug = slugify(self.title)
    #     if self.id:
    #         existed_page = Page.objects.get(pk=self.id)
    #         if existed_page.title != self.title:
    #             self.slug = create_slug(tempslug)
    #     else:
    #         self.slug = create_slug(tempslug)

    #     super(Page, self).save(*args, **kwargs)
    def get_contact_info(self):
        if self.contact_info:
            return json.dumps(self.contact_info)
        else:
            return ''

    def all_categories(self):

# Path: micro_blog/models.py
# class Category(models.Model):
#     name = models.CharField(max_length=50, unique=True)
#     slug = models.CharField(max_length=50, unique=True)
#     description = models.CharField(max_length=500)
#     is_display = models.BooleanField(default=False)
#     min_published_blogs = models.IntegerField()
#     max_published_blogs = models.IntegerField()
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Category, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def get_url(self):
#         return settings.SITE_BLOG_URL + "category/" + self.slug
# 
#     def get_blog_posts(self):
#         return Post.objects.filter(category=self, status='P')
# 
#     def no_of_blog_posts(self):
#         return Post.objects.filter(category=self).count()
# 
# class Country(models.Model):
#     name = models.CharField(max_length=50, unique=True)
#     code = models.CharField(max_length=50)
#     slug = models.CharField(max_length=50, unique=True)
# 
#     def __str__(self):
#         return self.name


# Path: pages/models.py
from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from micro_blog.models import Category, Country
from django.contrib.postgres.fields import ArrayField, JSONField
import json


class Page(models.Model):
    title = models.CharField(max_length=500)
    country = models.ForeignKey(Country, blank=True, null=True)
    is_default = models.BooleanField(default=False)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
    content = models.TextField()
    slug = models.SlugField()
    is_active = models.BooleanField(default=False)
    meta_data = models.TextField()
    category = models.ManyToManyField(Category)
    contact_info = JSONField(default={})

    # def save(self, *args, **kwargs):
    #     tempslug = slugify(self.title)
    #     if self.id:
    #         existed_page = Page.objects.get(pk=self.id)
    #         if existed_page.title != self.title:
    #             self.slug = create_slug(tempslug)
    #     else:
    #         self.slug = create_slug(tempslug)

    #     super(Page, self).save(*args, **kwargs)
    def get_contact_info(self):
        if self.contact_info:
            return json.dumps(self.contact_info)
        else:
            return ''

    def all_categories(self):

        categories = Category.objects.all()