Beispiel #1
0
def notify(obj, content, alert=False):
    """
    Positional Arguments

    :param alert: whether or not must pop an alert
    :param obj      object      Whatever Django Model Object
    :param content  textfield      A string to be notified
    """
    from leads.serializers import NotificationSerializer
    from django_mailbox.models import Message
    from leads.models import Lead
    from django.db import transaction

    # We don't notify an old lead
    #try:
    #    if obj.lead.arrival < timezone.now().date():
    #        return
    #except Exception as e:
    #    pass

    notification = Notification(
        content_object=obj,
        content=content,
        alert=alert
    )
    if notification.alert:
        with transaction.commit_manually():
            try:

                notification.save(force_insert=True)

            except Exception as e:
                transaction.rollback()
                logger.error('Could not save the notification\n'
                             'Error: {0}'.format(str(e)))
                return
            transaction.commit()
    try:
        if notification.content_type == ContentType.objects.get_for_model(Message):
            ser = NotificationSerializer(instance=notification)
            data = JSONRenderer().render(ser.data)
            r = get_redis_connection()
            room = 'agency{0}_room'.format(notification.agency_id)
            r.publish(room, data)
        if notification.content_type == ContentType.objects.get_for_model(Lead):
            ser = NotificationSerializer(instance=notification)
            data = JSONRenderer().render(ser.data)
            r = get_redis_connection()
            room = 'agency{0}_room'.format(notification.agency_id)
            r.publish(room, data)

        logger.debug(
            'Notification for object: {0}\n'
            'Comment: {1}'.format(obj, notification))
    except Exception as e:
        logger.error('Could not push the notification\n'
                     'Error: {0}'.format(str(e)))
Beispiel #2
0
 def get_context_data(self, **kwargs):
     context = super(AddAlert, self).get_context_data(**kwargs)
     notification = context['notification']
     context['user'] = self.request.user
     ser = NotificationSerializer(instance=notification)
     data = JSONRenderer().render(ser.data)
     context['data'] = data
     r = get_redis_connection()
     room = get_room_chan_name(self.request.user)
     r.publish(room, data)
     return context
Beispiel #3
0
    def listener(self, room):
        r = get_redis_connection()
        p = r.pubsub()
        p.subscribe(room)
        self.log("Listener started")

        for m in p.listen():
            if m['type'] == 'message':
                data = loads(m['data'])
                # self.emit('pop', 'msg')
                self.emit('alert', data)
Beispiel #4
0
    def initialize(self):
        self.logger = logging.getLogger("socketio.chat")
        self.redis = get_redis_connection()
        self.pubsub = self.redis.pubsub()
        self.listener_greenlet = None

        # self.user = CuserMiddleware.get_user()
        #
        # self.socket.session['user'] = self.user
        # self.environ.setdefault('users', []).append(self.user)
        # self.broadcast_event('announcement', '%s has connected' % self.user)
        # self.broadcast_event('users', self.environ['users'])

        self.emit('connect', 'connected')
        self.log('connected!')
Beispiel #5
0
def pub(room, data):
    logger.debug('publishing')
    r = get_redis_connection()
    logger.debug('connecting')
    r.publish(room, data)