Fix follow corpus mechanics

This commit is contained in:
Patrick Jentsch 2023-02-24 09:27:20 +01:00
parent 1d85e96d3a
commit 0609e2cd72

View File

@ -378,8 +378,7 @@ class CorpusFollowerRole(HashidMixin, db.Model):
# Relationships # Relationships
corpus_follower_associations = db.relationship( corpus_follower_associations = db.relationship(
'CorpusFollowerAssociation', 'CorpusFollowerAssociation',
back_populates='role', back_populates='role'
lazy='dynamic'
) )
def __repr__(self): def __repr__(self):
@ -481,11 +480,9 @@ class CorpusFollowerAssociation(HashidMixin, db.Model):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
if self.role is None:
self.role = CorpusFollowerRole.query.filter_by(default=True).first()
def __repr__(self): 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): def to_json_serializeable(self, backrefs=False, relationships=False):
json_serializeable = { json_serializeable = {
@ -545,8 +542,7 @@ class User(HashidMixin, UserMixin, db.Model):
) )
followed_corpora = association_proxy( followed_corpora = association_proxy(
'corpus_follower_associations', 'corpus_follower_associations',
'corpus', 'corpus'
creator=lambda c: CorpusFollowerAssociation(corpus=c)
) )
jobs = db.relationship( jobs = db.relationship(
'Job', 'Job',
@ -778,9 +774,11 @@ class User(HashidMixin, UserMixin, db.Model):
self.profile_privacy_settings = 0 self.profile_privacy_settings = 0
#endregion Profile Privacy settings #endregion Profile Privacy settings
def follow_corpus(self, corpus): def follow_corpus(self, corpus, role=None):
if not self.is_following_corpus(corpus): if role is None:
self.followed_corpora.append(corpus) 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): def unfollow_corpus(self, corpus):
if self.is_following_corpus(corpus): if self.is_following_corpus(corpus):
@ -1499,8 +1497,7 @@ class Corpus(HashidMixin, db.Model):
) )
followers = association_proxy( followers = association_proxy(
'corpus_follower_associations', 'corpus_follower_associations',
'follower', 'follower'
creator=lambda u: CorpusFollowerAssociation(followers=u)
) )
user = db.relationship('User', back_populates='corpora') user = db.relationship('User', back_populates='corpora')
# "static" attributes # "static" attributes