Merge branch 'public-corpus' of gitlab.ub.uni-bielefeld.de:sfb1288inf/nopaque into public-corpus

This commit is contained in:
Patrick Jentsch 2023-05-11 16:33:28 +02:00
commit 1c47d2a346
2 changed files with 78 additions and 76 deletions

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)

View File

@ -12,7 +12,7 @@ def get_user(user_hashid, backrefs=False, relationships=False):
user = User.query.get(user_id) user = User.query.get(user_id)
if user is None: if user is None:
return {'status': 404, 'statusText': 'Not found'} return {'status': 404, 'statusText': 'Not found'}
if not (user == current_user or current_user.is_administrator): if not (user == current_user or current_user.is_administrator()):
return {'status': 403, 'statusText': 'Forbidden'} return {'status': 403, 'statusText': 'Forbidden'}
return { return {
'body': user.to_json_serializeable( 'body': user.to_json_serializeable(
@ -24,25 +24,6 @@ def get_user(user_hashid, backrefs=False, relationships=False):
} }
# @socketio.on('GET /users/<user_id>')
# @socketio_login_required
# def get_user(user_hashid):
# user_id = hashids.decode(user_hashid)
# user = User.query.get(user_id)
# if user is None:
# return {'options': {'status': 404, 'statusText': 'Not found'}}
# if not (user == current_user or current_user.is_administrator):
# return {'options': {'status': 403, 'statusText': 'Forbidden'}}
# return {
# 'body': user.to_json_serializable2(),
# 'options': {
# 'status': 200,
# 'statusText': 'OK',
# 'headers': {'Content-Type: application/json'}
# }
# }
@socketio.on('SUBSCRIBE /users/<user_id>') @socketio.on('SUBSCRIBE /users/<user_id>')
@socketio_login_required @socketio_login_required
def subscribe_user(user_hashid): def subscribe_user(user_hashid):
@ -50,7 +31,7 @@ def subscribe_user(user_hashid):
user = User.query.get(user_id) user = User.query.get(user_id)
if user is None: if user is None:
return {'status': 404, 'statusText': 'Not found'} return {'status': 404, 'statusText': 'Not found'}
if not (user == current_user or current_user.is_administrator): if not (user == current_user or current_user.is_administrator()):
return {'status': 403, 'statusText': 'Forbidden'} return {'status': 403, 'statusText': 'Forbidden'}
join_room(f'/users/{user.hashid}') join_room(f'/users/{user.hashid}')
return {'status': 200, 'statusText': 'OK'} return {'status': 200, 'statusText': 'OK'}
@ -63,7 +44,36 @@ def unsubscribe_user(user_hashid):
user = User.query.get(user_id) user = User.query.get(user_id)
if user is None: if user is None:
return {'status': 404, 'statusText': 'Not found'} return {'status': 404, 'statusText': 'Not found'}
if not (user == current_user or current_user.is_administrator): if not (user == current_user or current_user.is_administrator()):
return {'status': 403, 'statusText': 'Forbidden'} return {'status': 403, 'statusText': 'Forbidden'}
leave_room(f'/users/{user.hashid}') leave_room(f'/users/{user.hashid}')
return {'status': 200, 'statusText': 'OK'} return {'status': 200, 'statusText': 'OK'}
# @socketio.on('GET User')
# @socketio_login_required
# def n_get_user(user_hashid):
# # This constructs a JSON response which can easily be converted to a Response object
# # Ref: https://developer.mozilla.org/en-US/docs/Web/API/Response/Response
# user_id = hashids.decode(user_hashid)
# user = User.query.get(user_id)
# if user is None:
# return {'options': {'status': 404, 'statusText': 'Not found'}}
# if not (user == current_user or current_user.is_administrator()):
# return {'options': {'status': 403, 'statusText': 'Forbidden'}}
# body = {
# 'id': user.hashid,
# # ...
# 'relationships': {
# 'corpora': {corpus.hashid for corpus in user.corpora},
# 'jobs': [job.hashid for job in user.jobs]
# }
# }
# return {
# 'body': user.to_json_serializable(),
# 'options': {
# 'status': 200,
# 'statusText': 'OK',
# 'headers': {'Content-Type: application/json'}
# }
# }