def __init__(self, address): # Add fields, replacing NULL values with the empty string self.field1 = address.a1.strip() if address.a1 is not None else '' self.field2 = address.a2.strip() if address.a2 is not None else '' self.field3 = address.a3.strip() if address.a3 is not None else '' # Set the actual country object # Uppercase the country code (just in case - you never know with Focus) self.country = FocusCountry.get_by_code(code=address.country_code.upper()) if self.country.code == 'NO': # Norwegians - set the actual zipcode object try: self.zipcode = Zipcode.get_by_zipcode(zipcode=address.zipcode_id) except Zipcode.DoesNotExist: # Some addresses have NULL in the zipcode field for some reason. # Use a zipcode object with empty fields. self.zipcode = Zipcode() # Set the actual County object based on the zipcode if self.zipcode.zipcode != '': county_code = address.zipcode.county_code if county_code == INTERNATIONAL_ADDRESS_COUNTY_CODE: # International address; define the county as None for now. self.county = None else: self.county = County.get_by_code(code=county_code) else: self.county = None else: # Foreigners - ignore zipcode/area # Remove country code prefixes if self.field1.lower().startswith("%s-" % self.country.code.lower()): self.field1 = self.field1[len(self.country.code) + 1:].strip() if self.field2.lower().startswith("%s-" % self.country.code.lower()): self.field2 = self.field2[len(self.country.code) + 1:].strip() if self.field3.lower().startswith("%s-" % self.country.code.lower()): self.field3 = self.field3[len(self.country.code) + 1:].strip()
def get_country(self): return FocusCountry.get_by_code(code=self.country)