16
16
from storm.base import Storm
17
17
from storm.info import get_obj_info
18
18
from zope.component import adapter
19
from zope.interface import implements
20
19
from zope.interface.interfaces import IAttribute
22
21
from lp.services.longpoll.adapters.event import (
23
22
generate_event_key,
26
from lp.services.longpoll.interfaces import ILongPollEvent
25
from lp.services.longpoll.interfaces import (
31
@long_poll_event(Storm)
29
32
class LongPollStormEvent(LongPollEvent):
30
33
"""A `ILongPollEvent` for events of `Storm` objects.
32
35
This class knows how to construct a stable event key given a Storm object.
35
implements(ILongPollEvent)
38
39
def event_key(self):
39
40
"""See `ILongPollEvent`.
42
43
Storm model object.
44
45
cls_info = get_obj_info(self.source).cls_info
45
key_parts = [cls_info.table.name.lower()]
47
primary_key_column.__get__(self.source)
48
for primary_key_column in cls_info.primary_key)
49
key_parts.append(self.event)
50
return generate_event_key(*key_parts)
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))
53
52
@adapter(Storm, IObjectCreatedEvent)
54
53
def object_created(model_instance, object_event):
55
54
"""Subscription handler for `Storm` creation events."""
56
event = LongPollStormEvent(model_instance, "created")
55
event = ILongPollEvent(model_instance)
56
event.emit(what="created")
60
59
@adapter(Storm, IObjectDeletedEvent)
61
60
def object_deleted(model_instance, object_event):
62
61
"""Subscription handler for `Storm` deletion events."""
63
event = LongPollStormEvent(model_instance, "deleted")
62
event = ILongPollEvent(model_instance)
63
event.emit(what="deleted")
67
66
@adapter(Storm, IObjectModifiedEvent)
72
71
edited_field_names = sorted(
73
72
(field.__name__ if IAttribute.providedBy(field) else field)
74
73
for field in edited_fields)
75
event = LongPollStormEvent(model_instance, "modified")
76
event.emit({"edited_fields": edited_field_names})
74
event = ILongPollEvent(model_instance)
75
event.emit(what="modified", edited_fields=edited_field_names)