2017-03-01 7 views
0

ユーザーがカスタムクエリと送信ボタンを作成するためのフィールドがあります。私がいる問題はWeb2pyの文字列処理の例外.AttributeError

のpythonファイル...私はSQLFORM.grid()でその文字列を使用できないことです

def reports(): 

BlankString = "" 
Report0=BlankString 
Report1="some category" 
Report2="some category" 
Report3="some category" 
Report4="some category" 
Report5="some category" 

myQuery="" #step1 - create a query to return results 

results = SQLFORM.factory(buttons=[]) 

form = SQLFORM.factory(
    Field('report_type', label='Report Type', requires=IS_IN_SET([Report0, Report1,Report2, Report3, Report4, Report5])), 
    Field('start_date', 'datetime', label='Start Date'), 
    Field('end_date', 'datetime', label='End Date'), 
    submit_button = T('Generate Report')) 

userQueryForm = SQLFORM.factory(
    Field('userQuery', label=''), 
    submit_button = T('Generate Report') 
    ) 


if userQueryForm.process(keepvalues=True).accepted: 
    myQuery = userQueryForm.vars.userQuery 
    if myQuery == BlankString: 
     return dict(results=results, form=form, userQueryForm=userQueryForm) 

if form.process(keepvalues=True).accepted: 
    atype = form.vars.report_type 

    if atype == BlankString: 
     return dict(results=results, form=form, userQueryForm=userQueryForm) 

    if atype==Report1: 
     myQuery=(db.auth_user) 
    elif atype==Report2: 
     myQuery=(db.auth_group) 


if myQuery != BlankString: 
    results=SQLFORM.grid(myQuery, 
         csv=True, 
         editable=False, 
         create=False, 
         details=False, 
         deletable=False, 
         searchable=False 
         ) 

return dict(results=results, form=form, userQueryForm=userQueryForm) 

HTMLファイル

{{left_sidebar_enabled=True}} 
{{extend 'layout.html'}} 

<style> 
    .container { 
     width:100%; 
     border:1px solid #d3d3d3; 
    } 
    .container div { 
     width:100%; 
    } 
    .container .header { 
     padding: 2px; 
     cursor: pointer; 
     font-weight: bold; 
    } 
    .container .content { 
     display: none; 
     padding : 5px; 
    } 
</style> 

<h2>Reports</h2> 

<p>Please select a report template or choose to write your own.</p> 


<div class="container"> 
    <div class="header" onclick="StdRptClick()"> 
     <span>Standard report</span> 
    </div> 
<div class="content" id="StdRptID"> 
    {{=form}} 
</div> 
<div class="header" onclick="CstmRptClick()"> 
    <span>Advanced report</span> 
</div> 
<div class="content" id="CstmRptID"> 
    <form method="post" action=""> 
     <table> 
      <tr> 
       <td> 

        <p> 
         Enter your desired query here. 
        </p> 
        {{=userQueryForm}} 
       </td> 
      </tr> 
     </table> 
    </form> 
</div> 
</div> 
    {{option = form.vars.report_type}} 
    {{option2 = form.vars.userQuery}} 
    <h3>{{=option2}}</h3> 
    <script type="text/javascript"> 

    function RequestReport() 
    { 
     if($('#CustomQueryID').val() == '') 
     { 
      alert("Query can not be blank"); 
      return; 
     } 
     else 
     { 
      alert("something"); 
     } 
    } 

    function StdRptClick() 
    { 
     if($('#no_table_report_type').val() == '') 
     { 
       $('#no_table_start_date__row').hide(); 
       $('#no_table_end_date__row').hide(); 
     } 

     $('#CstmRptID').slideUp(); 
     $('#StdRptID').slideToggle(); 

    } 

    function CstmRptClick() 
    { 
     $('#StdRptID').slideUp(); 
     $('#CstmRptID').slideToggle(); 
    } 

    $('#no_table_report_type__row').on('change', 
     function() 
     { 
      var selText = ($('#no_table_report_type').val()); 

      if(selText == '') 
      { 
       $('#no_table_start_date__row').hide(); 
       $('#no_table_end_date__row').hide(); 
      } 
      else 
      { 

       $('#no_table_start_date__row').show(); 
       $('#no_table_end_date__row').show(); 
      } 
     }); 

これまでに質問をuserQueryに入れて送信ボタンを押すと私は

「 『はstr』はオブジェクトが属性 『_db』を持っていないが、」どのように私は、クエリユーザが入力したを使用することができますというエラーを取得しますか?

答えて

1

SQLFORM.gridの最初の引数は、DAL Table objectまたはQuery objectである必要があります。 SQL injection攻撃を開始する可能性があるため、ユーザーが任意のクエリを入力できるようにすることも慎重にする必要があります。

ユーザーがカスタムクエリを作成できるようにするには、より制約の厳しいインターフェイスを提供し、入力を適切なDAL Queryに変換する必要があります。 1つのオプションは、ユーザの入力を解析するためにdb.smart_queryを使用することです(必要な構文についていくつかの指示を与える必要があります)。ユーザーがクエリを構築するためのより構造化されたインターフェイスを提供したい場合はSQLFORM.search_menuを試すことができます(これは主に内部使用目的のためによって記述されていますが、別途使用することができます)。 DAL Queryを作成するには、SQLFORM.search_menuで作成したクエリをdb.smart_queryに渡すことができます(グリッド検索機能の仕組みです)。

最終的には、カスタムクエリに基づいてグリッドをユーザーに提供することが目的であるため、ユーザーにすべてのデータをグリッドで提供することを検討して、高度な検索機能を使用して、望ましい結果を得ることができます。あなたは、ユーザーが最初にすべてのデータを表示したくない場合は、あなただけのグリッド検索ボックス示すことによって起動することができます。

grid = SQLFORM.grid(db.mytable) 
grid = grid if 'keywords' in request.get_vars else grid.element('.web2py_console') 

を上記、すなわち(グリッドの唯一の「.web2py_console」の部分を返します検索クエリが提出されていない場合(たとえば、ページが最初に読み込まれたとき)