~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to pybb/views.py

  • Committer: franku
  • Date: 2019-05-27 15:46:36 UTC
  • Revision ID: somal@arcor.de-20190527154636-fls6xrmjt1q5vxs3
fixed latest posts view when entering invalid values in the form

Show diffs side-by-side

added added

removed removed

Lines of Context:
434
434
            sort_by = sort_by_default
435
435
 
436
436
    # Executed on every request (POST and GET)
437
 
    search_date = date.today() - timedelta(int(days))
438
 
 
439
 
    # Create a QuerySet with only public posts
440
 
    last_posts = Post.objects.public(date_from=search_date)
441
 
 
442
 
    posts_count = len(last_posts)
443
 
 
444
 
    if sort_by == 'topic':
445
 
        # The use of an OrderedDict makes sure the ordering of
446
 
        # last_posts get not arbitrary
447
 
        topics = OrderedDict()
448
 
        for post in last_posts:
449
 
            if post.topic not in topics:
450
 
                # Create a new key with a list as value
451
 
                topics[post.topic] = [post]
452
 
            else:
453
 
                # key exists, just add the post
454
 
                topics[post.topic].append(post)
455
 
 
456
 
        object_list = topics
457
 
 
458
 
    elif sort_by == 'forum':
459
 
        forums = OrderedDict()
460
 
        for post in last_posts:
461
 
            if post.topic.forum.name not in forums:
462
 
                forums[post.topic.forum.name] = OrderedDict({post.topic: [post]})
463
 
            elif post.topic not in forums[post.topic.forum.name]:
464
 
                forums[post.topic.forum.name].update({post.topic: [post]})
465
 
            else:
466
 
                forums[post.topic.forum.name][post.topic].append(post)
467
 
 
468
 
        object_list = forums
469
 
 
 
437
    # We need a try clause here, because one can set invalid values for
 
438
    # days in the form.
 
439
    try:
 
440
        search_date = date.today() - timedelta(int(days))
 
441
 
 
442
        # Create a QuerySet with only public posts
 
443
        last_posts = Post.objects.public(date_from=search_date)
 
444
 
 
445
        posts_count = len(last_posts)
 
446
 
 
447
        if sort_by == 'topic':
 
448
            # The use of an OrderedDict makes sure the ordering of
 
449
            # last_posts get not arbitrary
 
450
            topics = OrderedDict()
 
451
            for post in last_posts:
 
452
                if post.topic not in topics:
 
453
                    # Create a new key with a list as value
 
454
                    topics[post.topic] = [post]
 
455
                else:
 
456
                    # key exists, just add the post
 
457
                    topics[post.topic].append(post)
 
458
 
 
459
            object_list = topics
 
460
 
 
461
        elif sort_by == 'forum':
 
462
            forums = OrderedDict()
 
463
            for post in last_posts:
 
464
                if post.topic.forum.name not in forums:
 
465
                    forums[post.topic.forum.name] = OrderedDict({post.topic: [post]})
 
466
                elif post.topic not in forums[post.topic.forum.name]:
 
467
                    forums[post.topic.forum.name].update({post.topic: [post]})
 
468
                else:
 
469
                    forums[post.topic.forum.name][post.topic].append(post)
 
470
 
 
471
            object_list = forums
 
472
 
 
473
    except UnboundLocalError:
 
474
        # Needed variables
 
475
        object_list = []
 
476
        posts_count = 0
 
477
        sort_by = sort_by_default
 
478
        
470
479
    return {
471
480
        'object_list': object_list,
472
481
        'posts_count': posts_count,