# Path: statirator/core/utils.py
# def i18n_permalink(*args):
#     """Return the correct URL in case of not the default language.
#     relaces djangos models.permalink.
# 
#     Assumes the name of the i18n version of the url pattern is prefix with
#     'i18n_' (This is done in the statirator's default urls.py).
# 
#     It looks for a `language` field for the instance. If the name is different,
#     pass it as the first param to the decorator. e.g::
# 
#         @i18n_permalink('lang_code')
#         def get_absolute_url(self):
#             return (...)
# 
#     """
#     def outer(f):
#         @functools.wraps(f)
#         def wrapper(obj, *args, **kwargs):
# 
#             bits = f(obj, *args, **kwargs)
#             name = bits[0]
# 
#             lang = getattr(obj, language_field)
#             return i18n_reverse(lang, name, None, *bits[1:3])
# 
#         return wrapper
# 
#     if len(args) == 1 and callable(args[0]):
#         # No arguments, this is the decorator
#         # Set default values for the arguments
#         language_field = 'language'
#         return outer(args[0])
#     else:
#         language_field = args[0]
#         return outer
# 
# Path: statirator/core/models.py
# class TranslationsMixin(object):
#     "Helper for getting transalations"
# 
#     SLUG_FIELD_FOR_TRANSLATIONS = 'slug'      # Overide in models if needed
#     LANG_FIELD_FOR_TRANSLATIONS = 'language'  # Overide in models if needed
# 
#     def get_translations(self):
#         "Query set for the translations"
# 
#         self_slug = getattr(self, self.SLUG_FIELD_FOR_TRANSLATIONS)
#         self_lang = getattr(self, self.LANG_FIELD_FOR_TRANSLATIONS)
# 
#         slug = {self.SLUG_FIELD_FOR_TRANSLATIONS + '__exact': self_slug}
#         lang = {self.LANG_FIELD_FOR_TRANSLATIONS + '__exact': self_lang}
#         return self.__class__.objects.filter(**slug).exclude(**lang)
# 
#     def get_language(self):
#         "Get the language display for this item's language"
# 
#         attr = 'get_{0}_display'.format(self.LANG_FIELD_FOR_TRANSLATIONS)
#         return getattr(self, attr)()


from django.db import models
from django.conf import settings
from statirator.core.utils import i18n_permalink
from statirator.core.models import TranslationsMixin


PAGE_TYPES = (
    ('rst', 'reStructured Text'),
    ('html', 'Django template'),
)


class Page(models.Model, TranslationsMixin):
    """A multilingual page"""

    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200)
    content = models.TextField()
    language = models.CharField(max_length=5, choices=settings.LANGUAGES,
                                blank=True, default=settings.LANGUAGE_CODE)
    page_type = models.CharField(max_length=5, choices=PAGE_TYPES)
    description = models.CharField(max_length=255, blank=True, null=True)

    def __unicode__(self):
        return self.title or self.slug


# Path: statirator/core/utils.py
# def i18n_permalink(*args):
#     """Return the correct URL in case of not the default language.
#     relaces djangos models.permalink.
# 
#     Assumes the name of the i18n version of the url pattern is prefix with
#     'i18n_' (This is done in the statirator's default urls.py).
# 
#     It looks for a `language` field for the instance. If the name is different,
#     pass it as the first param to the decorator. e.g::
# 
#         @i18n_permalink('lang_code')
#         def get_absolute_url(self):
#             return (...)
# 
#     """
#     def outer(f):
#         @functools.wraps(f)
#         def wrapper(obj, *args, **kwargs):
# 
#             bits = f(obj, *args, **kwargs)
#             name = bits[0]
# 
#             lang = getattr(obj, language_field)
#             return i18n_reverse(lang, name, None, *bits[1:3])
# 
#         return wrapper
# 
#     if len(args) == 1 and callable(args[0]):
#         # No arguments, this is the decorator
#         # Set default values for the arguments
#         language_field = 'language'
#         return outer(args[0])
#     else:
#         language_field = args[0]
#         return outer
# 
# Path: statirator/core/models.py
# class TranslationsMixin(object):
#     "Helper for getting transalations"
# 
#     SLUG_FIELD_FOR_TRANSLATIONS = 'slug'      # Overide in models if needed
#     LANG_FIELD_FOR_TRANSLATIONS = 'language'  # Overide in models if needed
# 
#     def get_translations(self):
#         "Query set for the translations"
# 
#         self_slug = getattr(self, self.SLUG_FIELD_FOR_TRANSLATIONS)
#         self_lang = getattr(self, self.LANG_FIELD_FOR_TRANSLATIONS)
# 
#         slug = {self.SLUG_FIELD_FOR_TRANSLATIONS + '__exact': self_slug}
#         lang = {self.LANG_FIELD_FOR_TRANSLATIONS + '__exact': self_lang}
#         return self.__class__.objects.filter(**slug).exclude(**lang)
# 
#     def get_language(self):
#         "Get the language display for this item's language"
# 
#         attr = 'get_{0}_display'.format(self.LANG_FIELD_FOR_TRANSLATIONS)
#         return getattr(self, attr)()


# Path: statirator/pages/models.py
from django.db import models
from django.conf import settings
from statirator.core.utils import i18n_permalink
from statirator.core.models import TranslationsMixin



PAGE_TYPES = (
    ('rst', 'reStructured Text'),
    ('html', 'Django template'),
)


class Page(models.Model, TranslationsMixin):
    """A multilingual page"""

    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200)
    content = models.TextField()
    language = models.CharField(max_length=5, choices=settings.LANGUAGES,
                                blank=True, default=settings.LANGUAGE_CODE)
    page_type = models.CharField(max_length=5, choices=PAGE_TYPES)
    description = models.CharField(max_length=255, blank=True, null=True)

    def __unicode__(self):
        return self.title or self.slug


    @i18n_permalink