def get_top(self): rf = Division.objects.filter(type="A", name=self.TOP_NAME).first() if rf: return rf self.info("Creating %s" % self.TOP_NAME) rf = Division(type="A", name=self.TOP_NAME) rf.save() return rf
def handle(self, *args, **options): ctr = options.get("countries", []) print(ctr) print(options) # Check countries for c in ctr: if c not in self.HEADERS or c not in self.DATA: raise CommandError("Unsupported country: %s" % c) # header = ["LEVEL%d" % d for d in range(self.LEVELS)] header += [ "STREET", "HOUSE_ADDR", "NUM", "NUM2", "NUM_LETTER", "BUILD", "BUILD_LETTER", "STRUCT", "STRUCT2", "STRUCT_LETTER", "POSTAL_CODE", ] for c in ctr: header += self.HEADERS[c] writer = csv.writer(sys.stdout) writer.writerow(header) for d in Division.get_top(): self.dump_division(writer, d, ctr, [])
def create_division(self, oktmo): """ Create division by OKATO object :returns: Division """ if oktmo in self.div_cache: return self.div_cache[oktmo] o = self.oktmo[oktmo] d = Division.objects.filter(data__OKTMO=oktmo).first() if not d: if o.parent: p = self.oktmo[o.parent].oktmo parent = self.create_division(p) else: parent = self.get_top() ao = self.get_addrobj(oktmo=oktmo) self.info("Creating %s" % o.name) data = { "OKTMO": oktmo } if ao: name = ao["offname"] short_name = ao["shortname"] if ao.get("okato", "").strip(): data["OKATO"] = ao["okato"] if ao.get("kladr", "").strip(): data["KLADR"] = ao["kladr"] data["FIAS_AOID"] = ao["aoid"] data["FIAS_AOGUID"] = ao["aoguid"] data["FIAS_CODE"] = ao["code"] else: name = o.name short_name = None if not short_name: short_name, name = normalize_division(name) d = Division( name=name, short_name=short_name, parent=parent, data=data ) d.save() self.div_cache[oktmo] = d return d
def create_division2(self, ao): """ Create division by ADDROBJ, used when OKATO is empty :returns: Division """ if ao["aoid"] in self.aoid_cache: return self.aoid_cache[ao["aoid"]] d = Division.objects.filter(data__FIAS_AOID=ao["aoid"]).first() if not d: if ao["parentguid"]: po = self.get_addrobj(aoguid=ao["parentguid"]) if po["oktmo"]: parent = self.create_division(po["oktmo"]) else: parent = self.create_division2(po) else: parent = self.get_top() self.info("Creating %s" % ao["offname"]) data = {} name = ao["offname"] short_name = ao["shortname"] if ao.get("okato", "").strip(): data["OKATO"] = ao["okato"] if ao.get("oktmo", "").strip(): data["OKTMO"] = ao["oktmo"] if ao.get("kladr", "").strip(): data["KLADR"] = ao["kladr"] data["FIAS_AOID"] = ao["aoid"] data["FIAS_AOGUID"] = ao["aoguid"] data["FIAS_CODE"] = ao["code"] if not short_name: short_name, name = normalize_division(name) d = Division( name=name, short_name=short_name, parent=parent, data=data ) d.save() self.aoid_cache[ao["aoid"]] = d return d
def update_levels(self): self.info("Updating levels") Division.update_levels()