def _queue(self): try: redis = Db('eventadmindetail', self.get_object().id) except: redis = Db('eventadmindetail', self.event_obj.id) pubsub = redis.object().pubsub() pubsub.subscribe(redis.key) for conn in connections.all(): conn.close() while True: for m in pubsub.listen(): if m['type'] == 'message': msg = u"compatibility: true\n" msg += u"retry: 10000\n" msg += u"data: {}\n\n".format(m['data']) yield msg stream = u"compatibility: true\n" stream += u"retry: 10000\n" stream += u"data: {}\n\n".format( json.dumps({"event": "stream"})) yield stream time.sleep(0.5)
def update_feed_for_active_transmissions(transmission_id=None): start_date = timezone.now() - datetime.timedelta(hours=4) end_date = timezone.now() + datetime.timedelta(hours=4) if not transmission_id: active_transmissions = Transmission.objects.filter( match__match_time__range=(start_date, end_date), event_type__slug="soccer", published=True) else: active_transmissions = Transmission.objects.filter(pk=transmission_id) # print active_transmissions for transmission in active_transmissions: get_match(country=transmission.match.category.country.name.lower(), match_id=[transmission.match.g_static_id], cat_id=transmission.match.category.g_id if transmission.match.category else None, get_players=True) data = json.dumps(data_match(transmission.match.id)) redis = Db('goalservematch', transmission.match.id) redis.publish(data) log_it('update_feed_for_active_transmissions')
def update_feed_for_active_transmissions(transmission_id=None): start_date = timezone.now() - datetime.timedelta(hours=4) end_date = timezone.now() + datetime.timedelta(hours=4) if not transmission_id: active_transmissions = Transmission.objects.filter( match__match_time__range=(start_date, end_date), event_type__slug="soccer", published=True ) else: active_transmissions = Transmission.objects.filter(pk=transmission_id) # print active_transmissions for transmission in active_transmissions: get_match(country=transmission.match.category.country.name.lower(), match_id=[transmission.match.g_static_id], cat_id=transmission.match.category.g_id if transmission.match.category else None, get_players=True) data = json.dumps(data_match(transmission.match.id)) redis = Db('goalservematch', transmission.match.id) redis.publish(data) log_it('update_feed_for_active_transmissions')
def _db(self, obj): _db = Db( obj.container.get_absolute_url(), obj.container.id) pubsub = _db.object().pubsub() pubsub.subscribe(_db.key) return pubsub
def test_pubsub_notification(self): noti = Notification.objects.all()[0] _db = Db(noti.container.get_absolute_url(), noti.container.id) pubsub = _db.object().pubsub() pubsub.subscribe(_db.key) self.assertTrue(pubsub)
def test_pubsub_notification(self): noti = Notification.objects.all()[0] _db = Db(noti.container.get_absolute_url(), noti.container.id) pubsub = _db.object().pubsub() pubsub.subscribe(_db.key) self.assertTrue(pubsub) self.assertEqual(pubsub.channels, set([u'opps_/home/test.html_1']))
def post(self, request, *args, **kwargs): msg = request.POST['message'] obj = Message.objects.create(message=msg, user=request.user, event=self.get_object(), published=True) if obj.published: redis = Db(self.__class__.__name__, self.get_object().id) redis.publish(msg) return HttpResponse(msg)
def _sse_queue(): redis = Db('goalservematch', match_pk) pubsub = redis.object().pubsub() pubsub.subscribe(redis.key) while True: for m in pubsub.listen(): if m['type'] == 'message': data = m['data'].decode('utf-8') yield u"data: {}\n\n".format(data) yield time.sleep(0.5)
def _queue(self): redis = Db('eventadmindetail', self.get_object().id) pubsub = redis.object().pubsub() pubsub.subscribe(redis.key) while True: for m in pubsub.listen(): if m['type'] == 'message': data = m['data'].decode('utf-8') yield u"data: {}\n\n".format(data) yield time.sleep(0.5)
def save(self, *args, **kwargs): self.channel_long_slug = self.container.channel_long_slug self.slug = self.container.slug super(Notification, self).save(*args, **kwargs) _db = Db(self.container.get_absolute_url(), self.container.id) message = self.message if self.type == "json": message = json.dumps(self.message) _db.publish(json.dumps({ "action": self.action, "id": self.id, "published": self.published, "date": self.date_available.strftime("%D %T"), "message": message}))
def post(self, request, *args, **kwargs): msg = request.POST.get('message', None) id = request.POST.get('id_message', None) event = self.get_object() redis = Db(self.__class__.__name__, self.get_object().id) if request.POST.get('stream', None): redis.publish(json.dumps({"event": "stream"})) return HttpResponse('stream') _list = {k: v for k, v in request.POST.iteritems()} if id: obj = Message.objects.get(id=id) published = request.POST.get('published', True) if published == 'false': published = False else: published = True obj.published = published if msg: obj.message = msg obj.user = request.user obj.save() redis.publish( json.dumps({ "event": "update", "id": id, "published": published, "msg": msg })) else: obj = Message.objects.create(message=msg, user=request.user, event=event, published=True) id = obj.id redis.publish( json.dumps({ "event": "message", "id": id, "msg": msg })) _list['id_message'] = id event.create_event(_list) return HttpResponse(msg)
def save(self, *args, **kwargs): self.channel_long_slug = self.container.channel_long_slug self.slug = self.container.slug super(Notification, self).save(*args, **kwargs) _db = Db(self.container.get_absolute_url(), self.container.id) message = self.message if self.type == "json": message = json.dumps(self.message) _db.publish( json.dumps({ "action": self.action, "id": self.id, "published": self.published, "date": self.date_available.strftime("%D %T"), "message": message })) _db.close()
def post(self, request, *args, **kwargs): msg = request.POST.get('message', None) id = request.POST.get('id_message', None) event = self.get_object() redis = Db(self.__class__.__name__, self.get_object().id) if request.POST.get('stream', None): redis_wrapper(redis, 'publish', json.dumps({"event": "stream"})) return HttpResponse('stream') _list = {} for k, v in request.POST.iteritems(): _list[k] = v if id: obj = Message.objects.get(id=id) published = True if request.POST.get('published') == 'false': published = False obj.published = published if msg: obj.message = msg obj.user = request.user if published: signals.message_pre_save.send(sender=self.__class__, instance=obj, extra_data=_list) else: signals.message_pre_delete.send(sender=self.__class__, instance=obj, extra_data=_list) obj.save() redis_wrapper( redis, 'publish', json.dumps( { "event": "update", "id": id, "published": published, "msg": msg, "date": obj.date_available }, cls=DjangoJSONEncoder)) if published: signals.message_post_save.send(sender=self.__class__, instance=obj, created=False, extra_data=_list) else: signals.message_post_delete.send(sender=self.__class__, instance=obj, extra_data=_list) else: obj = Message(message=msg, user=request.user, event=event, published=True) signals.message_pre_save.send(sender=self.__class__, instance=obj, extra_data=_list) obj.save(force_insert=True) signals.message_pre_save id = obj.id redis_wrapper( redis, 'publish', json.dumps( { "event": "message", "id": id, "msg": msg, "date": obj.date_available }, cls=DjangoJSONEncoder)) _list['id_message'] = id signals.message_post_save.send(sender=self.__class__, instance=obj, created=True, extra_data=_list) return HttpResponse(msg)
def _db(self, obj): _db = Db(obj.container.get_absolute_url(), obj.container.id) pubsub = _db.object().pubsub() pubsub.subscribe(_db.key) return pubsub