Example #1
0
def build_places(csv_path):
    import csv
    from geo.models import Place
    from common.models import Country, City
    from django.conf import settings

    israel = Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE)
    ta = City.objects.get(name="תל אביב יפו")
    reader = csv.reader(open(csv_path), delimiter=",")
    for row in reader:
        if len(row) == 4:
            place = Place()
            place.lon = row[0]
            place.lat = row[1]
            place.name = row[2]
            place.description = row[3]
            place.country = israel
            place.city = ta
            place.save()
            logging.info("new place created: %s" % row)
        else:
            logging.info("skipping row: %s" % row)
Example #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()