# Path: basic_cms/utils.py
# def export_po_files(path='poexport', stdout=None):
#     """
#     Export all the content from the published pages into
#     po files. The files will be automatically updated
#     with the new content if you run the command again.
#     """
#     if stdout is None:
#         import sys
#         stdout = sys.stdout
#     if not path.endswith('/'):
#         path += '/'
#     import polib
#     import os
#     from basic_cms.models import Page, Content
#     source_language = settings.PAGE_DEFAULT_LANGUAGE
#     source_list = []
#     for page in Page.objects.published():
#         source_list.extend(page.content_by_language(source_language))
# 
#     for lang in settings.PAGE_LANGUAGES:
#         if lang[0] != settings.PAGE_DEFAULT_LANGUAGE:
#             try:
#                 os.mkdir(path)
#             except OSError:
#                 pass
#             po_path = path + lang[0] + '.po'
#             stdout.write("Export language %s.\n" % lang[0])
#             po = polib.pofile(po_path)
#             po.metadata['Content-Type'] = 'text/plain; charset=utf-8'
# 
#             for source_content in source_list:
#                 page = source_content.page
#                 try:
#                     target_content = Content.objects.get_content_object(
#                         page, lang[0], source_content.type)
#                     msgstr = target_content.body
#                 except Content.DoesNotExist:
#                     target_content = None
#                     msgstr = ""
#                 if source_content.body:
#                     if target_content:
#                         tc_id = str(target_content.id)
#                     else:
#                         tc_id = ""
#                     entry = polib.POEntry(msgid=source_content.body,
#                         msgstr=msgstr)
#                     entry.tcomment = po_comment % (page.title(), do_not_msg,
#                         source_content.type, page.id, tc_id)
#                     if entry not in po:
#                         po.append(entry)
#             po.save(po_path)
#     stdout.write("""Export finished. The files are available """
#         """in the %s directory.\n""" % path)


from django.core.management.base import BaseCommand
from basic_cms.utils import export_po_files


class Command(BaseCommand):
    args = '<path>'
    help = export_po_files.__doc__

    def handle(self, *args, **options):

# Path: basic_cms/utils.py
# def export_po_files(path='poexport', stdout=None):
#     """
#     Export all the content from the published pages into
#     po files. The files will be automatically updated
#     with the new content if you run the command again.
#     """
#     if stdout is None:
#         import sys
#         stdout = sys.stdout
#     if not path.endswith('/'):
#         path += '/'
#     import polib
#     import os
#     from basic_cms.models import Page, Content
#     source_language = settings.PAGE_DEFAULT_LANGUAGE
#     source_list = []
#     for page in Page.objects.published():
#         source_list.extend(page.content_by_language(source_language))
# 
#     for lang in settings.PAGE_LANGUAGES:
#         if lang[0] != settings.PAGE_DEFAULT_LANGUAGE:
#             try:
#                 os.mkdir(path)
#             except OSError:
#                 pass
#             po_path = path + lang[0] + '.po'
#             stdout.write("Export language %s.\n" % lang[0])
#             po = polib.pofile(po_path)
#             po.metadata['Content-Type'] = 'text/plain; charset=utf-8'
# 
#             for source_content in source_list:
#                 page = source_content.page
#                 try:
#                     target_content = Content.objects.get_content_object(
#                         page, lang[0], source_content.type)
#                     msgstr = target_content.body
#                 except Content.DoesNotExist:
#                     target_content = None
#                     msgstr = ""
#                 if source_content.body:
#                     if target_content:
#                         tc_id = str(target_content.id)
#                     else:
#                         tc_id = ""
#                     entry = polib.POEntry(msgid=source_content.body,
#                         msgstr=msgstr)
#                     entry.tcomment = po_comment % (page.title(), do_not_msg,
#                         source_content.type, page.id, tc_id)
#                     if entry not in po:
#                         po.append(entry)
#             po.save(po_path)
#     stdout.write("""Export finished. The files are available """
#         """in the %s directory.\n""" % path)


# Path: basic_cms/management/commands/pages_export_po.py
from django.core.management.base import BaseCommand
from basic_cms.utils import export_po_files



class Command(BaseCommand):
    args = '<path>'
    help = export_po_files.__doc__

    def handle(self, *args, **options):

        export_po_files(*args)