Personal wiki notebook (not under development)

Groups.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from Expose import expose
  2. from Expire import strongly_expire
  3. from Users import grab_user_id, Access_error
  4. from model.Group import Group
  5. from model.User import User
  6. from view.Json import Json
  7. from Validate import validate, Valid_string, Valid_bool, Valid_int, Validation_error
  8. from Database import Valid_id, end_transaction
  9. class Groups( object ):
  10. def __init__( self, database, users ):
  11. self.__database = database
  12. self.__users = users
  13. @expose( view = Json )
  14. @strongly_expire
  15. @end_transaction
  16. @grab_user_id
  17. @validate(
  18. group_id = Valid_id(),
  19. user_id = Valid_id( none_okay = True ),
  20. )
  21. def load_users( self, group_id, user_id = None ):
  22. """
  23. Return the users within the given group. This method is only available to an admin of the
  24. group.
  25. @type group_id: unicode
  26. @param group_id: id of group whose users to return
  27. @type user_id: unicode or NoneType
  28. @param user_id: id of current logged-in user (if any)
  29. @rtype: dict
  30. @return: {
  31. 'group': group_info,
  32. 'admin_users': admin_user_list,
  33. 'other_users': non_admin_user_list,
  34. }
  35. @raise Access_error: the current user doesn't have admin membership to the given group
  36. @raise Validation_error: one of the arguments is invalid
  37. """
  38. if not self.__users.check_group( user_id, group_id, admin = True ):
  39. raise Access_error()
  40. group = self.__database.load( Group, group_id )
  41. if group is None:
  42. raise Access_error()
  43. admin_users = self.__database.select_many( User, group.sql_load_users( admin = True ) )
  44. other_users = self.__database.select_many( User, group.sql_load_users( admin = False ) )
  45. return dict(
  46. group = group,
  47. admin_users = admin_users,
  48. other_users = other_users,
  49. )
  50. @expose( view = Json )
  51. @end_transaction
  52. @grab_user_id
  53. @validate(
  54. group_id = Valid_id(),
  55. group_name = Valid_string( min = 0, max = 100 ),
  56. group_settings_button = unicode,
  57. user_id = Valid_id( none_okay = True ),
  58. )
  59. def update_settings( self, group_id, group_name, group_settings_button, user_id = None ):
  60. """
  61. Update the settings for the given group.
  62. @type group_id: unicode
  63. @param group_id: id of group whose users to return
  64. @type group_name: unicode
  65. @param group_name: new name of the group
  66. @type group_settings_button: unicode
  67. @param group_settings_button: ignored
  68. @rtype: dict
  69. @return: { 'message': message }
  70. @raise Access_error: the current user doesn't have admin membership to the given group
  71. @raise Validation_error: one of the arguments is invalid
  72. """
  73. if not self.__users.check_group( user_id, group_id, admin = True ):
  74. raise Access_error()
  75. group = self.__database.load( Group, group_id )
  76. if group is None:
  77. raise Access_error()
  78. group.name = group_name
  79. self.__database.save( group )
  80. return dict(
  81. message = u"The group settings have been saved.",
  82. )