Package Camelot :: Package camelot :: Package admin :: Module not_editable_admin
[frames] | no frames]

Source Code for Module Camelot.camelot.admin.not_editable_admin

  1  #  ============================================================================ 
  2  # 
  3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved. 
  4  #  www.conceptive.be / project-camelot@conceptive.be 
  5  # 
  6  #  This file is part of the Camelot Library. 
  7  # 
  8  #  This file may be used under the terms of the GNU General Public 
  9  #  License version 2.0 as published by the Free Software Foundation 
 10  #  and appearing in the file LICENSE.GPL included in the packaging of 
 11  #  this file.  Please review the following information to ensure GNU 
 12  #  General Public Licensing requirements will be met: 
 13  #  http://www.trolltech.com/products/qt/opensource.html 
 14  # 
 15  #  If you are unsure which license is appropriate for your use, please 
 16  #  review the following information: 
 17  #  http://www.trolltech.com/products/qt/licensing.html or contact 
 18  #  project-camelot@conceptive.be. 
 19  # 
 20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 
 21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
 22  # 
 23  #  For use of this library in commercial applications, please contact 
 24  #  project-camelot@conceptive.be 
 25  # 
 26  #  ============================================================================ 
 27   
 28  """Class decorator to make all fields visualized with the Admin into read-only 
 29  fields""" 
 30   
 31  from copy import copy 
32 -def notEditableAdmin(original_admin, actions=False, editable_fields=None):
33 34 """Turn all fields visualized with original_admin into read only fields 35 :param original_admin: an implementation of ObjectAdmin 36 :param actions: True if the notEditableAdmin should have its actions enabled, default to False 37 :param editable_fields: list of fields that should remain editable 38 39 usage :: 40 41 class Movie(Entity): 42 name = Field(Unicode(50)) 43 contributions = Field(Unicode(255)) 44 45 class Admin(EntityAdmin): 46 list_display = ['name', 'contributions] 47 48 Admin = notEditableAdmin(Admin, editable_fields=['contributions']) 49 """ 50 51 class NewAdmin(original_admin): 52 53 def get_related_entity_admin(self, entity): 54 admin = original_admin.get_related_entity_admin(self, entity) 55 56 class AdminReadOnlyDecorator(object): 57 58 def __init__(self, original_admin, editable_fields): 59 self._original_admin = original_admin 60 self._editable_fields = editable_fields 61 self._field_attributes = dict()
62 63 def __getattr__(self, name): 64 return self._original_admin.__getattribute__(name) 65 66 def get_fields(self): 67 fields = self._original_admin.get_fields() 68 return [(field_name,self.get_field_attributes(field_name)) for field_name,_attrs in fields] 69 70 def get_field_attributes(self, field_name): 71 try: 72 return self._field_attributes[field_name] 73 except KeyError: 74 attribs = copy( self._original_admin.get_field_attributes(field_name) ) 75 if self._editable_fields and field_name in self._editable_fields: 76 attribs['editable'] = True 77 else: 78 attribs['editable'] = False 79 return attribs 80 81 def get_related_entity_admin(self, entity): 82 return AdminReadOnlyDecorator(self._original_admin.get_related_entity_admin(entity)) 83 84 def get_form_actions(self, *a, **kwa): 85 return [] 86 87 def get_list_actions(self, *a, **kwa): 88 return [] 89 90 def get_columns(self): 91 return [(field, self.get_field_attributes(field)) 92 for field, _attrs in self._original_admin.get_columns()] 93 94 return AdminReadOnlyDecorator(admin, editable_fields) 95 96 def get_field_attributes(self, field_name): 97 attribs = original_admin.get_field_attributes(self, field_name) 98 if editable_fields and field_name in editable_fields: 99 attribs['editable'] = True 100 else: 101 attribs['editable'] = False 102 return attribs 103 104 def get_form_actions(self, *a, **kwa): 105 return [] 106 107 def get_list_actions(self, *a, **kwa): 108 return [] 109 110 return NewAdmin 111