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 place_copy(request, place_id): source = get_object_or_404(Place, id=place_id) place = Place(user=request.user, name=source.name, pltype=source.pltype, city=source.city, locale=source.locale, cuisine=source.cuisine, dog_friendly=source.dog_friendly, has_bar=source.has_bar, outdoor=source.outdoor, yelp=source.yelp, ) place.id = Place.next_id() place.save() ChangeLog.place_copy(place, request.user.username, place_id) m = 'User: {} copied place: {}'.format(request.user, place.name) logprint(m) log_to_slack(m) # because this is a new request it need a leading / return redirect('/places/edit/{}/'.format(place.id))