Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 175 lines (139 sloc) 6.333 kb
064f3e5 @johnboxall Initial import.
authored
1 import socket
2 import urllib
3 import urlparse
4 import datetime
5
6 from django.db import models
7 from django.contrib.contenttypes import generic
8 from django.contrib.contenttypes.models import ContentType
9
10
11 TIMEOUT = 3
12
13 class CreatedUpdatedModel(models.Model):
14 created_at = models.DateTimeField(auto_now_add=True)
15 updated_at = models.DateTimeField(auto_now=True)
16
17 class Meta:
18 abstract = True
19
20
21 class Message(CreatedUpdatedModel):
22 """
23 A Message is sent when a WebHook is activated.
24
25 """
26 obj_type = models.ForeignKey('contenttypes.ContentType', help_text="Content type of the object of the message.")
27 obj_id = models.PositiveIntegerField(help_text="ID of the object of the message.")
28 obj = generic.GenericForeignKey("obj_type", "obj_id")
29 payload = models.TextField(blank=True, help_text="Content of the message.")
30 digest = models.CharField(blank=True, max_length=256, help_text="Hexdigest of the message's payload. Used to verify postbacks.")
31 processed = models.BooleanField(default=False)
32
33 def __unicode__(self):
34 return unicode(self.obj)
35
36 def process(self, hook):
37 """
38 If anyone is listening to this message then deliver it to them. Otherwise delete it.
39
40 """
41 if self.has_listeners():
42 self.serialize(hook.fields, hook.serializer)
43 self.deliver()
44 self.processed = True
45 self.save()
46 else:
47 self.delete()
48
49 def deliver(self):
50 """
51 Deliver this message to each Listener by creating a MessageQueue instance and delivering it.
52
53 """
54 for listener in self.listeners:
55 MessageQueue.objects.create(message=self, listener=listener).process()
56
57 def serialize(self, fields, serializer, **kwargs):
58 """
59 Serialize obj using fields and serializer.
60
61 """
62 # ### TODO: Could use a hasher to calculate a message hash here.
63 self.payload = serializer.serialize([self.obj], fields=fields, **kwargs)
64
65 @property
66 def listeners(self):
67 """
68 Return a QuerySet of Listeners for this message.
69
70 Strategy:
71 1. Look at all the Listeners looking at this model and see what properties they are watching.
72 2. Look for all Listeners which have property values equal to the
73
74 """
75 if not hasattr(self, '_listeners_cache'):
76 props = Listener.objects.filter(obj_type=self.obj_type).values_list("obj_property", flat=True).distinct()
77 if len(props):
78 filter_listeners_query = None
79 for property_name in props:
80 query_property_value = {"obj_value": getattr(self.obj, property_name)}
81 query_property_name = {"obj_property": property_name}
82 query = models.Q(**query_property_value) & models.Q(**query_property_name)
83 if filter_listeners_query is None:
84 filter_listeners_query = query
85 else:
86 filter_listeners_query |= query
87 self._listeners_cache = Listener.objects.filter(filter_listeners_query)
88 else:
89 self._listeners_cache = self.obj._default_manager.none()
90 return self._listeners_cache
91
92 def has_listeners(self):
93 """
94 Return True if this Message has any Listeners.
95
96 """
97 return len(self.listeners) > 0
98
99
100 class Listener(CreatedUpdatedModel):
101 """
102 A Listener is waiting waiting at a url for a hook from a model with certain properties.
103
104 """
105 obj_type = models.ForeignKey('contenttypes.ContentType',
106 help_text="The type of object I'm listening to.",
107 related_name="listening_for")
108 obj_property = models.CharField(max_length=32, blank=True, help_text="The property I'm listening for.")
109 obj_value = models.CharField(max_length=32, blank=True, help_text="The value of the property I'm listening for.")
110 url = models.URLField(verify_exists=False, help_text="The URL I'm listening at.")
111 owner_type = models.ForeignKey('contenttypes.ContentType', related_name="listening_to")
112 owner_id = models.PositiveIntegerField()
113 owner = generic.GenericForeignKey('owner_type', 'owner_id')
114
115 def __unicode__(self):
116 return self.url
117
118
119 class MessageQueue(CreatedUpdatedModel):
120 """
121 A instance of a hook message.
122
123 """
124 message = models.ForeignKey('webhooks.Message', help_text="What message is being sent.")
125 listener = models.ForeignKey('webhooks.Listener', help_text="Where this message is being sent.")
126 processed = models.BooleanField(default=False, help_text="True if this message was successfully received.")
127 attempts = models.IntegerField(default=0, help_text="Number of attempts to deliver this message.")
128 failed_at = models.DateTimeField(null=True, blank=True, help_text="Last time this message failed.")
129
130 class Meta:
131 verbose_name = "message queue"
132 verbose_name_plural = "message queue"
133
134 def __unicode__(self):
135 return unicode(self.listener)
136
137 def process(self):
138 self.attempts += 1
139 if self.deliver(fail_silently=True):
140 self.processed = True
141 else:
142 self.failed_at = datetime.datetime.now()
143 self.save()
144
145 def deliver(self, fail_silently=False):
146 """
147 Returns True if the message was successfully delivered.
148
149 """
150 # ### Look at the response - if not 200 then failed.
151 import urllib2
152 original_timeout = socket.getdefaulttimeout()
153 socket.setdefaulttimeout(TIMEOUT)
154 try:
155 urllib2.urlopen(self.listener.url, self.message.payload)
156 except urllib2.URLError:
157 print 'except'
158 if fail_silently:
159 return False
160 else:
161 raise
162 else:
163 return True
164 finally:
165 socket.setdefaulttimeout(original_timeout)
166
167
168
169
170 # def _get_hasher(hasher):
171 # if hasher is None:
172 # from django.utils import hashcompat
173 # return hashcompat.sha_constructor
174 # else:
175 # return hasher
Something went wrong with that request. Please try again.