# Path: pages/models.py
# 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()
#         if self.category.all().count() == categories.count():
#             return True
#         else:
#             return False
# 
#     def related_pages(self):
#         menu = Menu.objects.filter(url=self.slug)
#         menus = Menu.objects.filter(parent=menu[0].parent, status='on')
#         return menus
# 
#     def __unicode__(self):
#         return self.title
# 
# class Menu(models.Model):
#     parent = models.ForeignKey('self', blank=True, null=True)
#     title = models.CharField(max_length=255)
#     country = models.ForeignKey(Country, blank=True, null=True)
#     url = models.URLField(max_length=255, blank=True, null=True)
#     created = models.DateTimeField(auto_now=True)
#     updated = models.DateTimeField(auto_now=True)
#     status = models.CharField(max_length=5, default="off", blank=True)
#     lvl = models.IntegerField()
# 
#     def menu_state(self):
#         if self.status == 'on':
#             return True
#         else:
#             return False
# 
#     def __unicode__(self):
#         return self.title
# 
#     def has_children(self):
#         if self.menu_set.exists():
#             return True
#         return False
# 
#     def is_child(self):
#         if self.parent:
#             return True
#         return False
# 
#     def get_active_children(self):
#         return self.menu_set.filter(status='on').order_by('lvl')
# 
# class Contact(models.Model):
# 
#     ENQUERY_TYPES = (
#                     ('general', 'Request For Services'),
#                     ('partnership', 'Partnership Queries'),
#                     ('media', 'Media Queries'),
#                     ('general queries', 'General Queries'),
#                     ('feedback', 'Website Feedback'),
#                     ('others', 'Others'),
#                     )
# 
#     domain = models.CharField(max_length=100, null=True, blank=True)
#     domain_url = models.URLField(max_length=200, null=True, blank=True)
#     country = models.CharField(max_length=100)
#     enquery_type = models.CharField(max_length=100, choices=ENQUERY_TYPES)
# 
#     # def __unicode__(self):
#     #     return self.contact_info.full_name
# 
# Path: micro_blog/models.py
# class Subscribers(models.Model):
#     email = models.EmailField(max_length=255)
#     created = models.DateField(auto_now_add=True)
#     blog_post = models.BooleanField(default=True)
#     category = models.ForeignKey(Category, blank=True, null=True)
# 
#     def __str__(self):
#         return self.email
# 
# 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()


from django import forms
from pages.models import Page, Menu, Contact
from micro_blog.models import Subscribers, Category
import json
    enquery_type = forms.CharField(max_length=200, required=False)

    class Meta:
        model = Contact
        exclude = ('full_name', 'message', 'email', 'phone', 'contact_info', 'country', 'enquery_type')

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        # if 'country' in self.data.keys():
        #     self.fields['country'].required = True

        if 'enquery_type' in self.data.keys():
            self.fields['enquery_type'].required = True


class SubscribeForm(forms.ModelForm):
    class Meta:
        model = Subscribers
        fields = ('email',)

    def __init__(self, *args, **kwargs):
        super(SubscribeForm, self).__init__(*args, **kwargs)

    def clean_email(self):

        if str(self.data['is_blog']) == 'True':
            if len(self.data['is_category']) > 0:
                category = Category.objects.get(id=self.data['is_category'])
                existing_subscribers = Subscribers.objects.filter(email=self.data['email'], blog_post=True, category=category)
            else:

# Path: pages/models.py
# 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()
#         if self.category.all().count() == categories.count():
#             return True
#         else:
#             return False
# 
#     def related_pages(self):
#         menu = Menu.objects.filter(url=self.slug)
#         menus = Menu.objects.filter(parent=menu[0].parent, status='on')
#         return menus
# 
#     def __unicode__(self):
#         return self.title
# 
# class Menu(models.Model):
#     parent = models.ForeignKey('self', blank=True, null=True)
#     title = models.CharField(max_length=255)
#     country = models.ForeignKey(Country, blank=True, null=True)
#     url = models.URLField(max_length=255, blank=True, null=True)
#     created = models.DateTimeField(auto_now=True)
#     updated = models.DateTimeField(auto_now=True)
#     status = models.CharField(max_length=5, default="off", blank=True)
#     lvl = models.IntegerField()
# 
#     def menu_state(self):
#         if self.status == 'on':
#             return True
#         else:
#             return False
# 
#     def __unicode__(self):
#         return self.title
# 
#     def has_children(self):
#         if self.menu_set.exists():
#             return True
#         return False
# 
#     def is_child(self):
#         if self.parent:
#             return True
#         return False
# 
#     def get_active_children(self):
#         return self.menu_set.filter(status='on').order_by('lvl')
# 
# class Contact(models.Model):
# 
#     ENQUERY_TYPES = (
#                     ('general', 'Request For Services'),
#                     ('partnership', 'Partnership Queries'),
#                     ('media', 'Media Queries'),
#                     ('general queries', 'General Queries'),
#                     ('feedback', 'Website Feedback'),
#                     ('others', 'Others'),
#                     )
# 
#     domain = models.CharField(max_length=100, null=True, blank=True)
#     domain_url = models.URLField(max_length=200, null=True, blank=True)
#     country = models.CharField(max_length=100)
#     enquery_type = models.CharField(max_length=100, choices=ENQUERY_TYPES)
# 
#     # def __unicode__(self):
#     #     return self.contact_info.full_name
# 
# Path: micro_blog/models.py
# class Subscribers(models.Model):
#     email = models.EmailField(max_length=255)
#     created = models.DateField(auto_now_add=True)
#     blog_post = models.BooleanField(default=True)
#     category = models.ForeignKey(Category, blank=True, null=True)
# 
#     def __str__(self):
#         return self.email
# 
# 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()


# Path: pages/forms.py
from django import forms
from pages.models import Page, Menu, Contact
from micro_blog.models import Subscribers, Category
import json

    enquery_type = forms.CharField(max_length=200, required=False)

    class Meta:
        model = Contact
        exclude = ('full_name', 'message', 'email', 'phone', 'contact_info', 'country', 'enquery_type')

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        # if 'country' in self.data.keys():
        #     self.fields['country'].required = True

        if 'enquery_type' in self.data.keys():
            self.fields['enquery_type'].required = True


class SubscribeForm(forms.ModelForm):
    class Meta:
        model = Subscribers
        fields = ('email',)

    def __init__(self, *args, **kwargs):
        super(SubscribeForm, self).__init__(*args, **kwargs)

    def clean_email(self):

        if str(self.data['is_blog']) == 'True':
            if len(self.data['is_category']) > 0:
                category = Category.objects.get(id=self.data['is_category'])
                existing_subscribers = Subscribers.objects.filter(email=self.data['email'], blog_post=True, category=category)
            else:

                existing_subscribers = Subscribers.objects.filter(email=self.data['email'], blog_post=True, category=None)