Add function to dynamically add followers to CorpusFollowerList

This commit is contained in:
Patrick Jentsch 2023-02-21 16:23:10 +01:00
parent d699fd09e5
commit 68dc8de476
4 changed files with 30 additions and 19 deletions

View File

@ -31,6 +31,16 @@ from .forms import (
) )
@bp.route('/fake-add')
@login_required
def fake_add():
pjentsch = User.query.filter_by(username='pjentsch').first()
alice = Corpus.query.filter_by(title='Alice in Wonderland').first()
pjentsch.follow_corpus(alice)
db.session.commit()
return ''
@bp.route('/<hashid:corpus_id>/is_public/enable', methods=['POST']) @bp.route('/<hashid:corpus_id>/is_public/enable', methods=['POST'])
@login_required @login_required
def enable_corpus_is_public(corpus_id): def enable_corpus_is_public(corpus_id):

View File

@ -1489,22 +1489,22 @@ def ressource_after_insert_handler(mapper, connection, ressource):
socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)
# @db.event.listens_for(CorpusFollowerAssociation, 'after_insert') @db.event.listens_for(CorpusFollowerAssociation, 'after_insert')
# def corpus_follower_association_after_insert_handler(mapper, connection, ressource): def corpus_follower_association_after_insert_handler(mapper, connection, ressource):
# corpus_owner_hashid = ressource.corpus.user.hashid corpus_owner_hashid = ressource.corpus.user.hashid
# corpus_hashid = hashids.encode(ressource.corpus_id) corpus_hashid = hashids.encode(ressource.corpus_id)
# follower_hashid = hashids.encode(ressource.follower_id) follower_hashid = hashids.encode(ressource.follower_id)
# value = ressource.to_json_serializeable() value = ressource.to_json_serializeable()
# # Send a PATCH to the corpus owner # Send a PATCH to the corpus owner
# jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}' jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}'
# jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}] jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}]
# room = f'/users/{corpus_owner_hashid}' room = f'/users/{corpus_owner_hashid}'
# socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)
# # Send a PATCH to the follower # Send a PATCH to the follower
# jsonpatch_path = f'/users/{follower_hashid}/corpus_follower_associations/{ressource.hashid}' jsonpatch_path = f'/users/{follower_hashid}/corpus_follower_associations/{ressource.hashid}'
# jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}] jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}]
# room = f'/users/{follower_hashid}' room = f'/users/{follower_hashid}'
# socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)
@db.event.listens_for(Corpus, 'after_update') @db.event.listens_for(Corpus, 'after_update')

View File

@ -175,8 +175,8 @@ class CorpusFollowerList extends ResourceList {
for (let operation of filteredPatch) { for (let operation of filteredPatch) {
switch(operation.op) { switch(operation.op) {
case 'add': { case 'add': {
// let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/corpus_follower_associations/([A-Za-z0-9]*)$`); let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/corpus_follower_associations/([A-Za-z0-9]*)$`);
// if (re.test(operation.path)) {this.add(operation.value);} if (re.test(operation.path)) {this.add(operation.value);}
break; break;
} }
case 'remove': { case 'remove': {

View File

@ -43,7 +43,8 @@ class ResourceList {
} }
add(resources, callback) { add(resources, callback) {
let values = resources.map((resource) => { let tmp = Array.isArray(resources) ? resources : [resources];
let values = tmp.map((resource) => {
return this.mapResourceToValue(resource); return this.mapResourceToValue(resource);
}); });
this.listjs.add(values, (items) => { this.listjs.add(values, (items) => {