def seed_cities(): """Add all cities in Biz table to Cities table""" # should be 95 cities # select city, state from biz group by state, city # group by state, city all_cities = db.session.query(PlatePalBiz.city, PlatePalBiz.state).filter( PlatePalBiz.city != u"blacksburg", PlatePalBiz.city != u'Carrboro Saxapahaw Chapel Hill Durham', PlatePalBiz.city != u'Greenbelt ') cities = all_cities.group_by(PlatePalBiz.state).group_by( PlatePalBiz.city).all() # calculate lat/lng for each city geolocator = Nominatim() for city in cities: location = geolocator.geocode(city[0] + " " + city[1]) print city print "Lat: {}, Lng: {}".format(location.latitude, location.longitude) new_city = City(city=city[0], state=city[1], lat=location.latitude, lng=location.longitude) db.session.add(new_city) db.session.commit() return
def add_city(): if request.method == 'POST': payload = request.json print(payload) if len(payload['name']) != 0: check_data = City.query.filter_by(name=payload['name'].lower().strip()) if check_data.first(): return jsonify({'message': 'Data - '+check_data.first().name+' already exists.'}) else: try: if 'state' not in payload.keys(): payload['state'] = "" if 'country' not in payload.keys(): payload['country'] = "" new_data = City( payload['name'].lower().strip() , payload['state'].lower().strip() , payload['country'].lower().strip()) db.session.add(new_data) db.session.commit() json_data = { 'id' : new_data.id , 'name' : new_data.name} return jsonify({'success': 'Data Added', 'data' : json_data}) except Exception as e: print(str(e)) db.session.rollback() db.session.close() return jsonify({'message': 'Something unexpected happened. Check logs', 'log': str(e)}) else: return jsonify({'message': 'Empty Data.'}) else: return jsonify({'message': 'Invalid HTTP method . Use POST instead.'})
def load_cities(): """Load users from u.user into database.""" print "City" for row in open("seed_data/u.flight_data"): row = row.rstrip() print row.split("|") city_id, origin_airport, destination_airport, destination, departure_date, return_date, highest_predicted_fares, currency_code, lowest_predicted_fares, recommendation, lowest_fare, destination_info = row.split( "|") city = City( city_id=city_id, origin_airport=origin_airport, destination_airport=destination_airport, destination=destination, departure_date=departure_date, return_date=return_date, lowest_predicted_fares=lowest_predicted_fares, lowest_fare=lowest_fare, recommendation=recommendation, destination_info=destination_info, ) # We need to add to the session or it won't ever be stored db.session.add(city) # provide some sense of progress # Once we're done, we should commit our work db.session.commit()
def setUp(self): """Stuff to do before every test.""" # Get the Flask test client self.client = app.test_client() app.config['TESTING'] = True app.config['SECRET_KEY'] = os.environ['SECRET_KEY'] # Logs in test user with self.client as c: with c.session_transaction() as sess: sess['user_id'] = 1 # Connect to test database connect_to_db(app, "postgresql:///testdb") # Create tables and add sample data db.create_all() user = User(username='******', password='******') city = City(name='San Francisco') destination = Destination( city=city, name='Mission Dolores Park', address='Dolores St &, 19th St, San Francisco, CA 94114') db.session.add_all([user, city]) db.session.commit()
def add_city(self): from model import City city = City() city.id = 2 city.cityname = "杭州" city.cityid = "2" city.provinceid = "1" self.session.add(city) self.session.commit()
def create_city(city, state, forecast_office_id): """ Create and return a new city""" city = City(city=city, state=state, forecast_office_id=forecast_office_id) db.session.add(city) db.session.commit() return city
def add_city(): name = request.form.get('name') try: cty = City(name=name) db.session.add(cty) db.session.commit() except Exception: flash('') return redirect(url_for('city')) return redirect(url_for('city'))
def getCity(city, province): if not city: return None c = City.nodes.get_or_none(name=city) if not c: c = City(name=city).save() if province: c.province.connect(province) c.save() return c
def add_city(city_name, country_code, teleport_id, urban_area=None, latlon=None): """Basic function to add a city to db.""" city = City(city_name=city_name, country_code=country_code, teleport_id=teleport_id, urban_area=urban_area, latlon=latlon) print(f">> before trying to init this city: {city}") db.session.add(city) db.session.commit() print(f">> after db commit: {city}") return city
def create_city(city_name, country, iso2, lat, lon, pop): """Create and return a city with their lat & lon""" city = City(city_name=city_name, country=country, iso2=iso2, lat=lat, lon=lon, pop=pop) db.session.add(city) db.session.commit() return city
def add_city(): body = request.json city = body.get('city') country_id = body.get('country_id') try: city = City(city, country_id) city_schema = CitySchema() db.session.add(city) db.session.commit() return jsonify(city_schema.dump(city)) except Exception as e: return jsonify(eror=str(e))
def create_city(city_name, country_name, geo_lat, geo_lng): city = City(city_name=city_name, country_name=country_name, geo_lat=geo_lat, geo_lng=geo_lng) print(city) db.session.add(city) db.session.commit() return city
def load_cities(): """Load all the available cities users can choose from.""" for row in open('seed_data/u.cities'): row = row.rstrip() city_id, name = row.split('|') city = City(city_id=city_id, name=name) db.session.add(city) db.session.commit()
def get_city_id(city): """Get the city id from database. Otherwise, add city to database and get the city id.""" try: existing_city = db.session.query(City).filter(City.name == city).one() except NoResultFound: new_city = City(name=city) db.session.add(new_city) db.session.commit() return new_city.city_id return existing_city.city_id
def load_cities(file_user): # City.query.delete() for row in (open(file_user)): row = row.strip() city, county = row.split(",") new_city = City(city=city, county=county) db.session.add(new_city) db.session.commit()
def load_cities(): """ load cities available """ for row in open("data/cities.csv"): row = row.rstrip() name = row.split(",")[1] city = City(name=name) try: tmp_res = db.session.query(City).filter(City.name == name).one() except NoResultFound: db.session.add(city) db.session.commit()
def create_city(city_name, zipcode): """function that saves city information of the race from API call to db""" exists = City.query.filter_by(city_name=city_name).first() city = City(city_name=city_name, zipcode=zipcode) if not exists: #add city to db db.session.add(city) db.session.commit() return city else: return exists
def load_cities(): print "Cities" url = "http://api.travelpayouts.com/data/cities.json?token={}".format( travel_payouts_api) r = requests.get(url) data = r.text results = json.loads(data) for key in results: city_name = key['name'].encode('utf-8') city_code = key['code'].encode('utf-8') country_code = key['country_code'].encode('utf-8') country = Country.query.filter(Country.code == country_code).first() if country: country_id = country.id city = City(name=city_name, code=city_code, country_id=country_id) else: city = City(name=city_name, code=city_code) db.session.add(city) db.session.commit()
def write_citiesdb(city_dict, countries): """ Write the cities from dictionary in read_city_file to database write_citiesdb({'Sao Paolo': [Brazil', '-23.56288', '-46.654659']}) 'Sao Paolo': [Brazil', '-23.56288', '-46.654659'] """ # instantiate objects of each city with values from dictionary for city in city_dict: city_table = City(city_name=city, country_code=countries[city_dict[city][0]], city_lat=city_dict[city][1], city_long=city_dict[city][2]) db.session.add(city_table) db.session.commit()
def make_test_cities(): """Create some cities for testing.""" cities = { 'San Francisco': {'ccode': 'USA', 'tid': 5391959, 'urban_area': 'Bay Area', 'latlon': (37.77493, -122.41942)}, 'London': {'ccode':'GBR', 'tid': 2643743, 'latlon': (51.50853, -0.12574)}, 'Paris': {'ccode': 'FRA', 'tid': 2988507, 'latlon': (48.85341, 2.3488)}, 'Taipei': {'ccode': 'TWN', 'tid': 1668341, 'latlon': (25.04776, 121.53185)} } for c, cinfo in cities.items(): city = City(city_name=c, urban_area=cinfo.get('urban_area'), teleport_id=cinfo.get('tid'), country_code=cinfo['ccode'], latlon=cinfo.get('latlon')) db.session.add(city) db.session.commit() print("*** Success: Added some cities ***")
def load_cities(): """Load cities into database.""" cities = [] for row in (open('cities.txt')): city = row.rstrip() cities.append(city) ordered_cities = sorted(cities) for city in ordered_cities: find_city = City.query.filter_by(name=city).first() if not find_city: city = City(name=city) db.session.add(city) db.session.commit() print "Cities loaded."
def create_missing_country_city(): to_save = [] cnt_added = 0 city_add = 0 with open('../../data/json/finale.json') as ffop: data = json.load(ffop) for line in data: if 'continent_code' in line.keys(): if not sess.query(Country).filter_by( country_code=line['country_code']).all(): continent = sess.query(Continent).filter_by( continent_code=line['continent_code']).all()[0] continent_id = continent.id country = Country(name=line['country_full'], country_code=line['country_code'], continent_id=continent_id) sess.add(country) sess.commit() print('Added a country:', line['country_full']) cnt_added += 1 print(cnt_added) else: country = sess.query(Country).filter_by( country_code=line['country_code']).all()[0] city = City(name=line['city_name'], city_id=line['city_id'], country_id=country.id, lat=line['lat'], lon=line['lon']) city_add += 1 to_save.append(city) print('Added a city:', line['city_name']) print(city_add) sess.bulk_save_objects(to_save) sess.commit()
def load_cities(cities_filename): """Load cities from file into database.""" print("Cities") for i, row in enumerate(open(cities_filename)): row = row.rstrip() name, lat, lon, country_code, population, timezone = row.split("|") lat = float(lat) lon = float(lon) city = City(name=name, lat=lat, lon=lon, country_code=country_code, timezone=timezone) # add user to the session db.session.add(city) # commit session db.session.commit()
def install(): Base.metadata.create_all(Session().bind) data = [('Chicago', 'United States', ('60601', '60602', '60603', '60604')), ('Montreal', 'Canada', ('H2S 3K9', 'H2B 1V4', 'H7G 2T8')), ('Edmonton', 'Canada', ('T5J 1R9', 'T5J 1Z4', 'T5H 1P6')), ('New York', 'United States', ('10001', '10002', '10003', '10004', '10005', '10006')), ('San Francisco', 'United States', ('94102', '94103', '94104', '94105', '94107', '94108'))] countries = {} all_post_codes = [] for city, country, postcodes in data: try: country = countries[country] except KeyError: countries[country] = country = Country(country) city = City(city, country) pc = [PostalCode(code, city) for code in postcodes] Session.add_all(pc) all_post_codes.extend(pc) for i in xrange(1, 51): person = Person( "person %.2d" % i, Address(street="street %.2d" % i, postal_code=all_post_codes[random.randint( 0, len(all_post_codes) - 1)])) Session.add(person) Session.commit() # start the demo fresh Session.remove()
from session import session, Base, engine from model import City from ppa_data import ppa_data_locations Base.metadata.create_all(engine) for city, info in ppa_data_locations().items(): current = session.query(City).filter_by(name=city).first() if current: current.lon = info['lon'] current.lat = info['lat'] else: new_city = City(name=city,lon=info['lon'],lat=info['lat']) session.add(new_city) session.commit()
def upload_contact(): file = request.files['file'] tags = json.loads(request.form['data']) try: if file: filename = file.filename foldertemp = os.path.join(UPLOAD_FOLDER, 'contacts') if os.path.exists(foldertemp): filetemp = os.path.join(foldertemp, filename) file.save(filetemp) else: os.makedirs(foldertemp) filetemp = os.path.join(foldertemp, filename) file.save(filetemp) with open(filetemp, mode="r") as csv_file: csv_data = list(csv.reader(csv_file, delimiter=",")) for item in csv_data: name = item[0] + '-' + item[1] contact_one = str(item[6]) contact_two = str(item[7]) address = str(item[2]) city = item[3].lower() state = item[4] country = item[5] email = item[8] tag = tags city = city.split('-')[0].split('(')[0] if len(contact_one) == 10: contact_one = '91' + contact_one if city == "": if state == "": city = country else: city = state # Checks for empty values if item[0] is not "": try: city_obj = City.query.filter_by( name=str(city)).first() if city_obj: pass else: city_obj = City(str(city), str(state), str(country)) db.session.add(city_obj) con_obj = Contact(name, contact_one, contact_two, address, email, city_obj) db.session.add(con_obj) for item in tag: tag_obj = Tag.query.filter_by( name=item['name']).first() if tag_obj: pass else: pass tag_obj = Tag(str(item['name'])) db.session.add(tag_obj) con_obj.tag_contact.append(tag_obj) db.session.commit() except IntegrityError as e: db.session.rollback() print('Duplicate ----- ' + name, city) pass except Exception as e: db.session.rollback() print('Oh No ---------', name, city) pass else: pass return jsonify({'success': 'Data Added'}) except Exception as e: # return Exception for duplicate data print(str(e)) return jsonify({'message': 'Returns error'})
def seed_city(): """Seed cities table.""" # This function will demonstrate how to seed city table. paris = City(city_name="Paris, France") london = City(city_name="London, United Kingdom") barcelona = City(city_name="Barcelona, Spain") istanbul = City(city_name="Istanbul, Turkey") madrid = City(city_name="Madrid, Spain") nice = City(city_name="Nice, France") berlin = City(city_name="Berlin, Germany") hongkong = City(city_name="Hong Kong, Hong Kong") singapore = City(city_name="Singapore, Singapore") sanfrancisco = City(city_name="San Francisco, United States") newyork = City(city_name="New York, United States") losangeles = City(city_name="Los Angeles, United States") chicago = City(city_name="Chicago, United States") db.session.add_all([ paris, london, barcelona, istanbul, madrid, nice, berlin, hongkong, singapore, sanfrancisco, newyork, losangeles, chicago ]) db.session.commit()
def test_calculate_tiles(self): c1 = City("London", x=5, y=1, x2=5, y2=1, city_type="normal") c2 = City("Berlin", 3, 9, 4, 10, "mammoth market") self.assertListEqual(c1.tiles, [(5, 1)]) self.assertListEqual(c2.tiles, [(3, 9), (3, 10), (4, 9), (4, 10)])
if city == "": if state == "": city = country else: city = state # Checks for empty values try: city_obj = City.query.filter_by(name=str(city)).first() if city_obj: pass else: city_obj = City(str(city), str(state), str(country)) db.session.add(city_obj) con_obj = Contact(name, contact, "", "", "", city_obj) db.session.add(con_obj) for item in tag: tag_obj = Tag.query.filter_by(name=item).first() if tag_obj: pass else: tag_obj = Tag(str(item)) db.session.add(tag_obj) con_obj.tag_contact.append(tag_obj)
def add_rec_to_db(): """recordes info submitted by user, creates the objects for the db, and adds to the db""" #Update session on login - get the username in the session username = session['username'] #then get that user's user object, user_obj = User.query.filter_by(username=username).first() #then get the user id to pass into the rec table user_id = user_obj.user_id #get the city_name & info separately since we need city_name for a query city_name = request.form.get('city_name') city_name = city_name.lower() city_info = request.form.get('city_info') #create the city obj if it doesn't exist other wise get the city_id for recs instantiation. city_obj = City.query.filter_by(city_name=city_name).first() if city_obj == None: city = City(city_name=city_name, city_info=city_info) db.session.add(city) db.session.commit() city_id = city.city_id else: city_id = city_obj.city_id #get the path for four images, save it in the directory and then save the path before adding to the db in url variable file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) app.logger.info(os.path.join(app.config['UPLOAD_FOLDER'])) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) img_url = f'static/images/{filename}' file = request.files['file2'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) app.logger.info(os.path.join(app.config['UPLOAD_FOLDER'])) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) img_url2 = f'static/images/{filename}' file = request.files['file3'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) app.logger.info(os.path.join(app.config['UPLOAD_FOLDER'])) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) img_url3 = f'static/images/{filename}' file = request.files['file4'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) app.logger.info(os.path.join(app.config['UPLOAD_FOLDER'])) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) img_url4 = f'static/images/{filename}' #add to the database recommendation = Recommendation(rec_name = request.form.get('rec_name'), stay_name= request.form.get('stay_name'), stay_info = request.form.get('stay_info'), eat_name= request.form.get('eat_name'), eat_info = request.form.get('eat_info'), do_name= request.form.get('do_name'), do_info = request.form.get('do_info'), user_id=user_id, city_id=city_id, img_url=img_url, img_url2=img_url2, img_url3=img_url3, img_url4=img_url4) #call the save function to add to the database db.session.add(recommendation) db.session.commit() flash("Awesome! You've created a recommendation") return redirect(f'/view_recommendation/{recommendation.rec_id}')