def test_localization_handling(): address = normalize_address({ 'country_code': 'us', 'country_area': 'California', 'postal_code': '94037', 'city': 'Mountain View', 'street_address': '1600 Charleston Rd.'}) assert address['country_code'] == 'US' assert address['country_area'] == 'CA' address = normalize_address({ 'country_code': 'us', 'country_area': 'CALIFORNIA', 'postal_code': '94037', 'city': 'Mountain View', 'street_address': '1600 Charleston Rd.'}) assert address['country_area'] == 'CA' address = normalize_address({ 'country_code': 'CN', 'country_area': 'Beijing Shi', 'postal_code': '100084', 'city': 'Haidian Qu', 'street_address': '#1 Zhongguancun East Road'}) assert address['country_area'] == '北京市' assert address['city'] == '海淀区' address = normalize_address({ 'country_code': 'AE', 'country_area': 'Dubai', 'postal_code': '123456', 'sorting_code': '654321', 'street_address': 'P.O Box 1234'}) assert address['country_area'] == 'إمارة دبيّ' assert address['city'] == '' assert address['postal_code'] == '' assert address['sorting_code'] == ''
def test_capitalization(): address = normalize_address({ 'country_code': 'GB', 'postal_code': 'sw1a 0aa', 'city': 'London', 'street_address': 'Westminster'}) assert address['city'] == 'LONDON' assert address['postal_code'] == 'SW1A 0AA'
def validate_address(self, data): try: data['country_code'] = data.get('country', '') if data['street_address_1'] or data['street_address_2']: data['street_address'] = '%s\n%s' % (data['street_address_1'], data['street_address_2']) data = i18naddress.normalize_address(data) del data['sorting_code'] except i18naddress.InvalidAddress as exc: self.add_field_errors(exc.errors) return data
def validate_address(self, data): try: data['country_code'] = data.get('country', '') if data['street_address_1'] or data['street_address_2']: data['street_address'] = '%s\n%s' % ( data['street_address_1'], data['street_address_2']) data = i18naddress.normalize_address(data) del data['sorting_code'] except i18naddress.InvalidAddress as exc: self.add_field_errors(exc.errors) return data
def validate_address(self, data): try: data["country_code"] = data.get("country", "") if data["street_address_1"] or data["street_address_2"]: data["street_address"] = "%s\n%s" % ( data["street_address_1"], data["street_address_2"], ) data = i18naddress.normalize_address(data) del data["sorting_code"] except i18naddress.InvalidAddress as exc: self.add_field_errors(exc.errors) return data
def greetings(): print("") param = dict() adress = dict() print( "####HELLO################git:$@/RentAnalyser#################################################################" ) print( "###########################################################################################@azseza###########" ) print( "##########################################################################################@islemoueslati#####" ) print( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Rent Balence Sheet Generator~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" ) print( "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" ) print("") print(" >>>Press Enter after each input") try: adress['country_code'] = 'US' adress['city'] = input( "Enter the Property Adress (e.g XXXX Somthing Av , City , State , ZIP CODE ),First The city : " ) adress['country_area'] = input("Enter the country area : ") adress['postal_code'] = input("Enter The postal Code : ") adress['street_address'] = input("Enter The Street Adress : ") adress = normalize_address(adress) param['propVal'] = int(input("Enter Property Value (no float) : ")) param['rentEst'] = int(input("Enter Rent Estimation : ")) param['taxRate'] = float( input("Enter The tax Rate (default 1.9%) : ")) param['annTaxRate'] = float( input("Enter Teh estimated anual Taxation(default .. ) : ")) except ValueError: print("Wrong input Values, Please relunch the program. ") except InvalidAddress: print("Wrong adress form, Please relunch the program. ") print(InvalidAddress.errors) return param
def test_validate_areas_errors(address, errors): with pytest.raises(InvalidAddress) as excinfo: normalize_address(address) assert excinfo.value.errors == errors
def test_validate_known_addresses(address): assert normalize_address(address)
def get_normalized_address_info(writer, x, latin=False): author = x.getparent().getparent() name = full_author_ascii_name(author) if latin else full_author_name( author) role = author.get('role') company = full_org_ascii_name(author) if latin else full_org_name(author) country_info = None country_element = x.find('country') if country_element != None and country_element.text: country_info = get_iso_country_info(country_element) if not country_info: for c in x.getchildren(): if c.tag == "postalLine": country_info = get_iso_country_info(c) if country_info: country_element = c break if country_info: country_name = get_value(country_element, latin=latin) if country_name in ['US', 'USA']: country_name = country_info.official_name elif re.match('[A-Z]{2,3}', country_name): country_name = country_info.name adr = { 'name': name, 'role': role, 'company_name': company, 'extended_address': [], 'street_address': [], 'sorting_code': '', 'postal_code': '', 'city_area': '', 'city': '', 'country_area': '', 'country_code': country_info.alpha_2, 'country_name': country_name, } else: adr = { 'name': name, 'role': role, 'company_name': company, 'extended_address': [], 'street_address': [], 'sorting_code': '', 'postal_code': '', 'city_area': '', 'city': '', 'country_area': '', 'country_code': '', 'country_name': '' } for c in x.getchildren(): if c == country_element: continue # Some of these will overwrite data if there are multiple elements value = get_value(c, latin=latin) if value: if c.tag == 'extaddr': adr['extended_address'].append(value) elif c.tag in ['street', 'postalLine', 'pobox']: adr['street_address'].append(value) elif c.tag == 'cityarea': adr['city_area'] = value elif c.tag == 'city': adr['city'] = value elif c.tag == 'region': adr['country_area'] = value elif c.tag == 'code': adr['postal_code'] = value elif c.tag == 'sortingcode': adr['sorting_code'] = value for a in adr: if isinstance(adr[a], list): adr[a] = '\u2028'.join(adr[a]) if country_info: # Address validation address_format, rules = get_address_format_rules(adr, latin) parts = [ address_field_mapping[c] for c in re.findall(r'%([A-Z])', address_format) if not c in ['N', 'O'] ] elements = [address_field_elements[p] for p in parts] list_parts = False for e in adr: if adr[e] and not (e in parts or e in [ 'name', 'role', 'company_name', 'country_code', ]): list_parts = True writer.warn( x, "Postal address part filled in, but not used: %s: %s\n" " Available elements for %s are: %s" % (address_field_elements[e], adr[e], country_info.name, ', '.join(elements))) try: i18naddress.normalize_address(adr) except i18naddress.InvalidAddress as e: list_parts = True writer.note( x, "Postal address check failed for author %s." % full_author_name(author, latin)) for item in e.errors: if adr[item]: writer.note( x, " Postal address has an unexpected address field or field value: %s: %s" % (address_field_elements[item], (adr[item])), label='') else: writer.note( x, " Postal address is missing an address element: %s" % (address_field_elements[item], ), label='') if list_parts: writer.note( x, "Recognized postal address elements for %s are: %s" % (rules.country_name.title(), ', '.join(elements))) return adr
def doctorRequest(request): validationStatus = "" if request.method == 'POST': if 'returnHome' in request.POST.keys(): return HttpResponseRedirect("/") print(vars(request.POST)) validated = True try: address = normalize_address({ 'country_code': request.POST['country'], 'country_area': request.POST['state'], 'city': request.POST['city'], 'postal_code': request.POST['zipCode'], 'street_address': request.POST['address'] }) except InvalidAddress as e: print("failed") print(e.errors) validated = False validationStatus += "Sorry, Address Validation failed. Please enter a valid address for delivery.\n" if (len(request.POST['fName']) < 1) or (len( request.POST['lName']) < 1) or (len(request.POST['email']) < 1): validated = False validationStatus += "Please ensure all fields are filled out. " if timezone.now().date() > datetime.date(int(request.POST['year']), int(request.POST['month']), int(request.POST['day'])): validated = False validationStatus += "Please set the delivery date later. " if validated: print("Address Validation Succeeded") newRequest = RequestModel( id=RequestModel.objects.latest('orderDate').id + random.randrange(1, 100, 1), status=0, fName=request.POST['fName'], lName=request.POST['lName'], email=request.POST['email'], phone=request.POST['phone'], organization=request.POST['organization'], numPPE=request.POST['numPPE'], typePPE=request.POST['typePPE'], typeHandle=request.POST['typeHandle'], address=request.POST['address'], city=request.POST['city'], state=request.POST['state'], country=request.POST['country'], lat=request.POST['lat'], lng=request.POST['lng'], zipCode=request.POST['zipCode'], delivDate=datetime.date(int(request.POST['year']), int(request.POST['month']), int(request.POST['day'])), orderDate=timezone.now(), notes=request.POST['otherNotes']) print(request.POST['lat'] + ", " + request.POST['lng']) newRequest.save() requestObj = RequestModel.objects.get(id=newRequest.id) service = getService() #Donor Email subject = "Request For PPE Submitted!" ppeType = "" if "shield" in requestObj.typePPE: ppeType = "3D Printed Face Shields" elif "strap" in requestObj.typePPE: ppeType = "Face Mask Comfort Strap" elif "handle" in requestObj.typePPE: ppeType = "Touch-less Door Handle; %s (Link: https://www.materialise.com/en/hands-free-door-opener/technical-information)" % requestObj.typeHandle elif "opener" in requestObj.typePPE: ppeType = "Personal Touchless Door Opener" message_text = "Thank You For Submitting a Request For PPE!\n\nRequest Details: \nRequester's Name: %s %s\nRequester's Email: %s\nRequester's Phone Number: %s\nRequester's Address: %s %s %s %s %s\n\nType of PPE Requested: %s\nAmount of PPE Requested: %s\nideal \"Deliver By\" date of requested PPE: %s\n\nOther Notes From the Requester: %s\n\nYou will be emailed again either when a donor chooses to claim and fulfill your request, or if your request expires before any donors get the chance. We hope you you stay protected during these times!" % ( requestObj.fName, requestObj.lName, requestObj.email, requestObj.phone, requestObj.address, requestObj.city, requestObj.state, requestObj.zipCode, requestObj.country, ppeType, requestObj.numPPE, requestObj.delivDate, requestObj.notes) message = makeMessage("*****@*****.**", requestObj.email, subject, message_text) sendMessage(service, 'me', message) return HttpResponseRedirect("/requestSubmitSuccessful/") template = loader.get_template('main/submitRequest.html') context = { #all inputs for the html go in these brackets 'validationStatus': validationStatus } return HttpResponse(template.render(context, request))