def update_address(): """ Update the user's address""" session = connect() try: user = load_user(current_user.id) address = get_address(user.address_id) except AttributeError: return 'Error getting user data' if address is None: address = Address() if request.method == 'POST': if request.form['street_1']: address.street_1 = request.form['street_1'] if request.form['street_2']: address.street_2 = request.form['street_2'] if request.form['city']: address.city = request.form['city'] if request.form['state']: address.state = request.form['state'] if request.form['zip_code']: address.zip_code = request.form['zip_code'] address_string = get_address_string(address) if validate_address(address_string) is False: flash("Address is invalid or outside delivery radius!") return redirect(url_for('cart_edit_address')) address = session.add(address) user.address_id = get_address_id(address) user = session.merge(user) flash("Address saved!") session.commit() return redirect(url_for('show_cart'))
def __get_address(self, cep): page = pq(url=self.CORREIOS_URL % cep) fields = page.find(".resposta") err = page.find(".erro").text() result = SearchAddrResult() if not err is None: result.message = err result.status = SearchAddrStatus.NOT_FOUND return result city, state = self.__get_field(fields, "Localidade / UF").split('/') address = Address() address.street = self.__get_field(fields, "Logradouro") address.district = self.__get_field(fields, "Bairro") address.city = city.strip('\n').strip() address.state = state.strip('\n').strip() address.cep = cep result.address = address result.status = SearchAddrStatus.FOUND return result
def post(self): self.response.headers['Content-Type'] = 'text/html' address = None if not getAddressObject( self.request.get("address1").lower(), self.request.get("address2").lower(), self.request.get("city").lower(), self.request.get("state").lower(), self.request.get("zip")): address = Address() address.address1 = self.request.get("address1").lower() address.address2 = self.request.get("address2").lower() address.city = self.request.get("city").lower() address.state = self.request.get("state").lower() if self.request.get("zip") != "": address.zip = int(self.request.get("zip")) address.put() else: pass location_add_template = jinja_env.get_template( "templates/addlocation.html") self.response.out.write(location_add_template.render())
def _parse_resource(self, resource): address = resource['address'] log.debug('Matched address is %s', address) parsed_address=Address() street=address.get('road') or address.get('pedestrian') or address.get('village') or address.get('hamlet') if address.get('house_number') and street: street+=' '+address['house_number'] parsed_address.street=street parsed_address.city=address.get('city') or address.get('town') parsed_address.country=address.get('country') parsed_address.county=address.get('county') parsed_address.state=address.get('state') parsed_address.postal_code=address.get('postcode') latitude = resource['lat'] or None longitude = resource['lon'] or None if latitude and longitude: latitude = float(latitude) longitude = float(longitude) return (parsed_address, (latitude, longitude))