export_as_csv.py 987 Bytes
# -*- coding:utf-8 -*-

import codecs
import datetime
import os

from django.core.management.base import BaseCommand

from settings import PROJECT_PATH
from webapp.models import Meaning

SOURCE = 'szarada'

CSV_PATH = os.path.join(PROJECT_PATH, 'data', '%s_%s.csv' % (SOURCE,
                                                             datetime.datetime.now().strftime('%Y%m%d')))

class Command(BaseCommand):
    help = 'Get database as csv.'

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


def export_csv():
    try:
        csv_file = codecs.open(CSV_PATH, 'wt', 'utf-8')
        meanings = Meaning.objects.filter(expressions__link__source__key=SOURCE).distinct()
        for meaning in meanings.order_by('id'):
            print meaning.id
            csv_file.write(u'%d\t%s\n' % (meaning.id,
                                          u'\t'.join([expr.orth_text for expr in meaning.expressions.filter(main_expression=None)])))
    finally:
        csv_file.close()