adminuser.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. from pod import model as pm
  3. from tgext.crud import CrudRestController
  4. from sprox.tablebase import TableBase
  5. from sprox.formbase import EditableForm, AddRecordForm
  6. from sprox.fillerbase import TableFiller, EditFormFiller
  7. from pod.model import auth as pma
  8. from tw2 import forms as tw2f
  9. import tg
  10. from sprox.widgets import PropertyMultipleSelectField
  11. from sprox._compat import unicode_text
  12. from formencode import Schema
  13. from formencode.validators import FieldsMatch
  14. from pod.lib import dbapi as pld
  15. class GroupField(PropertyMultipleSelectField):
  16. """ Shows a limited list of groups """
  17. def prepare(self):
  18. # self.entity = pma.Group
  19. #self.__class__.entity
  20. visible_groups = pm.DBSession.query(pma.Group).\
  21. filter(pma.Group.group_id>0).\
  22. filter(pma.Group.group_id!=pma.Group.GROUP_ID_ALL_USERS).all()
  23. self.options = [(group.group_id, group.getDisplayName()) for group in visible_groups]
  24. if not self.value:
  25. self.value = []
  26. self.value = [unicode_text(v) for v in self.value]
  27. super(PropertyMultipleSelectField, self).prepare()
  28. class AdminUserController(CrudRestController):
  29. model = pma.User
  30. substring_filters = True
  31. class new_form_type(AddRecordForm):
  32. __model__ = pma.User
  33. __require_fields__ = ['display_name', 'email_address', 'password', 'verify_password', 'groups']
  34. __omit_fields__ = ['_password', 'created', 'user_id', '_lAllNodes']
  35. __field_order__ = ['display_name', 'email_address', 'password', 'verify_password', 'groups']
  36. email_address = tw2f.TextField('email_address')
  37. display_name = tw2f.TextField('display_name')
  38. verify_password = tw2f.PasswordField('verify_password')
  39. groups = GroupField('groups')
  40. class edit_form_type(EditableForm):
  41. __model__ = pma.User
  42. __require_fields__ = ['display_name', 'email_address', 'groups']
  43. __omit_fields__ = ['_password', 'created', 'user_id', '_lAllNodes', 'password']
  44. __field_order__ = ['display_name', 'email_address', 'groups']
  45. email_address = tw2f.TextField('email_address')
  46. display_name = tw2f.TextField('display_name')
  47. groups = GroupField('groups')
  48. class edit_filler_type(EditFormFiller):
  49. __model__ = pma.User
  50. class table_type(TableBase):
  51. __model__ = pma.User
  52. __limit_fields__ = ['user_id', 'email_address', 'display_name', 'groups']
  53. __field_order__ = ['user_id', 'display_name', 'email_address', 'groups']
  54. __headers__ = dict(user_id='id', email_address='Email', display_name='Name', groups='Groups')
  55. __xml_fields__ = ['groups']
  56. class table_filler_type(TableFiller):
  57. __model__ = pma.User
  58. __limit_fields__ = ['user_id', 'email_address', 'display_name', 'groups']
  59. def groups(self, obj):
  60. groups = ''.join(['<li>{0}</li>'.format(group.getDisplayName()) for group in obj.groups if group.group_id>0])
  61. return groups.join(('<ul>', '</ul>'))
  62. @tg.expose()
  63. #@tg.validate(new_user_validator, error_handler=CrudRestController.new)
  64. def post(self, *args, **kw):
  65. real_name = kw['display_name']
  66. email = kw['email_address']
  67. groups = kw['groups'] if 'groups' in kw else []
  68. password = kw['password']
  69. new_user = pld.PODStaticController.createNewUser(real_name, email, password, groups)
  70. if tg.request.response_type == 'application/json':
  71. if new_user is not None and self.conditional_update_field is not None:
  72. tg.response.last_modified = getattr(new_user, self.conditional_update_field)
  73. return dict(model=self.model.__name__,
  74. value=self._dictify(new_user))
  75. return tg.redirect('./', params=self._kept_params())
  76. """
  77. @tg.expose()
  78. def post_delete(self, *args, **kw):
  79. user_id = int(args[0])
  80. pld.PODStaticController.deleteUser(user_id)
  81. return tg.redirect('./', params=self._kept_params())
  82. """
  83. def post_delete(self, *args, **kw):
  84. pass
  85. @tg.expose()
  86. def put(self, *args, **kw):
  87. """update"""
  88. user_id = kw['user_id']
  89. real_name = kw['display_name']
  90. email = kw['email_address']
  91. groups = kw['groups'] if 'groups' in kw else []
  92. updated_user = pld.PODStaticController.updateUser(user_id, real_name, email, groups)
  93. return tg.redirect('../', params=self._kept_params())