def refresh_general_notifications(self, oldest_timestamp): """ Retrieve the general notifications newer than the older one on the screen. This will accomplish 2 things: 1. Notifications currently visible will be updated 2. Any new notifications will be appending to the top of the list """ payload = {} user = self.scope["user"] if user.is_authenticated: timestamp = oldest_timestamp[0:oldest_timestamp.find( "+")] # remove timezone because who cares timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f') friend_request_ct = ContentType.objects.get_for_model( FriendRequest) friend_list_ct = ContentType.objects.get_for_model(FriendList) notifications = Notification.objects.filter( target=user, content_type__in=[friend_request_ct, friend_list_ct], timestamp__gte=timestamp).order_by('-timestamp') s = LazyNotificationEncoder() payload['notifications'] = s.serialize(notifications) else: raise NotificationClientError( "User must be authenticated to get notifications.") return json.dumps(payload)
def decline_friend_request(request, *args, **kwargs): user = request.user payload = {} if request.method == "GET" and user.is_authenticated: friend_request_id = kwargs.get("friend_request_id") if friend_request_id: friend_request = FriendRequest.objects.get(pk=friend_request_id) # confirm that is the correct request if friend_request.receiver == user: if friend_request: # found the request. Now decline it updated_notification = friend_request.decline() payload['response'] = "Friend request declined." # return the notification associated with this FriendRequest s = LazyNotificationEncoder() payload['notification'] = s.serialize( [updated_notification])[0] else: payload['response'] = "Something went wrong." else: payload[ 'response'] = "That is not your friend request to decline." else: payload['response'] = "Unable to decline that friend request." else: # should never happen payload[ 'response'] = "You must be authenticated to decline a friend request." return HttpResponse(json.dumps(payload), content_type="application/json")
def accept_friend_request(self, notification_id): """ Accept a friend request """ payload = {} user = self.scope["user"] if user.is_authenticated: try: notification = Notification.objects.get(pk=notification_id) friend_request = notification.content_object # confirm this is the correct user if friend_request.receiver == user: # accept the request and get the updated notification updated_notification = friend_request.accept() # return the notification associated with this FriendRequest s = LazyNotificationEncoder() payload['notification'] = s.serialize( [updated_notification])[0] return json.dumps(payload) except Notification.DoesNotExist: raise NotificationClientError( "An error occurred with that notification. Try refreshing the browser." ) return None
def get_chat_notifications(self, page_number): """ Get Chat Notifications with Pagination (next page of results). This is for appending to the bottom of the notifications list. Chat Notifications are: 1. UnreadChatRoomMessages """ user = self.scope["user"] if user.is_authenticated: chatmessage_ct = ContentType.objects.get_for_model( UnreadChatRoomMessages) notifications = Notification.objects.filter( target=user, content_type=chatmessage_ct).order_by('-timestamp') p = Paginator(notifications, DEFAULT_NOTIFICATION_PAGE_SIZE) # sleep 1s for testing # sleep(1) payload = {} if len(notifications) > 0: if int(page_number) <= p.num_pages: s = LazyNotificationEncoder() serialized_notifications = s.serialize( p.page(page_number).object_list) payload['notifications'] = serialized_notifications new_page_number = int(page_number) + 1 payload['new_page_number'] = new_page_number else: return None else: raise NotificationClientError( "User must be authenticated to get notifications.") return json.dumps(payload)
def get_general_notifications(user, page_number): """ Get General Notifications with Pagination (next page of results). This is for appending to the bottom of the notifications list. General Notifications are: 1. FriendRequest 2. FriendList """ if user.is_authenticated: friend_request_ct = ContentType.objects.get_for_model(FriendRequest) friend_list_ct = ContentType.objects.get_for_model(FriendList) notifications = Notification.objects.filter(target=user, content_type__in=[ friend_request_ct, friend_list_ct ]).order_by('-timestamp') p = Paginator(notifications, DEFAULT_NOTIFICATION_PAGE_SIZE) payload = {} if len(notifications) > 0: if int(page_number) <= p.num_pages: s = LazyNotificationEncoder() serialized_notifications = s.serialize( p.page(page_number).object_list) payload['notifications'] = serialized_notifications new_page_number = int(page_number) + 1 payload['new_page_number'] = new_page_number else: return None else: raise ClientError("AUTH_ERROR", "User must be authenticated to get notifications.") return json.dumps(payload)
def refresh_general_notifications(user, oldest_timestamp, newest_timestamp): """ Retrieve the general notifications newer than the oldest one on the screen and younger than the newest one the screen. The result will be: Notifications currently visible will be updated """ payload = {} if user.is_authenticated: oldest_ts = oldest_timestamp[0:oldest_timestamp.find( "+")] # remove timezone because who cares oldest_ts = datetime.strptime(oldest_ts, '%Y-%m-%d %H:%M:%S.%f') newest_ts = newest_timestamp[0:newest_timestamp.find( "+")] # remove timezone because who cares newest_ts = datetime.strptime(newest_ts, '%Y-%m-%d %H:%M:%S.%f') friend_request_ct = ContentType.objects.get_for_model(FriendRequest) friend_list_ct = ContentType.objects.get_for_model(FriendList) notifications = Notification.objects.filter( target=user, content_type__in=[friend_request_ct, friend_list_ct], timestamp__gte=oldest_ts, timestamp__lte=newest_ts).order_by('-timestamp') s = LazyNotificationEncoder() payload['notifications'] = s.serialize(notifications) else: raise ClientError("AUTH_ERROR", "User must be authenticated to get notifications.") return json.dumps(payload)
def refresh_chat_notifications(user, oldest_timestamp, newest_timestatmp): """ Retrieve the chat notifications newer than the oldest one on the screen and older than the newest on the screen. The result will be: Notifications currently visible will be updated """ payload = {} if user.is_authenticated: oldest_ts = oldest_timestamp[0:oldest_timestamp.find( "+")] # remove timezone because who cares oldest_ts = datetime.strptime(oldest_ts, '%Y-%m-%d %H:%M:%S.%f') newest_ts = newest_timestatmp[0:newest_timestatmp.find( "+")] # remove timezone because who cares newest_ts = datetime.strptime(newest_ts, '%Y-%m-%d %H:%M:%S.%f') chatmessage_ct = ContentType.objects.get_for_model( UnreadChatRoomMessages) notifications = Notification.objects.filter( target=user, content_type__in=[chatmessage_ct], timestamp__gte=oldest_ts, timestamp__lte=newest_ts).order_by('-timestamp') s = LazyNotificationEncoder() payload['notifications'] = s.serialize(notifications) else: raise NotificationClientError( "User must be authenticated to get notifications.") return json.dumps(payload)
def get_new_chat_notifications(user, newest_timestamp): """ Retrieve any notifications newer than the newest_timestamp on the screen. """ payload = {} if user.is_authenticated: timestamp = newest_timestamp[0:newest_timestamp.find("+")] timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f") chatmessage_ct = ContentType.objects.get_for_model(UnreadChatRoomMessages) # content_type=chatmessage_ct or use what we used below. this is how we make a list but we don't really need a list just for reference. notifications = Notification.objects.filter(target=user, content_type__in=[chatmessage_ct], timestamp__gt=timestamp).order_by("-timestamp") s = LazyNotificationEncoder() payload['notifications'] = s.serialize(notifications) return json.dumps(payload) else: ClientError("AUTH_ERROR", "User must be authenticated to get notifications.") return None
def get_new_general_notifications(user, newest_timestamp): """ Retrieve any notifications newer than the newest_timestamp on the screen. """ payload = {} if user.is_authenticated: timestamp = newest_timestamp[0:newest_timestamp.find("+")] timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f") friend_request_ct = ContentType.objects.get_for_model(FriendRequest) friend_list_ct = ContentType.objects.get_for_model(FriendList) notifications = Notification.objects.filter(target=user, content_type__in=[friend_request_ct, friend_list_ct], timestamp__gt=timestamp, read=False).order_by("-timestamp") s = LazyNotificationEncoder() payload['notifications'] = s.serialize(notifications) else: raise ClientError("AUTH_ERROR", "User must be authenticated to get notifications") return json.dumps(payload)
def decline_friend_request(user, notification_id): """ Decline a friend request """ payload = {} if user.is_authenticated: try: notification = Notification.objects.get(pk=notification_id) friend_request = notification.content_object # confirm this is the correct user if friend_request.receiver == user: updated_notification = friend_request.decline() s = LazyNotificationEncoder() payload['notification'] = s.serialize([updated_notification])[0] return json.dumps(payload) except Notification.DoesNotExist: raise CientError(422, "An error occurred with that notification. Try refreshing the browser.") return None
def get_new_chat_notifications(user, newest_timestatmp): """ Retrieve any notifications newer than the newest_timestatmp on the screen. """ payload = {} if user.is_authenticated: timestamp = newest_timestatmp[0:newest_timestatmp.find( "+")] # remove timezone because who cares timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f') chatmessage_ct = ContentType.objects.get_for_model( UnreadChatRoomMessages) notifications = Notification.objects.filter( target=user, content_type__in=[chatmessage_ct], timestamp__gt=timestamp).order_by('-timestamp') s = LazyNotificationEncoder() payload['notifications'] = s.serialize(notifications) else: raise NotificationClientError( "User must be authenticated to get notifications.") return json.dumps(payload)