#!/usr/bin/python
# A script to write initial content data into database


import sys
import os
import codecs
#from optparse import OptionParser

from django.core.management import setup_environ

my_path = os.path.abspath(os.path.dirname(__file__))
kamu_path = os.path.normpath(my_path + '/..')
# Change this if you have your initial content elsewhere
content_path = os.path.normpath(my_path + '/../Content/cms')

sys.path.insert(1, kamu_path)
sys.path.insert(2, os.path.normpath(kamu_path + '/..'))

allowed_markups = ['html', 'markdown']

from kamu import settings

setup_environ(settings)
from django.db import connection, transaction
from django import db

from kamu.cms.models import Category, Newsitem, Item, Content, Revision

def process_file(root, filename, category_name, type):
    print "Processing file %s" % os.path.join(root, filename)

    #Special case for newsitems
    if filename.startswith("news"):
        (category_name, newsdate, order) = filename.split("_")

    category = Category.objects.filter(name=category_name)

    if category.count() == 0:
        print "Creating category %s" % category_name
        category = Category(name=category_name)
        category.save()
    else:
        assert(category.count() == 1)
        category = category[0]

    if category_name == "news":
        print "Processing newsitem with date %s" % newsdate
        item = Newsitem.objects.filter(category=category, date=newsdate)
        # FIXME: Many newsitems per date
        if not item.count():
            item = Newsitem(category=category, date=newsdate)
            item.save()
        else:
            assert(item.count() == 1)
            item = item[0]
    else:
        item = Item.objects.filter(category=category)
        if item.count() == 0:
            print "Creating_item under category %s" % category_name
            item = Item(category=category)
            item.save()
        else:
            item = item[0]

    content = Content.objects.filter(item=item, language=language)
    if content.count() == 0:
        print "Creating content for item %s with lang %s" % (item, language)
        content = Content(item=item, language=language)
        content.save()
    else:
        # FIXME: Compare Revision date to file mtime.
        return

    f = codecs.open(os.path.join(root, filename), mode="r", encoding="utf8")

    if category_name == "news":
        # Newsfiles contain the subject as first line
        subject = f.readline()[:-1]
    else:
        subject = "Initial commit for %s in %s" % (category, language)

    data = f.read()
    content_data = data
    while data:
        data = f.read()
        content_data += data
    content_data = content_data.strip()

    revision = Revision(content=content, subject=subject, data=content_data, data_markup_type=type)
    revision.save()
    print "Prepared revision %s" % revision

# FIXME. This loop does not handle ordering of newsitems within
# the same day.
for root, dirs, files in os.walk(content_path):
    if not files:
        continue

    (head,tail) = os.path.split(root)
    language = tail

    for filename in files:
        (category_name, type) = os.path.splitext(filename)
        type = type[1:]

        if type not in allowed_markups:
            print "Ignoring file %s" % filename
            continue

        process_file(root, filename, category_name, type)

@@ -2475,16 +2475,288 @@
 ()%5B:-1%5D%0A
+        # Summary is defined as being all the text after subject until%0A        # marker %22%5Bfull%5D%22 alone at the beginning of line with LF at end%0A        summary = %22%22%0A        for line in f:%0A            if line == %22%5Bfull%5D%5Cn%22:%0A                break%0A            summary += line%0A
     else
@@ -2749,32 +2749,32 @@
  line%0A    else:%0A
-
         subject 
@@ -2827,16 +2827,37 @@
 anguage)
+%0A        summary = %22%22
 %0A%0A    da
@@ -3004,16 +3004,16 @@
 trip()%0A%0A
-
     revi
@@ -3061,16 +3061,59 @@
 subject,
+ summary=summary, summary_markup_type=type,
  data=co

Add Newsitem summary support to initial population script