def place_add(request, pltype='rest'): # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: if pltype == 'rest': form = RestaurantForm(request.POST) else: form = HotelForm(request.POST) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required d = form.cleaned_data p = Place(user=request.user, name=d['name'].strip(), city=d['city'].strip(), locale=d['locale'].strip(), visited=d['visited'], rating=d['rating']) p.id = Place.next_id() p.good_for = d['good_for'] p.comment = d['comment'] p.dog_friendly = d['dog_friendly'] if pltype == 'rest': p.pltype = Place.RESTAURANT p.cuisine = d['cuisine'] p.yelp = d['yelp'] p.outdoor = d['outdoor'] else: p.pltype = Place.HOTEL p.yelp = d['tripadvisor'] p.has_bar = d['has_bar'] p.save() m = 'User: {} added place: {} with id: {}'.format(p.user, p.name, p.id) logprint(m) ChangeLog.place_add(p, request.user.username) if not p.yelp and p.pltype == Place.RESTAURANT: try: yelp_matches = get_yelp_matches(p.name, p.city) if yelp_matches: return select_yelp_match(request, p, yelp_matches) except: pass return place_detail(request, p.id) else: # form = PlaceForm().as_table() if pltype == 'rest': form = RestaurantForm().as_table() else: form = HotelForm().as_table() if pltype == 'rest': pltypeS = 'Restaurant' else: pltypeS = 'Hotel' return render(request, 'places/place_add.html', {'form': form, 'pltype': pltype, 'pltypeS': pltypeS})
def test_restaurant_form_valid_with_full_fill(self): form_data = { "category": "Burgers", "name": "Five Guys", "min_party": 1, "max_party": 10, } form = RestaurantForm(form_data) self.assertTrue(form.is_valid())
def test_restaurant_form_valid_with_full_blank(self): form_data = { "category": "", "name": "", "min_party": None, "max_party": None, } form = RestaurantForm(form_data) self.assertTrue(form.is_valid())
def test_restaurant_form_invalid_with_party(self): form_data = { "category": "", "name": "", "min_party": "Hello", "max_party": "World", } form = RestaurantForm(form_data) self.assertFalse(form.is_valid())
def get(self, request): if request.GET: form = RestaurantForm(request.GET) if form.is_valid(): target_url = reverse("places:restaurant-list") query_params = { k: v for k, v in form.cleaned_data.items() if v } target_queries = urlencode(query_params) if target_queries: target_url = f"{target_url}?{target_queries}" return HttpResponseRedirect(target_url) else: form = RestaurantForm() return render(request, self.template_name, {"form": form})
def test_restaurant_form_valid_with_empty(self): form_data = {"category": "", "name": ""} form = RestaurantForm(form_data) self.assertTrue(form.is_valid())
def test_restaurant_form_valid_with_partial(self): form_data = {"category": "Burgers", "name": ""} form = RestaurantForm(form_data) self.assertTrue(form.is_valid())
def test_place_form(self): p = RestaurantForm() html = p.as_table() field_labels = sorted(PP.get_place_labels(html)) should_be = sorted([x + ":" for x in add_form_labels]) self.assertEqual(field_labels, should_be)