Example #1
0
def filter_read_threads_queryset(user, categories, list_type, queryset):
    # grab cutoffs for categories
    cutoff_date = get_cutoff_date(user)

    visible_posts = Post.objects.filter(posted_on__gt=cutoff_date)
    visible_posts = exclude_invisible_posts(user, categories, visible_posts)

    queryset = queryset.filter(
        id__in=visible_posts.distinct().values('thread'))

    read_posts = visible_posts.filter(id__in=user.postread_set.values('post'))

    if list_type == 'new':
        # new threads have no entry in reads table
        return queryset.exclude(id__in=read_posts.distinct().values('thread'))

    if list_type == 'unread':
        # unread threads were read in past but have new posts
        unread_posts = visible_posts.exclude(
            id__in=user.postread_set.values('post'))
        queryset = queryset.filter(
            id__in=read_posts.distinct().values('thread'))
        queryset = queryset.filter(
            id__in=unread_posts.distinct().values('thread'))
        return queryset
Example #2
0
    def test_get_cutoff_date_no_user(self):
        """get_cutoff_date utility works without user argument"""
        valid_cutoff_date = timezone.now() - timedelta(
            days=settings.MISAGO_READTRACKER_CUTOFF)
        returned_cutoff_date = get_cutoff_date()

        self.assertTrue(returned_cutoff_date > valid_cutoff_date)
Example #3
0
    def test_get_cutoff_date_user(self):
        """passing anonymous user to get_cutoff_date has no effect"""
        user = MockAnonymousUser()

        valid_cutoff_date = timezone.now() - timedelta(
            days=settings.MISAGO_READTRACKER_CUTOFF)
        returned_cutoff_date = get_cutoff_date(user)

        self.assertTrue(returned_cutoff_date > valid_cutoff_date)
Example #4
0
    def test_get_cutoff_date_user(self):
        """get_cutoff_date utility works with user argument"""
        user = MockUser()

        valid_cutoff_date = timezone.now() - timedelta(
            days=settings.MISAGO_READTRACKER_CUTOFF)
        returned_cutoff_date = get_cutoff_date(user)

        self.assertTrue(returned_cutoff_date > valid_cutoff_date)
        self.assertEqual(returned_cutoff_date, user.joined_on)
Example #5
0
    def get_first_unread_post(self, user, posts_queryset):
        if user.is_authenticated:
            expired_posts = Q(posted_on__lt=get_cutoff_date(user))
            read_posts = Q(id__in=user.postread_set.values('post'))

            first_unread = posts_queryset.exclude(
                expired_posts | read_posts, ).order_by('id').first()

            if first_unread:
                return first_unread

        return posts_queryset.order_by('id').last()
    def handle(self, *args, **options):
        queryset = PostRead.objects.filter(last_read_on__lt=get_cutoff_date())
        deleted_count = queryset.count()

        if deleted_count:
            queryset.delete()

            message = "\n\nDeleted %s expired entries" % deleted_count
        else:
            message = "\n\nNo expired entries were found"

        self.stdout.write(message)