Beispiel #1
0
def search_handler(response):
    context = {}
    entry = response.get_field("search")
    lat_field = response.get_field("latitude")
    long_field = response.get_field("longitude")
    if lat_field and long_field:
        lat = float(lat_field)
        long = float(long_field)
        context["user_loc"] = [lat, long]
    else:
        context["user_loc"] = None
    tags = response.get_field("tags")
    if entry:
        entry = entry.strip()
    context["query"] = entry
    context["tags"] = tags
    if tags == "":
        search_results = Location.search_name(entry)
    elif entry == "":
        search_results = Location.search_tag(tags)
    else:
        search_results = Location.search(tags, entry)
    if lat_field and long_field:
        search_results.sort(key=lambda x: x.distance_from(lat, long))
    context["results"] = search_results
    render_page("searchresult.html", response, context)
Beispiel #2
0
def location_editor(response, id):
    context = {"error": None}
    location = Location.find_id(id)
    orig_name = location.name
    context["location"] = location
    if location is None:
        context["error"] = "Place does not exist"
        render_page("edit_location.html", response, context)
        return

    try:
        lat = float(response.get_field("lat"))
        lon = float(response.get_field("long"))
    except ValueError:
        context["error"] = "Invalid latitude or longitude"
        render_page("create_location.html", response, context)
        return
    name = response.get_field("name")
    if orig_name != name:
        if Location.find_name(name):
            context["error"] = "Place already exists"
            render_page("edit_location.html", response, context)
            return None

    description = response.get_field("description")
    address = response.get_field("address")
    Location.change_location(id, name, description, location.picture, address, lat, lon)
    response.redirect("/location/" + id)
Beispiel #3
0
def location_editor(response, id):
    # file_input = response.get_file('picture')
    # filename_hash = hashlib.sha1(file_input[2]).hexdigest()

    # file_output = open('./static/place-images/{}'.format(filename_hash), 'wb')
    # file_output.write(file_input[2])
    # file_output.close()

    context = {"error": None}
    location = Location.find_id(id)
    orig_name = location.name
    context["location"] = location
    if location is None:
        context["error"] = "Place does not exist"
        render_page("edit_location.html", response, context)
        return
    name = response.get_field("name")
    if orig_name != name:
        if Location.find_name(name):
            context["error"] = "Place already exists"
            render_page("edit_location.html", response, context)
            return None

    description = response.get_field("description")
    address = response.get_field("address")
    username = get_login(response)
    user = User.find(username)
    Location.change_location(id, name, description, location.picture, address, location.latitude, location.longitude)
    response.redirect("/location/" + id)
Beispiel #4
0
def edit_handler(response, location_id):
    context = {"error": None}
    location = Location.find_id(location_id)
    if location:
        context["location"] = location
        render_page("edit_location.html", response, context)
    else:
        error_handler(response)
Beispiel #5
0
def profile_handler(response, username=None):
    if username is None:
        user_object = User.find(get_login(response))
    else:
        user_object = User.find(username)
        if user_object is None:
            error_handler(response)
            return
    context = {}
    user_locations = Location.find_user_locations(user_object.id)
    context["results"] = user_locations
    context["user_object"] = user_object
    render_page("account.html", response, context)
Beispiel #6
0
def location_creator(response):
    file_input = response.get_file("picture")
    filename_hash = hashlib.sha1(file_input[2]).hexdigest()

    file_output = open("./static/place-images/{}".format(filename_hash), "wb")
    file_output.write(file_input[2])
    file_output.close()

    context = {"error": None}

    name = response.get_field("name")
    description = response.get_field("description")
    address = response.get_field("address")
    username = get_login(response)
    user = User.find(username)

    try:
        lat = float(response.get_field("lat"))
        long = float(response.get_field("long"))
    except ValueError:
        context["error"] = "Invalid latitude or longitude"
        render_page("create_location.html", response, context)
        return
    if Location.find_name(name):
        context["error"] = "Place already exists"
        render_page("create_location.html", response, context)
    else:
        Location(name, description, filename_hash, user.id, address, long, lat).create()
        response.redirect("/location/{}".format(Location.find_name(name).id))

        tags = response.get_field("tags").split(",")
        if tags == [""]:
            tags = []
        for tag in tags:
            Tag(tag, Location.find_name(name).id).create()
    return
Beispiel #7
0
def location_handler(response, id):
    logged_in = get_login(response)
    context = {}
    user_object = User.find(get_login(response))
    location = Location.find_id(id)
    loc_tags = Tag.find_from_place(location.id)
    if logged_in:
        stars = location.get_user_rating(user_object.id)
        context["user_rating"] = stars
    if location:
        context["location"] = location
        context["loc_tags"] = loc_tags
        context["comments"] = Comment.find_place(location.id)
        render_page("location.html", response, context)
    else:
        error_handler(response)