def update_meal(meal_id): if request.method == 'GET': meal = Meal.query.get_or_404(meal_id) return render_template('meal_form.html', meal=meal) elif request.method == 'POST': meal = Meal.query.get_or_404(meal_id) if meal: if request.form['title']: meal.name = request.form['title'] if request.form['price']: meal.price = request.form['price'] if request.form['description']: meal.description = request.form['description'] if request.form['date'] and request.form['time']: meal.datetime = datetime.strptime( request.form['date'] + ' ' + request.form['time'], "%Y-%m-%d %H:%M") db_session.add(meal) if request.form.getlist('image_uuids'): MealPhoto.query.filter_by(meal_id=meal.id).delete() for uuid in request.form.getlist('image_uuids'): photo = MealPhoto(meal=meal, uuid=uuid) db_session.add(photo) db_session.flush() return redirect(url_for('meal.detail_meal', meal_id=meal_id)) else: abort(403)
def list_meal(): if request.method == 'GET': lat = request.args['lat'] lon = request.args['lng'] query = { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "20km", "pin.location": { "lat": lat, "lon": lon } } } } } } res = es.search(index="dining", doc_type="meal", body=query, filter_path=['hits.hits._id']) if (res): meals_id = [dic['_id'] for dic in res['hits']['hits']] meals = Meal.query.filter(Meal.id.in_(meals_id)).all() else: meals = [] return render_template('meal_main.html', meals=meals, lat=lat, lon=lon) elif request.method == 'POST': meal = Meal(name=request.form['title'], host_id=session['id'], price=request.form['price'], description=request.form['description'], datetime=datetime.strptime( request.form['date'] + ' ' + request.form['time'], "%Y-%m-%d %H:%M"), city=request.form['city'], state=request.form['state'], country=request.form['country'], lat=request.form['lat'], lng=request.form['lng']) db_session.add(meal) for uuid in request.form.getlist('image_uuids'): photo = MealPhoto(meal=meal, uuid=uuid) db_session.add(photo) db_session.flush() return redirect(url_for('meal.detail_meal', meal_id=meal.id))
def reservation(meal_id, guest_id): meal = Meal.query.get_or_404(meal_id) iamport = Iamport(imp_key=app.config['IAMPORT_API_KEY'], imp_secret=app.config['IAMPORT_API_SECRET']) if not iamport.is_paid(meal.price, imp_uid=request.form['payment_id']): iamport.cancel('Bad request', imp_uid=request.form['payment_id']) flash('잘못된 접근입니다.') raise BadRequest() else: reservations = Reservation(meal_id=meal_id, guest_id=guest_id, payment_id=request.form['payment_id']) db_session.add(reservations) db_session.flush() return redirect(url_for('meal.detail_meal', meal_id=meal_id))
def test_meal_deletion(client, meal, dummy_guest): m = Meal.query.order_by(Meal.id.desc()).first() assert m is not None guest = User.query.filter_by(username=dummy_guest['username']).first() resp = client.post(url_for('account.signin'), data={ 'username': dummy_guest['username'], 'password': '******' }) assert resp.status_code == 302 reservation = Reservation(meal_id=m.id, guest_id=guest.id, payment_id=1234444) db_session.add(reservation) db_session.flush() resp = client.post(url_for('meal.delete_meal', meal_id=m.id)) assert resp.status_code == 302
def client(): app.config.from_object('fourmen.configurations.DevelopConfig') client = app.test_client() if not hasattr(Response, 'get_json'): Response.get_json = get_json headers = {'Accept': 'application/json'} client.post = partial(client.post, headers=headers) client.get = partial(client.get, headers=headers) with app.app_context(): init_db(drop=True) admin = User(username='******', password='******', email='*****@*****.**', name='Admin', is_admin=True, is_host=True) db_session.add(admin) db_session.flush() yield client
def create_review(): if request.method == 'GET': meal = Meal.query.get(request.args['meal_id']) user = User.query.get(session['id']) guest = User.query.get(request.args['guest_id']) author_type = request.args['author_type'] return render_template('review_form.html', author_type=author_type, meal=meal, user=user, guest=guest) elif request.method == 'POST': review = Review( meal_id=request.form['meal_id'], guest_id=request.form['guest_id'], author_id=session['id'], date=date.today(), content=request.form['content'], rating=request.form['rating'] ) db_session.add(review) db_session.flush() return redirect(url_for('dining.list_dining'))
def message_list(): if request.method == 'GET': rcv_msg = db_session.query(Message.sender_id, func.max(Message.id)). \ filter_by(receiver_id=session['id']).group_by(Message.sender_id) send_msg = db_session \ .query(Message.receiver_id, func.max(Message.id)). \ filter_by(sender_id=session['id']).group_by(Message.receiver_id) message = rcv_msg.union(send_msg).all() msg = [] messages_id = [] for m in message: messages_id.append(m[1]) msg.append(Message.query.filter(Message.id.in_(messages_id)).all()) recent_msg = {} for m in msg[0]: counter_id = 0 if m.sender_id == session['id']: counter_id = m.receiver_id elif m.sender_id != session['id']: counter_id = m.sender_id if counter_id not in recent_msg: recent_msg[counter_id] = m.id elif counter_id in recent_msg and counter_id < m.id: recent_msg[counter_id] = m.id recent_msgs = Message.query \ .filter(Message.id.in_(recent_msg.values())). \ order_by(Message.id.desc()).all() return render_template('message_box.html', message=recent_msgs) elif request.method == 'POST': if session['id'] == int(request.form['receiver_id']): return 'no' else: msg = Message(sender_id=session['id'], receiver_id=request.form['receiver_id'], meal_id=request.form['meal_id'], content=request.form['content']) db_session.add(msg) db_session.flush() return 'ok'