Пример #1
0
def post_view_unread(request):
    """View for seeing all posts at once. It'll mark them all read."""
    def post_map(post_to_map):
        """Returns dict of information about each individual post to add to context"""
        return {
            'id': post_to_map.id,
            'board': post_to_map.bulletin_board.key,
            'poster': post_to_map.poster_name,
            'subject': ansi.strip_ansi(post_to_map.db_header),
            'date': post_to_map.db_date_created.strftime("%x"),
            'text': ansi.strip_ansi(post_to_map.db_message)
        }

    raw_boards = get_boards(request.user)

    if request.user.is_authenticated():
        alts = []
        alt_unread_posts = []
        unread_posts = Post.objects.all_unread_by(
            request.user).filter(db_receivers_objects__in=raw_boards).order_by(
                'db_receivers_objects')
        if request.user.db.bbaltread:
            try:
                alts = [ob.player for ob in request.user.roster.alts]
            except AttributeError:
                pass
            if alts:
                alt_unread_posts = list(
                    unread_posts.exclude(db_receivers_accounts__in=alts))
        ReadPostModel = Post.db_receivers_accounts.through
        bulk_list = []

        mapped_posts = []

        for unread_post in unread_posts:
            mapped_posts.append(post_map(unread_post))
            bulk_list.append(
                ReadPostModel(accountdb=request.user, msg=unread_post))
            for alt in alts:
                if unread_post in alt_unread_posts:
                    bulk_list.append(
                        ReadPostModel(accountdb=alt, msg=unread_post))

        # They've read everything, clear out their unread cache count
        accounts = [request.user] + alts
        for board in raw_boards:
            for account in accounts:
                board.zero_unread_cache(account)

        ReadPostModel.objects.bulk_create(bulk_list)
    else:
        mapped_posts = [
            post_map(post) for post in Post.objects.filter(
                db_receivers_objects__in=raw_boards)
        ]

    return render(request, 'msgs/post_view_unread.html', {
        'page_title': 'All Unread Posts',
        'posts': mapped_posts
    })
Пример #2
0
def posts_for_request_all_search_global(user, searchstring):
    """Get all posts from all boards for this user, containing the searchstring"""
    boards = get_boards(user)
    posts = list(
        Post.objects.filter(db_receivers_objects__in=boards).filter(
            db_message__icontains=searchstring).distinct().order_by(
                '-db_date_created'))
    return posts
Пример #3
0
def board_list(request):
    """View for getting list of boards"""
    def map_board(board):
        """Helper function for getting dict of information for each board to add to context"""
        last_post = board.posts.last()
        last_date = ""
        if last_post:
            last_date = last_post.db_date_created.strftime("%x")

        if request.user.is_authenticated():
            unread = board.num_of_unread_posts(user, old=False)
        else:
            unread = board.posts.count()

        return {
            'id': board.id,
            'name': board.key,
            'unread': unread,
            'last_date': last_date
        }

    unread_only = None

    save_unread = request.GET.get("save_unread")
    if save_unread is None:
        if not request.user or not request.user.is_authenticated():
            unread_only = False
        else:
            unread_only = request.user.tags.get('web_boards_only_unread',
                                                category='boards')

    if unread_only is None:
        unread_only = False

    request_unread = request.GET.get("unread_only")

    if request_unread:
        if request_unread == "1" or request_unread == "on":
            unread_only = True
        else:
            unread_only = False

    if save_unread and request.user.is_authenticated():
        if unread_only:
            request.user.tags.add('web_boards_only_unread', category='boards')
        else:
            request.user.tags.remove('web_boards_only_unread',
                                     category='boards')

    user = request.user
    if not user or not user.is_authenticated():
        Account = get_user_model()
        try:
            user = Account.objects.get(username__iexact="Guest1")
        except (Account.DoesNotExist, Account.MultipleObjectsReturned):
            raise Http404

    raw_boards = get_boards(user)
    boards = map(lambda board: map_board(board), raw_boards)
    if unread_only:
        boards = filter(lambda board: board['unread'] > 0, boards)

    context = {
        'boards': boards,
        'page_title': 'Boards',
        'unread_only': "checked" if unread_only else ""
    }
    return render(request, 'msgs/board_list.html', context)