# Path: statirator/core/utils.py
# def find_files(root, extensions):
#     """Generator finding files from a root dir with specific extensions
# 
#     :param root: Root directory for the search
#     :param extensions: List of extensions to search (e.g: ['.rst', '.txt'])
#     """
# 
#     for root, dirs, files in os.walk(root):
#         for f in files:
#             if os.path.splitext(f)[1] in extensions:
#                 yield os.path.join(root, f)
# 
# def render_block_to_string(template_or_name, block, dictionary=None,
#                            context_instance=None):
#     """
#     Loads the given template_name and renders the given block with the given dictionary as
#     context. Returns a string.
# 
#     Can be used to extract data from template blocks (e.g: get the title from
#     {% block title %}{% endblock %})
# 
#     """
#     from django.template import Context, Template
# 
#     dictionary = dictionary or {}
#     if isinstance(template_or_name, Template):
#         t = template_or_name
#     else:
#         t = get_template(template_or_name)
# 
#     if context_instance:
#         context_instance.update(dictionary)
#     else:
#         context_instance = Context(dictionary)
#     return render_template_block(t, block, context_instance)
# 
# Path: statirator/pages/utils.py
# def get_pages_dir():
#     "Returns the pages directory from settings, or default one if not found"
# 
#     from django.conf import settings
# 
#     return getattr(settings, 'PAGES_DIR',
#                    os.path.join(settings.ROOT_DIR, 'pages'))
# 
# Path: statirator/pages/models.py
# 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
#     def get_absolute_url(self):
#         # index is a special case
#         if self.slug == 'index':
#             return ('pages_index', (), {})
# 
#         return ('pages_page', (), {'slug': self.slug})


import os
from django.conf import settings
from django.template import Template, RequestContext
from django.utils import translation
from django.test.client import RequestFactory
from statirator.core.utils import find_files, render_block_to_string
from .utils import get_pages_dir
from .models import Page
from __future__ import print_function, absolute_import




def update_post(slug, lang_code, defaults):
    with translation.override(lang_code):
        page, created = Page.objects.get_or_create(
            slug=slug,
            language=lang_code,
            defaults=defaults)

        if not created:
            for field, val in defaults.iteritems():
                setattr(page, field, val)
        # get the title from the template
        t = Template(page.content)
        req = RequestFactory().get(page.get_absolute_url(), LANGUAGE_CODE=lang_code)
        page.title = render_block_to_string(
            t, 'title', context_instance=RequestContext(req))
        page.description = render_block_to_string(
            t, 'description', context_instance=RequestContext(req))

        page.save()


def html_reader():
    "Finds django html pages, parses them and loads into the db."


# Path: statirator/core/utils.py
# def find_files(root, extensions):
#     """Generator finding files from a root dir with specific extensions
# 
#     :param root: Root directory for the search
#     :param extensions: List of extensions to search (e.g: ['.rst', '.txt'])
#     """
# 
#     for root, dirs, files in os.walk(root):
#         for f in files:
#             if os.path.splitext(f)[1] in extensions:
#                 yield os.path.join(root, f)
# 
# def render_block_to_string(template_or_name, block, dictionary=None,
#                            context_instance=None):
#     """
#     Loads the given template_name and renders the given block with the given dictionary as
#     context. Returns a string.
# 
#     Can be used to extract data from template blocks (e.g: get the title from
#     {% block title %}{% endblock %})
# 
#     """
#     from django.template import Context, Template
# 
#     dictionary = dictionary or {}
#     if isinstance(template_or_name, Template):
#         t = template_or_name
#     else:
#         t = get_template(template_or_name)
# 
#     if context_instance:
#         context_instance.update(dictionary)
#     else:
#         context_instance = Context(dictionary)
#     return render_template_block(t, block, context_instance)
# 
# Path: statirator/pages/utils.py
# def get_pages_dir():
#     "Returns the pages directory from settings, or default one if not found"
# 
#     from django.conf import settings
# 
#     return getattr(settings, 'PAGES_DIR',
#                    os.path.join(settings.ROOT_DIR, 'pages'))
# 
# Path: statirator/pages/models.py
# 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
#     def get_absolute_url(self):
#         # index is a special case
#         if self.slug == 'index':
#             return ('pages_index', (), {})
# 
#         return ('pages_page', (), {'slug': self.slug})


# Path: statirator/pages/readers.py
import os
from django.conf import settings
from django.template import Template, RequestContext
from django.utils import translation
from django.test.client import RequestFactory
from statirator.core.utils import find_files, render_block_to_string
from .utils import get_pages_dir
from .models import Page

from __future__ import print_function, absolute_import




def update_post(slug, lang_code, defaults):
    with translation.override(lang_code):
        page, created = Page.objects.get_or_create(
            slug=slug,
            language=lang_code,
            defaults=defaults)

        if not created:
            for field, val in defaults.iteritems():
                setattr(page, field, val)
        # get the title from the template
        t = Template(page.content)
        req = RequestFactory().get(page.get_absolute_url(), LANGUAGE_CODE=lang_code)
        page.title = render_block_to_string(
            t, 'title', context_instance=RequestContext(req))
        page.description = render_block_to_string(
            t, 'description', context_instance=RequestContext(req))

        page.save()


def html_reader():
    "Finds django html pages, parses them and loads into the db."


    for page in find_files(get_pages_dir(), ['.html']):