Просмотр исходного кода

Allow creating of user invites

Getty Ritter 4 лет назад
Родитель
Сommit
72bc2d2861
2 измененных файлов с 33 добавлено и 1 удалено
  1. 23 1
      lc/model.py
  2. 10 0
      tests/model.py

+ 23 - 1
lc/model.py

@@ -185,7 +185,28 @@ class HasTag(Model):
 
 
 class UserInvite(Model):
-    token: str
+    token = peewee.TextField()
+
+    created_by = peewee.ForeignKeyField(User, backref="invites")
+    created_at = peewee.DateTimeField()
+
+    claimed_by = peewee.ForeignKeyField(User, null=True)
+    claimed_at = peewee.DateTimeField(null=True)
+
+    @staticmethod
+    def manufacture(creator: User) -> "UserInvite":
+        now = datetime.datetime.now()
+        token = c.serializer.dumps({
+            "created_at": now.timestamp(),
+            "created_by": creator.name,
+        })
+        return UserInvite.create(
+            token=token,
+            created_by=creator,
+            created_at=now,
+            claimed_by=None,
+            claimed_at=None,
+        )
 
 
 MODELS = [
@@ -193,6 +214,7 @@ MODELS = [
     Link,
     Tag,
     HasTag,
+    UserInvite,
 ]
 
 

+ 10 - 0
tests/model.py

@@ -90,3 +90,13 @@ class Testdb:
         # one already entered
         assert t.id == m.Tag.get(name="food/bread/rye").id
         assert t2.id == m.Tag.get(name="food/bread/baguette").id
+
+    def test_create_invite(self):
+        u = self.mk_user()
+        invite = m.UserInvite.manufacture(u)
+
+        assert invite.created_by.id == u.id
+
+        raw_data = c.serializer.loads(invite.token)
+
+        assert(raw_data["created_by"] == u.name)