def get(self): # This seems expensive. TODO: refactor messages = [] for message in db.GqlQuery("SELECT * FROM Message ORDER BY created DESC"): recipients = Delivery.all().filter('message =', message).count() delivered = Delivery.all().filter('message =', message).filter('status =', STATUS_DELIVERED).count() messages.append({ 'message': message, 'recipients': recipients, 'delivered': delivered, }) template_values = { 'messages': messages, } path = os.path.join(os.path.dirname(__file__), 'message.html') self.response.out.write(template.render(path, template_values))
def delete(self, channelid, subscriberid): """Handle deletion of a subscribers. Only allow if there are no outstanding deliveries.""" channel = self._getentity(Channel, channelid) if channel is None: return subscriber = self._getentity(Subscriber, subscriberid) if subscriber is None: return nrdeliveries = Delivery.all().filter('recipient =', subscriber).filter('status !=', STATUS_DELIVERED).count() if nrdeliveries: # Can't delete if deliveries still outstanding self.response.set_status(405, "CANNOT DELETE - %s DELIVERIES OUTSTANDING" % nrdeliveries) self.response.headers['Allow'] = "GET" else: subscriber.delete() self.response.set_status(204)
def post(self, messageid): # Retrieve the message, make sure it exists message = Message.get(messageid) if message is None: logging.debug("Message %s does not exist, returning 200" % (messageid, )) self.response.set_status(200) return # Assume all deliveries are successful (i.e. this task is done) deliveriessucceeded = True # For this message, process those deliveries that have not yet been # delivered (status will be None) for delivery in Delivery.all().filter('message =', message).filter('status =', None): logging.debug("Processing delivery %s" % (delivery.key(), )) # Make the delivery with a POST to the recipient's resource # sending the published body, with the published body's content-type status = 999 try: result = urlfetch.fetch( url = delivery.recipient.resource, payload = message.body, method = urlfetch.POST, headers = { 'Content-Type': message.contenttype }, follow_redirects = False, ) status = result.status_code except: logging.error("urlfetch encountered an EXCEPTION") # If we've had a successful status then consider this # particular delivery done. Otherwise, mark the delivery # as failed. if status < 400: delivery.status = STATUS_DELIVERED delivery.put() else: deliveriessucceeded = False # If there are failed deliveries, mark this task as failed # so that the task queue mechanism will retry. if not deliveriessucceeded: self.response.set_status(500)
def get(self, channelid, messageid): message = Message.get(messageid) if message is None: self.response.out.write("Message %s not found" % (messageid, )) self.response.set_status(404) return deliveries = Delivery.all().filter('message =', message) channelurl = "%s://%s/channel/%d/" % (self.request.scheme, self.request.host, message.channel.key().id()) # Poor conneg if (self.request.headers.has_key('Accept') and self.request.headers['Accept'] == CT_JSON): logging.info("JSON requested") deliveryinfo = [] for d in deliveries: deliveryinfo.append({ 'recipient': "%ssubscriber/%d/" % (channelurl, d.recipient.key().id()), 'status': d.status, 'timestamp': d.updated.strftime("%Y-%m-%dT%H:%M:%SZ"), }) info = { 'message': { 'resource': "%smessage/%s" % (channelurl, str(message.key())), 'key': str(message.key()), 'created': message.created.strftime("%Y-%m-%dT%H:%M:%SZ"), 'channel': channelurl, 'delivery': deliveryinfo, }, } self.response.out.write(simplejson.dumps(info)) self.response.headers['Content-Type'] = CT_JSON return template_values = { 'message': message, #'deliveries': Delivery.all().filter('message =', message), 'deliveries': deliveries, } path = os.path.join(os.path.dirname(__file__), 'messagedetail.html') self.response.out.write(template.render(path, template_values))
def delete_all_deliveries(): while Delivery.all().fetch(CHUNK): db.delete(Delivery.all().fetch(CHUNK))