Example #1
0
class Notifications(View, Controller):
    """List Notifications Private Endpoint Controller"""
    def __init__(self):
        self.__notification = NotificationModule()
        self.__humanize = Humanize()

    @allow_if_authenticated
    def get(self, request):

        self.__user_id = request.user.id

        request_data = self.get_request_data(request, "get", {
            "offset": 0,
            "limit": 20
        })

        try:
            offset = int(request_data["offset"])
            limit = int(request_data["limit"])
        except Exception:
            offset = 0
            limit = 20

        return self.json(
            [], {
                'notifications':
                self.__format_notification(
                    self.__notification.get(self.__user_id, offset, limit)),
                'metadata': {
                    'offset': offset,
                    'limit': limit,
                    'count': self.__notification.count(self.__user_id)
                }
            })

    def __format_notification(self, notifications):
        notifications_list = []

        for notification in notifications:
            notifications_list.append({
                "id":
                notification.id,
                "type":
                notification.type,
                "highlight":
                notification.highlight,
                "description":
                notification.notification,
                "url":
                notification.url,
                "delivered":
                notification.delivered,
                "created_at":
                self.__humanize.datetime(notification.created_at)
            })

        return notifications_list
Example #2
0
class Notifications(View):

    __request = None
    __response = None
    __helpers = None
    __form = None
    __logger = None
    __user_id = None
    __notification = None
    __humanize = None
    __correlation_id = None

    def __init__(self):
        self.__helpers = Helpers()
        self.__form = Form()
        self.__logger = self.__helpers.get_logger(__name__)
        self.__response = Response()
        self.__request = Request()
        self.__notification = NotificationModule()
        self.__humanize = Humanize()
        self.__form.add_validator(ExtraRules())

    @allow_if_authenticated
    def get(self, request):

        self.__correlation_id = request.META[
            "X-Correlation-ID"] if "X-Correlation-ID" in request.META else ""
        self.__user_id = request.user.id
        self.__request.set_request(request)

        request_data = self.__request.get_request_data("get", {
            "offset": "",
            "limit": ""
        })

        try:
            offset = int(request_data["offset"])
            limit = int(request_data["limit"])
        except Exception:
            offset = 0
            limit = 0

        return JsonResponse(
            self.__response.send_private_success(
                [], {
                    'notifications':
                    self.__format_notification(
                        self.__notification.get(self.__user_id, offset,
                                                limit)),
                    'metadata': {
                        'offset': offset,
                        'limit': limit,
                        'count': self.__notification.count(self.__user_id)
                    }
                }, self.__correlation_id))

    def __format_notification(self, notifications):
        notifications_list = []

        for notification in notifications:
            notifications_list.append({
                "id":
                notification.id,
                "type":
                notification.type,
                "highlight":
                notification.highlight,
                "description":
                notification.notification,
                "url":
                notification.url,
                "delivered":
                notification.delivered,
                "created_at":
                self.__humanize.datetime(notification.created_at)
            })

        return notifications_list