<s> """<STR_LIT>""" <EOL> from flask import request , redirect , jsonify , url_for , render_template <EOL> from flask_restful import fields , abort <EOL> from sqlalchemy import exc , select , func , desc <EOL> import voluptuous <EOL> from voluptuous import Schema , Required , Any <EOL> from common . helpers import tables , find <EOL> from cycledash import db <EOL> from cycledash . helpers import get_id_where , abort_if_none_for <EOL> from cycledash . validations import Doc <EOL> from . import bams , tasks , Resource , marshal_with , validate_with <EOL> CreateProject = Schema ( { <EOL> Required ( '<STR_LIT:name>' ) : unicode , <EOL> '<STR_LIT>' : unicode <EOL> } ) <EOL> UpdateProject = Schema ( { <EOL> '<STR_LIT:name>' : unicode , <EOL> '<STR_LIT>' : unicode <EOL> } ) <EOL> ProjectFields = Schema ( { <EOL> Doc ( '<STR_LIT:id>' , docstring = '<STR_LIT>' ) : long , <EOL> Doc ( '<STR_LIT:name>' , docstring = '<STR_LIT>' ) : Any ( basestring , None ) , <EOL> Doc ( '<STR_LIT>' , docstring = '<STR_LIT>' ) : Any ( basestring , None ) <EOL> } , extra = voluptuous . REMOVE_EXTRA ) <EOL> class ProjectList ( Resource ) : <EOL> require_auth = True <EOL> @ marshal_with ( ProjectFields , envelope = '<STR_LIT>' ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> with tables ( db . engine , '<STR_LIT>' ) as ( con , projects ) : <EOL> q = select ( projects . c ) . order_by ( desc ( projects . c . id ) ) <EOL> return [ dict ( r ) for r in con . execute ( q ) . fetchall ( ) ] <EOL> @ validate_with ( CreateProject ) <EOL> @ marshal_with ( ProjectFields ) <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> with tables ( db . engine , '<STR_LIT>' ) as ( con , projects ) : <EOL> try : <EOL> result = projects . insert ( <EOL> request . validated_body <EOL> ) . returning ( * projects . c ) . execute ( ) <EOL> except exc . IntegrityError as e : <EOL> abort ( <NUM_LIT> , message = '<STR_LIT>' , errors = [ str ( e ) ] ) <EOL> return dict ( result . fetchone ( ) ) , <NUM_LIT> <EOL> class Project ( Resource ) : <EOL> require_auth = True <EOL> @ marshal_with ( ProjectFields ) <EOL> def get ( self , project_id ) : <EOL> """<STR_LIT>""" <EOL> with tables ( db . engine , '<STR_LIT>' ) as ( con , projects ) : <EOL> q = select ( projects . c ) . where ( projects . c . id == project_id ) <EOL> return dict ( _abort_if_none ( con . execute ( q ) . fetchone ( ) , project_id ) ) <EOL> @ validate_with ( UpdateProject ) <EOL> @ marshal_with ( ProjectFields ) <EOL> def put ( self , project_id ) : <EOL> """<STR_LIT>""" <EOL> with tables ( db . engine , '<STR_LIT>' ) as ( con , projects ) : <EOL> q = projects . update ( projects . c . id == project_id ) . values ( <EOL> ** request . validated_body <EOL> ) . returning ( * projects . c ) <EOL> return dict ( _abort_if_none ( q . execute ( ) . fetchone ( ) , project_id ) ) <EOL> @ marshal_with ( ProjectFields ) <EOL> def delete ( self , project_id ) : <EOL> """<STR_LIT>""" <EOL> with tables ( db . engine , '<STR_LIT>' ) as ( con , projects ) : <EOL> q = projects . delete ( <EOL> projects . c . id == project_id ) . returning ( * projects . c ) <EOL> return dict ( _abort_if_none ( q . execute ( ) . fetchone ( ) , project_id ) ) <EOL> _abort_if_none = abort_if_none_for ( '<STR_LIT>' ) <EOL> def get_projects_tree ( ) : <EOL> """<STR_LIT>""" <EOL> with tables ( db . engine , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) as ( con , vcfs , user_comments , bams_table , projects ) : <EOL> joined = ( vcfs <EOL> . outerjoin ( user_comments , vcfs . c . id == user_comments . c . vcf_id ) ) <EOL> num_comments = func . count ( user_comments . c . vcf_id ) . label ( '<STR_LIT>' ) <EOL> q = ( select ( vcfs . c + [ num_comments ] ) <EOL> . select_from ( joined ) <EOL> . group_by ( vcfs . c . id ) <EOL> . order_by ( desc ( vcfs . c . id ) ) ) <EOL> vcfs = [ dict ( v ) for v in con . execute ( q ) . fetchall ( ) ] <EOL> q = select ( bams_table . c ) <EOL> all_bams = [ dict ( b ) for b in con . execute ( q ) . fetchall ( ) ] <EOL> q = select ( projects . c ) <EOL> projects = [ dict ( b ) for b in con . execute ( q ) . fetchall ( ) ] <EOL> bams . attach_bams_to_vcfs ( vcfs ) <EOL> for vcf in vcfs : <EOL> project_id = vcf . get ( '<STR_LIT>' ) <EOL> vcf [ '<STR_LIT>' ] = dict ( find ( projects , <EOL> lambda x : x . get ( '<STR_LIT:id>' ) == project_id ) or { } ) <EOL> _join_task_states ( vcfs ) <EOL> for project in projects : <EOL> project_id = project [ '<STR_LIT:id>' ] <EOL> project_bams = [ bam for bam in all_bams <EOL> if bam . get ( '<STR_LIT>' ) == project_id ] <EOL> project_vcfs = [ vcf for vcf in vcfs <EOL> if vcf . get ( '<STR_LIT>' ) == project_id ] <EOL> project [ '<STR_LIT>' ] = project_bams <EOL> project [ '<STR_LIT>' ] = project_vcfs <EOL> return projects <EOL> def _join_task_states ( vcfs ) : <EOL> """<STR_LIT>""" <EOL> ts = tasks . all_non_success_tasks ( ) <EOL> for vcf in vcfs : <EOL> vcf [ '<STR_LIT>' ] = ts . get ( vcf [ '<STR_LIT:id>' ] , [ ] ) <EOL> def set_and_verify_project_id_on ( data ) : <EOL> project_name = data . get ( '<STR_LIT>' ) <EOL> if project_name : <EOL> project_id = get_id_where ( '<STR_LIT>' , db , name = project_name ) <EOL> data [ '<STR_LIT>' ] = project_id <EOL> del data [ '<STR_LIT>' ] <EOL> if project_id is None : <EOL> raise voluptuous . Invalid ( '<STR_LIT>' . format ( project_name ) ) <EOL> else : <EOL> project_id = data [ '<STR_LIT>' ] <EOL> if get_id_where ( '<STR_LIT>' , db , id = project_id ) is None : <EOL> raise voluptuous . Invalid ( '<STR_LIT>' . format ( project_id ) ) </s>
