#!/usr/local/bin/python
from flask import Flask, render_template, session, redirect, request, url_for
from models import Cart
from settings import SECRET_KEY, STORE_FILE
import json


app = Flask(__name__)
app.secret_key = SECRET_KEY
carts = Cart()

f = open(STORE_FILE)
api_key = f.readlines()[0].strip()
f.close()


# Base function, renders the template with the api key as input
"""Map for the home function for the GET method:
    I. Check if the username is in session
        1) If it is...
            i. grab the user from the database
                - u = users.find_one(username=session['username'])
            ii. render the template, and pass the user as an argument
                - render_template("index.html", ..., user=u)
        2) If it isn't
            i. render the template as normal
                - render_template("index.html", ...)
TODO: render the template with a list of trending food carts, or reviews"""
@app.route("/")
def home():
    return render_template('index.html', API_KEY=api_key)


# User page
"""Map for the user function for the GET method:
    I. Grab the user from the database
        - target_user = users.find_one(_id=uid)
    II. Check if a user exists (use if target_user is not None) with the given id
        1) If it does...
            i. Check if the username is in session
                - if it is...
                    * grab the user from the database
                        - u = users.find_one(username=session['username'])
                    * render the template with the user and the target_user to the function 
                        - render_template("index.html", ..., target_user=target_user, u=u)
                - if it isn't
                    * render the template with the target_user
                        - render_template("index.html", ..., target_user=target_user, u=None)
TODO: Implement POST method"""
@app.route("/users/<id>")
def user(uid):
    pass


# Cart page
"""Map for the cart function for the GET method:
    I. Grab the cart from the database
        - target_cart = carts.find_one(_id=cid)
    II. Check if a cart exists (use if target_cart is not None) with the given id
        1) If it does...
            i. Check if the username is in session
                - if it is...
                    * grab the user from the database
                        - u = users.find_one(username=session['username'])
                    * render the template with the user and the cart to the function 
                        - render_template("index.html", ..., target_cart=target_cart, u=u)
                - if it isn't
                    * render the template with the target_cart
                        - render_template("index.html", ..., target_cart=target_cart, u=None)
TODO: Implement POST method"""
@app.route("/carts/<cid>")
def cart(cid):
    pass


# Tag page
"""Map for the tag function
    I. Grab the tag from the database
        - tag = tags.find_one(label=label)
    II. Check if the tag exists (use if target is not None)
        i. Check if the username is in session
            - if it is...
                * grab the user from the database
                    - u = users.find_one(username=session['username'])
                * render the template with the user and the tag passed to the function 
                    - render_template("index.html", ..., tag=tag, u=u)
            - if it isn't
                * render the template with the tag
                    - render_template("index.html", ..., tag=tag, u=None)
TODO: Implement POST method"""
@app.route("/carts/<label>")
def tag(label):
    pass


# Serves the data from the backend to the frontend js using json module
@app.route('/_data')
def serve_data():
    rargs = request.args.copy()
    kwargs = {}
    for k in rargs.keys():
        kwargs[k] = rargs[k]

    objs = carts.find(**kwargs)
    results = [o._obj for o in objs]

    for r in results:
        r.pop('date', None)
        r['_id'] = str(r['_id'])

    data = {'results': results}
    return json.dumps(data)


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

@@ -1026,12 +1026,14 @@
 %0A%0A# 
-User
+Review
  pag
@@ -1091,36 +1091,38 @@
     I. Grab the 
-user
+review
  from the databa
@@ -1145,19 +1145,20 @@
 get_
-user = user
+rev = review
 s.fi
@@ -1192,20 +1192,22 @@
 ck if a 
-user
+review
  exists 
@@ -1221,20 +1221,22 @@
  target_
-user
+review
  is not 
@@ -1565,20 +1565,22 @@
  target_
-user
+review
  to the 
@@ -1653,36 +1653,38 @@
 ..., target_
-user
+review
 =target_
 user, u=u)%0A 
@@ -1667,28 +1667,30 @@
 view=target_
-user
+review
 , u=u)%0A     
@@ -1772,20 +1772,22 @@
  target_
-user
+review
 %0A       
@@ -1851,20 +1851,22 @@
 get_
-user
+review
 =target_
 user
@@ -1861,20 +1861,22 @@
 =target_
-user
+review
 , u=None
@@ -1921,20 +1921,22 @@
 route(%22/
-user
+review
 s/%3Cid%3E%22)
@@ -1944,14 +1944,16 @@
 def 
-user(u
+review(r
 id):

Remove unecessary user page and update comments, add review function