From 0609e2cd7239c74e1ffb9a7c2c15294e6797cb65 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch <p.jentsch@uni-bielefeld.de>
Date: Fri, 24 Feb 2023 09:27:20 +0100
Subject: [PATCH] Fix follow corpus mechanics

---
 app/models.py | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/app/models.py b/app/models.py
index 01e7c09a..ed36a8ff 100644
--- a/app/models.py
+++ b/app/models.py
@@ -378,8 +378,7 @@ class CorpusFollowerRole(HashidMixin, db.Model):
     # Relationships
     corpus_follower_associations = db.relationship(
         'CorpusFollowerAssociation',
-        back_populates='role',
-        lazy='dynamic'
+        back_populates='role'
     )
 
     def __repr__(self):
@@ -481,11 +480,9 @@ class CorpusFollowerAssociation(HashidMixin, db.Model):
 
     def __init__(self, **kwargs):
         super().__init__(**kwargs)
-        if self.role is None:
-            self.role = CorpusFollowerRole.query.filter_by(default=True).first()
 
     def __repr__(self):
-        return f'<CorpusFollowerAssociation {self.follower.__repr__()} ~ {self.corpus.__repr__()}>'
+        return f'<CorpusFollowerAssociation {self.follower.__repr__()} ~ {self.role.__repr__()} ~ {self.corpus.__repr__()}>'
 
     def to_json_serializeable(self, backrefs=False, relationships=False):
         json_serializeable = {
@@ -545,8 +542,7 @@ class User(HashidMixin, UserMixin, db.Model):
     )
     followed_corpora = association_proxy(
         'corpus_follower_associations',
-        'corpus',
-        creator=lambda c: CorpusFollowerAssociation(corpus=c)
+        'corpus'
     )
     jobs = db.relationship(
         'Job',
@@ -778,9 +774,11 @@ class User(HashidMixin, UserMixin, db.Model):
         self.profile_privacy_settings = 0
     #endregion Profile Privacy settings
 
-    def follow_corpus(self, corpus):
-        if not self.is_following_corpus(corpus):
-            self.followed_corpora.append(corpus)
+    def follow_corpus(self, corpus, role=None):
+        if role is None:
+            r = CorpusFollowerRole.query.filter_by(default=True).first()
+        cfa = CorpusFollowerAssociation(corpus=corpus, role=r, follower=self)
+        db.session.add(cfa)
 
     def unfollow_corpus(self, corpus):
         if self.is_following_corpus(corpus):
@@ -1499,8 +1497,7 @@ class Corpus(HashidMixin, db.Model):
     )
     followers = association_proxy(
         'corpus_follower_associations',
-        'follower',
-        creator=lambda u: CorpusFollowerAssociation(followers=u)
+        'follower'
     )
     user = db.relationship('User', back_populates='corpora')
     # "static" attributes
-- 
GitLab