def restaurants(invalid=None): form = RestaurantForm(request.form) print(form.validate()) con = sql.connect("database.db") con.row_factory = sql.Row if request.method == 'POST': try: email = request.form['email'].strip() restaurant = request.form['restaurant'].strip() address = request.form['address'].strip() if len(email) != 0 and len(restaurant) != 0 and len(address) != 0: with sql.connect("database.db") as con: cur = con.cursor() cur.execute( "INSERT INTO restaurants(email, restaurant, address) VALUES (?,?, ?)", (email, restaurant, address)) con.commit() return render_template("submitted.html", link=url_for("restaurants"), location="restaurants") else: print("Not added") return render_template("restaurants.html", invalid=True) except: con.rollback() con.close() return render_template("restaurants.html")
def add(table): # get form template from WTForm class for this table form = RestaurantForm (request.form) if table == 'Restaurant': form = RestaurantForm(request.form) elif table == 'MenuItem': form = MenuItemForm(request.form) else: flash ('I didn\'t understand! Please try adding a /Restaurant/ or a /MenuItem/.') # POST add restaurant to database if request.method == 'POST' and form.validate(): if table == 'Restaurant': new_row = Restaurant (name=form.name.data, address=form.address.data, city=form.city.data, state=form.state.data, zipCode=form.zipCode.data, website=form.website.data, cuisine=form.cuisine.data) flash ('New restaurant created!') elif table == 'MenuItem': new_row = MenuItem (name=form.name.data, description=form.description.data, restaurant_id=form.restaurant_id.data) flash ('New menu item created!') else: flash ('Please try to add a /Restaurant/ or a /MenuItem/.') session.add (new_row) session.commit () return redirect (url_for('home')) # GET display form for adding restaurant return render_template ('form.php', market=user_city, form=form, content='')
def add(request): try: extra_hours = int(request.GET["eh"]) except: extra_hours = 7 HourFormSet = formset_factory(HourForm, extra=extra_hours) if request.method == "POST": form = RestaurantForm(request.POST) hfs = HourFormSet(request.POST) slug = slugify(form.data.get("name")) original = None try: original = Restaurant.objects.all().get(slug=slug) except: pass if original: #Add an error to the name field. form.errors["name"] = ErrorList(["Name has already been taken"]) if form.is_valid() and hfs.is_valid() and original != None: model = form.save(commit=False) model.slug = slugify(model.name) model.user = request.user model.save() for hour in hfs.forms: hmodel = hour.save(commit=False) if not hmodel.open_time: continue hmodel.restaurant = model hmodel.save() return HttpResponseRedirect(model.get_absolute_url()) else: form = RestaurantForm() hfs = HourFormSet() return render_to_response("restaurants/add.html", {'form': form, 'hfs':hfs, 'extra_hours':extra_hours+1}, context_instance=RequestContext(request))
def deleteRestaurant(restaurant_id): session = DBSession() form = RestaurantForm() restaurantToDelete = session.query(Restaurant).filter_by(id=restaurant_id).one() if form.validate_on_submit(): session.delete(restaurantToDelete) session.commit() session.close() flash("Restaurant Deleted Successfully!!! ") return redirect(url_for('showRestaurants', restaurant_id=restaurant_id)) return render_template('restaurant/deleterestaurant.html', item = restaurantToDelete, form=form)
def createRestaurant(): session = DBSession() form = RestaurantForm() if form.validate_on_submit(): name = form.name.data location = form.location.data description = form.description.data restaurant = models.Restaurant(name=name, location =location, description = description) session.add(restaurant) session.commit() session.close() flash("Added '{}'".format(name)) return redirect(url_for('showRestaurants')) return render_template('restaurant/createrestaurant.html', form=form)
def post(self, request, *args, **kwargs): restaurant_form = RestaurantForm(request.POST, request.FILES, prefix='restaurant') address_form = AddressForm(request.POST, prefix='address') if not (restaurant_form.is_valid() and address_form.is_valid()): return render(request, 'business/edit_restaurant_page.html', {'restaurant_form': restaurant_form, 'address_form': address_form}) restaurant = Restaurant.objects.get(baseUser=request.user) address = Address.objects.get(restaurant=restaurant) RestaurantForm(request.POST, request.FILES, prefix='restaurant', instance=restaurant).save() AddressForm(request.POST, prefix='address', instance=address).save() return HttpResponseRedirect(reverse('business:RestaurantPage'))
def editRestaurant(restaurant_id): session = DBSession() form = RestaurantForm() editedrestaurant = session.query(Restaurant).filter_by(id=restaurant_id).one() if form.validate_on_submit(): if request.form['name']: editedrestaurant.name = form.name.data if request.form['location']: editedrestaurant.location = form.location.data if request.form['description']: editedrestaurant.description = form.description.data session.add(editedrestaurant) session.commit() session.close() flash("Restaurant Edited Successfully!!! ") return redirect(url_for('showRestaurants', restaurant_id=restaurant_id)) return render_template('restaurant/editrestaurant.html', restaurant_id=restaurant_id, item=editedrestaurant, form=form)
def post(self, request, *args, **kwargs): restaurant_form = RestaurantForm(request.POST, request.FILES, prefix='restaurant') address_form = AddressForm(request.POST, prefix='address') if not (restaurant_form.is_valid() and address_form.is_valid()): return render(request, 'business/signup.html', {'restaurant_form': restaurant_form, 'address_form': address_form}) restaurant = restaurant_form.save(commit=False) restaurant.baseUser = request.user restaurant.save() address = address_form.save(commit=False) address.restaurant = restaurant address.save() return HttpResponseRedirect(reverse('business:RestaurantPage'))
def update(table,index): # retrieve form class from forms.py based on URL keyword if table == 'Restaurant': form = RestaurantForm (request.form) elif table == 'MenuItem': form = MenuItemForm (request.form) else: flash ('This isn\'t the /update/ you are looking for! We don\'t recognize that URL.') return redirect (url_for('home')) # POST edit database on form submission if request.method == 'POST': is_valid = form.validate() # store validation test result # make edits to a restaurant if table == 'Restaurant' and is_valid: mod_row = session.query(Restaurant).filter_by(id=index)[0] mod_row.name = form.name.data mod_row.cuisine = form.cuisine.data mod_row.address = form.address.data mod_row.city = form.city.data mod_row.state = form.state.data mod_row.zipCode = form.zipCode.data mod_row.website = form.website.data mod_row.image = form.image.data # make edits to a menu item elif table == 'MenuItem' and is_valid: mod_row = session.query(MenuItem).filter_by(id=index)[0] mod_row.name = form.name.data mod_row.description = form.description.data mod_row.restaurant_id = form.restaurant_id.data # cannot update the table else: flash ('Help, I couldn\'t add that info to FoodBase! Do you mind checking if you filled out the whole form?') return redirect (url_for('update', table=table, index=index)) # update table row in the db session.add (mod_row) session.commit() flash ('Successfully updated %s!'%mod_row.name) return redirect (url_for('home')) # GET - retrieve data and build the form if table == 'Restaurant': # if user requests update restaurant form r = session.query(Restaurant).filter_by(id=index)[0] form.name.data = r.name form.cuisine.data = r.cuisine form.address.data = r.address form.city.data = r.city form.state.data = r.state form.zipCode.data = r.zipCode form.website.data = r.website form.image.data = r.image elif table == 'MenuItem': m = session.query(MenuItem).filter_by(id=index)[0] form.name.data = m.name form.description.data = m.description form.restaurant_id.data = m.restaurant_id else: # if user requests other update forms flash ('I couldn\'t update that. Please try to update a /Restaurant or a /MenuItem instead!') return render_template('form.php', market=user_city, form=form, content='')