~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/longpoll/adapters/storm.py

[r=abentley,
        rvb][no-qa] UpdatePreviewDiffJob now issues an ObjectModifiedEvent
        when it updates the preview_diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
21
20
 
22
21
from lp.services.longpoll.adapters.event import (
23
22
    generate_event_key,
24
23
    LongPollEvent,
25
24
    )
26
 
from lp.services.longpoll.interfaces import ILongPollEvent
27
 
 
28
 
 
 
25
from lp.services.longpoll.interfaces import (
 
26
    ILongPollEvent,
 
27
    long_poll_event,
 
28
    )
 
29
 
 
30
 
 
31
@long_poll_event(Storm)
29
32
class LongPollStormEvent(LongPollEvent):
30
33
    """A `ILongPollEvent` for events of `Storm` objects.
31
34
 
32
35
    This class knows how to construct a stable event key given a Storm object.
33
36
    """
34
37
 
35
 
    implements(ILongPollEvent)
36
 
 
37
38
    @property
38
39
    def event_key(self):
39
40
        """See `ILongPollEvent`.
42
43
        Storm model object.
43
44
        """
44
45
        cls_info = get_obj_info(self.source).cls_info
45
 
        key_parts = [cls_info.table.name.lower()]
46
 
        key_parts.extend(
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))
51
50
 
52
51
 
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")
57
 
    event.emit({})
 
55
    event = ILongPollEvent(model_instance)
 
56
    event.emit(what="created")
58
57
 
59
58
 
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")
64
 
    event.emit({})
 
62
    event = ILongPollEvent(model_instance)
 
63
    event.emit(what="deleted")
65
64
 
66
65
 
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)