import_odm.py 1.09 KB
#-*- coding:utf-8 -*-

import sys
from django.db import connection, transaction
from django.core.management.base import BaseCommand, CommandError
from common.util import debug
from dictionary.models import Lexeme

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)


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

  transaction.commit_unless_managed()
  transaction.enter_transaction_management()
  transaction.managed(True)

  for line in open(filename):
    forms = line.strip().decode('utf-8').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])

  transaction.commit()
  transaction.leave_transaction_management()