import datetime

from flask import jsonify, request
from server import app
from .penndata import studyspaces


@app.route('/studyspaces/availability/<int:building>', methods=['GET'])
def parse_times(building):
    """
    Returns JSON containing all rooms for a given building.

    Usage:
        /studyspaces/availability/<building> gives all rooms for the next 24 hours
        /studyspaces/availability/<building>?start=2018-25-01T11:00:00-0500 gives all rooms from start to 24 hours later
        /studyspaces/availability/<building>?start=...&end=... gives all rooms between the two times
    """
    date = datetime.datetime.now()
    try:
        start = request.args.get('start')
        if start is not None:
            start = datetime.datetime.strptime(start, "%Y-%m-%dT%H:%M:%S%z")
        else:
            start = date
        end = request.args.get('end')
        if end is not None:
            end = datetime.datetime.strptime(end, "%Y-%m-%dT%H:%M:%S%z")
        else:
            end = start + datetime.timedelta(days=1)
    except ValueError:
        return jsonify({"error": "Incorrect date format!"})

    return jsonify({
        "location_id": building,
        "date": start.strftime("%Y-%m-%d"),
        "rooms": studyspaces.get_rooms(building, start, end)
    })


@app.route('/studyspaces/locations', methods=['GET'])
def display_id_pairs():
    """
    Returns JSON containing a list of buildings with their ids.
    """
    return jsonify({"locations": studyspaces.get_buildings()})

@@ -102,16 +102,47 @@
 yspaces%0A
+from .base import cached_route%0A
 %0A%0A@app.r
@@ -1479,31 +1479,47 @@
 %22%22%22%0A    
-return jsonify(
+def get_data():%0A        return 
 %7B%22locati
@@ -1552,10 +1552,97 @@
 dings()%7D
+%0A%0A    return cached_route('studyspaces:locations', datetime.timedelta(days=1), get_data
 )%0A

add caching for locations endpoint