from mcapp import app
from decorators import apikey
from flask import request, g
import rethinkdb as r
import resp


@app.route('/processes/<process_id>', methods=['PUT'])
@apikey
def update_process(process_id):
    j = request.get_json()
    # Update Name, What, Why
    r.table('processes').get(process_id).update({'name': j['name'],
        'what': j['what'], 'why': j['why']})\
        .run(g.conn, time_format="raw")
    # Update setup
    if 'setup' in j:
        for prop in j['setup']:
            update_property('setupproperties', prop)
    if 'samples' in j:
        for sample in j['samples']:
            if 'properties' in sample:
                for prop in sample['properties']:
                    for measure in prop['measurements']:
                        update_property('measurements', measure)
    return resp.to_json_id(process_id)


def update_property(table_name, prop):
    rv = r.table(table_name).get(prop['id'])\
        .update({'value': prop['value'], 'unit': prop['unit']})\
        .run(g.conn, time_format="raw")
    return rv


def get_process_information(project_id):
    processes = list(r.table('project2process')
                     .get_all(project_id, index="project_id")
                     .eq_join("process_id", r.table("processes")).zip()
                     .order_by(r.desc('birthtime'))
                     .merge(lambda process:
                            {
                                'setup': r.table('process2setup')
                                .get_all(process['process_id'],
                                         index='process_id')
                                .eq_join("setup_id", r.table("setups")).zip()
                                .merge(lambda setup: {
                                    'setupproperties': r.table('setupproperties')
                                       .get_all(setup['setup_id'], index="setup_id")
                                       .coerce_to('array')
                                })
                                .coerce_to('array'),

                                'samples': r.table('process2sample')
                                .get_all(process['process_id'],
                                         index='process_id')
                                .eq_join('sample_id', r.table("samples"))
                                .without({"right": {"_type": True}})
                                .zip()
                                .coerce_to('array'),

                                'files_used': r.table('process2file')
                                .get_all(process['process_id'], index='process_id')
                                .filter({'direction': "in"})
                                .eq_join('datafile_id', r.table('datafiles'))
                                .zip()
                                .coerce_to('array'),

                                'files_produced': r.table('process2file')
                                .get_all(process['process_id'], index='process_id').filter({'direction': "out"})
                                .eq_join('datafile_id', r.table('datafiles'))
                                .zip()
                                .coerce_to('array')
                            })
                     .run(g.conn, time_format="raw"))
    return processes

@@ -44,17 +44,24 @@
 t apikey
+, jsonp
 %0A
-
 from fla
@@ -117,16 +117,578 @@
  resp%0A%0A%0A
+@app.route('/processes/project/%3Cproject_id%3E', methods=%5B'GET'%5D)%0A@apikey%0A@jsonp%0Adef get_processes_for_project(project_id):%0A    processes = get_process_information(project_id)%0A    return resp.to_json(processes)%0A%0A%0Adef get_process_information(project_id):%0A    processes = list(r.table('project2process')%0A                     .get_all(project_id, index=%22project_id%22)%0A                     .eq_join(%22process_id%22, r.table(%22processes%22)).zip()%0A                     .order_by(r.desc('birthtime'))%0A                     .run(g.conn, time_format=%22raw%22))%0A    return processes%0A%0A%0A
 @app.rou
@@ -1606,24 +1606,24 @@
 rmat=%22raw%22)%0A
+
     return r
@@ -1628,2272 +1628,4 @@
  rv%0A
-%0A%0Adef get_process_information(project_id):%0A    processes = list(r.table('project2process')%0A                     .get_all(project_id, index=%22project_id%22)%0A                     .eq_join(%22process_id%22, r.table(%22processes%22)).zip()%0A                     .order_by(r.desc('birthtime'))%0A                     .merge(lambda process:%0A                            %7B%0A                                'setup': r.table('process2setup')%0A                                .get_all(process%5B'process_id'%5D,%0A                                         index='process_id')%0A                                .eq_join(%22setup_id%22, r.table(%22setups%22)).zip()%0A                                .merge(lambda setup: %7B%0A                                    'setupproperties': r.table('setupproperties')%0A                                       .get_all(setup%5B'setup_id'%5D, index=%22setup_id%22)%0A                                       .coerce_to('array')%0A                                %7D)%0A                                .coerce_to('array'),%0A%0A                                'samples': r.table('process2sample')%0A                                .get_all(process%5B'process_id'%5D,%0A                                         index='process_id')%0A                                .eq_join('sample_id', r.table(%22samples%22))%0A                                .without(%7B%22right%22: %7B%22_type%22: True%7D%7D)%0A                                .zip()%0A                                .coerce_to('array'),%0A%0A                                'files_used': r.table('process2file')%0A                                .get_all(process%5B'process_id'%5D, index='process_id')%0A                                .filter(%7B'direction': %22in%22%7D)%0A                                .eq_join('datafile_id', r.table('datafiles'))%0A                                .zip()%0A                                .coerce_to('array'),%0A%0A                                'files_produced': r.table('process2file')%0A                                .get_all(process%5B'process_id'%5D, index='process_id').filter(%7B'direction': %22out%22%7D)%0A                                .eq_join('datafile_id', r.table('datafiles'))%0A                                .zip()%0A                                .coerce_to('array')%0A                            %7D)%0A                     .run(g.conn, time_format=%22raw%22))%0A    return processes%0A

Remove call to get the projects drafts. Drafts no longer exist.