44
44
45
45
from .permissions import SchemaAdminAction , SchemaReadAction
46
46
from .serializers import resolved_schemas_serializer , schema_serializer
47
+ from .signals import deposit_mapping_updated , record_mapping_updated
47
48
48
49
ES_FORBIDDEN = r' ,"\<*>|?'
49
50
@@ -208,11 +209,30 @@ def add_read_access_for_all_users(self):
208
209
"""Give read access to all authenticated users."""
209
210
assert self .id
210
211
211
- db .session .add (
212
- ActionSystemRoles .allow (SchemaReadAction (self .id ),
213
- role = authenticated_user ))
212
+ try :
213
+ ActionSystemRoles .query .filter (
214
+ ActionSystemRoles .action == 'schema-object-read' ,
215
+ ActionSystemRoles .argument == str (self .id ),
216
+ ActionSystemRoles .role_name == 'authenticated_user' ).one ()
217
+ except NoResultFound :
218
+ db .session .add (
219
+ ActionSystemRoles .allow (SchemaReadAction (self .id ),
220
+ role = authenticated_user ))
214
221
db .session .flush ()
215
222
223
+ def revoke_access_for_all_users (self ):
224
+ """Revoke read access to all authenticated users."""
225
+ assert self .id
226
+
227
+ try :
228
+ db .session .delete (
229
+ ActionSystemRoles .query .filter (
230
+ ActionSystemRoles .action == 'schema-object-read' ,
231
+ ActionSystemRoles .argument == str (self .id ),
232
+ ActionSystemRoles .role_name == 'authenticated_user' ).one ())
233
+ except NoResultFound :
234
+ pass
235
+
216
236
def give_admin_access_for_user (self , user ):
217
237
"""Give admin access for users."""
218
238
assert self .id
@@ -270,25 +290,6 @@ def name_to_es_name(name):
270
290
return name .replace ('/' , '-' )
271
291
272
292
273
- def create_index (index_name , mapping_body , aliases ):
274
- """Create index in elasticsearch, add under given aliases."""
275
- if not es .indices .exists (index_name ):
276
- current_search .mappings [index_name ] = {} # invenio search needs it
277
-
278
- es .indices .create (index = index_name ,
279
- body = {'mappings' : mapping_body },
280
- ignore = False )
281
-
282
- for alias in aliases :
283
- es .indices .update_aliases (
284
- {'actions' : [{
285
- 'add' : {
286
- 'index' : index_name ,
287
- 'alias' : alias
288
- }
289
- }]})
290
-
291
-
292
293
@event .listens_for (Schema , 'after_insert' )
293
294
def after_insert_schema (target , value , schema ):
294
295
"""On schema insert, create corresponding indexes and aliases in ES."""
@@ -303,6 +304,43 @@ def after_insert_schema(target, value, schema):
303
304
current_cache .delete_memoized (import_string (mappings_imp ))
304
305
305
306
307
+ @event .listens_for (Schema .deposit_mapping , 'set' )
308
+ def after_deposit_mapping_updated (target , value , oldvalue , initiator ):
309
+ """If deposit mapping field was updated:
310
+ * trigger mapping update in ES
311
+ * send signal
312
+ """
313
+ if target .is_indexed :
314
+ if es .indices .exists (target .deposit_index ):
315
+ es .indices .delete (index = target .deposit_index )
316
+
317
+ create_index (target .deposit_index , value , target .deposit_aliases )
318
+ deposit_mapping_updated .send (target )
319
+
320
+ if target .use_deposit_as_record :
321
+ if es .indices .exists (target .record_index ):
322
+ es .indices .delete (index = target .record_index )
323
+
324
+ create_index (target .record_index , value , target .record_aliases )
325
+
326
+ record_mapping_updated .send (target )
327
+
328
+
329
+ @event .listens_for (Schema .record_mapping , 'set' )
330
+ def after_record_mapping_updated (target , value , oldvalue , initiator ):
331
+ """If record mapping field was updated:
332
+ * trigger mapping update in ES
333
+ * send signal
334
+ """
335
+ if target .is_indexed and not target .use_deposit_as_record :
336
+ if es .indices .exists (target .record_index ):
337
+ es .indices .delete (index = target .record_index )
338
+
339
+ create_index (target .record_index , value , target .record_aliases )
340
+
341
+ record_mapping_updated .send (target )
342
+
343
+
306
344
@event .listens_for (Schema , 'after_delete' )
307
345
def before_delete_schema (mapper , connect , schema ):
308
346
"""On schema delete, delete corresponding indexes and aliases in ES."""
@@ -316,7 +354,20 @@ def before_delete_schema(mapper, connect, schema):
316
354
current_cache .delete_memoized (import_string (mappings_imp ))
317
355
318
356
319
- @db .event .listens_for (Schema , 'before_update' , propagate = True )
320
- def timestamp_before_update (mapper , connection , target ):
321
- """Update `updated` property with current time on `before_update` event."""
322
- target .updated = datetime .utcnow ()
357
+ def create_index (index_name , mapping_body , aliases ):
358
+ """Create index in elasticsearch, add under given aliases."""
359
+ if not es .indices .exists (index_name ):
360
+ current_search .mappings [index_name ] = {} # invenio search needs it
361
+
362
+ es .indices .create (index = index_name ,
363
+ body = {'mappings' : mapping_body },
364
+ ignore = False )
365
+
366
+ for alias in aliases :
367
+ es .indices .update_aliases (
368
+ {'actions' : [{
369
+ 'add' : {
370
+ 'index' : index_name ,
371
+ 'alias' : alias
372
+ }
373
+ }]})
0 commit comments