<s> from __future__ import unicode_literals <EOL> import frappe <EOL> out = frappe . response <EOL> from frappe . utils import cint <EOL> import frappe . defaults <EOL> def get_sql_tables ( q ) : <EOL> if q . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> tl = q . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) <EOL> elif q . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> tl = q . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> tl = q . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) <EOL> return [ t . strip ( ) . strip ( '<STR_LIT>' ) [ <NUM_LIT:3> : ] for t in tl ] <EOL> def get_parent_dt ( dt ) : <EOL> pdt = '<STR_LIT>' <EOL> if frappe . db . sql ( '<STR_LIT>' , dt ) : <EOL> import frappe . model . meta <EOL> return frappe . model . meta . get_parent_dt ( dt ) <EOL> return pdt <EOL> def get_sql_meta ( tl ) : <EOL> std_columns = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:100>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:100>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:100>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:100>' ) <EOL> } <EOL> meta = { } <EOL> for dt in tl : <EOL> meta [ dt ] = std_columns . copy ( ) <EOL> pdt = get_parent_dt ( dt ) <EOL> if pdt : <EOL> meta [ dt ] [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , pdt , '<STR_LIT>' ) <EOL> res = frappe . db . sql ( "<STR_LIT>" , dt ) <EOL> for r in res : <EOL> if r [ <NUM_LIT:0> ] : <EOL> meta [ dt ] [ r [ <NUM_LIT:0> ] ] = ( r [ <NUM_LIT:1> ] , r [ <NUM_LIT:2> ] , r [ <NUM_LIT:3> ] , r [ <NUM_LIT:4> ] ) ; <EOL> meta [ dt ] [ '<STR_LIT:name>' ] = ( '<STR_LIT>' , '<STR_LIT>' , dt , '<STR_LIT>' ) <EOL> return meta <EOL> def add_match_conditions ( q , tl ) : <EOL> from frappe . desk . reportview import build_match_conditions <EOL> sl = [ ] <EOL> for dt in tl : <EOL> s = build_match_conditions ( dt ) <EOL> if s : <EOL> sl . append ( s ) <EOL> if sl : <EOL> condition_st = q . find ( '<STR_LIT>' ) != - <NUM_LIT:1> and '<STR_LIT>' or '<STR_LIT>' <EOL> condition_end = q . find ( '<STR_LIT>' ) != - <NUM_LIT:1> and '<STR_LIT>' or '<STR_LIT>' <EOL> condition_end = q . find ( '<STR_LIT>' ) != - <NUM_LIT:1> and '<STR_LIT>' or condition_end <EOL> if q . find ( '<STR_LIT>' ) != - <NUM_LIT:1> or q . find ( '<STR_LIT>' ) != - <NUM_LIT:1> or q . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> q = q . split ( condition_end ) <EOL> q = q [ <NUM_LIT:0> ] + condition_st + '<STR_LIT:(>' + '<STR_LIT>' . join ( sl ) + '<STR_LIT>' + condition_end + q [ <NUM_LIT:1> ] <EOL> else : <EOL> q = q + condition_st + '<STR_LIT:(>' + '<STR_LIT>' . join ( sl ) + '<STR_LIT:)>' <EOL> return q <EOL> def guess_type ( m ) : <EOL> """<STR_LIT>""" <EOL> import MySQLdb <EOL> if m in MySQLdb . NUMBER : <EOL> return '<STR_LIT>' <EOL> elif m in MySQLdb . DATE : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def build_description_simple ( ) : <EOL> colnames , coltypes , coloptions , colwidths = [ ] , [ ] , [ ] , [ ] <EOL> for m in frappe . db . get_description ( ) : <EOL> colnames . append ( m [ <NUM_LIT:0> ] ) <EOL> coltypes . append ( guess_type [ m [ <NUM_LIT:0> ] ] ) <EOL> coloptions . append ( '<STR_LIT>' ) <EOL> colwidths . append ( '<STR_LIT:100>' ) <EOL> return colnames , coltypes , coloptions , colwidths <EOL> def build_description_standard ( meta , tl ) : <EOL> desc = frappe . db . get_description ( ) <EOL> colnames , coltypes , coloptions , colwidths = [ ] , [ ] , [ ] , [ ] <EOL> merged_meta = { } <EOL> for d in meta : <EOL> merged_meta . update ( meta [ d ] ) <EOL> for f in desc : <EOL> fn , dt = f [ <NUM_LIT:0> ] , '<STR_LIT>' <EOL> if '<STR_LIT:.>' in fn : <EOL> dt , fn = fn . split ( '<STR_LIT:.>' ) <EOL> if ( not dt ) and merged_meta . get ( fn ) : <EOL> desc = merged_meta [ fn ] <EOL> colnames . append ( desc [ <NUM_LIT:0> ] or fn ) <EOL> coltypes . append ( desc [ <NUM_LIT:1> ] or '<STR_LIT>' ) <EOL> coloptions . append ( desc [ <NUM_LIT:2> ] or '<STR_LIT>' ) <EOL> colwidths . append ( desc [ <NUM_LIT:3> ] or '<STR_LIT:100>' ) <EOL> elif meta . get ( dt , { } ) . has_key ( fn ) : <EOL> desc = meta [ dt ] [ fn ] <EOL> colnames . append ( desc [ <NUM_LIT:0> ] or fn ) <EOL> coltypes . append ( desc [ <NUM_LIT:1> ] or '<STR_LIT>' ) <EOL> coloptions . append ( desc [ <NUM_LIT:2> ] or '<STR_LIT>' ) <EOL> colwidths . append ( desc [ <NUM_LIT:3> ] or '<STR_LIT:100>' ) <EOL> else : <EOL> colnames . append ( fn ) <EOL> coltypes . append ( guess_type ( f [ <NUM_LIT:1> ] ) ) <EOL> coloptions . append ( '<STR_LIT>' ) <EOL> colwidths . append ( '<STR_LIT:100>' ) <EOL> return colnames , coltypes , coloptions , colwidths <EOL> @ frappe . whitelist ( ) <EOL> def runquery ( q = '<STR_LIT>' , ret = <NUM_LIT:0> , from_export = <NUM_LIT:0> ) : <EOL> import frappe . utils <EOL> formatted = cint ( frappe . form_dict . get ( '<STR_LIT>' ) ) <EOL> if frappe . form_dict . get ( '<STR_LIT>' ) or frappe . form_dict . get ( '<STR_LIT>' ) : <EOL> if not q : q = frappe . form_dict . get ( '<STR_LIT>' ) or frappe . form_dict . get ( '<STR_LIT>' ) <EOL> if q . split ( ) [ <NUM_LIT:0> ] . lower ( ) != '<STR_LIT>' : <EOL> raise Exception , '<STR_LIT>' <EOL> as_dict = cint ( frappe . form_dict . get ( '<STR_LIT>' ) ) <EOL> res = frappe . db . sql ( q , as_dict = as_dict , as_list = not as_dict , formatted = formatted ) <EOL> colnames , coltypes , coloptions , colwidths = [ ] , [ ] , [ ] , [ ] <EOL> else : <EOL> if not q : q = frappe . form_dict . get ( '<STR_LIT>' ) <EOL> tl = get_sql_tables ( q ) <EOL> meta = get_sql_meta ( tl ) <EOL> q = add_match_conditions ( q , tl ) <EOL> q = q . replace ( '<STR_LIT>' , frappe . session . user ) <EOL> q = q . replace ( '<STR_LIT>' , frappe . utils . nowdate ( ) ) <EOL> res = frappe . db . sql ( q , as_list = <NUM_LIT:1> , formatted = formatted ) <EOL> colnames , coltypes , coloptions , colwidths = build_description_standard ( meta , tl ) <EOL> style , header_html , footer_html , page_template = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> out [ '<STR_LIT>' ] = colnames <EOL> out [ '<STR_LIT>' ] = coltypes <EOL> out [ '<STR_LIT>' ] = coloptions <EOL> out [ '<STR_LIT>' ] = colwidths <EOL> out [ '<STR_LIT>' ] = header_html <EOL> out [ '<STR_LIT>' ] = footer_html <EOL> out [ '<STR_LIT>' ] = page_template <EOL> if style : <EOL> out [ '<STR_LIT>' ] = style <EOL> if ret == <NUM_LIT:1> : <EOL> return res <EOL> out [ '<STR_LIT>' ] = res <EOL> qm = frappe . form_dict . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> if qm and qm . strip ( ) : <EOL> if qm . split ( ) [ <NUM_LIT:0> ] . lower ( ) != '<STR_LIT>' : <EOL> raise Exception , '<STR_LIT>' <EOL> if not frappe . form_dict . get ( '<STR_LIT>' ) : <EOL> qm = add_match_conditions ( qm , tl ) <EOL> out [ '<STR_LIT>' ] = frappe . utils . cint ( frappe . db . sql ( qm ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> @ frappe . whitelist ( ) <EOL> def runquery_csv ( ) : <EOL> global out <EOL> res = runquery ( from_export = <NUM_LIT:1> ) <EOL> q = frappe . form_dict . get ( '<STR_LIT>' ) <EOL> rep_name = frappe . form_dict . get ( '<STR_LIT>' ) <EOL> if not frappe . form_dict . get ( '<STR_LIT>' ) : <EOL> if not rep_name : <EOL> rep_name = get_sql_tables ( q ) [ <NUM_LIT:0> ] <EOL> if not rep_name : rep_name = '<STR_LIT>' <EOL> heads = [ ] <EOL> rows = [ [ rep_name ] , out [ '<STR_LIT>' ] ] + out [ '<STR_LIT>' ] <EOL> from cStringIO import StringIO <EOL> import csv <EOL> f = StringIO ( ) <EOL> writer = csv . writer ( f ) <EOL> for r in rows : <EOL> writer . writerow ( map ( lambda v : isinstance ( v , unicode ) and v . encode ( '<STR_LIT:utf-8>' ) or v , r ) ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> out [ '<STR_LIT:result>' ] = unicode ( f . read ( ) , '<STR_LIT:utf-8>' ) <EOL> out [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> out [ '<STR_LIT>' ] = rep_name <EOL> def add_limit_to_query ( query , args ) : <EOL> """<STR_LIT>""" <EOL> if args . get ( '<STR_LIT>' ) : <EOL> query += """<STR_LIT>""" <EOL> import frappe . utils <EOL> args [ '<STR_LIT>' ] = frappe . utils . cint ( args . get ( '<STR_LIT>' ) ) <EOL> args [ '<STR_LIT>' ] = frappe . utils . cint ( args . get ( '<STR_LIT>' ) ) <EOL> return query , args </s>
