def save_notification(self, type, message, receiver_id, sender_id): notification = Notification() notification.sender_uid = sender_id notification.receiver_uid = receiver_id notification.message = message notification.type = type db.session.add(notification) try: db.session.commit() return True except SQLAlchemyError as e: data = e.orig.args return False
def send(cls, query: dict, session: dict) -> Notification: """ Business method to send a new notification. """ assert isinstance(session, dict) SendNotificationQueryValidation.validate(query) user = User.objects.get(pk=query[cls.USER_ID]) notification = Notification() notification.code = query[cls.CODE] notification.is_error = query.get(cls.IS_ERROR) notification.title = query[cls.TITLE] notification.message = query[cls.MESSAGE] user.notifications.append(notification) user.save() queue = Queue(f"notifications-{user.id}") queue.add(notification.to_json()) return notification
def create(self, payloads): response = ResponseBuilder() notification = Notification() notification.message = payloads['message'] notification.receiver_uid = payloads['receiver'] notification.type = payloads['type'] notification.sender_uid = payloads['user']['id'] db.session.add(notification) try: db.session.commit() data = notification.as_dict() data['receiver'] = notification.receiver.include_photos().as_dict() data['sender'] = notification.sender.include_photos().as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build()