1
# -*- coding: utf-8 -*-
2
# vi:si:et:sw=4:sts=4:ts=4
4
from kiwi.environ import environ
6
from nfedomain import NFeCityData
7
from utils import remove_accentuation
9
def apply_patch(trans):
10
trans.query("""CREATE TABLE nfe_city_data (
11
id bigserial NOT NULL PRIMARY KEY,
12
te_created_id bigint UNIQUE REFERENCES transaction_entry(id),
13
te_modified_id bigint UNIQUE REFERENCES transaction_entry(id),
21
csv = environ.find_resource('nfecsv', 'dtb_brazilian_city_codes.csv')
22
for line in open(csv, 'r').readlines():
23
state_code, state_name, city_code, city_name = line.split(',')
24
# the first line contain the titles, lets ignore it.
25
if state_code == '"UF"':
28
# in *_name attributes we remove the extra spaces and the double-quote
30
state_name = unicode(state_name.strip().strip('"'))
31
city_name = unicode(city_name.strip().strip('"'))
32
NFeCityData(state_code=int(state_code.strip('"')),
33
state_name=remove_accentuation(state_name),
34
city_code=int(city_code.strip('"')),
35
city_name=remove_accentuation(city_name),
37
trans.query("""CREATE INDEX nfe_city_name_state_code_idx ON
38
nfe_city_data (city_name, state_code);""")