Example #1
0
    def import_mu_data(self, legal_entities, reader, encoding):
        reader.next()
        header = self.parse_header(reader.next(), encoding)

        counter = 2
        created_types = 0
        empty_types = 0
        empty_streets = 0
        unknown_streets = 0
        unknown_addresses = 0
        for row in reader:
            print "Processing row", counter
            counter += 1
            lpu = row[header["GLAVNOE_LPU"]].decode(encoding).strip()
            if len(lpu) == 0:
                lpu = row[header["NAME"]].decode(encoding).strip()
            if len(lpu) == 0:
                stderr.write("Legal entity name is empty at %d" % (counter))
                continue
            lpu = legal_entities[lpu]
            hotype = row[header["TYPE"]].decode(encoding).strip()
            if len(hotype) == 0:
                stderr.write(u"Empty type at %d\n" % (str(counter + 3),))
                empty_types += 1
                continue
            hotype, created = HealthObjectType.objects.get_or_create(name=hotype)
            if created:
                created_types += 1

            street = row[header["ADRES_UL_NAME"]].decode(encoding).strip()
            address = None
            if len(street) == 0:
                stderr.write(u"Empty street at %d\n" % (counter + 3,))
                empty_streets += 1
                unknown_addresses += 1
            else:
                streets = self.get_streets(header, row, street, encoding)
                if len(streets) == 0:
                    stderr.write(u"Unknown street at %d\n" % (counter + 3,))
                    error = u"Улица не найдена: " + street
                    unknown_streets += 1
                    unknown_addresses += 1
                else:
                    address, error = self.get_address(header, row, streets, encoding, counter + 3)
                    if address is None:
                        unknown_addresses += 1

            mu = HealingObject()
            mu.object_type = hotype
            mu.legal_entity = lpu
            #self.fill_chief_data(header, row, mu, counter + 3, encoding)
            mu.address = address
            mu.original_address = row[header["ADRES_STR"]].decode(encoding).strip()
            name = row[header["NAME"]].decode(encoding).strip()
            mu.name = name
            mu.short_name = row[header["SHORT_NAME"]].decode(encoding).strip()
            mu.full_name = row[header["FULL_NAME"]].decode(encoding).strip() or name
            mu.global_id = row[header["GLOBALID"]].strip()
            mu.info = row[header["INFO"]].decode(encoding).strip()
            mu.errors = error
            try:
                mu.clean_fields()
                mu.save()
            except ValidationError as e:
                self.show_validation_error("Error validating healing object", mu, e)
                raise

            service = Service()
            service.healing_object = mu
            service_type_name = object_type_to_service_type[hotype.name]
            service.service = ServiceType.objects.get(name=service_type_name)
            self.fill_chief_data(header, row, service, counter + 3, encoding)
            service.phone = row[header["TEL_NOMER"]].decode(encoding).strip()
            service.fax = row[header["FAX_NOMER"]].decode(encoding).strip()
            service.info = row[header["INFO"]].decode(encoding).strip()
            service.workdays = row[header["DNY_RABOTY1"]].decode(encoding).strip()
            service.workhours = row[header["CHAS_RABOTY"]].decode(encoding).strip()
            service.daysoff = row[header["DNY_NE_RABOT"]].decode(encoding).strip()
            service.daysoff_restrictions = row[header["VYHODNOJ_TYPE"]].decode(encoding).strip()
            service.specialization = row[header["SPECIAL"]].decode(encoding).strip()
            service.paid_services = row[header["PLAT_USLUGI"]].decode(encoding).strip()
            service.free_services = row[header["BESPL_USLUGI"]].decode(encoding).strip()
            service.drug_provisioning = row[header["LEK_OBESP"]].decode(encoding).strip()
            service.departments = row[header["OTDELENIE"]].decode(encoding).strip()
            service.hospital_levels = row[header["LVL"]].decode(encoding).strip()
            service.tour = row[header["SMENA"]].decode(encoding).strip()
            service.receipes_provisioning = row[header["RECEPT"]].decode(encoding).strip()
            service.drugstore_type = row[header["DRUGSTORE_TYPE"]].decode(encoding).strip()
            service.hospital_type = row[header["HOSPITAL_TYPE"]].decode(encoding).strip()
            beds = row[header["KOIKI"]].decode(encoding).strip()
            if len(beds) == 0:
                beds = row[header["KOJKA"]].decode(encoding).strip()
                if len(beds) == 0:
                    beds = row[header["KOIKA"]].decode(encoding).strip()
            service.hospital_beds = beds
            try:
                service.clean_fields()
                service.save()
            except ValidationError as e:
                self.show_validation_error("Error validating service", service, e)
                raise

        print "Total: %d" % (counter,)
        print "Unknown addresses: %d" % (unknown_addresses,)
        print "Empty streets: %d" % (empty_streets,)
        print "Unknown streets: %d" % (unknown_streets,)
        print "Empty types: %d" % (empty_types,)