Personal wiki notebook (not under development)

Invite.py 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. from Persistent import Persistent, quote
  2. class Invite( Persistent ):
  3. """
  4. An invitiation to view or edit a notebook.
  5. """
  6. def __init__( self, object_id, revision = None, from_user_id = None, notebook_id = None,
  7. email_address = None, read_write = False, owner = False, redeemed_user_id = None,
  8. redeemed_username = None ):
  9. """
  10. Create an invitation with the given id.
  11. @type object_id: unicode
  12. @param object_id: id of the invitation
  13. @type revision: datetime or NoneType
  14. @param revision: revision timestamp of the object (optional, defaults to now)
  15. @type from_user_id: unicode or NoneType
  16. @param from_user_id: id of the user who sent the invite (optional)
  17. @type notebook_id: unicode or NoneType
  18. @param notebook_id: id of the notebook that the invitation is for
  19. @type email_address: unicode or NoneType
  20. @param email_address: where the invitation was emailed (optional)
  21. @type read_write: bool or NoneType
  22. @param read_write: whether the invitation is for read-write access (optional, defaults to False)
  23. @type owner: bool or NoneType
  24. @param owner: whether the invitation is for owner-level access (optional, defaults to False)
  25. @type redeemed_user_id: unicode or NoneType
  26. @param redeemed_user_id: id of the user who has redeemed this invitation, if any (optional)
  27. @type redeemed_username: unicode or NoneType
  28. @param redeemed_username: username of the user who has redeemed this invitation, if any (optional)
  29. @rtype: Invite
  30. @return: newly constructed notebook invitation
  31. """
  32. Persistent.__init__( self, object_id, revision )
  33. self.__from_user_id = from_user_id
  34. self.__notebook_id = notebook_id
  35. self.__email_address = email_address
  36. self.__read_write = read_write
  37. self.__owner = owner
  38. self.__redeemed_user_id = redeemed_user_id
  39. self.__redeemed_username = redeemed_username
  40. @staticmethod
  41. def create( object_id, from_user_id = None, notebook_id = None, email_address = None, read_write = False, owner = False ):
  42. """
  43. Convenience constructor for creating a new invitation.
  44. @type object_id: unicode
  45. @param object_id: id of the invitation
  46. @type from_user_id: unicode or NoneType
  47. @param from_user_id: id of the user who sent the invite (optional)
  48. @type notebook_id: unicode or NoneType
  49. @param notebook_id: id of the notebook that the invitation is for
  50. @type email_address: unicode or NoneType
  51. @param email_address: where the invitation was emailed (optional)
  52. @type read_write: bool or NoneType
  53. @param read_write: whether the invitation is for read-write access (optional, defaults to False)
  54. @type owner: bool or NoneType
  55. @param owner: whether the invitation is for owner-level access (optional, defaults to False)
  56. @rtype: Invite
  57. @return: newly constructed notebook invitation
  58. """
  59. return Invite( object_id, from_user_id = from_user_id, notebook_id = notebook_id,
  60. email_address = email_address, read_write = read_write, owner = owner )
  61. @staticmethod
  62. def sql_load( object_id, revision = None ):
  63. # invites don't store old revisions
  64. if revision:
  65. raise NotImplementedError()
  66. return \
  67. """
  68. select
  69. invite.id, invite.revision, invite.from_user_id, invite.notebook_id, invite.email_address,
  70. invite.read_write, invite.owner, invite.redeemed_user_id, luminotes_user_current.username
  71. from
  72. invite left outer join luminotes_user_current
  73. on
  74. ( invite.redeemed_user_id = luminotes_user_current.id )
  75. where
  76. invite.id = %s;
  77. """ % quote( object_id )
  78. @staticmethod
  79. def sql_id_exists( object_id, revision = None ):
  80. if revision:
  81. raise NotImplementedError()
  82. return "select id from invite where id = %s;" % quote( object_id )
  83. def sql_exists( self ):
  84. return Invite.sql_id_exists( self.object_id )
  85. def sql_create( self ):
  86. return "insert into invite ( id, revision, from_user_id, notebook_id, email_address, read_write, owner, redeemed_user_id ) values ( %s, %s, %s, %s, %s, %s, %s, %s );" % \
  87. ( quote( self.object_id ), quote( self.revision ), quote( self.__from_user_id ), quote( self.__notebook_id ),
  88. quote( self.__email_address ), quote( self.__read_write and "t" or "f" ), quote( self.__owner and "t" or "f" ),
  89. quote( self.__redeemed_user_id ) )
  90. def sql_update( self ):
  91. return "update invite set revision = %s, from_user_id = %s, notebook_id = %s, email_address = %s, read_write = %s, owner = %s, redeemed_user_id = %s where id = %s;" % \
  92. ( quote( self.revision ), quote( self.__from_user_id ), quote( self.__notebook_id ),
  93. quote( self.__email_address ), quote( self.__read_write and "t" or "f" ), quote( self.__owner and "t" or "f" ),
  94. quote( self.__redeemed_user_id ), quote( self.object_id ) )
  95. def sql_load_similar( self ):
  96. # select invites with the same notebook_id, and email_address as this invite
  97. return \
  98. """
  99. select
  100. invite.id, invite.revision, invite.from_user_id, invite.notebook_id, invite.email_address,
  101. invite.read_write, invite.owner, invite.redeemed_user_id, luminotes_user_current.username
  102. from
  103. invite left outer join luminotes_user_current
  104. on
  105. ( invite.redeemed_user_id = luminotes_user_current.id )
  106. where
  107. invite.notebook_id = %s and invite.email_address = %s and invite.id != %s;
  108. """ % ( quote( self.__notebook_id ), quote( self.__email_address ), quote( self.object_id ) )
  109. @staticmethod
  110. def sql_load_notebook_invites( notebook_id ):
  111. # select a list of invites to the given notebook
  112. return \
  113. """
  114. select
  115. invite.id, invite.revision, invite.from_user_id, invite.notebook_id, invite.email_address,
  116. invite.read_write, invite.owner, invite.redeemed_user_id, luminotes_user_current.username
  117. from
  118. invite left outer join luminotes_user_current
  119. on
  120. ( invite.redeemed_user_id = luminotes_user_current.id )
  121. where
  122. invite.notebook_id = %s
  123. order by
  124. invite.email_address, invite.redeemed_user_id;
  125. """ % quote( notebook_id )
  126. def sql_revoke_invites( self ):
  127. return "delete from invite where notebook_id = %s and email_address = %s;" % \
  128. ( quote( self.__notebook_id ), quote( self.__email_address ) )
  129. def to_dict( self ):
  130. d = Persistent.to_dict( self )
  131. d.update( dict(
  132. from_user_id = self.__from_user_id,
  133. notebook_id = self.__notebook_id,
  134. email_address = self.__email_address,
  135. read_write = self.__read_write,
  136. owner = self.__owner,
  137. redeemed_user_id = self.__redeemed_user_id,
  138. redeemed_username = self.__redeemed_username,
  139. ) )
  140. return d
  141. def __set_read_write( self, read_write ):
  142. if read_write != self.__read_write:
  143. self.update_revision()
  144. self.__read_write = read_write
  145. def __set_owner( self, owner ):
  146. if owner != self.__owner:
  147. self.update_revision()
  148. self.__owner = owner
  149. def __set_redeemed_user_id( self, redeemed_user_id ):
  150. if redeemed_user_id != self.__redeemed_user_id:
  151. self.update_revision()
  152. self.__redeemed_user_id = redeemed_user_id
  153. from_user_id = property( lambda self: self.__from_user_id )
  154. notebook_id = property( lambda self: self.__notebook_id )
  155. email_address = property( lambda self: self.__email_address )
  156. read_write = property( lambda self: self.__read_write, __set_read_write )
  157. owner = property( lambda self: self.__owner, __set_owner )
  158. redeemed_user_id = property( lambda self: self.__redeemed_user_id, __set_redeemed_user_id )
  159. redeemed_username = property( lambda self: self.__redeemed_username )