from ..mcapp import app
from ..decorators import crossdomain, apikey, jsonp
from flask import g, request, jsonify
import rethinkdb as r
from .. import args
from .. import dmutil
from .. import access
from .. import error
from loader.model import review


@app.route('/reviews/requested', methods=['GET'])
@apikey(shared=True)
@jsonp
def get_reviews_requested():
    requested_reviews = []
    user = access.get_user()
    selection = list(r.table('reviews')
                     .get_all(user, index='author').order_by('project').eq_join('project', r.table('projects'))
                     .run(g.conn, time_format='raw'))
    for item in selection:
        review = item['left']
        project = item['right']
        review['project_name'] = project['name']
        requested_reviews.append(review)
    return args.json_as_format_arg(requested_reviews)


@app.route('/reviews/to_conduct', methods=['GET'])
@apikey(shared=True)
@jsonp
def get_reviews_to_be_conducted():
    conduct_reviews = []
    user = access.get_user()
    selection = list(r.table('reviews')
                     .get_all(user, index='assigned_to').order_by('project').eq_join('project', r.table('projects'))
                     .run(g.conn, time_format='raw'))
    for item in selection:
        review = item['left']
        project = item['right']
        review['project_name'] = project['name']
        conduct_reviews.append(review)
    return args.json_as_format_arg(conduct_reviews)


@app.route('/reviews/<id>', methods=['GET'])
@apikey(shared=True)
@jsonp
def get_review(id):
    selection = r.table('reviews').get(id).run(g.conn, time_format='raw')
    return args.json_as_format_arg(selection)

@app.route('/reviews/<id>', methods=['DELETE'])
@apikey
@crossdomain(origin='*')
def delete_review(id):
    user = access.get_user()
    item = r.table('reviews').get(id).run(g.conn)
    if item is None:
        return error.not_found("No such review: %s" % id)
    if item['requested_to'] == user or item['requested_by'] == user:
        rv = r.table('reviews').get(id).delete().run(g.conn)
        return jsonify(rv)
    else:
        return error.not_authorized("User %s does not have access" % user)


@app.route('/reviews', methods=['POST'])
@apikey(shared=True)
@crossdomain(origin='*')
def add_review():
    j = request.get_json()
    assigned_to = dmutil.get_required('assigned_to', j)
    author = dmutil.get_required('author', j)
    r = review.Review(author, assigned_to)
    r.messages = dmutil.get_required('messages', j)
    r.items = dmutil.get_optional('items', j)
    r.title = dmutil.get_required('title', j)
    r.status = "open"
    r.project = dmutil.get_required('project', j)
    return  dmutil.insert_entry('reviews', r.__dict__)

@app.route('/reviews/<id>', methods=['PUT'])
@apikey(shared=True)
@jsonp
def update_review(id):
    j = request.get_json()
    messages = dmutil.get_optional('messages', j)
    status = dmutil.get_optional('status', j)
    items = dmutil.get_optional('items', j)
    if messages:
        rv = r.table('reviews').get(id).update({'messages': messages, 'modifiedtime': r.now()}).run(g.conn)
    if status:
        rv = r.table('reviews').get(id).update({'status': status, 'modifiedtime': r.now()}).run(g.conn)
    if items:
        rv = r.table('reviews').get(id).update({'items': items, 'modifiedtime': r.now()}).run(g.conn) 
    return jsonify(rv)

@@ -252,1222 +252,8 @@
 w%0A%0A%0A
-@app.route('/reviews/requested', methods=%5B'GET'%5D)%0A@apikey(shared=True)%0A@jsonp%0Adef get_reviews_requested():%0A    requested_reviews = %5B%5D%0A    user = access.get_user()%0A    selection = list(r.table('reviews')%0A                     .get_all(user, index='author').order_by('project').eq_join('project', r.table('projects'))%0A                     .run(g.conn, time_format='raw'))%0A    for item in selection:%0A        review = item%5B'left'%5D%0A        project = item%5B'right'%5D%0A        review%5B'project_name'%5D = project%5B'name'%5D%0A        requested_reviews.append(review)%0A    return args.json_as_format_arg(requested_reviews)%0A%0A%0A@app.route('/reviews/to_conduct', methods=%5B'GET'%5D)%0A@apikey(shared=True)%0A@jsonp%0Adef get_reviews_to_be_conducted():%0A    conduct_reviews = %5B%5D%0A    user = access.get_user()%0A    selection = list(r.table('reviews')%0A                     .get_all(user, index='assigned_to').order_by('project').eq_join('project', r.table('projects'))%0A                     .run(g.conn, time_format='raw'))%0A    for item in selection:%0A        review = item%5B'left'%5D%0A        project = item%5B'right'%5D%0A        review%5B'project_name'%5D = project%5B'name'%5D%0A        conduct_reviews.append(review)%0A    return args.json_as_format_arg(conduct_reviews)%0A%0A%0A
 @app
@@ -462,16 +462,17 @@
 ction)%0A%0A
+%0A
 @app.rou
@@ -1035,33 +1035,8 @@
 ue)%0A
-@crossdomain(origin='*')%0A
 def 
@@ -1448,17 +1448,16 @@
  return 
- 
 dmutil.i
@@ -1489,17 +1489,39 @@
 __dict__
-)
+, return_created=True)%0A
 %0A%0A@app.r
@@ -1842,16 +1842,29 @@
 update(%7B
+%0A            
 'message
@@ -1875,31 +1875,24 @@
 messages, 'm
-odified
 time': r.now
@@ -1885,32 +1885,41 @@
 'mtime': r.now()
+%0A        
 %7D).run(g.conn)%0A 
@@ -1980,16 +1980,29 @@
 update(%7B
+%0A            
 'status'
@@ -2010,26 +2010,31 @@
  status,
- 'modified
+%0A            'm
 time': r
@@ -2031,32 +2031,41 @@
 'mtime': r.now()
+%0A        
 %7D).run(g.conn)%0A 
@@ -2125,16 +2125,29 @@
 update(%7B
+%0A            
 'items':
@@ -2160,15 +2160,8 @@
 , 'm
-odified
 time
@@ -2170,16 +2170,25 @@
  r.now()
+%0A        
 %7D).run(g
@@ -2193,17 +2193,16 @@
 (g.conn)
- 
 %0A    ret

Remove unused calls. Have create return the object that was created.