2017-07-20 39 views
1

我是新來的瓶子管理員,我需要將刪除按鈕移動到編輯視圖。瓶頸管理 - 移動刪除按鈕來編輯視圖

這裏是我的其他視圖繼承的AdminModelView類。

class AdminModelView(sqla.ModelView): 
    can_view_details = True 


    # column_extra_row_actions = [ViewRowAction()] 

    def is_accessible(self): 
     if not current_user.is_active or not current_user.is_authenticated: 
      return False 
     if current_user.can('administrator'): 
      return True 
     return False 

    def _handle_view(self, name, **kwargs): 
     """ 
     Override builtin _handle_view in order to redirect users when a view is not accessible. 
     """ 
     if not self.is_accessible(): 
      if current_user.is_authenticated: 
       # permission denied 
       abort(403) 
      else: 
       # login 
       return redirect(url_for('auth.login', next=request.url)) 
      print(self._edit_form_class) 


    def get_list_row_actions(self): 
     """ 
      Return list of row action objects, each is instance of 
      :class:`~flask_admin.model.template.BaseListRowAction` 
     """ 
     actions = [] 

     if self.can_view_details: 
      if self.details_modal: 
       actions.append(template.ViewPopupRowAction()) 
      else: 
       actions.append(template.ViewRowAction()) 

     return actions + (self.column_extra_row_actions or []) 

我重新定義了get_list_row_actions,使編輯和刪除按鈕從列表視圖中消失。我想知道是否有我可以更改的AdminModelView類的一部分,或者是否需要更改編輯表單的模板。

+0

也想解決這個問題。你找到答案了嗎? – Sparrowcide

+0

我們必須編寫'/ delete'端點並將該按鈕添加到模板。現在發佈答案。 –

回答

1

我的解決辦法:

class AdminModelView(sqla.ModelView):

@expose('/delete', methods=('DELETE',)) 
def delete_view(self): 
    """ 
     Delete model 
    """ 
    id = request.args.get('id') 
    if id is None: 
     return jsonify({"success": False}), 404 
    model = self.get_one(id) 
    db.session.delete(model) 
    db.session.commit() 
    flash("{0} deleted".format(model)) 
    return jsonify({"success": True}), 200 

寫新的端點使用這些宏來渲染一個刪除按鈕和一個模式到編輯視圖模板。

{% if admin_view.can_delete and model %} 
    {{ add_modal_button(url='#', title='Delete', content='Delete', modal_window_id='delete_modal', btn_class='btn btn-danger') }} 
{% endif %} 

{% macro delete_modal() %} 
<div class="modal fade" id="delete_modal" tabindex="-1" role="dialog"> 
    <div class="modal-dialog" role="document"> 
     {# bootstrap version > 3.1.0 required for this to work #} 
    <div class="modal-content"> 
     <div class="panel panel-danger"> 
     <div class="panel-heading">Delete Record</div> 
     <div class="panel-body"> 
      <p>You are about to delete this record. This action cannot be undone.</p> 
      <p>Would you like to proceed?</p> 
     </div> 
     <div class="panel-footer text-center"> 
      <button type="button" class="btn btn-secondary" id="cancel_delete">Cancel</button> 
      <button type="button" class="btn btn-danger" id="confirm_delete">Delete</button> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 
{% endmacro %} 

Hybrid Jinja2和JavaScript。這應該被重構。 {{model.id}}可以存儲在DOM中,並在進行AJAX請求之前用jQuery檢索。

$("#cancel_delete").click(function() { 
    $("#delete_modal").modal("toggle") 
}); 

$("#confirm_delete").click(function() { 
    $.ajax({ 
    url: "../delete?id={{ model.id }}", 
    method: "DELETE" 
    }).done(function() { 
    window.location.replace("{{ return_url }}"); 
    }).fail(function() { 
    $(".actions-nav").before(
     '<div class="alert alert-danger alert-dismissable fade in">' + 
     '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>' + 
     '<strong>Error:</strong> This object failed to delete.' + 
     '</div>' 
    ) 
    }) 
})