@@ -19,32 +19,41 @@ def handle(self, *args, **options):
1919 filename = options ['filename' ][0 ]
2020 with open (filename ) as csvfile :
2121 reader = csv .DictReader (csvfile )
22- for row in reader :
22+ for i , row in enumerate (reader ):
23+ # Without positions we can't use the row:
24+ if not row ['LONGITUDE' ] or not row ['LATITUDE' ]:
25+ continue
26+ if len (row ['POSTCODE' ]) > 10 :
27+ row ['POSTCODE' ] = '' # better then inserting wrong textual data
2328 unit = LocalUnit ()
2429 unit .country = Country .objects .get (iso3 = row ['ISO3' ])
25- unit .type , created = LocalUnitType .objects .get_or_create (
30+ if row ['TYPECODE' ] == 'NS0' : row ['TYPECODE' ] = 1
31+ elif row ['TYPECODE' ] == 'NS1' : row ['TYPECODE' ] = 2
32+ elif row ['TYPECODE' ] == 'NS2' : row ['TYPECODE' ] = 3
33+ elif row ['TYPECODE' ] == 'NS3' : row ['TYPECODE' ] = 4
34+ else : row ['TYPECODE' ] = int (row ['TYPECODE' ])
35+ unit .type , created = LocalUnitType .objects .all ().get_or_create (
2636 level = row ['TYPECODE' ],
27- name = row ['TYPENAME' ]
37+ # name=row['TYPENAME'] -- we should create it in advance, not this way.
2838 )
2939 if created :
3040 print (f'New LocalUnitType created: { unit .type .name } ' )
3141
3242 unit .local_branch_name = row ['NAME_LOC' ]
3343 unit .english_branch_name = row ['NAME_EN' ]
34- # TODO: why is it here? No such column in this table: unit.branch_level = int(row['TYPECODE'])
35- unit .postcode = int (row ['POSTCODE' ]) if row ['POSTCODE' ] else None
44+ unit .postcode = row ['POSTCODE' ].strip ()[:10 ]
3645 unit .address_loc = row ['ADDRESS_LOC' ]
3746 unit .address_en = row ['ADDRESS_EN' ]
3847 unit .city_loc = row ['CITY_LOC' ]
3948 unit .city_en = row ['CITY_EN' ]
4049 unit .focal_person_loc = row ['FOCAL_PERSON_LOC' ]
4150 unit .focal_person_en = row ['FOCAL_PERSON_EN' ]
42- unit .phone = row ['TELEPHONE' ]
51+ unit .phone = row ['TELEPHONE' ]. strip ()[: 30 ]
4352 unit .email = row ['EMAIL' ]
4453 unit .link = row ['WEBSITE' ]
4554 unit .source_en = row ['SOURCE_EN' ]
4655 unit .source_loc = row ['SOURCE_LOC' ]
4756 unit .location = Point (float (row ['LONGITUDE' ]), float (row ['LATITUDE' ]))
4857 unit .save ()
4958 name = unit .local_branch_name if unit .local_branch_name else unit .english_branch_name
50- print (f'{ name } saved' )
59+ print (f'{ i } | { name } saved' )
0 commit comments