def test_visible(self, db): d = DestinationFactory() assert not d.hidden assert d in Destination.find_all_visible().all() d.hidden = True db.session.add(d) db.session.commit() assert d not in Destination.find_all_visible().all()
def setUp(self): super().setUp() with self.client: self.loginTestUser() ds1 = DataSource(id=1, label="Test Source", transfer_mechanism_type="curl") ds2 = DataSource(id=2, label="Another Source", transfer_mechanism_type="curl") transform = Transform( from_data_source_id=1, to_data_source_id=2, transform_id=1, preference_order=1, transform_type="change_host", transform_options={"new_host": "example.org"}) dest = Destination(id=1, label="Test Destination") transform.for_destinations.append(dest) self.addToDatabase(ds1, ds2, transform, dest)
def create_dest(self): country_id = self.create_country().id dest = Destination(id=randint(0, 100), name=self.faker.state(), description=self.faker.text(), update_date=self.faker.past_datetime(), country_id=country_id) return dest
def setUp(self): super().setUp() with self.client: self.loginTestUser() origin_source = DataSource(id=1, label="Test Source", transfer_mechanism_type="curl") origin_source.url_matchers.append( UrlMatcher(matcher_id=1, matcher_type="scheme_and_host", matcher_options=dict(scheme="http", host="example.com"))) target_source_1 = DataSource(id=2, label="Target Source 1", transfer_mechanism_type="curl") target_source_1.url_matchers.append( UrlMatcher(matcher_id=1, matcher_type="scheme_and_host", matcher_options=dict(scheme="http", host="example.org"))) target_source_2 = DataSource(id=3, label="Target Source 2", transfer_mechanism_type="curl") target_source_2.url_matchers.append( UrlMatcher(matcher_id=1, matcher_type="scheme_and_host", matcher_options=dict(scheme="http", host="example.net"))) transform_1 = Transform( from_data_source=origin_source, to_data_source=target_source_1, transform_id=1, preference_order=1, transform_type="change_host", transform_options=dict(new_host="example.org")) dest = Destination(id=1, label="Test Destination") transform_2 = Transform( from_data_source=origin_source, to_data_source=target_source_2, for_destinations=[dest], transform_id=2, preference_order=2, transform_type="change_host", transform_options=dict(new_host="example.net")) self.addToDatabase(origin_source, target_source_1, target_source_2, dest, transform_1, transform_2)
def test_list_destinations_json(self): with self.client as client: self.loginTestUser() d1 = Destination(label="Destination 1") d2 = Destination(label="Destination 2") self.addToDatabase(d1, d2) r = client.get("/destinations", follow_redirects=True, headers=dict(accept="application/json")) r = json.loads(r.get_data(as_text=True)) self.assertEqual(len(r["destinations"]), 2) labels = [d["label"] for d in r["destinations"]] self.assertIn("Destination 1", labels) self.assertIn("Destination 2", labels)
def test_list_destinations(self): with self.client as client: self.loginTestUser() d = Destination(label="Test Destination") self.addToDatabase(d) r = client.get("/destinations", follow_redirects=True) self.assertTrue(b"Test Destination" in r.data)
def addDestination(): did = request.json['did'] destType = request.json['destType'] description = request.json['description'] new_destination = Destination(did, destType, description) db.session.add(new_destination) db.session.commit() return jsonify(new_destination)
def test_delete_destination(self): with self.client as client: self.loginTestUser() d = Destination(id=1, label="Test Destination") self.addToDatabase(d) r = client.post("/destinations/1/delete", follow_redirects=True) self.assertTrue(b"Destination deleted" in r.data) self.assertEqual(Destination.query.count(), 0)
def test_prevent_duplicate_destination_label(self): with self.client as client: self.loginTestUser() d = Destination(label="Test Destination") self.addToDatabase(d) r = client.post("/destinations/new", data=dict(label="Test Destination"), follow_redirects=True) self.assertTrue(b"Label already taken" in r.data) self.assertEqual(Destination.query.count(), 1)
def index(): form = DestinationForm() if form.validate_on_submit(): dest = Destination() form.populate_obj(dest) db.session.add(dest) db.session.commit() flash(f'Destination successfully added for {form.city_name.data}') return render_template('index.html', title='Home', form=form)
def new_destination(): """Create a new destination.""" form = DestinationForm() countries = Country.query.order_by(Country.name.asc()).all() tags = [tag.name for tag in Tag.query.all()] form.country_id.choices = [(0, '')] + ([(country.id, country.name) for country in countries]) if request.method == 'POST': if form.validate_on_submit(): dest = Destination(name=form.name.data, country_id=form.country_id.data, description=form.description.data) tags = (form.tags.data).split(',') for tag_name in tags: tag_name = tag_name[1:-1].capitalize() tag = Tag.query.filter_by(name=tag_name).first() dest.add_tag(tag) db.session.add(dest) db.session.commit() dest_img = Dest_Image(dest_id=dest.id, img_url=form.img_url.data) dest_location = Dest_Location(dest_id=dest.id, lat=form.lat.data, lng=form.lng.data) db.session.add_all([dest_img, dest_location]) db.session.commit() flash('Destination successfully created.', 'success') return redirect(url_for('admin.index')) else: flash("Please fill in all forms correctly.", "danger") return render_template('admin/new_destination.html', form=form, tags=tags)
def test_edit_destination(self): with self.client as client: self.loginTestUser() d = Destination(id=1, label="Test Destination") self.addToDatabase(d) r = client.post("/destinations/1/edit", data=dict(label="Edited Destination", description="A destination"), follow_redirects=True) self.assertTrue(b"Destination updated" in r.data) edited_destination = Destination.query.first() self.assertEqual(edited_destination.label, "Edited Destination") self.assertEqual(edited_destination.description, "A destination")
def test_edit_destination_same_label(self): # Test for errors caused by the Unique validator on destination's label with self.client as client: self.loginTestUser() d = Destination(id=1, label="Test Destination") self.addToDatabase(d) r = client.post("/destinations/1/edit", data=dict(label="Test Destination", description="Edited description"), follow_redirects=True) self.assertTrue(b"Destination updated" in r.data) edited_destination = Destination.query.first() self.assertEqual(edited_destination.label, "Test Destination") self.assertEqual(edited_destination.description, "Edited description")
def setUp(self): super().setUp() with self.client: self.loginTestUser() ds = DataSource(id=1, label="Test Source", transfer_mechanism_type="curl") matcher = UrlMatcher(data_source_id=1, matcher_id=1, matcher_type="scheme_and_host", matcher_options={ "scheme": "http", "host": "example.com" }) dest = Destination(id=1, label="Test destination") self.addToDatabase(ds, matcher, dest)
[ 'Hoi An', 'Hoi An Town is an exceptionally well-preserved example of a Southeast Asian trading port dating from the 15th to the 19th century. Its buildings and its street plan reflect the influences, both indigenous and foreign, that have combined to produce this unique heritage site.', 'hoian_1.jpeg hoian_2.jpg hoian_3.jpg', 7, 'October', 2019, 'Vietnam', True ], [ 'Lempuyang Temple', 'Pura Penataran Agung Lempuyang is a Balinese Hindu temple or pura located in the slope of Mount Lempuyang in Karangasem, Bali. Pura Penataran Agung Lempuyang is considered as part of a complex of pura surrounding Mount Lempuyang, one of the highly regarded temples of Bali.', 'lempuyang_1.jpg lempuyang_2.jpg lempuyang_3.jpg', 14, 'January', 2020, 'Indonesia', True ], [ 'Everest Basecamp', 'Everest is more than a mountain and the journey to its base camp is more than just a trek. Every bend in the trail provides another photo opportunity - beautiful forests, Sherpa villages, glacial moraines, and foothills.', 'everest_1.jpg everest_2.jpg everest_3.jpg', 18, 'September', 2021, 'Nepal', False ] ] for destination in destinations: new_dest = Destination(name=destination[0], caption=destination[1], image_names=destination[2], date=destination[3], month=destination[4], year=destination[5], country=destination[6], visited=destination[7]) db.session.add(new_dest) db.session.commit()
def save_destination(form, destination=None, new=True): # PHOTOS try: request_featured_photo = request.files['featured_photo'] except KeyError: request_featured_photo = False if request_featured_photo: if new: photo_folder_name = form.title.data + '-' + secrets.token_hex(16) featured_photo_with_folder = images.save( request.files['featured_photo'], folder=photo_folder_name) featured_photo = featured_photo_with_folder.split('/')[1] featured_photo_filename = featured_photo.split('.')[0] featured_photo_extension = featured_photo.split('.')[-1] photo_dir = os.path.join( current_app.config["UPLOADED_IMAGES_DEST"], photo_folder_name) photo_folder_url = images.url(featured_photo_with_folder).split( featured_photo)[0] current_app.q.enqueue(create_image_set, photo_dir, featured_photo) else: # Bruke tidligere skapt folder, og hente featured_photo fra form: photo_folder_name = destination.photo_folder_url.split( '/static/uploads/images/' )[-1] # http://127.0.0.1:5000/static/uploads/images/Oskarshamn-7296b6784120247d3125ace582cdc17e/ featured_photo_with_folder = images.save( request.files['featured_photo'], folder=photo_folder_name) featured_photo = featured_photo_with_folder.split('/')[1] featured_photo_filename = featured_photo.split('.')[0] featured_photo_extension = featured_photo.split('.')[-1] photo_dir = os.path.join( current_app.config["UPLOADED_IMAGES_DEST"], photo_folder_name ) # samma struktur som photo_dir: app/static/uploads/images/Oskarshamn-7296b6784120247d3125ace582cdc17e/ # photo_folder_url = images.url(featured_photo_with_folder).split(featured_photo)[0] # Sende til enqueue current_app.q.enqueue(create_image_set, photo_dir, featured_photo) # Oppdatere befintlig destination med url/fil/extension # - Gjørs nedan # COUNTRY & CONTINENT country = pycountry_convert.country_alpha2_to_country_name( form.country.data) continent_code = pycountry_convert.country_alpha2_to_continent_code( form.country.data) continents = { 'AF': 'Africa', 'AN': 'Antarctica', 'AS': 'Asia', 'EU': 'Europe', 'NA': 'North America', 'OC': 'Oceania', 'SA': 'South America' } continent = continents[continent_code] # Add new destination if new: d = Destination(title=form.title.data, country=country, continent=continent, weather_ltd=form.weather_ltd.data, weather_lng=form.weather_lng.data, photo_folder_url=photo_folder_url, featured_photo_filename=featured_photo_filename, featured_photo_extension=featured_photo_extension, description=form.description.data, author=current_user) db.session.add(d) db.session.commit() else: destination.title = form.title.data destination.country = country destination.continent = continent destination.weather_ltd = form.weather_ltd.data destination.weather_ltd = form.weather_lng.data # destination.photo_folder_url = photo_folder_url # trenger ikke oppdatere denne if request_featured_photo: destination.featured_photo_filename = featured_photo_filename if request_featured_photo: destination.featured_photo_extension = featured_photo_extension destination.description = form.description.data # edited by ?? db.session.commit() if new: d = Destination.query.order_by(Destination.id.desc()).first() else: d = destination # Additional photos if request.files.getlist('additional_photos'): additional_photos_object = [] for photo in request.files.getlist('additional_photos'): photo_folder_name = d.photo_folder_url.split( '/static/uploads/images/')[-1] additional_photo_folder_name = photo_folder_name + 'additional_photos' additional_photo_with_folder = images.save( photo, folder=additional_photo_folder_name) additional_photo = additional_photo_with_folder.split('/')[2] additional_photo_filename = additional_photo.split('.')[0] additional_photo_extension = additional_photo.split('.')[-1] photo_dir = os.path.join( current_app.config["UPLOADED_IMAGES_DEST"], additional_photo_folder_name) current_app.q.enqueue(create_image_set, photo_dir, additional_photo) additional_photos_object.append( AdditionalPhotos( additional_photo_filename=additional_photo_filename, additional_photo_extension=additional_photo_extension, destination_id=d.id)) db.session.bulk_save_objects(additional_photos_object) # COST cost_form_currency = form.cost_form_currency.data if cost_form_currency != 'EUR': # EUR on default # Getting rate from ratesAPI payload = {'base': cost_form_currency, 'symbols': 'EUR'} api_currency_data = requests.get('https://api.ratesapi.io/api/latest', params=payload) if api_currency_data.status_code == 200: json_api_currency_data = api_currency_data.json() conversion_rate = json_api_currency_data['rates']['EUR'] cost_form_currency = 'EUR' beer_at_establishment = Decimal( conversion_rate) * form.beer_at_establishment.data # required coffee_at_establishment = Decimal( conversion_rate ) * form.coffee_at_establishment.data # required restaurant_inexpensive_meal = Decimal( conversion_rate ) * form.restaurant_inexpensive_meal.data # required groceries_one_week = Decimal( conversion_rate) * form.groceries_one_week.data # required car_rent_one_week = \ (Decimal(conversion_rate) * form.car_rent_one_week.data) if (type(form.car_rent_one_week.data) == Decimal) \ else 0 gas_one_liter = \ (Decimal(conversion_rate) * form.gas_one_liter.data) if (type(form.gas_one_liter.data) == Decimal) else 0 km_per_day = \ (Decimal(conversion_rate) * form.km_per_day.data) if (type(form.km_per_day.data) == Decimal) else 0 tent_per_day = \ (Decimal(conversion_rate) * form.tent_per_day.data) if (type(form.tent_per_day.data) == Decimal) else None van_per_day = \ (Decimal(conversion_rate) * form.van_per_day.data) if (type(form.van_per_day.data) == Decimal) else None camping_per_day = \ (Decimal(conversion_rate) * form.camping_per_day.data) if (type(form.camping_per_day.data) == Decimal) \ else None hostel_per_day = \ (Decimal(conversion_rate) * form.hostel_per_day.data) if (type(form.hostel_per_day.data) == Decimal) \ else None apartment_per_day = \ (Decimal(conversion_rate) * form.apartment_per_day.data) if (type(form.apartment_per_day.data) == Decimal) \ else None house_per_day = \ (Decimal(conversion_rate) * form.house_per_day.data) if (type(form.house_per_day.data) == Decimal) else None hotel_per_day = \ (Decimal(conversion_rate) * form.hotel_per_day.data) if (type(form.hotel_per_day.data) == Decimal) else None else: beer_at_establishment = form.beer_at_establishment.data # required coffee_at_establishment = form.coffee_at_establishment.data # required restaurant_inexpensive_meal = form.restaurant_inexpensive_meal.data # required groceries_one_week = form.groceries_one_week.data # required car_rent_one_week = form.car_rent_one_week.data if (type(form.car_rent_one_week.data) == Decimal) \ else 0 gas_one_liter = form.gas_one_liter.data if (type( form.gas_one_liter.data) == Decimal) else 0 km_per_day = form.km_per_day.data if (type(form.km_per_day.data) == Decimal) else 0 tent_per_day = form.tent_per_day.data if (type( form.tent_per_day.data) == Decimal) else None van_per_day = form.van_per_day.data if (type(form.van_per_day.data) == Decimal) else None camping_per_day = form.camping_per_day.data if (type( form.camping_per_day.data) == Decimal) else None hostel_per_day = form.hostel_per_day.data if (type( form.hostel_per_day.data) == Decimal) else None apartment_per_day = form.apartment_per_day.data if (type(form.apartment_per_day.data) == Decimal) \ else None house_per_day = form.house_per_day.data if (type( form.house_per_day.data) == Decimal) else None hotel_per_day = form.hotel_per_day.data if (type( form.hotel_per_day.data) == Decimal) else None else: beer_at_establishment = form.beer_at_establishment.data # required coffee_at_establishment = form.coffee_at_establishment.data # required restaurant_inexpensive_meal = form.restaurant_inexpensive_meal.data # required groceries_one_week = form.groceries_one_week.data # required car_rent_one_week = form.car_rent_one_week.data if (type( form.car_rent_one_week.data) == Decimal) else 0 gas_one_liter = form.gas_one_liter.data if (type( form.gas_one_liter.data) == Decimal) else 0 km_per_day = form.km_per_day.data if (type(form.km_per_day.data) == Decimal) else 0 tent_per_day = form.tent_per_day.data if (type(form.tent_per_day.data) == Decimal) else None van_per_day = form.van_per_day.data if (type(form.van_per_day.data) == Decimal) else None camping_per_day = form.camping_per_day.data if (type( form.camping_per_day.data) == Decimal) else None hostel_per_day = form.hostel_per_day.data if (type( form.hostel_per_day.data) == Decimal) else None apartment_per_day = form.apartment_per_day.data if (type( form.apartment_per_day.data) == Decimal) else None house_per_day = form.house_per_day.data if (type( form.house_per_day.data) == Decimal) else None hotel_per_day = form.hotel_per_day.data if (type( form.hotel_per_day.data) == Decimal) else None accomodations_form_data = { "tent_per_day": tent_per_day, "van_per_day": van_per_day, "camping_per_day": camping_per_day, "hostel_per_day": hostel_per_day, "apartment_per_day": apartment_per_day, "house_per_day": house_per_day, "hotel_per_day": hotel_per_day } for key in list(accomodations_form_data ): # Makes a list of al the keys in the dict if accomodations_form_data[key] is None: del accomodations_form_data[key] if accomodations_form_data: # Checking so it's not empty cheapest_accomodation = min(accomodations_form_data, key=accomodations_form_data.get) else: # if it is empty accomodations_form_data = {'no_info': 0} cheapest_accomodation = 'no_info' avg_weekly_cost = \ 3 * beer_at_establishment + \ 3 * coffee_at_establishment + \ 2 * restaurant_inexpensive_meal + \ 1 * groceries_one_week + \ 1 * car_rent_one_week + \ 7 * gas_one_liter * km_per_day + \ 7 * accomodations_form_data[cheapest_accomodation] avg_weekly_cost_rounded = int(avg_weekly_cost) if new: cost = Cost( cost_form_currency=cost_form_currency, beer_at_establishment=beer_at_establishment, coffee_at_establishment=coffee_at_establishment, restaurant_inexpensive_meal=restaurant_inexpensive_meal, groceries_one_week=groceries_one_week, car_rent_one_week=car_rent_one_week, gas_one_liter=gas_one_liter, km_per_day=km_per_day, tent_per_day=tent_per_day, van_per_day=van_per_day, camping_per_day=camping_per_day, hostel_per_day=hostel_per_day, apartment_per_day=apartment_per_day, house_per_day=house_per_day, hotel_per_day=hotel_per_day, accomodation_used_for_avg_weekly_cost=cheapest_accomodation, avg_weekly_cost=avg_weekly_cost_rounded, destination_id=d.id) db.session.add(cost) else: cost = Cost.query.filter(Cost.destination_id == d.id).first() cost.beer_at_establishment = beer_at_establishment cost.cost_form_currency = cost_form_currency cost.coffee_at_establishment = coffee_at_establishment cost.restaurant_inexpensive_meal = restaurant_inexpensive_meal cost.groceries_one_week = groceries_one_week cost.car_rent_one_week = car_rent_one_week cost.gas_one_liter = gas_one_liter cost.km_per_day = km_per_day cost.tent_per_day = tent_per_day cost.van_per_day = van_per_day cost.camping_per_day = camping_per_day cost.hostel_per_day = hostel_per_day cost.apartment_per_day = apartment_per_day cost.house_per_day = house_per_day cost.hotel_per_day = hotel_per_day cost.accomodation_used_for_avg_weekly_cost = cheapest_accomodation cost.avg_weekly_cost = avg_weekly_cost_rounded # ROUTES if new: routes = Routes(traditional=form.traditional.data, sport=form.sport.data, bouldering=form.bouldering.data, main_discipline=form.main_discipline.data, easy_routes=form.easy_routes.data, intermediate_routes=form.intermediate_routes.data, hard_routes=form.hard_routes.data, very_hard_routes=form.very_hard_routes.data, total_routes=form.total_routes.data, total_trad=form.total_trad.data, total_sport=form.total_sport.data, total_boulders=form.total_boulders.data, destination_id=d.id) db.session.add(routes) else: routes = Routes.query.filter(Routes.destination_id == d.id).first() routes.traditional = form.traditional.data routes.sport = form.sport.data routes.bouldering = form.bouldering.data routes.main_discipline = form.main_discipline.data routes.easy_routes = form.easy_routes.data routes.intermediate_routes = form.intermediate_routes.data routes.hard_routes = form.hard_routes.data routes.very_hard_routes = form.very_hard_routes.data routes.total_routes = form.total_routes.data routes.total_trad = form.total_trad.data routes.total_sport = form.total_sport.data routes.total_boulders = form.total_boulders.data # db.session.commit() # her? # MONTHS if new: months = Months(january=form.january.data, february=form.february.data, march=form.march.data, april=form.april.data, may=form.may.data, june=form.june.data, july=form.july.data, august=form.august.data, september=form.september.data, october=form.october.data, november=form.november.data, december=form.december.data, destination_id=d.id) db.session.add(months) else: months = Months.query.filter(Months.destination_id == d.id).first() months.january = form.january.data months.february = form.february.data months.march = form.march.data months.april = form.april.data months.may = form.may.data months.june = form.june.data months.july = form.july.data months.august = form.august.data months.september = form.september.data months.october = form.october.data months.november = form.november.data months.december = form.december.data # ACCOMODATION if new: accomodation = Accomodation(tent=form.tent.data, van=form.van.data, hostel=form.hostel.data, camping=form.camping.data, apartment=form.apartment.data, house=form.house.data, hotel=form.hotel.data, destination_id=d.id) db.session.add(accomodation) else: accomodation = Accomodation.query.filter( Accomodation.destination_id == d.id).first() accomodation.tent = form.tent.data accomodation.van = form.van.data accomodation.hostel = form.hostel.data accomodation.camping = form.camping.data accomodation.apartment = form.apartment.data accomodation.house = form.house.data accomodation.hotel = form.hotel.data # APPROACH if new: approach = Approach(easy=form.easy.data, moderate=form.moderate.data, hardcore=form.hardcore.data, destination_id=d.id) db.session.add(approach) else: approach = Approach.query.filter( Approach.destination_id == d.id).first() approach.easy = form.easy.data approach.moderate = form.moderate.data approach.hardcore = form.hardcore.data # CAR if new: car = Car( not_needed=form.not_needed.data, good_to_have=form.good_to_have.data, must_have=form.must_have.data, # rent_scooter_locally=form.rent_scooter_locally.data, # rent_car_locally=form.rent_car_locally.data, destination_id=d.id) db.session.add(car) else: car = Car.query.filter(Car.destination_id == d.id).first() car.not_needed = form.not_needed.data car.good_to_have = form.good_to_have.data car.must_have = form.must_have.data # car.rent_scooter_locally = form.rent_scooter_locally.data, # car.rent_car_locally = form.rent_car_locally.data db.session.commit() return True # Hvordan returnere False hvis den failer?
iata_code=fields[0], lat=float(fields[1]), long=float(fields[2])) db.session.add(a) db.session.commit() with open('data/destinations.csv', encoding='utf-8') as file: for line in file: # "city","country","iata_code","lat","long" # "Madrid","Spain","MAD",40.47193,-3.56264 line = line.replace('"', '') fields = line.split(',') d = Destination( city=fields[0], country=fields[1], iata_code=fields[2], lat=float(fields[3]), long=float(fields[4])) db.session.add(d) db.session.commit() with open('data/weather_conditions.csv', encoding='utf-8') as file: for line in file: # "iata_code","month","min_temperature_celsius","max_temperature_celsius","daily_precipitation_mm" # "MAD","January","2.0","12.9","1.6" line = line.replace('"', '') fields = line.split(',') w = Weather( iata_code=fields[0], month=fields[1],
for i in tlps: obj = Tlp() obj.name = i db.session.add(obj) db.session.commit() for i in types: obj = Itype(i[0], i[1]) db.session.add(obj) db.session.commit() for i in sources: obj = Source() obj.name = i db.session.add(obj) db.session.commit() for i in statuses: obj = Status() obj.name = i db.session.add(obj) db.session.commit() for i in destinations: obj = Destination() obj.name = i[0] obj.description = i[1] obj.formatter = i[2] db.session.add(obj) db.session.commit()