Example #1
0
    def test_is_date_tracked(self):
        """is_date_tracked validates dates"""
        self.assertFalse(is_date_tracked(None, MockUser()))

        past_date = timezone.now() - timedelta(minutes=10)
        self.assertFalse(is_date_tracked(past_date, MockUser()))

        future_date = timezone.now() + timedelta(minutes=10)
        self.assertTrue(is_date_tracked(future_date, MockUser()))
Example #2
0
    def test_is_date_tracked_with_forum_cutoff(self):
        """is_date_tracked validates dates using forum cutoff"""
        self.assertFalse(is_date_tracked(None, MockUser()))
        past_date = timezone.now() + timedelta(minutes=10)

        forum_cutoff = timezone.now() + timedelta(minutes=20)
        self.assertFalse(is_date_tracked(past_date, MockUser(), forum_cutoff))

        forum_cutoff = timezone.now() - timedelta(minutes=20)
        self.assertTrue(is_date_tracked(past_date, MockUser(), forum_cutoff))
Example #3
0
    def test_is_date_tracked_with_category_cutoff(self):
        """is_date_tracked validates dates using category cutoff"""
        self.assertFalse(is_date_tracked(None, MockUser()))
        past_date = timezone.now() + timedelta(minutes=10)

        category_cutoff = timezone.now() + timedelta(minutes=20)
        self.assertFalse(
            is_date_tracked(past_date, MockUser(), category_cutoff))

        category_cutoff = timezone.now() - timedelta(minutes=20)
        self.assertTrue(
            is_date_tracked(past_date, MockUser(), category_cutoff))
Example #4
0
def make_thread_read_aware(user, thread):
    thread.is_read = True
    thread.is_new = False
    thread.read_record = None

    if user.is_anonymous():
        thread.last_read_on = timezone.now()
    else:
        thread.last_read_on = user.reads_cutoff

    if user.is_authenticated() and is_date_tracked(thread.last_post_on, user):
        thread.is_read = False
        thread.is_new = True

        try:
            forum_record = user.forumread_set.get(forum_id=thread.forum_id)
            if thread.last_post_on > forum_record.last_read_on:
                try:
                    thread_record = user.threadread_set.get(thread=thread)
                    thread.last_read_on = thread_record.last_read_on
                    thread.is_new = False
                    if thread.last_post_on <= thread_record.last_read_on:
                        thread.is_read = True
                    thread.read_record = thread_record
                except ThreadRead.DoesNotExist:
                    pass
            else:
                thread.is_read = True
                thread.is_new = False
        except ForumRead.DoesNotExist:
            forumstracker.start_record(user, thread.forum)
Example #5
0
def make_thread_read_aware(user, thread):
    thread.is_read = True
    thread.is_new = False
    thread.read_record = None

    if user.is_anonymous():
        thread.last_read_on = timezone.now()
    else:
        thread.last_read_on = user.reads_cutoff

    if user.is_authenticated() and is_date_tracked(thread.last_post_on, user):
        thread.is_read = False
        thread.is_new = True

        try:
            forum_record = user.forumread_set.get(forum_id=thread.forum_id)
            if thread.last_post_on > forum_record.last_read_on:
                try:
                    thread_record = user.threadread_set.get(thread=thread)
                    thread.last_read_on = thread_record.last_read_on
                    thread.is_new = False
                    if thread.last_post_on <= thread_record.last_read_on:
                        thread.is_read = True
                    thread.read_record = thread_record
                except ThreadRead.DoesNotExist:
                    pass
            else:
                thread.is_read = True
                thread.is_new = False
        except ForumRead.DoesNotExist:
            forumstracker.start_record(user, thread.forum)
Example #6
0
def make_thread_read_aware(user, thread):
    thread.is_read = True
    thread.is_new = False
    thread.read_record = None

    if user.is_anonymous():
        thread.last_read_on = timezone.now()
    else:
        thread.last_read_on = user.joined_on

    if user.is_authenticated() and is_date_tracked(thread.last_post_on, user):
        thread.is_read = False
        thread.is_new = True

        try:
            category_record = user.categoryread_set.get(
                category_id=thread.category_id)

            if thread.last_post_on > category_record.last_read_on:
                try:
                    thread_record = user.threadread_set.get(thread=thread)
                    thread.last_read_on = thread_record.last_read_on
                    thread.is_new = False
                    if thread.last_post_on <= thread_record.last_read_on:
                        thread.is_read = True
                    thread.read_record = thread_record
                except ThreadRead.DoesNotExist:
                    pass
            else:
                thread.is_read = True
                thread.is_new = False
        except CategoryRead.DoesNotExist:
            categoriestracker.start_record(user, thread.category)
Example #7
0
def make_threads_read_aware(user, threads):
    if user.is_anonymous():
        make_read(threads)
        return None

    threads_dict = {}
    for thread in threads:
        thread.is_read = not is_date_tracked(user, thread.last_post_on)
        thread.is_new = True
        if thread.is_read:
            thread.unread_replies = 0
        else:
            thread.unread_replies = thread.replies
        threads_dict[thread.pk] = thread

    for record in user.threadread_set.filter(thread__in=threads_dict.keys()):
        if record.thread_id in threads_dict:
            thread = threads_dict[record.thread_id]
            thread.is_new = False
            thread.is_read = record.last_read_on >= thread.last_post_on
            if thread.is_read:
                thread.unread_replies = 0
            else:
                thread.unread_replies = thread.replies - record.read_replies
                if thread.unread_replies < 1:
                    thread.unread_replies = 1
Example #8
0
def make_thread_read_aware(user, thread):
    thread.is_read = True
    if user.is_authenticated() and is_date_tracked(thread.last_post_on):
        try:
            record = user.threadread_set.filter(thread=thread).all()[0]
            thread.last_read_on = record.last_read_on
            thread.is_read = thread.last_post_on <= record.last_read_on
            thread.read_record = record
        except IndexError:
            thread.read_record = None
            thread.is_read = False
            thread.last_read_on = cutoff_date()
Example #9
0
def make_read_aware(user, forums):
    if user.is_anonymous():
        make_read(forums)
        return None

    forums_dict = {}
    for forum in forums:
        forum.is_read = not is_date_tracked(user, forum.last_post_on)
        forums_dict[forum.pk] = forum

    for record in user.forumread_set.filter(forum__in=forums_dict.keys()):
        if not forum.is_read and record.forum_id in forums_dict:
            forum = forums_dict[record.forum_id]
            forum.is_read = record.last_cleared_on >= forum.last_post_on
Example #10
0
def make_thread_read_aware(user, thread):
    thread.is_read = True
    if user.is_authenticated() and is_date_tracked(user, thread.last_post_on):
        try:
            record = user.threadread_set.filter(thread=thread).all()[0]
            thread.last_read_on = record.last_read_on
            thread.is_new = False
            thread.is_read = thread.last_post_on <= record.last_read_on
            thread.read_record = record
        except IndexError:
            thread.read_record = None
            thread.is_new = True
            thread.is_read = False
            thread.last_read_on = user.joined_on
Example #11
0
def make_categories_threads_read_aware(user, threads):
    categories_cutoffs = fetch_categories_cutoffs_for_threads(user, threads)

    threads_dict = {}
    for thread in threads:
        category_cutoff = categories_cutoffs.get(thread.category_id)
        thread.is_read = not is_date_tracked(
            thread.last_post_on, user, category_cutoff)
        thread.is_new = True

        if not thread.is_read:
            threads_dict[thread.pk] = thread

    if threads_dict:
        make_threads_dict_read_aware(user, threads_dict)
Example #12
0
def make_posts_read_aware(user, thread, posts):
    try:
        is_thread_read = thread.is_read
    except AttributeError:
        raise ValueError("thread passed make_posts_read_aware should be "
                         "read aware too via make_thread_read_aware")

    if is_thread_read:
        for post in posts:
            post.is_read = True
    else:
        for post in posts:
            if is_date_tracked(post.posted_on, user):
                post.is_read = post.posted_on <= thread.last_read_on
            else:
                post.is_read = True
Example #13
0
def make_forums_threads_read_aware(user, threads):
    forums_cutoffs = fetch_forums_cutoffs_for_threads(user, threads)

    threads_dict = {}
    for thread in threads:
        thread.is_read = not is_date_tracked(
            thread.last_post_on, user, forums_cutoffs.get(thread.forum_id))
        thread.is_new = True
        if thread.is_read:
            thread.unread_replies = 0
        else:
            thread.unread_replies = thread.replies
            threads_dict[thread.pk] = thread

    if threads_dict:
        make_threads_dict_read_aware(user, threads_dict)
Example #14
0
def make_posts_read_aware(thread, posts):
    try:
        is_thread_read = thread.is_read
    except AttributeError:
        raise ValueError("thread passed make_posts_read_aware should be "
                         "read aware too via make_thread_read_aware")

    if is_thread_read:
        for post in posts:
            post.is_read = True
    else:
        for post in posts:
            if is_date_tracked(post.updated_on):
                post.is_read = post.updated_on <= thread.last_read_on
            else:
                post.is_read = True
Example #15
0
def make_forums_threads_read_aware(user, threads):
    forums_cutoffs = fetch_forums_cutoffs_for_threads(user, threads)

    threads_dict = {}
    for thread in threads:
        thread.is_read = not is_date_tracked(
            thread.last_post_on, user, forums_cutoffs.get(thread.forum_id))
        thread.is_new = True
        if thread.is_read:
            thread.unread_replies = 0
        else:
            thread.unread_replies = thread.replies
            threads_dict[thread.pk] = thread

    if threads_dict:
        make_threads_dict_read_aware(user, threads_dict)
Example #16
0
def make_forum_threads_read_aware(user, forum, threads):
    if forum.is_read:
        make_read(threads)
    else:
        threads_dict = {}
        for thread in threads:
            thread.is_read = not is_date_tracked(
                thread.last_post_on, user, forum.last_read_on)
            thread.is_new = True
            if thread.is_read:
                thread.unread_replies = 0
            else:
                thread.unread_replies = thread.replies
                threads_dict[thread.pk] = thread

        if threads_dict:
            make_threads_dict_read_aware(user, threads_dict)
Example #17
0
def make_forum_threads_read_aware(user, forum, threads):
    if forum.is_read:
        make_read(threads)
    else:
        threads_dict = {}
        for thread in threads:
            thread.is_read = not is_date_tracked(thread.last_post_on, user,
                                                 forum.last_read_on)
            thread.is_new = True
            if thread.is_read:
                thread.unread_replies = 0
            else:
                thread.unread_replies = thread.replies
                threads_dict[thread.pk] = thread

        if threads_dict:
            make_threads_dict_read_aware(user, threads_dict)
Example #18
0
def make_read_aware(user, forums):
    if not hasattr(forums, '__iter__'):
        forums = [forums]

    if user.is_anonymous():
        make_read(forums)
        return None

    forums_dict = {}
    for forum in forums:
        forum.last_read_on = user.reads_cutoff
        forum.is_read = not is_date_tracked(forum.last_post_on, user)
        if not forum.is_read:
            forums_dict[forum.pk] = forum

    if forums_dict:
        for record in user.forumread_set.filter(forum__in=forums_dict.keys()):
            forum = forums_dict[record.forum_id]
            forum.last_read_on = record.last_read_on
            forum.is_read = forum.last_read_on >= forum.last_post_on
Example #19
0
def make_read_aware(user, forums):
    if not hasattr(forums, '__iter__'):
        forums = [forums]

    if user.is_anonymous():
        make_read(forums)
        return None

    forums_dict = {}
    for forum in forums:
        forum.last_read_on = user.reads_cutoff
        forum.is_read = not is_date_tracked(forum.last_post_on, user)
        if not forum.is_read:
            forums_dict[forum.pk] = forum

    if forums_dict:
        for record in user.forumread_set.filter(forum__in=forums_dict.keys()):
            forum = forums_dict[record.forum_id]
            forum.last_read_on = record.last_read_on
            forum.is_read = forum.last_read_on >= forum.last_post_on
Example #20
0
def make_read_aware(user, categories):
    if not hasattr(categories, '__iter__'):
        categories = [categories]

    if user.is_anonymous():
        make_read(categories)
        return None

    categories_dict = {}
    for category in categories:
        category.last_read_on = user.joined_on
        category.is_read = not is_date_tracked(category.last_post_on, user)
        if not category.is_read:
            categories_dict[category.pk] = category

    if categories_dict:
        categories_records = user.categoryread_set.filter(
            category__in=categories_dict.keys())

        for record in categories_records:
            category = categories_dict[record.category_id]
            category.last_read_on = record.last_read_on
            category.is_read = category.last_read_on >= category.last_post_on
Example #21
0
def make_read_aware(user, categories):
    if not hasattr(categories, '__iter__'):
        categories = [categories]

    if user.is_anonymous():
        make_read(categories)
        return None

    categories_dict = {}
    for category in categories:
        category.last_read_on = user.joined_on
        category.is_read = not is_date_tracked(category.last_post_on, user)
        if not category.is_read:
            categories_dict[category.pk] = category

    if categories_dict:
        categories_records = user.categoryread_set.filter(
            category__in=categories_dict.keys())

        for record in categories_records:
            category = categories_dict[record.category_id]
            category.last_read_on = record.last_read_on
            category.is_read = category.last_read_on >= category.last_post_on
Example #22
0
 def test_is_date_tracked(self):
     """is_date_tracked validates dates"""
     self.assertFalse(is_date_tracked(None))
     self.assertFalse(is_date_tracked(cutoff_date() - timedelta(seconds=1)))
     self.assertTrue(is_date_tracked(cutoff_date() + timedelta(minutes=1)))