comparison idavoll/backend.py @ 90:59378610b16e

Implement node purging and node deletion.
author Ralph Meijer <ralphm@ik.nu>
date Wed, 17 Nov 2004 20:43:13 +0000
parents ec557449d1aa
children 878a5b7697f2
comparison
equal deleted inserted replaced
89:ccf699adae5e 90:59378610b16e
50 50
51 def create_node(self, node_id, requestor): 51 def create_node(self, node_id, requestor):
52 """ Create a node. 52 """ Create a node.
53 53
54 @return: a deferred that fires when the node has been created. 54 @return: a deferred that fires when the node has been created.
55 """
56
57 class INodeDeletionService(components.Interface):
58 """ A service for deleting nodes. """
59
60 def register_pre_delete(self, pre_delete_fn):
61 """ Register a callback that is called just before a node deletion.
62
63 The function C{pre_deleted_fn} is added to a list of functions
64 to be called just before deletion of a node. The callback
65 C{pre_delete_fn} is called with the C{node_id} that is about to be
66 deleted and should return a deferred that returns a list of deferreds
67 that are to be fired after deletion. The backend collects the lists
68 from all these callbacks before actually deleting the node in question.
69 After deletion all collected deferreds are fired to do post-processing.
70
71 The idea is that you want to be able to collect data from the
72 node before deleting it, for example to get a list of subscribers
73 that have to be notified after the node has been deleted. To do this,
74 C{pre_delete_fn} fetches the subscriber list and passes this
75 list to a callback attached to a deferred that it sets up. This
76 deferred is returned in the list of deferreds.
77 """
78
79 def get_subscribers(self, node_id):
80 """ Get node subscriber list.
81
82 @return: a deferred that fires with the list of subscribers.
83 """
84
85 def delete_node(self, node_id, requestor):
86 """ Delete a node.
87
88 @return: a deferred that fires when the node has been deleted.
55 """ 89 """
56 90
57 class IPublishService(components.Interface): 91 class IPublishService(components.Interface):
58 """ A service for publishing items to a node. """ 92 """ A service for publishing items to a node. """
59 93
377 def _do_notify_retraction(self, result, node_id): 411 def _do_notify_retraction(self, result, node_id):
378 self.parent.dispatch({ 'item_ids': result, 'node_id': node_id }, 412 self.parent.dispatch({ 'item_ids': result, 'node_id': node_id },
379 '//event/pubsub/retract') 413 '//event/pubsub/retract')
380 414
381 def purge_node(self, node_id, requestor): 415 def purge_node(self, node_id, requestor):
382 pass 416 d1 = self.parent.storage.get_node_configuration(node_id)
417 d2 = self.parent.storage.get_affiliation(node_id, requestor)
418 d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
419 d.addErrback(lambda x: x.value[0])
420 d.addCallback(self._do_purge, node_id)
421 return d
422
423 def _do_purge(self, result, node_id):
424 configuration = result[0][1]
425 persist_items = configuration["persist_items"]
426 affiliation = result[1][1]
427
428 if affiliation != 'owner':
429 raise NotAuthorized
430
431 if not persist_items:
432 raise NodeNotPersistent
433
434 d = self.parent.storage.purge_node(node_id)
435 d.addCallback(self._do_notify_purge, node_id)
436 return d
437
438 def _do_notify_purge(self, result, node_id):
439 self.parent.dispatch(node_id, '//event/pubsub/purge')
440
441 class NodeDeletionService(service.Service):
442
443 __implements__ = INodeDeletionService,
444
445 def __init__(self):
446 self._callback_list = []
447
448 def register_pre_delete(self, pre_delete_fn):
449 self._callback_list.append(pre_delete_fn)
450
451 def get_subscribers(self, node_id):
452 return self.parent.storage.get_subscribers(node_id)
453
454 def delete_node(self, node_id, requestor):
455 d1 = self.parent.storage.get_node_configuration(node_id)
456 d2 = self.parent.storage.get_affiliation(node_id, requestor)
457 d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
458 d.addErrback(lambda x: x.value[0])
459 d.addCallback(self._do_pre_delete, node_id)
460 return d
461
462 def _do_pre_delete(self, result, node_id):
463 configuration = result[0][1]
464 persist_items = configuration["persist_items"]
465 affiliation = result[1][1]
466
467 if affiliation != 'owner':
468 raise NotAuthorized
469
470 d = defer.DeferredList([cb(node_id) for cb in self._callback_list],
471 consumeErrors=1)
472 d.addCallback(self._do_delete, node_id)
473
474 def _do_delete(self, result, node_id):
475 dl = []
476 for succeeded, r in result:
477 if succeeded and r:
478 dl.extend(r)
479
480 d = self.parent.storage.delete_node(node_id)
481 d.addCallback(self._do_notify_delete, dl)
482
483 return d
484
485 def _do_notify_delete(self, result, dl):
486 for d in dl:
487 d.callback(None)