Revert changes and fix some typos

This commit is contained in:
Patrick Jentsch 2023-05-09 14:18:59 +02:00
parent 8c935820e8
commit 91e42d6d92

View File

@ -1678,35 +1678,30 @@ class Corpus(HashidMixin, db.Model):
@db.event.listens_for(JobResult, 'after_delete') @db.event.listens_for(JobResult, 'after_delete')
@db.event.listens_for(SpaCyNLPPipelineModel, 'after_delete') @db.event.listens_for(SpaCyNLPPipelineModel, 'after_delete')
@db.event.listens_for(TesseractOCRPipelineModel, 'after_delete') @db.event.listens_for(TesseractOCRPipelineModel, 'after_delete')
def ressource_after_delete(mapper, connection, ressource): def resource_after_delete(mapper, connection, resource):
jsonpatch = [{'op': 'remove', 'path': ressource.jsonpatch_path}] jsonpatch = [
room = f'/users/{ressource.user_hashid}' {
'op': 'remove',
'path': resource.jsonpatch_path
}
]
room = f'/users/{resource.user_hashid}'
socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)
@db.event.listens_for(CorpusFollowerAssociation, 'after_delete') @db.event.listens_for(CorpusFollowerAssociation, 'after_delete')
def corpus_follower_association_after_delete_handler(mapper, connection, ressource): def cfa_after_delete_handler(mapper, connection, cfa):
corpus_owner_hashid = ressource.corpus.user.hashid jsonpatch_path = f'/users/{cfa.corpus.user.hashid}/corpora/{cfa.corpus.hashid}/corpus_follower_associations/{cfa.hashid}'
corpus_hashid = ressource.corpus.hashid jsonpatch = [
# Send a PATCH to the corpus owner {
jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}' 'op': 'remove',
jsonpatch = [{'op': 'remove', 'path': jsonpatch_path}] 'path': jsonpatch_path
room = f'/users/{corpus_owner_hashid}' }
socketio.emit('PATCH', jsonpatch, room=room)
# Send a PATCH to the followers (the deleted follower and others with permission "MANAGE_FOLLOWERS")
followers = [
x for x in ressource.corpus.corpus_follower_associations
if x.follower_id == ressource.follower_id
or x.role.has_permission('MANAGE_FOLLOWERS')
] ]
for follower in followers: room = f'/users/{cfa.corpus.user.hashid}'
jsonpatch_path = f'/users/{follower.hashid}/corpus_follower_associations/{ressource.hashid}'
jsonpatch = [{'op': 'remove', 'path': jsonpatch_path}]
room = f'/users/{follower.hashid}'
socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)
@db.event.listens_for(Corpus, 'after_insert') @db.event.listens_for(Corpus, 'after_insert')
@db.event.listens_for(CorpusFile, 'after_insert') @db.event.listens_for(CorpusFile, 'after_insert')
@db.event.listens_for(Job, 'after_insert') @db.event.listens_for(Job, 'after_insert')
@ -1714,37 +1709,33 @@ def corpus_follower_association_after_delete_handler(mapper, connection, ressour
@db.event.listens_for(JobResult, 'after_insert') @db.event.listens_for(JobResult, 'after_insert')
@db.event.listens_for(SpaCyNLPPipelineModel, 'after_insert') @db.event.listens_for(SpaCyNLPPipelineModel, 'after_insert')
@db.event.listens_for(TesseractOCRPipelineModel, 'after_insert') @db.event.listens_for(TesseractOCRPipelineModel, 'after_insert')
def ressource_after_insert_handler(mapper, connection, ressource): def resource_after_insert_handler(mapper, connection, resource):
value = ressource.to_json_serializeable() jsonpatch_value = resource.to_json_serializeable()
for attr in mapper.relationships: for attr in mapper.relationships:
value[attr.key] = {} jsonpatch_value[attr.key] = {}
jsonpatch = [ jsonpatch = [
{'op': 'add', 'path': ressource.jsonpatch_path, 'value': value} {
'op': 'add',
'path': resource.jsonpatch_path,
'value': jsonpatch_value
}
] ]
room = f'/users/{ressource.user_hashid}' room = f'/users/{resource.user_hashid}'
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 cfa_after_insert_handler(mapper, connection, cfa):
corpus_owner_hashid = ressource.corpus.user.hashid jsonpatch_value = cfa.to_json_serializeable()
corpus_hashid = ressource.corpus.hashid jsonpatch_path = f'/users/{cfa.corpus.user.hashid}/corpora/{cfa.corpus.hashid}/corpus_follower_associations/{cfa.hashid}'
value = ressource.to_json_serializeable() jsonpatch = [
# Send a PATCH to the corpus owner {
jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}' 'op': 'add',
jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}] 'path': jsonpatch_path,
room = f'/users/{corpus_owner_hashid}' 'value': jsonpatch_value
socketio.emit('PATCH', jsonpatch, room=room) }
# Send a PATCH to the followers (the deleted follower and others with permission "MANAGE_FOLLOWERS")
followers = [
x for x in ressource.corpus.corpus_follower_associations
if x.follower_id == ressource.follower_id
or x.role.has_permission('MANAGE_FOLLOWERS')
] ]
for follower in followers: room = f'/users/{cfa.corpus.user.hashid}'
jsonpatch_path = f'/users/{follower.hashid}/corpus_follower_associations/{ressource.hashid}'
jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}]
room = f'/users/{follower.hashid}'
socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)
@ -1755,28 +1746,29 @@ def corpus_follower_association_after_insert_handler(mapper, connection, ressour
@db.event.listens_for(JobResult, 'after_update') @db.event.listens_for(JobResult, 'after_update')
@db.event.listens_for(SpaCyNLPPipelineModel, 'after_update') @db.event.listens_for(SpaCyNLPPipelineModel, 'after_update')
@db.event.listens_for(TesseractOCRPipelineModel, 'after_update') @db.event.listens_for(TesseractOCRPipelineModel, 'after_update')
def ressource_after_update_handler(mapper, connection, ressource): def resource_after_update_handler(mapper, connection, resource):
jsonpatch = [] jsonpatch = []
for attr in db.inspect(ressource).attrs: for attr in db.inspect(resource).attrs:
if attr.key in mapper.relationships: if attr.key in mapper.relationships:
continue continue
if not attr.load_history().has_changes(): if not attr.load_history().has_changes():
continue continue
jsonpatch_path = f'{resource.jsonpatch_path}/{attr.key}'
if isinstance(attr.value, datetime): if isinstance(attr.value, datetime):
value = f'{attr.value.isoformat()}Z' jsonpatch_value = f'{attr.value.isoformat()}Z'
elif isinstance(attr.value, Enum): elif isinstance(attr.value, Enum):
value = attr.value.name jsonpatch_value = attr.value.name
else: else:
value = attr.value jsonpatch_value = attr.value
jsonpatch.append( jsonpatch.append(
{ {
'op': 'replace', 'op': 'replace',
'path': f'{ressource.jsonpatch_path}/{attr.key}', 'path': jsonpatch_path,
'value': value 'value': jsonpatch_value
} }
) )
if jsonpatch: if jsonpatch:
room = f'/users/{ressource.user_hashid}' room = f'/users/{resource.user_hashid}'
socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)