-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisteners.py
47 lines (36 loc) · 1.81 KB
/
listeners.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: UTF-8 -*-
# Copyright (C) 2010 Sylvain Taverne <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
listeners = {}
def register_listener(class_id, property_name, listener):
print '=> Register listener (%s,%s)' % (class_id, property_name)
if not listeners.has_key((class_id, property_name)):
listeners[(class_id, property_name)] = []
listeners[(class_id, property_name)].append(listener)
def alert_listerners(action, resource, class_id, property_name, old_value, new_value):
for listerner in listeners.get((class_id, property_name), []):
listerner.alert(action, resource, class_id, property_name, old_value, new_value)
# Works on progress
# We want to use listener to alert from stock operations for example:
#
# def register_listeners(self):
# register_listener('product', 'stock-quantity', self)
# register_listener('product-declination', 'stock-quantity', self)
#
#
# def alert(self, action, resource, class_id, property_name, old_value, new_value):
# operations = self.get_resource('operations')
# operations.handler.add_record({'reference': resource.get_property('reference'),
# 'quantity': old_value - new_value})