def time_range(cls, user, lowerbound, upperbound, desc=True, mode=0): """lowbound and upperbound in unixtimestamp The mode is: 0 - unread 1 - read 2 - all """ r = get_redis_push_notifications_connection() key = Notification._select_get_mode(user, mode, r) func = r.zrangebyscore if not desc else r.zrevrangebyscore result = map(Notification._notification_factory, func(key, lowerbound, upperbound)) return result
def find(cls, user, offset=0, limit=-1, desc=True, mode=0): """The offset starts in 0. The mode is: 0 - unread 1 - read 2 - all """ r = get_redis_push_notifications_connection() key = Notification._select_get_mode(user, mode, r) func = r.zrange if not desc else r.zrevrange # result = map(Notification._notification_factory, func(key, offset, limit)) result = [] for i in func(key, offset, limit): res = Notification._notification_factory(i) if res: result.append(res) return result
def _select_get_mode(cls, user, mode, redis_client=None): if mode > 2 and mode < 0: raise ValueError("Mode needs to be between 0 and 2 -> 0:unread, 1:read, 2:all") if not redis_client: redis_client = get_redis_push_notifications_connection() # Select the key to make the query if mode == 0: key = Notification.STORE_KEY_UNREAD_FORMAT.format(user.id) elif mode == 1: key = Notification.STORE_KEY_READ_FORMAT.format(user.id) elif mode == 2: # Save in an union to check later key_read = Notification.STORE_KEY_READ_FORMAT.format(user.id) key_unread = Notification.STORE_KEY_UNREAD_FORMAT.format(user.id) key = Notification.STORE_KEY_ALL_FORMAT.format(user.id) redis_client.zunionstore(key, (key_read, key_unread)) return key
def send_push(self): # Publish in redis r = get_redis_push_notifications_connection() r.publish(self._push_key, self.to_json())
def count(cls, user, min_time="-inf", max_time="+inf", mode=0): r = get_redis_push_notifications_connection() key = Notification._select_get_mode(user, mode, r) return int(r.zcount(key, min_time, max_time))