Update CorpusFollowerAssociation table

This commit is contained in:
Patrick Jentsch 2023-02-21 11:05:09 +01:00
parent 8168a2384f
commit 8d70e93856
4 changed files with 100 additions and 54 deletions

View File

@ -74,15 +74,15 @@ def disable_corpus_is_public(corpus_id):
@bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/unfollow', methods=['POST']) @bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/unfollow', methods=['POST'])
@login_required @login_required
def unfollow_corpus(corpus_id, follower_id): def unfollow_corpus(corpus_id, follower_id):
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, following_user_id=follower_id).first_or_404() corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus_follower_association.followed_corpus.user == current_user follower = User.query.get_or_404(follower_id)
or corpus_follower_association.following_user == current_user if not (corpus.user == current_user or follower == current_user or current_user.is_administrator()):
or current_user.is_administrator()):
abort(403) abort(403)
if not corpus_follower_association.following_user.is_following_corpus(corpus_follower_association.followed_corpus): if not follower.is_following_corpus(corpus):
abort(409) # 'User is not following the corpus' abort(409) # 'User is not following the corpus'
corpus_follower_association.following_user.unfollow_corpus(corpus_follower_association.followed_corpus) follower.unfollow_corpus(corpus)
db.session.commit() db.session.commit()
flash(f'{follower.username} is not following {corpus.title} anymore', category='corpus')
return '', 204 return '', 204
@ -103,10 +103,9 @@ def add_permission(corpus_id, follower_id, permission_name):
try: try:
permission = CorpusFollowerPermission[permission_name] permission = CorpusFollowerPermission[permission_name]
except KeyError: except KeyError:
abort(409) # 'Permission "{permission_name}" does not exist' abort(409) # f'Permission "{permission_name}" does not exist'
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, following_user_id=follower_id).first_or_404() corpus_follower_association = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=follower_id).first_or_404()
if not (corpus_follower_association.followed_corpus.user == current_user if not (corpus_follower_association.corpus.user == current_user or current_user.is_administrator()):
or current_user.is_administrator()):
abort(403) abort(403)
corpus_follower_association.add_permission(permission) corpus_follower_association.add_permission(permission)
db.session.commit() db.session.commit()
@ -119,9 +118,8 @@ def remove_permission(corpus_id, follower_id, permission_name):
permission = CorpusFollowerPermission[permission_name] permission = CorpusFollowerPermission[permission_name]
except KeyError: except KeyError:
return make_response(f'Permission "{permission_name}" does not exist', 409) return make_response(f'Permission "{permission_name}" does not exist', 409)
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, following_user_id=follower_id).first_or_404() corpus_follower_association = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=follower_id).first_or_404()
if not (corpus_follower_association.followed_corpus.user == current_user if not (corpus_follower_association.corpus.user == current_user or current_user.is_administrator()):
or current_user.is_administrator()):
abort(403) abort(403)
corpus_follower_association.remove_permission(permission) corpus_follower_association.remove_permission(permission)
db.session.commit() db.session.commit()

View File

@ -296,16 +296,16 @@ class CorpusFollowerAssociation(HashidMixin, db.Model):
# Primary key # Primary key
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
# Foreign keys # Foreign keys
following_user_id = db.Column(db.Integer, db.ForeignKey('users.id')) corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id'))
followed_corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id')) follower_id = db.Column(db.Integer, db.ForeignKey('users.id'))
# Fields # Fields
permissions = db.Column(db.Integer, default=0, nullable=False) permissions = db.Column(db.Integer, default=0, nullable=False)
# Relationships # Relationships
followed_corpus = db.relationship('Corpus', back_populates='following_user_associations') corpus = db.relationship('Corpus', back_populates='corpus_follower_associations')
following_user = db.relationship('User', back_populates='followed_corpus_associations') follower = db.relationship('User', back_populates='corpus_follower_associations')
def __repr__(self): def __repr__(self):
return f'<CorpusFollowerAssociation {self.following_user.__repr__()} ~ {self.followed_corpus.__repr__()}>' return f'<CorpusFollowerAssociation {self.follower.__repr__()} ~ {self.corpus.__repr__()}>'
def has_permission(self, permission: CorpusFollowerPermission): def has_permission(self, permission: CorpusFollowerPermission):
return self.permissions & permission.value == permission.value return self.permissions & permission.value == permission.value
@ -325,8 +325,8 @@ class CorpusFollowerAssociation(HashidMixin, db.Model):
x.name for x in CorpusFollowerPermission x.name for x in CorpusFollowerPermission
if self.has_permission(x) if self.has_permission(x)
], ],
'followed_corpus': self.followed_corpus.to_json_serializeable(), 'corpus': self.corpus.to_json_serializeable(),
'following_user': self.following_user.to_json_serializeable() 'follower': self.follower.to_json_serializeable()
} }
return json_serializeable return json_serializeable
@ -368,15 +368,15 @@ class User(HashidMixin, UserMixin, db.Model):
cascade='all, delete-orphan', cascade='all, delete-orphan',
lazy='dynamic' lazy='dynamic'
) )
followed_corpus_associations = db.relationship( corpus_follower_associations = db.relationship(
'CorpusFollowerAssociation', 'CorpusFollowerAssociation',
back_populates='following_user', back_populates='follower',
cascade='all, delete-orphan' cascade='all, delete-orphan'
) )
followed_corpora = association_proxy( followed_corpora = association_proxy(
'followed_corpus_associations', 'corpus_follower_associations',
'followed_corpus', 'corpus',
creator=lambda c: CorpusFollowerAssociation(followed_corpus=c) creator=lambda c: CorpusFollowerAssociation(corpus=c)
) )
jobs = db.relationship( jobs = db.relationship(
'Job', 'Job',
@ -652,9 +652,9 @@ class User(HashidMixin, UserMixin, db.Model):
json_serializeable['role'] = \ json_serializeable['role'] = \
self.role.to_json_serializeable(backrefs=True) self.role.to_json_serializeable(backrefs=True)
if relationships: if relationships:
json_serializeable['followed_corpus_associations'] = { json_serializeable['corpus_follower_associations'] = {
x.hashid: x.to_json_serializeable() x.hashid: x.to_json_serializeable()
for x in self.followed_corpus_associations for x in self.corpus_follower_associations
} }
json_serializeable['corpora'] = { json_serializeable['corpora'] = {
x.hashid: x.to_json_serializeable(relationships=True) x.hashid: x.to_json_serializeable(relationships=True)
@ -1319,15 +1319,15 @@ class Corpus(HashidMixin, db.Model):
lazy='dynamic', lazy='dynamic',
cascade='all, delete-orphan' cascade='all, delete-orphan'
) )
following_user_associations = db.relationship( corpus_follower_associations = db.relationship(
'CorpusFollowerAssociation', 'CorpusFollowerAssociation',
back_populates='followed_corpus', back_populates='corpus',
cascade='all, delete-orphan' cascade='all, delete-orphan'
) )
following_users = association_proxy( followers = association_proxy(
'following_user_associations', 'corpus_follower_associations',
'following_user', 'follower',
creator=lambda u: CorpusFollowerAssociation(following_user=u) creator=lambda u: CorpusFollowerAssociation(followers=u)
) )
user = db.relationship('User', back_populates='corpora') user = db.relationship('User', back_populates='corpora')
# "static" attributes # "static" attributes
@ -1429,9 +1429,9 @@ class Corpus(HashidMixin, db.Model):
json_serializeable['user'] = \ json_serializeable['user'] = \
self.user.to_json_serializeable(backrefs=True) self.user.to_json_serializeable(backrefs=True)
if relationships: if relationships:
json_serializeable['following_user_associations'] = { json_serializeable['corpus_follower_associations'] = {
x.hashid: x.to_json_serializeable() x.hashid: x.to_json_serializeable()
for x in self.following_user_associations for x in self.corpus_follower_associations
} }
json_serializeable['files'] = { json_serializeable['files'] = {
x.hashid: x.to_json_serializeable(relationships=True) x.hashid: x.to_json_serializeable(relationships=True)

View File

@ -19,7 +19,7 @@ class CorpusFollowerList extends ResourceList {
}); });
}); });
app.getUser(this.userId).then((user) => { app.getUser(this.userId).then((user) => {
this.add(Object.values(user.corpora[this.corpusId].following_user_associations)); this.add(Object.values(user.corpora[this.corpusId].corpus_follower_associations));
this.isInitialized = true; this.isInitialized = true;
}); });
} }
@ -32,20 +32,26 @@ class CorpusFollowerList extends ResourceList {
<td><b class="username"><b></td> <td><b class="username"><b></td>
<td><span class="full-name"></span><br><i class="about-me"></i></td> <td><span class="full-name"></span><br><i class="about-me"></i></td>
<td> <td>
<span class="disable-on-click">
<label> <label>
<input ${values['permission-can-VIEW'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission" data-permission="VIEW" type="checkbox"> <input ${values['permission-can-VIEW'] ? 'checked' : ''} class="permission-can-VIEW list-action-trigger" data-list-action="toggle-permission" data-permission="VIEW" type="checkbox">
<span>View</span> <span>View</span>
</label> </label>
</span>
<br> <br>
<span class="disable-on-click">
<label> <label>
<input ${values['permission-can-CONTRIBUTE'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission" data-permission="CONTRIBUTE" type="checkbox"> <input ${values['permission-can-CONTRIBUTE'] ? 'checked' : ''} class="permission-can-CONTRIBUTE list-action-trigger" data-list-action="toggle-permission" data-permission="CONTRIBUTE" type="checkbox">
<span>Contribute</span> <span>Contribute</span>
</label> </label>
</span>
<br> <br>
<span class="disable-on-click">
<label> <label>
<input ${values['permission-can-ADMINISTRATE'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission" data-permission="ADMINISTRATE" type="checkbox"> <input ${values['permission-can-ADMINISTRATE'] ? 'checked' : ''} class="permission-can-ADMINISTRATE list-action-trigger" data-list-action="toggle-permission" data-permission="ADMINISTRATE" type="checkbox">
<span>Administrate</span> <span>Administrate</span>
</label> </label>
</span>
</td> </td>
<td class="right-align"> <td class="right-align">
<a class="list-action-trigger btn-floating red waves-effect waves-light" data-list-action="unfollow-request"><i class="material-icons">delete</i></a> <a class="list-action-trigger btn-floating red waves-effect waves-light" data-list-action="unfollow-request"><i class="material-icons">delete</i></a>
@ -95,14 +101,13 @@ class CorpusFollowerList extends ResourceList {
} }
mapResourceToValue(corpusFollowerAssociation) { mapResourceToValue(corpusFollowerAssociation) {
let user = corpusFollowerAssociation.following_user;
return { return {
'id': corpusFollowerAssociation.id, 'id': corpusFollowerAssociation.id,
'follower-id': user.id, 'follower-id': corpusFollowerAssociation.follower.id,
'avatar': user.avatar ? `/users/${user.id}/avatar` : '/static/images/user_avatar.png', 'avatar': corpusFollowerAssociation.follower.avatar ? `/users/${corpusFollowerAssociation.follower.id}/avatar` : '/static/images/user_avatar.png',
'username': user.username, 'username': corpusFollowerAssociation.follower.username,
'full-name': user.full_name ? user.full_name : '', 'full-name': corpusFollowerAssociation.follower.full_name ? corpusFollowerAssociation.follower.full_name : '',
'about-me': user.about_me ? user.about_me : '', 'about-me': corpusFollowerAssociation.follower.about_me ? corpusFollowerAssociation.follower.about_me : '',
'permission-can-VIEW': corpusFollowerAssociation.permissions.includes('VIEW'), 'permission-can-VIEW': corpusFollowerAssociation.permissions.includes('VIEW'),
'permission-can-CONTRIBUTE': corpusFollowerAssociation.permissions.includes('CONTRIBUTE'), 'permission-can-CONTRIBUTE': corpusFollowerAssociation.permissions.includes('CONTRIBUTE'),
'permission-can-ADMINISTRATE': corpusFollowerAssociation.permissions.includes('ADMINISTRATE') 'permission-can-ADMINISTRATE': corpusFollowerAssociation.permissions.includes('ADMINISTRATE')
@ -141,6 +146,7 @@ class CorpusFollowerList extends ResourceList {
} }
onClick(event) { onClick(event) {
if (event.target.closest('.disable-on-click') !== null) {return;}
let listItemElement = event.target.closest('.list-item[data-id]'); let listItemElement = event.target.closest('.list-item[data-id]');
if (listItemElement === null) {return;} if (listItemElement === null) {return;}
let itemId = listItemElement.dataset.id; let itemId = listItemElement.dataset.id;

View File

@ -0,0 +1,42 @@
"""empty message
Revision ID: 03c7211f089d
Revises: 5fe6a6c7870c
Create Date: 2023-02-21 09:57:22.005883
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '03c7211f089d'
down_revision = '5fe6a6c7870c'
branch_labels = None
depends_on = None
def upgrade():
op.alter_column(
'corpus_follower_associations',
'followed_corpus_id',
new_column_name='corpus_id'
)
op.alter_column(
'corpus_follower_associations',
'following_user_id',
new_column_name='follower_id'
)
def downgrade():
op.alter_column(
'corpus_follower_associations',
'corpus_id',
new_column_name='followed_corpus_id'
)
op.alter_column(
'corpus_follower_associations',
'follower_id',
new_column_name='following_user_id'
)