Exemplo n.º 1
0
    def get_data(self, **kwargs):
        queryset = self.get_queryset()

        # cut off at timestamp if given
        if self.offset_timestamp:
            offset_datetime = datetime_from_timestamp(self.offset_timestamp)
            filter_exp = {'%s__lt' % self.offset_model_field: offset_datetime}
            queryset = queryset.filter(**filter_exp)

        # calculate has_more and new offset timestamp
        has_more = queryset.count() > self.page_size
        queryset = queryset[:self.page_size]

        items = self.get_items_from_queryset(queryset)
        queryset = list(queryset)

        last_offset_timestamp = None
        if len(queryset) > 0:
            last_offset_timestamp = timestamp_from_datetime(
                getattr(queryset[-1], self.offset_model_field))

        return {
            'items':
            items,
            'widget_title':
            self.model._meta.verbose_name_plural if self.model else None,
            'has_more':
            has_more,
            'offset_timestamp':
            last_offset_timestamp,
        }
Exemplo n.º 2
0
 def get_items_from_queryset(self, queryset):
     alerts = list(queryset)
     # retrieve the newest item's timestamp, but only if we arent loading "more..." paged items
     self.newest_timestamp = None
     if not self.offset_timestamp and len(alerts) > 0:
         self.newest_timestamp = timestamp_from_datetime(alerts[0].last_event_at)
     # get (user_obj, profile_obj) for each user_id into a dict
     # to optimize and retrieve each user once, even if they are action_user of multiple alerts
     user_ids = list(set([alert.action_user_id for alert in alerts]))
     users = get_user_model().objects.filter(id__in=user_ids).prefetch_related('cosinnus_profile')
     user_cache = dict(((user.id, (user, user.cosinnus_profile)) for user in users))
     # serialize items
     items = [
         SerializedNotificationAlert(
             alert, 
             action_user=user_cache[alert.action_user_id][0], 
             action_user_profile=user_cache[alert.action_user_id][1],
         ) for alert in alerts
     ]
     return items
Exemplo n.º 3
0
 def render_to_response(self, items):
     """ Renders a list of items and returns a JsonResponse with the items 
         and additional meta info.
         Returned data:
         @return: 
             `items`: list[str]: a list of rendered html items
             `count` int: count of the number of rendered items
             `has_more` bool: if more items are potentially available
             `last_timestamp` float: the timestamp of the oldest returned item. 
                 used as offset for the next paginated request. Will be None
                 if 0 items were returned. 
          """
     rendered_items = [self.render_item(item) for item in items]
     last_timestamp = None
     if len(items) > 0:
         last_timestamp = timestamp_from_datetime(
             getattr(items[-1], self.sort_key_natural))
     response = {
         'items': rendered_items,
         'count': len(rendered_items),
         'has_more': len(rendered_items) == self.page_size,
         'last_timestamp': last_timestamp,
     }
     return JsonResponse({'data': response})
Exemplo n.º 4
0
 def secret_from_created(self):
     """ Returns an (unsafe) secret id based on the created date timestamp """
     return str(timestamp_from_datetime(self.created)).replace('.', '')
Exemplo n.º 5
0
    def get_data(self, **kwargs):
        has_more = False
        offset_timestamp = None

        if self.show_recent:
            # showing "last-visited" content, ordering by visit datetime
            ct = ContentType.objects.get_for_model(self.model)
            queryset = LastVisitedObject.objects.filter(
                content_type=ct,
                user=self.request.user,
                portal=CosinnusPortal.get_current())
            queryset = queryset.order_by('-visited')

            # cut off at timestamp if given
            if self.offset_timestamp:
                offset_datetime = datetime_from_timestamp(
                    self.offset_timestamp)
                queryset = queryset.filter(visited__lt=offset_datetime)

            # calculate has_more and new offset timestamp
            has_more = queryset.count() > self.page_size
            queryset = queryset[:self.page_size]
            # the `item_data` field already contains the JSON of `DashboardItem`
            items = list(queryset.values_list('item_data', flat=True))

            queryset = list(queryset)
            if len(queryset) > 0:
                offset_timestamp = timestamp_from_datetime(
                    queryset[-1].visited)
        else:
            # all content, ordered by creation date
            only_mine = True
            sort_key = '-created'
            queryset = self.fetch_queryset_for_user(self.model,
                                                    self.request.user,
                                                    sort_key=sort_key,
                                                    only_mine=only_mine)
            if queryset is None:
                return {
                    'items': [],
                    'widget_title': '(error: %s)' % self.model.__name__
                }

            # cut off at timestamp if given
            if self.offset_timestamp:
                offset_datetime = datetime_from_timestamp(
                    self.offset_timestamp)
                queryset = queryset.filter(created__lt=offset_datetime)

            # calculate has_more and new offset timestamp
            has_more = queryset.count() > self.page_size
            queryset = list(queryset[:self.page_size])
            if len(queryset) > 0:
                offset_timestamp = timestamp_from_datetime(
                    queryset[-1].created)

            items = [
                DashboardItem(item, user=self.request.user)
                for item in queryset
            ]

        return {
            'items': items,
            'widget_title': self.model._meta.verbose_name_plural,
            'has_more': has_more,
            'offset_timestamp': offset_timestamp,
        }