def fromRequest(cls, request):
        request_data = simplejson.loads(request.REQUEST.get("data"))
        request_data = dict_to_str_keys(request_data)

        pickup = request_data.get("pickup")
        dropoff = request_data.get("dropoff")
        booking_settings = request_data.get("settings")
        asap = request_data.get("asap")


        inst = cls()
        inst.debug = settings.DEV
        inst.num_seats = int(booking_settings["num_seats"])
        inst.luggage = int(booking_settings["luggage"])
        inst.private = bool(booking_settings["private"])
        inst.pickup_address = Address(**pickup)
        inst.dropoff_address = Address(**dropoff)

        # TODO_WB: save extra data when pickup/dropoff have place_id field
        logging.info("pickup_place_id: %s" % pickup.get("place_id"))
        logging.info("dropoff_place_id: %s" % dropoff.get("place_id"))

        if asap:
            inst.asap = True
            inst.pickup_dt = default_tz_now() + datetime.timedelta(minutes=asap_interval())
            logging.info("ASAP set as %s" % inst.pickup_dt.strftime("%H:%M"))
        else:
            inst.pickup_dt = dateutil.parser.parse(request_data.get("pickup_dt"))

        inst.mobile = request.mobile
        inst.language_code = request.POST.get("language_code", get_language_from_request(request))
        inst.user_agent = request.META.get("HTTP_USER_AGENT")

        return inst
示例#2
0
def crud_place(request):
    lib_ng = True
    lib_map = True
    lib_geo = True

    if request.method == "GET":
        places = simplejson.dumps([place.serialize() for place in Place.objects.all()])
        return render_to_response("crud_place.html", locals())

    elif request.method == "POST":  # CRUD actions
        payload = simplejson.loads(request.raw_post_data)
        action = payload["action"]
        place_data = payload["data"]
        place_data = dict_to_str_keys(place_data)

        if action in ["create", "update"]:
            if place_data.get("city_name"):
                place_data["city"] = City.objects.get(name=place_data["city_name"].strip())
                del(place_data["city_name"])

            place_data["aliases"] = place_data["aliases"]

            if action == "create":
                place = Place(**place_data)
                place.save()
                logging.info("created new place: %s" % place.name)

            else:  # update
                place = Place.by_id(place_data["id"])
                del(place_data["id"])
                place.update(**place_data)
                logging.info("updated place %s" % place.name)

            return JSONResponse({'place': place.serialize()})

        elif action == "remove":
            place = Place.by_id(place_data["id"])
            deleted = False
            if place:
                place.delete()
                deleted = True
                logging.info("deleted place %s" % place.name)

            return JSONResponse({'success': deleted})

    return HttpResponseBadRequest()