import_odm.py 959 Bytes
# -*- coding: utf-8 -*-

from django.db import connection
from django.core.management.base import BaseCommand
from django.db.transaction import atomic

from common.util import uniopen


class Command(BaseCommand):
    args = '<nazwa pliku wejściowego>'
    help = 'Imports forms from odm.txt used for import'

    def handle(self, input_file, **options):
        import_odm(input_file)


@atomic
def import_odm(filename):
    cursor = connection.cursor()

    for line in uniopen(filename):
        forms = line.split(', ')
        cursor.execute("INSERT INTO dictionary_inputlexeme (entry) "
                       "VALUES (%s)", [forms[0]])
        cursor.execute(
            "select currval('dictionary_inputlexeme_id_seq'::regclass)")
        il_id = cursor.fetchone()[0]
        for form in forms:
            cursor.execute(
                "INSERT INTO dictionary_inputform (form, input_lexeme_id) "
                "VALUES (%s, %s)", [form, il_id])