~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Stuart Bishop
  • Date: 2011-09-28 12:49:24 UTC
  • mfrom: (9893.10.1 trivial)
  • mto: This revision was merged to the branch mainline in revision 14178.
  • Revision ID: stuart.bishop@canonical.com-20110928124924-m5a22fymqghw6c5i
Merged trivial into distinct-db-users.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Long-poll life-cycle adapters."""
 
5
 
 
6
from __future__ import absolute_import
 
7
 
 
8
__metaclass__ = type
 
9
__all__ = []
 
10
 
 
11
from lazr.lifecycle.interfaces import (
 
12
    IObjectCreatedEvent,
 
13
    IObjectDeletedEvent,
 
14
    IObjectModifiedEvent,
 
15
    )
 
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
 
20
 
 
21
from lp.services.longpoll.adapters.event import (
 
22
    generate_event_key,
 
23
    LongPollEvent,
 
24
    )
 
25
from lp.services.longpoll.interfaces import (
 
26
    ILongPollEvent,
 
27
    long_poll_event,
 
28
    )
 
29
 
 
30
 
 
31
@long_poll_event(Storm)
 
32
class LongPollStormEvent(LongPollEvent):
 
33
    """A `ILongPollEvent` for events of `Storm` objects.
 
34
 
 
35
    This class knows how to construct a stable event key given a Storm object.
 
36
    """
 
37
 
 
38
    @property
 
39
    def event_key(self):
 
40
        """See `ILongPollEvent`.
 
41
 
 
42
        Constructs the key from the table name and primary key values of the
 
43
        Storm model object.
 
44
        """
 
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))
 
50
 
 
51
 
 
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")
 
57
 
 
58
 
 
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")
 
64
 
 
65
 
 
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)