Browse Source

New model.Invite object for the invite table.

Renamed sent_user_id column to from_user_id, which makes a lot more sense.
Dan Helfman 10 years ago
parent
commit
274a87dee2
5 changed files with 150 additions and 6 deletions
  1. 103
    0
      model/Invite.py
  2. 4
    4
      model/Password_reset.py
  3. 1
    1
      model/delta/1.0.4.sql
  4. 1
    1
      model/schema.sql
  5. 41
    0
      model/test/Test_invite.py

+ 103
- 0
model/Invite.py View File

@@ -0,0 +1,103 @@
1
+from Persistent import Persistent, quote
2
+
3
+
4
+class Invite( Persistent ):
5
+  """
6
+  An invitiation to view or edit a notebook.
7
+  """
8
+  def __init__( self, object_id, revision = None, from_user_id = None, notebook_id = None,
9
+                email_address = None, read_write = False, owner = False, redeemed_user_id = None ):
10
+    """
11
+    Create an invitation with the given id.
12
+
13
+    @type object_id: unicode
14
+    @param object_id: id of the invitation
15
+    @type revision: datetime or NoneType
16
+    @param revision: revision timestamp of the object (optional, defaults to now)
17
+    @type from_user_id: unicode or NoneType
18
+    @param from_user_id: id of the user who sent the invite (optional)
19
+    @type notebook_id: unicode or NoneType
20
+    @param notebook_id: id of the notebook that the invitation is to
21
+    @type email_address: unicode or NoneType
22
+    @param email_address: where the invitation was emailed (optional)
23
+    @type read_write: bool or NoneType
24
+    @param read_write: whether the invitation is for read-write access (optional, defaults to False)
25
+    @type owner: bool or NoneType
26
+    @param owner: whether the invitation is for owner-level access (optional, defaults to False)
27
+    @type redeemed_user_id: unicode or NoneType
28
+    @param redeemed_user_id: id 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
+
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
+
45
+    @type object_id: unicode
46
+    @param object_id: id of the invitation
47
+    @type from_user_id: unicode or NoneType
48
+    @param from_user_id: id of the user who sent the invite (optional)
49
+    @type notebook_id: unicode or NoneType
50
+    @param notebook_id: id of the notebook that the invitation is to
51
+    @type email_address: unicode or NoneType
52
+    @param email_address: where the invitation was emailed (optional)
53
+    @type read_write: bool or NoneType
54
+    @param read_write: whether the invitation is for read-write access (optional, defaults to False)
55
+    @type owner: bool or NoneType
56
+    @param owner: whether the invitation is for owner-level access (optional, defaults to False)
57
+    @rtype: Invite
58
+    @return: newly constructed notebook invitation
59
+    """
60
+    return Invite( object_id, from_user_id = from_user_id, notebook_id = notebook_id,
61
+                   email_address = email_address, read_write = read_write, owner = owner )
62
+
63
+  @staticmethod
64
+  def sql_load( object_id, revision = None ):
65
+    # password resets don't store old revisions
66
+    if revision:
67
+      raise NotImplementedError()
68
+
69
+    return "select id, revision, from_user_id, notebook_id, email_address, read_write, owner, redeemed_user_id from invite where id = %s;" % quote( object_id )
70
+
71
+  @staticmethod
72
+  def sql_id_exists( object_id, revision = None ):
73
+    if revision:
74
+      raise NotImplementedError()
75
+
76
+    return "select id from invite where id = %s;" % quote( object_id )
77
+
78
+  def sql_exists( self ):
79
+    return Invite.sql_id_exists( self.object_id )
80
+
81
+  def sql_create( self ):
82
+    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 );" % \
83
+    ( quote( self.object_id ), quote( self.revision ), quote( self.__from_user_id ), quote( self.__notebook_id ),
84
+      quote( self.__email_address ), quote( self.__read_write and "t" or "f" ), quote( self.__owner and "t" or "f" ),
85
+      quote( self.__redeemed_user_id ) )
86
+
87
+  def sql_update( self ):
88
+    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;" % \
89
+    ( quote( self.object_id ), quote( self.revision ), quote( self.__from_user_id ), quote( self.__notebook_id ),
90
+      quote( self.__email_address ), quote( self.__read_write and "t" or "f" ), quote( self.__owner and "t" or "f" ),
91
+      quote( self.__redeemed_user_id ) )
92
+
93
+  def __set_redeemed_user_id( self, redeemed_user_id ):
94
+    if redeemed_user_id != self.__redeemed_user_id:
95
+      self.update_revision()
96
+      self.__redeemed_user_id = redeemed_user_id
97
+
98
+  from_user_id = property( lambda self: self.__from_user_id )
99
+  notebook_id = property( lambda self: self.__notebook_id )
100
+  email_address = property( lambda self: self.__email_address )
101
+  read_write = property( lambda self: self.__read_write )
102
+  owner = property( lambda self: self.__owner )
103
+  redeemed_user_id = property( lambda self: self.__redeemed_user_id, __set_redeemed_user_id )

+ 4
- 4
model/Password_reset.py View File

@@ -13,8 +13,8 @@ class Password_reset( Persistent ):
13 13
     @param object_id: id of the password reset
14 14
     @type revision: datetime or NoneType
15 15
     @param revision: revision timestamp of the object (optional, defaults to now)
16
-    @type email_address: unicode
17
-    @param email_address: where the reset confirmation was emailed
16
+    @type email_address: unicode or NoneType
17
+    @param email_address: where the reset confirmation was emailed (optional)
18 18
     @type redeemed: bool or NoneType
19 19
     @param redeemed: whether this password reset has been redeemed yet (optional, defaults to False)
20 20
     @rtype: Password_reset
@@ -27,7 +27,7 @@ class Password_reset( Persistent ):
27 27
   @staticmethod
28 28
   def create( object_id, email_address = None ):
29 29
     """
30
-    Convenience constructor for creating a new note.
30
+    Convenience constructor for creating a new password reset.
31 31
 
32 32
     @type email_address: unicode
33 33
     @param email_address: where the reset confirmation was emailed
@@ -38,7 +38,7 @@ class Password_reset( Persistent ):
38 38
 
39 39
   @staticmethod
40 40
   def sql_load( object_id, revision = None ):
41
-    # password resets don't track revisions
41
+    # password resets don't store old revisions
42 42
     if revision:
43 43
       raise NotImplementedError()
44 44
 

+ 1
- 1
model/delta/1.0.4.sql View File

@@ -1,7 +1,7 @@
1 1
 create table invite (
2 2
   id text not null,
3 3
   revision timestamp with time zone not null,
4
-  sent_user_id text,
4
+  from_user_id text,
5 5
   notebook_id text,
6 6
   email_address text,
7 7
   read_write boolean,

+ 1
- 1
model/schema.sql View File

@@ -38,7 +38,7 @@ ALTER FUNCTION public.drop_html_tags(text) OWNER TO luminotes;
38 38
 CREATE TABLE invite (
39 39
     id text NOT NULL,
40 40
     revision timestamp with time zone NOT NULL,
41
-    sent_user_id text,
41
+    from_user_id text,
42 42
     notebook_id text,
43 43
     email_address text,
44 44
     read_write boolean,

+ 41
- 0
model/test/Test_invite.py View File

@@ -0,0 +1,41 @@
1
+from model.User import User
2
+from model.Invite import Invite
3
+
4
+
5
+class Test_invite( object ):
6
+  def setUp( self ):
7
+    self.object_id = u"17"
8
+    self.from_user_id = u"18"
9
+    self.notebook_id = u"19"
10
+    self.email_address = u"bob@example.com"
11
+    self.read_write = True
12
+    self.owner = False
13
+
14
+    self.invite = Invite.create( self.object_id, self.from_user_id, self.notebook_id,
15
+                                 self.email_address, self.read_write, self.owner )
16
+
17
+  def test_create( self ):
18
+    assert self.invite.object_id == self.object_id
19
+    assert self.invite.from_user_id == self.from_user_id
20
+    assert self.invite.notebook_id == self.notebook_id
21
+    assert self.invite.email_address == self.email_address
22
+    assert self.invite.read_write == self.read_write
23
+    assert self.invite.owner == self.owner
24
+    assert self.invite.redeemed_user_id == None
25
+
26
+  def test_redeem( self ):
27
+    previous_revision = self.invite.revision
28
+    redeemed_user_id = u"20"
29
+    self.invite.redeemed_user_id = redeemed_user_id
30
+
31
+    assert self.invite.redeemed_user_id == redeemed_user_id
32
+    assert self.invite.revision > previous_revision
33
+
34
+  def test_redeem_twice( self ):
35
+    redeemed_user_id = u"20"
36
+    self.invite.redeemed_user_id = redeemed_user_id
37
+    current_revision = self.invite.revision
38
+    self.invite.redeemed_user_id = redeemed_user_id
39
+
40
+    assert self.invite.redeemed_user_id == redeemed_user_id
41
+    assert self.invite.revision == current_revision