diff --git a/app/models.py b/app/models.py index 8fc08c1824d398ae48566cfb99456b530788fafc..d363c0bb5757fb7a8e64b889d67fb21edf64f994 100644 --- a/app/models.py +++ b/app/models.py @@ -479,6 +479,8 @@ class CorpusFollowerAssociation(HashidMixin, db.Model): ) def __init__(self, **kwargs): + if 'role' not in kwargs: + kwargs['role'] = CorpusFollowerRole.query.filter_by(default=True).first() super().__init__(**kwargs) def __repr__(self): @@ -542,7 +544,8 @@ class User(HashidMixin, UserMixin, db.Model): ) followed_corpora = association_proxy( 'corpus_follower_associations', - 'corpus' + 'corpus', + creator=lambda c: CorpusFollowerAssociation(corpus=c) ) jobs = db.relationship( 'Job', @@ -574,13 +577,13 @@ class User(HashidMixin, UserMixin, db.Model): ) def __init__(self, **kwargs): + if 'role' not in kwargs: + kwargs['role'] = ( + Role.query.filter_by(name='Administrator').first() + if kwargs['email'] == current_app.config['NOPAQUE_ADMIN'] + else Role.query.filter_by(default=True).first() + ) super().__init__(**kwargs) - if self.role is not None: - return - if self.email == current_app.config['NOPAQUE_ADMIN']: - self.role = Role.query.filter_by(name='Administrator').first() - else: - self.role = Role.query.filter_by(default=True).first() def __repr__(self): return f'<User {self.username}>' @@ -1532,7 +1535,8 @@ class Corpus(HashidMixin, db.Model): ) followers = association_proxy( 'corpus_follower_associations', - 'follower' + 'follower', + creator=lambda u: CorpusFollowerAssociation(follower=u) ) user = db.relationship('User', back_populates='corpora') # "static" attributes