blob: 60b615ac5020184e61ae5a4904ada30bed93edd8 [file] [log] [blame]
Tony Mack79a49c82013-06-15 23:51:57 -04001import threading
2import requests, json
Sapan Bhatia66f4e612013-07-02 12:12:38 -04003
Sapan Bhatia66f4e612013-07-02 12:12:38 -04004from planetstack.config import Config
Sapan Bhatiadbaf1932013-09-03 11:28:52 -04005from observer.deleters import deleters
Tony Mack79a49c82013-06-15 23:51:57 -04006
Sapan Bhatia9182b322013-06-25 16:22:14 -04007import os
8import base64
Tony Mack5c0c4552013-07-03 09:36:51 -04009from fofum import Fofum
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040010import json
Sapan Bhatia9182b322013-06-25 16:22:14 -040011
Tony Mackce79de02013-09-24 10:12:33 -040012# decorator that marks dispatachable event methods
Tony Mack79a49c82013-06-15 23:51:57 -040013def event(func):
Tony Mackce79de02013-09-24 10:12:33 -040014 setattr(func, 'event', func.__name__)
15 return func
Tony Mack79a49c82013-06-15 23:51:57 -040016
17class EventHandler:
Tony Mackce79de02013-09-24 10:12:33 -040018 # This code is currently not in use.
19 def __init__(self):
Tony Mack387a73f2013-09-18 07:59:14 -040020 pass
Tony Mack79a49c82013-06-15 23:51:57 -040021
Tony Mackce79de02013-09-24 10:12:33 -040022 @staticmethod
23 def get_events():
24 events = []
25 for name in dir(EventHandler):
26 attribute = getattr(EventHandler, name)
27 if hasattr(attribute, 'event'):
28 events.append(getattr(attribute, 'event'))
29 return events
Tony Mack79a49c82013-06-15 23:51:57 -040030
Tony Mackce79de02013-09-24 10:12:33 -040031 def dispatch(self, event, *args, **kwds):
32 if hasattr(self, event):
33 return getattr(self, event)(*args, **kwds)
34
35
Sapan Bhatia66f4e612013-07-02 12:12:38 -040036class EventSender:
Tony Mackce79de02013-09-24 10:12:33 -040037 def __init__(self,user=None,clientid=None):
38 try:
39 clid = Config().feefie_client_id
40 user = Config().feefie_client_user
41 except:
42 clid = 'planetstack_core_team'
43 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -040044
Tony Mackce79de02013-09-24 10:12:33 -040045 self.fofum = Fofum(user=user)
46 self.fofum.make(clid)
Sapan Bhatia66f4e612013-07-02 12:12:38 -040047
Tony Mackce79de02013-09-24 10:12:33 -040048 def fire(self,**args):
49 self.fofum.fire(json.dumps(args))
Tony Mack79a49c82013-06-15 23:51:57 -040050
51class EventListener:
Tony Mackce79de02013-09-24 10:12:33 -040052 def __init__(self,wake_up=None):
53 self.handler = EventHandler()
54 self.wake_up = wake_up
Tony Mack79a49c82013-06-15 23:51:57 -040055
Tony Mackce79de02013-09-24 10:12:33 -040056 def handle_event(self, payload):
57 payload_dict = json.loads(payload)
Sapan Bhatia43c3a772013-07-03 11:19:07 -040058
Tony Mackce79de02013-09-24 10:12:33 -040059 try:
60 deletion = payload_dict['deletion_flag']
61 if (deletion):
62 model = payload_dict['model']
63 pk = payload_dict['pk']
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040064
Tony Mackce79de02013-09-24 10:12:33 -040065 for deleter in deleters[model]:
66 deleter(pk)
67 except:
68 deletion = False
Sapan Bhatia66f4e612013-07-02 12:12:38 -040069
Tony Mackce79de02013-09-24 10:12:33 -040070 if (not deletion and self.wake_up):
71 self.wake_up()
72
Sapan Bhatia9182b322013-06-25 16:22:14 -040073
Tony Mackce79de02013-09-24 10:12:33 -040074 def run(self):
75 # This is our unique client id, to be used when firing and receiving events
76 # It needs to be generated once and placed in the config file
Sapan Bhatia66f4e612013-07-02 12:12:38 -040077
Tony Mackce79de02013-09-24 10:12:33 -040078 try:
79 clid = Config().feefie_client_id
80 user = Config().feefie_client_user
81 except:
82 clid = 'planetstack_core_team'
83 user = 'pl'
Sapan Bhatiab9c59342013-09-03 10:21:13 -040084
Tony Mackce79de02013-09-24 10:12:33 -040085 f = Fofum(user=user)
86
87 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
88 listener_thread.start()