Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 3 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 4 | from core.models import * |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 5 | from planetstack.config import Config |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 6 | from observer.deleters import deleters |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 7 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 8 | import os |
| 9 | import base64 |
Tony Mack | 5c0c455 | 2013-07-03 09:36:51 -0400 | [diff] [blame] | 10 | from fofum import Fofum |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 11 | import json |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 12 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 13 | # decorator that marks dispatachable event methods |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 14 | def event(func): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 15 | setattr(func, 'event', func.__name__) |
| 16 | return func |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 17 | |
| 18 | class EventHandler: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 19 | # This code is currently not in use. |
| 20 | def __init__(self): |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 21 | pass |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 22 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 23 | @staticmethod |
| 24 | def get_events(): |
| 25 | events = [] |
| 26 | for name in dir(EventHandler): |
| 27 | attribute = getattr(EventHandler, name) |
| 28 | if hasattr(attribute, 'event'): |
| 29 | events.append(getattr(attribute, 'event')) |
| 30 | return events |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 31 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 32 | def dispatch(self, event, *args, **kwds): |
| 33 | if hasattr(self, event): |
| 34 | return getattr(self, event)(*args, **kwds) |
| 35 | |
| 36 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 37 | class EventSender: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 38 | def __init__(self,user=None,clientid=None): |
| 39 | try: |
| 40 | clid = Config().feefie_client_id |
| 41 | user = Config().feefie_client_user |
| 42 | except: |
| 43 | clid = 'planetstack_core_team' |
| 44 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 45 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 46 | self.fofum = Fofum(user=user) |
| 47 | self.fofum.make(clid) |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 48 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 49 | def fire(self,**args): |
| 50 | self.fofum.fire(json.dumps(args)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 51 | |
| 52 | class EventListener: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 53 | def __init__(self,wake_up=None): |
| 54 | self.handler = EventHandler() |
| 55 | self.wake_up = wake_up |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 56 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 57 | def handle_event(self, payload): |
| 58 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 59 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 60 | try: |
| 61 | deletion = payload_dict['deletion_flag'] |
| 62 | if (deletion): |
| 63 | model = payload_dict['model'] |
| 64 | pk = payload_dict['pk'] |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 65 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 66 | for deleter in deleters[model]: |
| 67 | deleter(pk) |
| 68 | except: |
| 69 | deletion = False |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 70 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 71 | if (not deletion and self.wake_up): |
| 72 | self.wake_up() |
| 73 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 74 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 75 | def run(self): |
| 76 | # This is our unique client id, to be used when firing and receiving events |
| 77 | # It needs to be generated once and placed in the config file |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 78 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 79 | try: |
| 80 | clid = Config().feefie_client_id |
| 81 | user = Config().feefie_client_user |
| 82 | except: |
| 83 | clid = 'planetstack_core_team' |
| 84 | user = 'pl' |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 85 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 86 | f = Fofum(user=user) |
| 87 | |
| 88 | listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event)) |
| 89 | listener_thread.start() |