def update_restaurant(): """Saves changes to an existing restaurant. :Returns: JSON with restaurant ID or error message. """ obj = request.get_json() item = obj.pop("restaurant") if item.get("id", None): app.q_Restaurant().filter_by(id=item["id"]).update(item) app.db_session.commit() return jsonify(id=item["id"]) else: return jsonify(error="Restaurant update failed"), 500
def update_restaurant(): """Saves changes to an existing restaurant. :Returns: JSON with restaurant ID or error message. """ obj = request.get_json() item = obj.pop('restaurant') if item.get('id', None): app.q_Restaurant().filter_by(id=item['id']).update(item) app.db_session.commit() return jsonify(id=item['id']) else: return jsonify(error='Restaurant update failed'), 500
def api_restaurants(): """Returns a list of restaurants in the database. :returns: JSON with a 'restaurants' key and list of restaurants. """ recs = app.q_Restaurant().order_by('name') resp = [each.sdict for each in recs] return jsonify(restaurants=resp)
def api_restaurants(): """Returns a list of restaurants in the database. :returns: JSON with a 'restaurants' key and list of restaurants. """ recs = app.q_Restaurant().order_by("name") resp = [each.sdict for each in recs] return jsonify(restaurants=resp)
def restaurant_form(): """Returns a **private** page for adding or editing a restaurant. Redirects to restaurant list if user is not logged in. """ r_id = request.args.get('id', None) context = { 'title': 'Edit Restaurant' if r_id else 'New Restaurant', 'restaurant': None, 'username': login_session.get('username', ''), 'picture': login_session.get('picture', '') } if r_id is not None: context['restaurant'] = app.q_Restaurant().get(r_id).sdict return render_template('form_restaurant.html', **context)
def restaurant_view(restaurant_id): """Returns a public menu page showing a restaurant's menu items. Editing and rating buttons are invisible/disabled if user is not logged in. """ restaurant = app.q_Restaurant().get(restaurant_id) if restaurant is None: return redirect(url_for('restaurants')) context = { 'title': 'Menu', 'restaurant': restaurant.sdict, 'username': login_session.get('username', ''), 'picture': login_session.get('picture', '') } return render_template('menu.html', **context)
def delete_restaurant(): """Deletes a restaurant from the database. :Returns: JSON object with status of 'ok' or HTML error message. """ if 'id' not in request.get_json(): return abort(400) if not isinstance(request.get_json()['id'], int): return abort(400) try: record = app.q_Restaurant().get(request.get_json()['id']) app.db_session.delete(record) app.db_session.commit() return jsonify(status='ok') except IntegrityError: app.db_session.rollback() return abort(500)
def delete_restaurant(): """Deletes a restaurant from the database. :Returns: JSON object with status of 'ok' or HTML error message. """ if "id" not in request.get_json(): return abort(400) if not isinstance(request.get_json()["id"], int): return abort(400) try: record = app.q_Restaurant().get(request.get_json()["id"]) app.db_session.delete(record) app.db_session.commit() return jsonify(status="ok") except IntegrityError: app.db_session.rollback() return abort(500)
def item_form(restaurant_id): """Returns a **private** page for adding or editing a menu item. Redirects to restaurant list if user is not logged in. """ item_id = request.args.get('id', None) rating = request.args.get('rating', 0) restaurant = app.q_Restaurant().get(restaurant_id) context = { 'title': 'Edit Item' if item_id else 'New Item', 'restaurant': restaurant.sdict, 'item': None, 'rating': rating, 'username': login_session.get('username', ''), 'picture': login_session.get('picture', '') } if item_id is not None: context['item'] = app.q_MenuItem().get(item_id).sdict return render_template('form_item.html', **context)