def create_newdate(): errors = MealDate.validate(request.json) if len(errors) == 0: proposal_id = request.json.get('proposal_id') accept_proposal = request.json.get('accept_proposal') proposal = session.query(Proposal).filter_by(id=proposal_id).first() if proposal is None: return jsonify({'response': False}) req = proposal.request if req.filled: return jsonify({'response': False}) if accept_proposal: proposal.update({'filled': True}) req.update({'filled': True}) restaurant_name = '' restaurant_address = '' restaurant_picture = '' try: restaurant = findARestaurant(req.meal_type, req.location_string) if type(restaurant) == dict: restaurant_name = restaurant['name'] restaurant_address = restaurant['address'] restaurant_picture = restaurant['image_url'] except Exception as e: print e date = MealDate( user_1 = req.user_id, \ user_2 = proposal.user_proposed_from, \ restaurant_name = restaurant_name, \ restaurant_address = restaurant_address, \ restaurant_picture = restaurant_picture, \ meal_time = req.meal_time \ ) session.add(date) session.commit() return jsonify({'response': True}), 201 else: session.delete(proposal) session.commit() return jsonify({'response': 'Deleted proposal'}) else: return jsonify({'errors': errors}), 400
def new_date(): user = g.user if not request.json: abort(400) errors = MealDate.validate(request.json) if len(errors) == 0: proposalsa_id = request.json.get('proposal_id') accept_proposal = request.json.get('accept_proposal') proposal = session.query(Proposal).filter_by(id=proposal_id).first() if proposal is None: abort(404) r = proposal.request if r.filled: return jsonify({'result': False}) if accept_proposal: proposal.filled = True r.filled = True restaurant_picture = "No Restaurants Found" restaurant_address = "No Restaurants Found" restaurant_name = "No Restaurants Found" try: result = findARestaurant(r.meal_type, r.location_string) if type(result) == dict: restaurant_picture = result.get('name') restaurant_address = result.get('address') restaurant_name = result.get('image') except Exception as e: print e date = MealDate(meal_time=r.meal_time, user_1=r.user_id, user_2=proposal.user_proposed_from, restaurant_picture=restaurant_picture, restaurant_address=restaurant_address, restaurant_name=restaurant_name) session.add(date) session.add(proposal) session.add(r) session.commit() return jsonify({'result': True}) else: session.delete(proposal) session.commit() return jsonify({'result': True}) return jsonify({"message": "The request is invalid."}, errors=[error for error in errors]), 400
def update_date(id): user = g.user if not request.json: abort(400) date = session.query(MealDate).filter_by(id = id).first() if data is None: abort(404) errors = MealDate.validate(request.json) if len(errors) == 0: return jsonify( { 'result': True } ) return jsonify({"message": "The request is invalid."},errors = [error for error in errors]) ,400
def dates(): user_id = User.verify_auth_token(token) user_1 = session.query(User).filter_by(id=user_id).first().email if request.method == 'GET': dates = session.query(MealDate).all() return jsonify(dates=[d.serialize for d in dates]) elif request.method == 'POST': user_2 = request.json.get('user_2') meal_time = request.json.get('meal_time') proposal = session.query(Proposal).filter_by( user_proposed_to=user_2).filter_by( user_proposed_from=user_1).first() requestMeal = session.query(RequestMeal).filter_by( id=proposal.request_id).first() meal_type = requestMeal.meal_type location_string = requestMeal.location_string restaurantInfo = findARestaurant( meal_type, location_string) # name, address, picture of the restaurant. restaurant_name = restaurantInfo['name'] restaurant_address = restaurantInfo['address'] restaurant_picture = restaurantInfo['picture'] if user_2 is None or meal_time is None: print "사용자와 식사 시간을 입력해주세요." abort(400) if restaurant_name is None or restaurant_address is None: print "레스토랑 정보를 입력해주세요" abort(400) dates = MealDate(user_1=user_1, user_2=user_2, restaurant_name=restaurant_name, restaurant_address=restaurant_address, restaurant_picture=restaurant_picture, meal_time=meal_time) session.add(dates) session.commit() return jsonify({ 'user_1': dates.user_1, 'user_2': dates.user_2, 'restaurant_name': dates.restaurant_name, 'restaurant_address': dates.restaurant_address, 'restaurant_picture': dates.restaurant_picture, 'meal_time': dates.meal_time })
def update_date(id): user = g.user if not request.json: abort(400) date = session.query(MealDate).filter_by(id=id).first() if data is None: abort(404) errors = MealDate.validate(request.json) if len(errors) == 0: return jsonify({'result': True}) return jsonify({"message": "The request is invalid."}, errors=[error for error in errors]), 400
def new_date(): user = g.user if not request.json: abort(400) errors = MealDate.validate(request.json) if len(errors) == 0: proposalsa_id = request.json.get('proposal_id') accept_proposal = request.json.get('accept_proposal') proposal = session.query(Proposal).filter_by(id = proposal_id).first() if proposal is None: abort(404) r = proposal.request if r.filled: return jsonify( { 'result': False } ) if accept_proposal: proposal.filled = True r.filled = True restaurant_picture = "No Restaurants Found" restaurant_address = "No Restaurants Found" restaurant_name = "No Restaurants Found" try: result = findARestaurant(r.meal_type, r.location_string) if type(result) == dict: restaurant_picture = result.get('name') restaurant_address = result.get('address') restaurant_name = result.get('image') except Exception as e: print e date = MealDate(meal_time = r.meal_time, user_1 = r.user_id , user_2 = proposal.user_proposed_from, restaurant_picture = restaurant_picture,restaurant_address = restaurant_address, restaurant_name= restaurant_name ) session.add(date) session.add(proposal) session.add(r) session.commit() return jsonify( { 'result': True } ) else: session.delete(proposal) session.commit() return jsonify( { 'result': True } ) return jsonify({"message": "The request is invalid."},errors = [error for error in errors]) ,400
def dates_function(): if request.method == 'GET': all_dates = session.query(MealDate).filter(( MealDate.user_1 == g.user.id) | (MealDate.user_2 == g.user.id)) return jsonify(AllDates=[date.serialize for date in all_dates]) if request.method == 'POST': current_proposal = session.query(Proposal).filter_by( user_proposed_to=g.user.id).first() current_request = session.query(Request).filter_by( id=current_proposal.request_id).first() user_1 = g.user.id user_2 = current_proposal.user_proposed_from accepted = request.json.get('accepted') if accepted == "False": session.delete(current_proposal) session.commit() return jsonify({"Status": "Rejected"}), 202 restaurantJSON = findARestaurant(current_request.meal_type, current_request.location_string) restaurant_name = restaurantJSON.get('name') restaurant_address = restaurantJSON.get('address') date = MealDate(user_1=user_1, user_2=user_2, restaurant_name=restaurant_name, restaurant_address=restaurant_address, meal_time=current_request.meal_time) session.add(date) session.commit() return jsonify(date.serialize), 201
def dates(request): was_limited = getattr(request, 'limited', False) if was_limited: return JsonResponse( { 'status': 'you hit the rate limit!' } ) else: token = get_token(request) user_id_login = User.verify_auth_token(token) if user_id_login: if request.method == 'GET': meal_dates = MealDate.objects.filter(Q(user_1=user_id_login) | Q(user_2=user_id_login)) data = [] for meal_date in meal_dates: item = { 'id': meal_date.id, 'user_1': meal_date.user_1, 'user_2': meal_date.user_2, 'restaurant_name': meal_date.restaurant_name, 'restaurant_address': meal_date.restaurant_address, 'restaurant_picture': meal_date.restaurant_picture, 'meal_time': meal_date.meal_time } data.append(item) return JsonResponse( { 'objects': data } ) elif request.method == 'POST': is_agreed = request.POST.get('agreed') proposals_id = request.POST.get('proposal_id') if is_agreed: proposals_instance = Proposal.objects.get(id=proposals_id) request_instance = Request.objects.get(id=proposals_instance.request_id.id) meal_date = MealDate( user_1=proposals_instance.user_proposed_to, user_2=proposals_instance.user_proposed_from, meal_time=request_instance.meal_time ) meal_date.save() return JsonResponse( { 'status': 'instance created successfully', 'object': { 'id': meal_date.id, 'user_1': meal_date.user_1, 'user_2': meal_date.user_2, 'restaurant_name': meal_date.restaurant_name, 'restaurant_address': meal_date.restaurant_address, 'restaurant_picture': meal_date.restaurant_picture, 'meal_time': meal_date.meal_time } } ) else: proposals_instance = Proposal.objects.get(id=proposals_id) proposals_instance.delete() return JsonResponse({'status': 'instance deleted successfully'}) else: return HttpResponseForbidden('invalid token')