# Path: Blog/posts/utils.py
# def count_words(html_string):
#     string = strip_tags(html_string)
#     num_chinese = len(re.findall(u'[\u4e00-\u9fa5]', string))
#     num_english = len(re.findall(r'\b[a-zA-Z.,"]+\b',string))
#     return num_chinese, num_english


from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db.models.signals import pre_save, post_delete
from django.utils.text import slugify
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.contrib.contenttypes.models import ContentType
from markdown2 import markdown
from unidecode import unidecode
from .utils import count_words
        from comments.models import Comment

def upload_location(instance, filename):
    PostModel = instance.__class__
    try:
        new_id = PostModel.objects.order_by("id").last().id + 1
    except:
        new_id = 1
    return "%s/%s" % (new_id, filename)

class ActivePostManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(ActivePostManager, self).filter(draft=False, published__lte=timezone.now().date())

# Create your models here.
class Post(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    title = models.CharField(max_length=120)
    slug = models.SlugField(unique=True, null=True)
    image = models.ImageField(null=True, blank=True,
                              upload_to=upload_location,
                              width_field="width_field",
                              height_field="height_field")
    width_field = models.IntegerField(default=0)
    height_field = models.IntegerField(default=0)
    content = models.TextField()
    draft = models.BooleanField(default=False)
    published = models.DateField(auto_now=False, auto_now_add=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)  # auto_now triggered when model.save() called
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)  # auto_now_add triggered when object first created

# Path: Blog/posts/utils.py
# def count_words(html_string):
#     string = strip_tags(html_string)
#     num_chinese = len(re.findall(u'[\u4e00-\u9fa5]', string))
#     num_english = len(re.findall(r'\b[a-zA-Z.,"]+\b',string))
#     return num_chinese, num_english


# Path: Blog/posts/models.py
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db.models.signals import pre_save, post_delete
from django.utils.text import slugify
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.contrib.contenttypes.models import ContentType
from markdown2 import markdown
from unidecode import unidecode
from .utils import count_words
        from comments.models import Comment


def upload_location(instance, filename):
    PostModel = instance.__class__
    try:
        new_id = PostModel.objects.order_by("id").last().id + 1
    except:
        new_id = 1
    return "%s/%s" % (new_id, filename)

class ActivePostManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(ActivePostManager, self).filter(draft=False, published__lte=timezone.now().date())

# Create your models here.
class Post(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    title = models.CharField(max_length=120)
    slug = models.SlugField(unique=True, null=True)
    image = models.ImageField(null=True, blank=True,
                              upload_to=upload_location,
                              width_field="width_field",
                              height_field="height_field")
    width_field = models.IntegerField(default=0)
    height_field = models.IntegerField(default=0)
    content = models.TextField()
    draft = models.BooleanField(default=False)
    published = models.DateField(auto_now=False, auto_now_add=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)  # auto_now triggered when model.save() called
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)  # auto_now_add triggered when object first created

    num_chinese = models.IntegerField(default=0)