# Path: micro_blog/models.py
# class Post(models.Model):
#     STATUS_CHOICE = (
#                     ('D', 'Draft'),
#                     ('P', 'Published'),
#                     ('T', 'Rejected'),
#                     ('R', 'Review'),
#                     )
# 
#     title = models.CharField(max_length=100)
#     created_on = models.DateTimeField(auto_now_add=True)
#     updated_on = models.DateField(auto_now=True)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     content = models.TextField()
#     excerpt = models.CharField(max_length=500, default="")
#     category = models.ForeignKey(Category, related_name='blog_posts')
#     tags = models.ManyToManyField(Tags, related_name='rel_posts', blank=True)
#     status = models.CharField(max_length=2, choices=STATUS_CHOICE, blank=True)
#     published_on = models.DateField(blank=True, null=True)
#     meta_description = models.TextField(max_length=500, default='')
#     old_slugs = models.TextField()
# 
#     def __unicode__(self):
#         return self.title
# 
#     def create_blog_slug(self, slugs):
#         for each_slug in list(set(slugs)):
#             blog_slugs = self.slugs.filter(slug=each_slug)
#             if not blog_slugs:
#                 actual_slug = get_blog_slug(each_slug)
#                 Post_Slugs.objects.create(
#                     blog=self, slug=actual_slug, is_active=False
#                 )
# 
#     def check_and_activate_slug(self):
#         # Check whether active slug is their for blog-post or not
#         blog_slugs = self.slugs.all().order_by("id")
#         if not blog_slugs.filter(is_active=True):
#             active_slug = blog_slugs.last()
#             active_slug.is_active = True
#             active_slug.save()
# 
#     @property
#     def slug(self):
#         blog_slug = self.slugs.filter(is_active=True).first()
#         if blog_slug:
#             return str(blog_slug.slug)
#         return ""
# 
#     @property
#     def author(self):
#         return self.user.first_name + ' ' + self.user.last_name
# 
#     def save(self, *args, **kwargs):
#         if self.status == 'P':
#             if not self.published_on:
#                 self.published_on = datetime.datetime.today()
#         super(Post, self).save(*args, **kwargs)
# 
#     @property
#     def get_url(self):
#         return settings.SITE_BLOG_URL + self.slug
# 
#     def is_editable_by(self, user):
#         if self.user == user or user.is_superuser:
#             return True
#         return False
# 
#     def is_deletable_by(self, user):
#         if self.user == user or user.is_superuser:
#             return True
#         return False
# 
#     def get_content(self):
#         table = html.fromstring(self.content)
#         content = ''
#         for item in table:
#             if item.text:
#                 content += item.text.strip()
#         return content[:350]


from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager
from micro_blog.models import Post
from django.template.defaultfilters import slugify
from django.utils import timezone
from datetime import datetime
from datetime import timedelta
        return self.first_name

    def __unicode__(self):
        return self.first_name + ' (' + self.email + ')'

    def total_posts(self):
        return Post.objects.filter(user=self).count()

    def drafted_posts(self):
        return Post.objects.filter(user=self, status="D").count()

    def is_site_admin(self):
        if self.user_roles == "Admin" or self.is_superuser:
            return True
        return False

    class Meta:
        permissions = (
            ("blog_moderator", "Can enable or disable blog posts"),
            ("blogger", "Can write blog posts"),
        )

    def get_week_posts(self, value):
        if value:
            value = value.split('-')
            previous_date = datetime.strptime(value[0].strip(), "%Y/%m/%d").strftime("%Y-%m-%d")
            current_date = datetime.strptime(value[1].strip(), "%Y/%m/%d").strftime("%Y-%m-%d")
        else:
            current_date = datetime.strptime(str(datetime.now().date()), "%Y-%m-%d").strftime("%Y-%m-%d")
            previous_date = datetime.strptime(str(datetime.now().date() - timedelta(days=7)), "%Y-%m-%d").strftime("%Y-%m-%d")

# Path: micro_blog/models.py
# class Post(models.Model):
#     STATUS_CHOICE = (
#                     ('D', 'Draft'),
#                     ('P', 'Published'),
#                     ('T', 'Rejected'),
#                     ('R', 'Review'),
#                     )
# 
#     title = models.CharField(max_length=100)
#     created_on = models.DateTimeField(auto_now_add=True)
#     updated_on = models.DateField(auto_now=True)
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     content = models.TextField()
#     excerpt = models.CharField(max_length=500, default="")
#     category = models.ForeignKey(Category, related_name='blog_posts')
#     tags = models.ManyToManyField(Tags, related_name='rel_posts', blank=True)
#     status = models.CharField(max_length=2, choices=STATUS_CHOICE, blank=True)
#     published_on = models.DateField(blank=True, null=True)
#     meta_description = models.TextField(max_length=500, default='')
#     old_slugs = models.TextField()
# 
#     def __unicode__(self):
#         return self.title
# 
#     def create_blog_slug(self, slugs):
#         for each_slug in list(set(slugs)):
#             blog_slugs = self.slugs.filter(slug=each_slug)
#             if not blog_slugs:
#                 actual_slug = get_blog_slug(each_slug)
#                 Post_Slugs.objects.create(
#                     blog=self, slug=actual_slug, is_active=False
#                 )
# 
#     def check_and_activate_slug(self):
#         # Check whether active slug is their for blog-post or not
#         blog_slugs = self.slugs.all().order_by("id")
#         if not blog_slugs.filter(is_active=True):
#             active_slug = blog_slugs.last()
#             active_slug.is_active = True
#             active_slug.save()
# 
#     @property
#     def slug(self):
#         blog_slug = self.slugs.filter(is_active=True).first()
#         if blog_slug:
#             return str(blog_slug.slug)
#         return ""
# 
#     @property
#     def author(self):
#         return self.user.first_name + ' ' + self.user.last_name
# 
#     def save(self, *args, **kwargs):
#         if self.status == 'P':
#             if not self.published_on:
#                 self.published_on = datetime.datetime.today()
#         super(Post, self).save(*args, **kwargs)
# 
#     @property
#     def get_url(self):
#         return settings.SITE_BLOG_URL + self.slug
# 
#     def is_editable_by(self, user):
#         if self.user == user or user.is_superuser:
#             return True
#         return False
# 
#     def is_deletable_by(self, user):
#         if self.user == user or user.is_superuser:
#             return True
#         return False
# 
#     def get_content(self):
#         table = html.fromstring(self.content)
#         content = ''
#         for item in table:
#             if item.text:
#                 content += item.text.strip()
#         return content[:350]


# Path: micro_admin/models.py
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager
from micro_blog.models import Post
from django.template.defaultfilters import slugify
from django.utils import timezone
from datetime import datetime
from datetime import timedelta

        return self.first_name

    def __unicode__(self):
        return self.first_name + ' (' + self.email + ')'

    def total_posts(self):
        return Post.objects.filter(user=self).count()

    def drafted_posts(self):
        return Post.objects.filter(user=self, status="D").count()

    def is_site_admin(self):
        if self.user_roles == "Admin" or self.is_superuser:
            return True
        return False

    class Meta:
        permissions = (
            ("blog_moderator", "Can enable or disable blog posts"),
            ("blogger", "Can write blog posts"),
        )

    def get_week_posts(self, value):
        if value:
            value = value.split('-')
            previous_date = datetime.strptime(value[0].strip(), "%Y/%m/%d").strftime("%Y-%m-%d")
            current_date = datetime.strptime(value[1].strip(), "%Y/%m/%d").strftime("%Y-%m-%d")
        else:
            current_date = datetime.strptime(str(datetime.now().date()), "%Y-%m-%d").strftime("%Y-%m-%d")
            previous_date = datetime.strptime(str(datetime.now().date() - timedelta(days=7)), "%Y-%m-%d").strftime("%Y-%m-%d")

        post = Post.objects.filter(user=self, created_on__range=(previous_date, current_date))