/usr/share/pyshared/tgext/admin/tgadminconfig.py is in python-tgext.admin 0.2.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import inspect
from tg import expose, redirect
from tw.forms import TextField, PasswordField
from tgext.crud import CrudRestController
from tgext.crud.decorators import registered_validate
from sqlalchemy.orm import class_mapper
from sqlalchemy.orm.exc import UnmappedClassError
from config import AdminConfig, CrudRestControllerConfig
from sprox.fillerbase import EditFormFiller
from sprox.formbase import FilteringSchema
from formencode.validators import FieldsMatch
class PasswordFieldsMatch(FieldsMatch):
field_names = ['password', 'verify_password']
def validate_partial(self, field_dict, state):
#no password is set to change
if not field_dict.get('verify_password') and not field_dict.get('password'):
return
for name in self.field_names:
if not field_dict.has_key(name):
return
self.validate_python(field_dict, state)
try:
import tw.dojo
from sprox.dojo.tablebase import DojoTableBase as TableBase
from sprox.dojo.fillerbase import DojoTableFiller as TableFiller
from sprox.dojo.formbase import DojoAddRecordForm as AddRecordForm, DojoEditableForm as EditableForm
except ImportError:
from sprox.tablebase import TableBase
from sprox.fillerbase import TableFiller
from sprox.formbase import AddRecordForm, EditableForm
from sprox.fillerbase import RecordFiller, AddFormFiller
class UserControllerConfig(CrudRestControllerConfig):
def _do_init_with_translations(self, translations):
user_id_field = translations.get('user_id', 'user_id')
user_name_field = translations.get('user_name', 'user_name')
email_field = translations.get('email_address', 'email_address')
password_field = translations.get('password', 'password')
display_name_field = translations.get('display_name', 'display_name')
class Table(TableBase):
__entity__ = self.model
__omit_fields__ = [user_id_field, '_password', password_field]
__url__ = '../users.json'
self.table_type = Table
class MyTableFiller(TableFiller):
__entity__ = self.model
__omit_fields__ = ['_password', password_field]
self.table_filler_type = MyTableFiller
edit_form_validator = FilteringSchema(chained_validators=(FieldsMatch('password',
'verify_password',
messages={'invalidNoMatch':
'Passwords do not match'}),))
class EditForm(EditableForm):
__entity__ = self.model
__require_fields__ = [user_name_field, email_field]
__omit_fields__ = ['created', '_password']
__hidden_fields__ = [user_id_field]
__field_order__ = [user_id_field, user_name_field, email_field, display_name_field, 'password', 'verify_password', 'groups']
password = PasswordField('password', value='')
verify_password = PasswordField('verify_password')
__base_validator__ = edit_form_validator
if email_field is not None:
setattr(EditForm, email_field, TextField)
if display_name_field is not None:
setattr(EditForm, display_name_field, TextField)
self.edit_form_type = EditForm
class UserEditFormFiller(EditFormFiller):
__entity__ = self.model
def get_value(self, *args, **kw):
v = super(UserEditFormFiller, self).get_value(*args, **kw)
del v['password']
return v
self.edit_filler_type = UserEditFormFiller
class NewForm(AddRecordForm):
__entity__ = self.model
__require_fields__ = [user_name_field, email_field]
__omit_fields__ = [password_field, 'created', '_password']
__hidden_fields__ = [user_id_field]
__field_order__ = [user_name_field, email_field, display_name_field, 'groups']
if email_field is not None:
setattr(NewForm, email_field, TextField)
if display_name_field is not None:
setattr(NewForm, display_name_field, TextField)
self.new_form_type = NewForm
class defaultCrudRestController(CrudRestController):
@expose('genshi:tgext.crud.templates.edit')
def edit(self, *args, **kw):
return CrudRestController.edit(self, *args, **kw)
@expose()
@registered_validate(error_handler=edit)
def put(self, *args, **kw):
"""update"""
if not kw['password']:
del kw['password']
pks = self.provider.get_primary_fields(self.model)
for i, pk in enumerate(pks):
if pk not in kw and i < len(args):
kw[pk] = args[i]
self.provider.update(self.model, params=kw)
redirect('../')
class GroupControllerConfig(CrudRestControllerConfig):
def _do_init_with_translations(self, translations):
group_id_field = translations.get('group_id', 'group_id')
group_name_field = translations.get('group_name', 'group_name')
class GroupTable(TableBase):
__model__ = self.model
__limit_fields__ = [group_name_field, 'permissions']
__url__ = '../groups.json'
self.table_type = GroupTable
class GroupTableFiller(TableFiller):
__model__ = self.model
__limit_fields__ = [group_id_field, group_name_field, 'permissions']
self.table_filler_type = GroupTableFiller
class GroupNewForm(AddRecordForm):
__model__ = self.model
__limit_fields__ = [group_name_field, 'permissions']
self.new_form_type = GroupNewForm
class GroupEditForm(EditableForm):
__model__ = self.model
__limit_fields__ = [group_id_field, 'group_name', 'permissions']
self.edit_form_type = GroupEditForm
class PermissionControllerConfig(CrudRestControllerConfig):
def _do_init_with_translations(self, translations):
permission_id_field = translations.get('permission_id', 'permission_id')
permission_name_field = translations.get('permission_name', 'permission_name')
permission_description_field = translations.get('permission_description', 'description')
class PermissionTable(TableBase):
__model__ = self.model
__limit_fields__ = [permission_name_field, permission_description_field, 'groups']
__url__ = '../permissions.json'
self.table_type = PermissionTable
class PermissionTableFiller(TableFiller):
__model__ = self.model
__limit_fields__ = [permission_id_field, permission_name_field, permission_description_field, 'groups']
self.table_filler_type = PermissionTableFiller
class PermissionNewForm(AddRecordForm):
__model__ = self.model
__limit_fields__ = [permission_name_field, permission_description_field, 'groups']
self.new_form_type = PermissionNewForm
class PermissionEditForm(EditableForm):
__model__ = self.model
__limit_fields__ = [permission_name_field, permission_description_field,'groups']
self.edit_form_type = PermissionEditForm
class PermissionEditFiller(RecordFiller):
__model__ = self.model
self.edit_filler_type = PermissionEditFiller
class TGAdminConfig(AdminConfig):
user = UserControllerConfig
group = GroupControllerConfig
permission = PermissionControllerConfig
|