Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def approve_reservation(meal_id, guest_id):
    meal = Meal.query.get_or_404(meal_id)

    reservation = meal.reservations.filter_by(guest_id=guest_id).first()
    reservation.approval = 1
    db_session.flush()
    return redirect(url_for('meal.detail_meal', meal_id=meal_id))
Ejemplo n.º 3
0
def delete_meal(meal_id):
    meal = Meal.query.get_or_404(meal_id)
    flag = meal.reservations.filter_by(approval=1).first()
    if flag:
        flash('예약승인이 된 회원이 존재합니다.')
    else:
        db_session.delete(meal)
        db_session.flush()
    return redirect(url_for('account.list_meals'))
Ejemplo n.º 4
0
def cancel_reservation(meal_id, guest_id):
    reservation = Reservation.query.filter_by(meal_id=meal_id,
                                              guest_id=guest_id).first()
    if reservation.payment_id:
        # cancel payment transaction
        iamport = Iamport(imp_key=app.config['IAMPORT_API_KEY'],
                          imp_secret=app.config['IAMPORT_API_SECRET'])
        iamport.cancel('예약 취소', imp_uid=reservation.payment_id)

    db_session.delete(reservation)
    db_session.flush()
    return redirect(url_for('meal.detail_meal', meal_id=meal_id))
Ejemplo n.º 5
0
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))
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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'))
Ejemplo n.º 10
0
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'
Ejemplo n.º 11
0
def user(client):
    user = {
        'username': '******',
        'password': '******',
        'confirmed_password': '******',
        'name': 'Allan Turing',
        'email': '*****@*****.**'
    }

    resp = client.post(url_for('account.signup'), data=user).get_json()

    del user['confirmed_password'], user['password']
    for field, value in user.items():
        assert resp['user'][field] == user[field]
    yield user
    resp = client.post(url_for('account.unregister'))
    assert resp.status_code == 302
    user = User.query.with_deleted.filter_by(username='******').first()
    assert user.deleted_at is not None
    if user.profile_uuid:
        minio_client.remove_object('fourmen', user.profile_uuid)
    db_session.delete(user)
    db_session.flush()
Ejemplo n.º 12
0
def dummy_guest(client):
    dummy_guest = {
        'username': '******',
        'password': '******',
        'confirmed_password': '******',
        'name': 'qqqqq',
        'email': '*****@*****.**'
    }

    resp = client.post(url_for('account.signup'), data=dummy_guest).get_json()

    del dummy_guest['confirmed_password'], dummy_guest['password']
    for field, value in dummy_guest.items():
        assert resp['user'][field] == dummy_guest[field]
    yield dummy_guest
    resp = client.post(url_for('account.unregister'))
    assert resp.status_code == 302
    dummy_guest = User.query.with_deleted.filter_by(username='******').first()
    assert dummy_guest.deleted_at is not None
    if dummy_guest.profile_uuid:
        minio_client.remove_object('fourmen', dummy_guest.profile_uuid)
    db_session.delete(dummy_guest)
    db_session.flush()