Browse Source

Fix tag editing again

Trevor Elliott 4 years ago
parent
commit
b3743d9854
2 changed files with 18 additions and 10 deletions
  1. 12 8
      lc/model.py
  2. 6 2
      tests/model.py

+ 12 - 8
lc/model.py

@@ -168,15 +168,19 @@ class Link(Model):
         return l
 
     def update_from_request(self, user: User, link: r.Link):
-        new_tags = set()
-        for tag_name in link.tags:
-            t = Tag.get_or_create_tag(user, tag_name)
-            new_tags.add(t)
-            HasTag.get_or_create(self, t)
 
-        for tag in self.tags:
-            if tag not in new_tags:
-                HasTag.delete().where((HasTag.link == self.id) & (HasTag.tag == tag))
+        req_tags = set(link.tags)
+
+        for hastag in self.tags:
+            name = hastag.tag.name
+            if name not in req_tags:
+                hastag.delete_instance()
+            else:
+                req_tags.remove(name)
+
+        for tag_name in req_tags:
+            t = Tag.get_or_create_tag(user, tag_name)
+            HasTag.get_or_create(link=self, tag=t)
 
         self.url = link.url
         self.name = link.name

+ 6 - 2
tests/model.py

@@ -134,6 +134,10 @@ class Testdb:
         with pytest.raises(e.NoSuchInvite):
             m.User.from_invite(r.User(name="u4", password="u4"), "a-non-existent-token")
 
+    def check_tags(self, l, tags):
+        present = set(map(lambda hastag: hastag.tag.name, l.tags))
+        assert present == set(tags)
+
     def test_edit_link(self):
         u = self.mk_user()
 
@@ -148,13 +152,13 @@ class Testdb:
         req.private = True
         l.update_from_request(u, req)
         assert l.name == req.name
-        assert l.tags == req.tags
         assert l.private
         assert l.created != req.created
+        self.check_tags(l, req.tags)
 
         # check that the link was persisted
         l = m.Link.by_id(l.id)
         assert l.name == req.name
-        assert l.tags == req.tags
         assert l.private
         assert l.created != req.created
+        self.check_tags(l, req.tags)