1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Long-poll life-cycle adapters."""
6
from __future__ import absolute_import
11
from lazr.lifecycle.interfaces import (
16
from storm.base import Storm
17
from storm.info import get_obj_info
18
from zope.component import adapter
19
from zope.interface.interfaces import IAttribute
21
from lp.services.longpoll.adapters.event import (
25
from lp.services.longpoll.interfaces import (
31
@long_poll_event(Storm)
32
class LongPollStormEvent(LongPollEvent):
33
"""A `ILongPollEvent` for events of `Storm` objects.
35
This class knows how to construct a stable event key given a Storm object.
40
"""See `ILongPollEvent`.
42
Constructs the key from the table name and primary key values of the
45
cls_info = get_obj_info(self.source).cls_info
46
return generate_event_key(
47
cls_info.table.name.lower(), *(
48
primary_key_column.__get__(self.source)
49
for primary_key_column in cls_info.primary_key))
52
@adapter(Storm, IObjectCreatedEvent)
53
def object_created(model_instance, object_event):
54
"""Subscription handler for `Storm` creation events."""
55
event = ILongPollEvent(model_instance)
56
event.emit(what="created")
59
@adapter(Storm, IObjectDeletedEvent)
60
def object_deleted(model_instance, object_event):
61
"""Subscription handler for `Storm` deletion events."""
62
event = ILongPollEvent(model_instance)
63
event.emit(what="deleted")
66
@adapter(Storm, IObjectModifiedEvent)
67
def object_modified(model_instance, object_event):
68
"""Subscription handler for `Storm` modification events."""
69
edited_fields = object_event.edited_fields
70
if edited_fields is not None and len(edited_fields) != 0:
71
edited_field_names = sorted(
72
(field.__name__ if IAttribute.providedBy(field) else field)
73
for field in edited_fields)
74
event = ILongPollEvent(model_instance)
75
event.emit(what="modified", edited_fields=edited_field_names)