# Path: cms/managers.py
# class BlogPostManager(models.Manager):
#     def by_academic_year(self, year):
#         if isinstance(year, list):
#             return reduce(lambda a,b: a|b, [ self.by_academic_year(x) for x in year ])
#         else:
#             if isinstance(year, str) or isinstance(year, unicode):
#                 year = int(year)
# 
#             lower_limit = datetime.datetime(year, 7, 1)
#             upper_limit = datetime.datetime(year+1, 6, 30)
# 
#             return self.get_query_set().filter(date__gte=lower_limit, date__lte=upper_limit)
# 
# Path: cms/storage.py
# class OverwriteStorage(FileSystemStorage):
# 
#     def get_available_name(self, name):
#         """
#         Returns a filename that's free on the target storage system, and
#         available for new content to be written to.
#         """
#         # If the filename already exists, remove it as if it was a true file system
#         if self.exists(name):
#             os.remove(os.path.join(settings.MEDIA_ROOT, name))
#         return name


import hashlib
import os
from django.db import models
from django.conf import settings
from django.db.models import signals
from django.contrib.auth.models import User
from django.db.models import Q
from cms.managers import BlogPostManager
from cms.storage import OverwriteStorage

class Page(models.Model):
    title           = models.CharField(max_length=255)
    content         = models.TextField()
    slug            = models.SlugField(max_length=100, unique=True)
    weight          = models.IntegerField(default=0)
    pub_front_page  = models.BooleanField(default=False, verbose_name='Publish on homepage')
    pub_menu        = models.BooleanField(default=False, verbose_name='Publish on top menu')

    def __unicode__(self):
        return unicode(self.title)

    @models.permalink
    def get_absolute_url(self):
        return ('cms:page_url', (), {'slug': self.slug})

class Tag(models.Model):
    name            = models.CharField(max_length=255)

    def __unicode__(self):
        return unicode(self.name)

class BlogPost(models.Model):
    author          = models.ForeignKey(Member)
    title           = models.CharField(max_length=255)
    date            = models.DateTimeField(auto_now_add=True)
    edit_date       = models.DateTimeField(auto_now=True)
    post            = models.TextField(help_text='HTML is allowed')
    tags            = models.ManyToManyField(Tag, blank=True, related_name='blogposts')


# Path: cms/managers.py
# class BlogPostManager(models.Manager):
#     def by_academic_year(self, year):
#         if isinstance(year, list):
#             return reduce(lambda a,b: a|b, [ self.by_academic_year(x) for x in year ])
#         else:
#             if isinstance(year, str) or isinstance(year, unicode):
#                 year = int(year)
# 
#             lower_limit = datetime.datetime(year, 7, 1)
#             upper_limit = datetime.datetime(year+1, 6, 30)
# 
#             return self.get_query_set().filter(date__gte=lower_limit, date__lte=upper_limit)
# 
# Path: cms/storage.py
# class OverwriteStorage(FileSystemStorage):
# 
#     def get_available_name(self, name):
#         """
#         Returns a filename that's free on the target storage system, and
#         available for new content to be written to.
#         """
#         # If the filename already exists, remove it as if it was a true file system
#         if self.exists(name):
#             os.remove(os.path.join(settings.MEDIA_ROOT, name))
#         return name


# Path: cms/models.py
import hashlib
import os
from django.db import models
from django.conf import settings
from django.db.models import signals
from django.contrib.auth.models import User
from django.db.models import Q
from cms.managers import BlogPostManager
from cms.storage import OverwriteStorage


class Page(models.Model):
    title           = models.CharField(max_length=255)
    content         = models.TextField()
    slug            = models.SlugField(max_length=100, unique=True)
    weight          = models.IntegerField(default=0)
    pub_front_page  = models.BooleanField(default=False, verbose_name='Publish on homepage')
    pub_menu        = models.BooleanField(default=False, verbose_name='Publish on top menu')

    def __unicode__(self):
        return unicode(self.title)

    @models.permalink
    def get_absolute_url(self):
        return ('cms:page_url', (), {'slug': self.slug})

class Tag(models.Model):
    name            = models.CharField(max_length=255)

    def __unicode__(self):
        return unicode(self.name)

class BlogPost(models.Model):
    author          = models.ForeignKey(Member)
    title           = models.CharField(max_length=255)
    date            = models.DateTimeField(auto_now_add=True)
    edit_date       = models.DateTimeField(auto_now=True)
    post            = models.TextField(help_text='HTML is allowed')
    tags            = models.ManyToManyField(Tag, blank=True, related_name='blogposts')


    objects         = BlogPostManager()