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 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