Пример #1
0
def last_views(request):
    store = get_store(request)
    # Last viewed threads
    try:
        last_views = LastView.objects.filter(
            user=request.user).order_by("view_date")
    except Favorite.DoesNotExist:
        last_views = []
    last_views = paginate(last_views, request.GET.get('lvpage'))
    for last_view in last_views:
        thread = store.get_thread(last_view.list_address, last_view.threadid)
        last_view.thread = thread
        if thread is None:
            last_view.delete()
            continue
        if thread.date_active.replace(tzinfo=utc) > last_view.view_date:
            # small optimization: only query the replies if necessary
            # XXX: Storm-specific (count method)
            thread.unread = thread.replies_after(last_view.view_date).count()
        else:
            thread.unread = 0
    last_views = [lv for lv in last_views if lv.thread is not None]

    return render(request, 'ajax/last_views.html', {
        "last_views": last_views,
    })
Пример #2
0
def last_views(request):
    store = get_store(request)
    # Last viewed threads
    try:
        last_views = LastView.objects.filter(user=request.user
                                            ).order_by("view_date")
    except Favorite.DoesNotExist:
        last_views = []
    last_views = paginate(last_views, request.GET.get('lvpage'))
    for last_view in last_views:
        thread = store.get_thread(last_view.list_address, last_view.threadid)
        last_view.thread = thread
        if thread is None:
            last_view.delete()
            continue
        if thread.date_active.replace(tzinfo=utc) > last_view.view_date:
            # small optimization: only query the replies if necessary
            # XXX: Storm-specific (count method)
            thread.unread = thread.replies_after(last_view.view_date).count()
        else:
            thread.unread = 0
    last_views = [ lv for lv in last_views if lv.thread is not None ]

    return render(request, 'ajax/last_views.html', {
                "last_views": last_views,
            })
Пример #3
0
def search(request, page=1):
    """ Returns messages corresponding to a query """
    results_per_page = 10
    store = get_store(request)
    query = request.GET.get("query")
    mlist_fqdn = request.GET.get("list")
    sort_mode = request.GET.get('sort')

    if mlist_fqdn is None:
        mlist = None
    else:
        mlist = store.get_list(mlist_fqdn)
        if mlist is None:
            raise Http404("No archived mailing-list by that name.")

    if not query:
        return render(
            request, "search_results.html", {
                'mlist': mlist,
                "query": query or "",
                'messages': [],
                'total': 0,
                'sort_mode': sort_mode,
            })

    try:
        page_num = int(request.GET.get('page', "1"))
    except ValueError:
        page_num = 1

    sortedby = None
    reverse = False
    if sort_mode == "date-asc":
        sortedby = "date"
    elif sort_mode == "date-desc":
        sortedby = "date"
        reverse = True

    query_result = store.search(query,
                                mlist_fqdn,
                                page_num,
                                results_per_page,
                                sortedby=sortedby,
                                reverse=reverse)
    total = query_result["total"]
    messages = query_result["results"]
    for message in messages:
        set_message_votes(message, request.user)

    paginator = SearchPaginator(messages, 10, total)
    messages = paginate(messages, page_num, paginator=paginator)

    context = {
        'mlist': mlist,
        "query": query,
        'messages': messages,
        'total': total,
        'sort_mode': sort_mode,
    }
    return render(request, "search_results.html", context)
Пример #4
0
def _thread_list(request, mlist, threads, template_name='thread_list.html', extra_context={}):
    if mlist is None:
        raise Http404("No archived mailing-list by that name.")
    store = get_store(request)

    participants = set()
    for thread in threads:
        participants.update(thread.participants)

        # Votes
        set_thread_votes(thread, request.user)

        # Favorites
        thread.favorite = False
        if request.user.is_authenticated():
            try:
                Favorite.objects.get(list_address=mlist.name,
                                     threadid=thread.thread_id,
                                     user=request.user)
            except Favorite.DoesNotExist:
                pass
            else:
                thread.favorite = True

        # Tags
        try:
            thread.tags = Tag.objects.filter(threadid=thread.thread_id,
                                             list_address=mlist.name)
        except Tag.DoesNotExist:
            thread.tags = []

        # Category
        thread.category_hk, thread.category_form = \
                get_category_widget(request, thread.category)

        # Unread status
        thread.unread = is_thread_unread(request, mlist.name, thread)

    threads = paginate(threads, request.GET.get('page'))

    flash_messages = []
    flash_msg = request.GET.get("msg")
    if flash_msg:
        flash_msg = { "type": FLASH_MESSAGES[flash_msg][0],
                      "msg": FLASH_MESSAGES[flash_msg][1] }
        flash_messages.append(flash_msg)

    context = {
        'mlist' : mlist,
        'threads': threads,
        'participants': len(participants),
        'months_list': get_months(store, mlist.name),
        'flash_messages': flash_messages,
    }
    context.update(extra_context)
    return render(request, template_name, context)
Пример #5
0
def _thread_list(request, mlist, threads, template_name='thread_list.html', extra_context={}):
    if mlist is None:
        raise Http404("No archived mailing-list by that name.")
    store = get_store(request)

    participants = set()
    for thread in threads:
        participants.update(thread.participants)

        # Votes
        set_thread_votes(thread, request.user)

        # Favorites
        thread.favorite = False
        if request.user.is_authenticated():
            try:
                Favorite.objects.get(list_address=mlist.name,
                                     threadid=thread.thread_id,
                                     user=request.user)
            except Favorite.DoesNotExist:
                pass
            else:
                thread.favorite = True

        # Tags
        try:
            thread.tags = Tag.objects.filter(threadid=thread.thread_id,
                                             list_address=mlist.name)
        except Tag.DoesNotExist:
            thread.tags = []

        # Category
        thread.category_hk, thread.category_form = \
                get_category_widget(request, thread.category)

        # Unread status
        thread.unread = is_thread_unread(request, mlist.name, thread)

    threads = paginate(threads, request.GET.get('page'))

    flash_messages = []
    flash_msg = request.GET.get("msg")
    if flash_msg:
        flash_msg = { "type": FLASH_MESSAGES[flash_msg][0],
                      "msg": FLASH_MESSAGES[flash_msg][1] }
        flash_messages.append(flash_msg)

    context = {
        'mlist' : mlist,
        'threads': threads,
        'participants': len(participants),
        'months_list': get_months(store, mlist.name),
        'flash_messages': flash_messages,
    }
    context.update(extra_context)
    return render(request, template_name, context)
Пример #6
0
def search(request, page=1):
    """ Returns messages corresponding to a query """
    results_per_page = 10
    store = get_store(request)
    query = request.GET.get("query")
    mlist_fqdn = request.GET.get("list")
    sort_mode = request.GET.get('sort')

    if mlist_fqdn is None:
        mlist = None
    else:
        mlist = store.get_list(mlist_fqdn)
        if mlist is None:
            raise Http404("No archived mailing-list by that name.")

    if not query:
        return render(request, "search_results.html", {
            'mlist' : mlist,
            "query": query or "",
            'messages': [],
            'total': 0,
            'sort_mode': sort_mode,
        })

    try:
        page_num = int(request.GET.get('page', "1"))
    except ValueError:
        page_num = 1

    sortedby = None
    reverse = False
    if sort_mode == "date-asc":
        sortedby = "date"
    elif sort_mode == "date-desc":
        sortedby = "date"
        reverse = True

    query_result = store.search(query, mlist_fqdn, page_num, results_per_page,
                                sortedby=sortedby, reverse=reverse)
    total = query_result["total"]
    messages = query_result["results"]
    for message in messages:
        set_message_votes(message, request.user)

    paginator = SearchPaginator(messages, 10, total)
    messages = paginate(messages, page_num, paginator=paginator)

    context = {
        'mlist' : mlist,
        "query": query,
        'messages': messages,
        'total': total,
        'sort_mode': sort_mode,
    }
    return render(request, "search_results.html", context)
Пример #7
0
def votes(request):
    store = get_store(request)
    # Votes
    try:
        votes = Rating.objects.filter(user=request.user)
    except Rating.DoesNotExist:
        votes = []
    votes = paginate(votes, request.GET.get('vpage'))
    for vote in votes:
        vote.message = store.get_message_by_hash_from_list(
            vote.list_address, vote.messageid)
        if vote.message is None:
            vote.delete()
    votes = [v for v in votes if v.message is not None]
    return render(request, 'ajax/votes.html', {
        "votes": votes,
    })
Пример #8
0
def votes(request):
    store = get_store(request)
    # Votes
    try:
        votes = Rating.objects.filter(user=request.user)
    except Rating.DoesNotExist:
        votes = []
    votes = paginate(votes, request.GET.get('vpage'))
    for vote in votes:
        vote.message = store.get_message_by_hash_from_list(
                vote.list_address, vote.messageid)
        if vote.message is None:
            vote.delete()
    votes = [ v for v in votes if v.message is not None ]
    return render(request, 'ajax/votes.html', {
                "votes": votes,
            })
Пример #9
0
 def test_page_range(self):
     objects = range(1000)
     self.assertEqual(
         paginate(objects, 1).page_range, [1, 2, 3, 4, '...', 100])
     self.assertEqual(
         paginate(objects, 2).page_range, [1, 2, 3, 4, 5, '...', 100])
     self.assertEqual(
         paginate(objects, 3).page_range, [1, 2, 3, 4, 5, 6, '...', 100])
     self.assertEqual(
         paginate(objects, 4).page_range, [1, 2, 3, 4, 5, 6, 7, '...', 100])
     self.assertEqual(
         paginate(objects, 5).page_range,
         [1, 2, 3, 4, 5, 6, 7, 8, '...', 100])
     self.assertEqual(
         paginate(objects, 6).page_range,
         [1, 2, 3, 4, 5, 6, 7, 8, 9, '...', 100])
     self.assertEqual(
         paginate(objects, 7).page_range,
         [1, '...', 4, 5, 6, 7, 8, 9, 10, '...', 100])
     self.assertEqual(
         paginate(objects, 8).page_range,
         [1, '...', 5, 6, 7, 8, 9, 10, 11, '...', 100])
     self.assertEqual(
         paginate(objects, 9).page_range,
         [1, '...', 6, 7, 8, 9, 10, 11, 12, '...', 100])
     self.assertEqual(
         paginate(objects, 10).page_range,
         [1, '...', 7, 8, 9, 10, 11, 12, 13, '...', 100])
     self.assertEqual(
         paginate(objects, 40).page_range,
         [1, '...', 37, 38, 39, 40, 41, 42, 43, '...', 100])
     self.assertEqual(
         paginate(objects, 90).page_range,
         [1, '...', 87, 88, 89, 90, 91, 92, 93, '...', 100])
     self.assertEqual(
         paginate(objects, 91).page_range,
         [1, '...', 88, 89, 90, 91, 92, 93, 94, '...', 100])
     self.assertEqual(
         paginate(objects, 92).page_range,
         [1, '...', 89, 90, 91, 92, 93, 94, 95, '...', 100])
     self.assertEqual(
         paginate(objects, 93).page_range,
         [1, '...', 90, 91, 92, 93, 94, 95, 96, '...', 100])
     self.assertEqual(
         paginate(objects, 94).page_range,
         [1, '...', 91, 92, 93, 94, 95, 96, 97, '...', 100])
     self.assertEqual(
         paginate(objects, 95).page_range,
         [1, '...', 92, 93, 94, 95, 96, 97, 98, 99, 100])
     self.assertEqual(
         paginate(objects, 96).page_range,
         [1, '...', 93, 94, 95, 96, 97, 98, 99, 100])
     self.assertEqual(
         paginate(objects, 97).page_range,
         [1, '...', 94, 95, 96, 97, 98, 99, 100])
     self.assertEqual(
         paginate(objects, 98).page_range,
         [1, '...', 95, 96, 97, 98, 99, 100])
     self.assertEqual(
         paginate(objects, 99).page_range, [1, '...', 96, 97, 98, 99, 100])
     self.assertEqual(
         paginate(objects, 100).page_range, [1, '...', 97, 98, 99, 100])