Exemplo n.º 1
0
def favorites(request, user, args, page=1):
    """
    Show all of user's favorites
    """
    FAVORITES_PER_PAGE = 15
    
    if not args["profile_settings"].public_favorites and request.user != args["profile_user"]:
        # Don't show pastes to other users if the user doesn't want to
        return render(request, "users/profile/favorites/favorites_hidden.html", args)
    
    args["total_pages"] = int(math.ceil(float(args["total_favorite_count"]) / float(FAVORITES_PER_PAGE)))
    
    if page > args["total_pages"]:
        page = max(int(args["total_pages"]), 1)
        
    start = (page-1) * FAVORITES_PER_PAGE
    end = start + FAVORITES_PER_PAGE
    
    args["favorites"] = cache.get("user_favorites:%s:%s" % (user.username, page))
    
    if args["favorites"] == None:
        args["favorites"] = Favorite.objects.filter(user=user).select_related("paste")[start:end]
        cache.set("user_favorites:%s:%s" % (user.username, page), args["favorites"])
        
    args["pages"] = Paginator.get_pages(page, FAVORITES_PER_PAGE, args["total_favorite_count"])
    args["current_page"] = page
    
    return render(request, "users/profile/favorites/favorites.html", args)
Exemplo n.º 2
0
def pastes(request, user, args, page=1):
    """
    Show all of user's pastes
    """
    PASTES_PER_PAGE = 15
    
    args["total_pages"] = int(math.ceil(float(args["total_paste_count"]) / float(PASTES_PER_PAGE)))
    
    if page > args["total_pages"]:
        page = max(int(args["total_pages"]), 1)
    
    offset = (page-1) * PASTES_PER_PAGE
    
    if request.user == user:
        args["pastes"] = cache.get("user_pastes:%s:%s" % (user.username, page))
        
        if args["pastes"] == None:
            args["pastes"] = Paste.objects.get_pastes(user, count=PASTES_PER_PAGE, include_hidden=True, offset=offset)
            cache.set("user_pastes:%s:%s" % (user.username, page), args["pastes"])
    else:
        args["pastes"] = cache.get("user_public_pastes:%s:%s" % (user.username, page))
        
        if args["pastes"] == None:
            args["pastes"] = Paste.objects.get_pastes(user, count=PASTES_PER_PAGE, include_hidden=False, offset=offset)
            cache.set("user_public_pastes:%s:%s" % (user.username, page), args["pastes"])
        
    args["pages"] = Paginator.get_pages(page, PASTES_PER_PAGE, args["total_paste_count"])
    args["current_page"] = page
    
    return render(request, "users/profile/pastes/pastes.html", args)
Exemplo n.º 3
0
def latest_pastes(request, page=1):
    """
    Show all of the pastes starting from the newest
    """
    PASTES_PER_PAGE = 15
    
    page = int(page)
    
    current_datetime = timezone.now()
    
    total_paste_count = cache.get("total_latest_pastes_count")
    
    if total_paste_count == None:
        total_paste_count = Paste.objects.filter(hidden=False).filter(Q(expiration_datetime__isnull=True) | Q(expiration_datetime__gte=current_datetime)).count()    
        cache.set("total_latest_pastes_count", total_paste_count)
    
    total_pages = int(math.ceil(float(total_paste_count) / float(PASTES_PER_PAGE)))
    if page > total_pages:
        page = max(int(total_pages), 1)
    
    offset = (page-1) * PASTES_PER_PAGE
    pastes = cache.get("latest_pastes:%s" % page)
    
    if pastes == None:
        pastes = Paste.objects.get_pastes(count=PASTES_PER_PAGE, offset=offset, include_hidden=False)
        cache.set("latest_pastes:%s" % page, pastes, 5)
    
    pages = Paginator.get_pages(page, PASTES_PER_PAGE, total_paste_count)
    total_pages = math.ceil(float(total_paste_count) / float(PASTES_PER_PAGE))
    
    return render(request, "latest_pastes/latest_pastes.html", {"current_page": page,
                                                                "pastes": pastes,
                                                                "pages": pages,
                                                                "total_pages": total_pages,
                                                                "total_paste_count": total_paste_count})
Exemplo n.º 4
0
def paste_history(request, char_id, page=1):
    """
    Show the earlier versions of the paste
    """
    VERSIONS_PER_PAGE = 15
    
    try:
        paste = cache.get("paste:%s" % char_id)
        
        if paste == None:
            paste = Paste.objects.select_related("user").get(char_id=char_id)
            cache.set("paste:%s" % char_id, paste)
        elif paste == False:
            return render(request, "pastes/show_paste/show_error.html", {"reason": "not_found"}, status=404)
        
    except ObjectDoesNotExist:
        return render(request, "pastes/show_paste/show_error.html", {"reason": "not_found"}, status=404)
    
    if paste.is_expired():
        return render(request, "pastes/show_paste/show_error.html", {"reason": "expired"}, status=404)
    if paste.is_removed():
        if paste.removed == Paste.USER_REMOVAL:
            return render(request, "pastes/show_paste/show_error.html", {"reason": "user_removed",
                                                                         "removal_reason": paste.removal_reason}, status=404)
        elif paste.removed == Paste.ADMIN_REMOVAL:
            return render(request, "pastes/show_paste/show_error.html", {"reason": "admin_removed",
                                                                         "removal_reason": paste.removal_reason}, status=404)
    
    total_version_count = PasteVersion.objects.filter(paste=paste).count()
    total_pages = int(math.ceil(float(total_version_count) / float(VERSIONS_PER_PAGE)))
    
    if page > total_pages:
        page = max(int(total_pages), 1)
        
    offset = (page-1) * VERSIONS_PER_PAGE
    
    start = offset
    end = offset + VERSIONS_PER_PAGE
    
    history = cache.get("paste_history:%s:%s" % (char_id, page))
    
    if history == None:
        history = PasteVersion.objects.filter(paste=paste).order_by("-submitted")[start:end]
        cache.set("paste_history:%s:%s" % (char_id, page), history)
        
    pages = Paginator.get_pages(page, VERSIONS_PER_PAGE, total_version_count)
    
    return render(request, "pastes/paste_history/paste_history.html", {"paste": paste,
                                                                       "history": history,
                                                                       
                                                                       "current_page": page,
                                                                       
                                                                       "total_version_count": total_version_count,
                                                                       
                                                                       "total_pages": total_pages,
                                                                       "pages": pages})
Exemplo n.º 5
0
def paste_history(request, char_id, page=1):
    """
    Show the earlier versions of the paste
    """
    VERSIONS_PER_PAGE = 15
    
    try:
        paste = cache.get("paste:%s" % char_id)
        
        if paste == None:
            paste = Paste.objects.select_related("user").get(char_id=char_id)
            cache.set("paste:%s" % char_id, paste)
        elif paste == False:
            return render(request, "pastes/show_paste/show_error.html", {"reason": "not_found"}, status=404)
        
    except ObjectDoesNotExist:
        return render(request, "pastes/show_paste/show_error.html", {"reason": "not_found"}, status=404)
    
    if paste.is_expired():
        return render(request, "pastes/show_paste/show_error.html", {"reason": "expired"}, status=404)
    if paste.is_removed():
        if paste.removed == Paste.USER_REMOVAL:
            return render(request, "pastes/show_paste/show_error.html", {"reason": "user_removed",
                                                                         "removal_reason": paste.removal_reason}, status=404)
        elif paste.removed == Paste.ADMIN_REMOVAL:
            return render(request, "pastes/show_paste/show_error.html", {"reason": "admin_removed",
                                                                         "removal_reason": paste.removal_reason}, status=404)
    
    total_version_count = PasteVersion.objects.filter(paste=paste).count()
    total_pages = int(math.ceil(float(total_version_count) / float(VERSIONS_PER_PAGE)))
    
    if page > total_pages:
        page = max(int(total_pages), 1)
        
    offset = (page-1) * VERSIONS_PER_PAGE
    
    start = offset
    end = offset + VERSIONS_PER_PAGE
    
    history = cache.get("paste_history:%s:%s" % (char_id, page))
    
    if history == None:
        history = PasteVersion.objects.filter(paste=paste).order_by("-submitted")[start:end]
        cache.set("paste_history:%s:%s" % (char_id, page), history)
        
    pages = Paginator.get_pages(page, VERSIONS_PER_PAGE, total_version_count)
    
    return render(request, "pastes/paste_history/paste_history.html", {"paste": paste,
                                                                       "history": history,
                                                                       
                                                                       "current_page": page,
                                                                       
                                                                       "total_version_count": total_version_count,
                                                                       
                                                                       "total_pages": total_pages,
                                                                       "pages": pages})
Exemplo n.º 6
0
def latest_pastes(request, page=1):
    """
    Show all of the pastes starting from the newest
    """
    PASTES_PER_PAGE = 15

    page = int(page)

    current_datetime = timezone.now()

    total_paste_count = cache.get("total_latest_pastes_count")

    if total_paste_count == None:
        total_paste_count = Paste.objects.filter(hidden=False).filter(
            Q(expiration_datetime__isnull=True)
            | Q(expiration_datetime__gte=current_datetime)).count()
        cache.set("total_latest_pastes_count", total_paste_count)

    total_pages = int(
        math.ceil(float(total_paste_count) / float(PASTES_PER_PAGE)))
    if page > total_pages:
        page = max(int(total_pages), 1)

    offset = (page - 1) * PASTES_PER_PAGE
    pastes = cache.get("latest_pastes:%s" % page)

    if pastes == None:
        pastes = Paste.objects.get_pastes(count=PASTES_PER_PAGE,
                                          offset=offset,
                                          include_hidden=False)
        cache.set("latest_pastes:%s" % page, pastes, 5)

    pages = Paginator.get_pages(page, PASTES_PER_PAGE, total_paste_count)
    total_pages = math.ceil(float(total_paste_count) / float(PASTES_PER_PAGE))

    return render(
        request, "latest_pastes/latest_pastes.html", {
            "current_page": page,
            "pastes": pastes,
            "pages": pages,
            "total_pages": total_pages,
            "total_paste_count": total_paste_count
        })
Exemplo n.º 7
0
def pastes(request, user, args, page=1):
    """
    Show all of user's pastes
    """
    PASTES_PER_PAGE = 15

    args["total_pages"] = int(
        math.ceil(float(args["total_paste_count"]) / float(PASTES_PER_PAGE)))

    if page > args["total_pages"]:
        page = max(int(args["total_pages"]), 1)

    offset = (page - 1) * PASTES_PER_PAGE

    if request.user == user:
        args["pastes"] = cache.get("user_pastes:%s:%s" % (user.username, page))

        if args["pastes"] == None:
            args["pastes"] = Paste.objects.get_pastes(user,
                                                      count=PASTES_PER_PAGE,
                                                      include_hidden=True,
                                                      offset=offset)
            cache.set("user_pastes:%s:%s" % (user.username, page),
                      args["pastes"])
    else:
        args["pastes"] = cache.get("user_public_pastes:%s:%s" %
                                   (user.username, page))

        if args["pastes"] == None:
            args["pastes"] = Paste.objects.get_pastes(user,
                                                      count=PASTES_PER_PAGE,
                                                      include_hidden=True,
                                                      offset=offset)
            cache.set("user_public_pastes:%s:%s" % (user.username, page),
                      args["pastes"])

    args["pages"] = Paginator.get_pages(page, PASTES_PER_PAGE,
                                        args["total_paste_count"])
    args["current_page"] = page

    return render(request, "users/profile/pastes/pastes.html", args)
Exemplo n.º 8
0
def favorites(request, user, args, page=1):
    """
    Show all of user's favorites
    """
    FAVORITES_PER_PAGE = 15

    if not args["profile_settings"].public_favorites and request.user != args[
            "profile_user"]:
        # Don't show pastes to other users if the user doesn't want to
        return render(request, "users/profile/favorites/favorites_hidden.html",
                      args)

    args["total_pages"] = int(
        math.ceil(
            float(args["total_favorite_count"]) / float(FAVORITES_PER_PAGE)))

    if page > args["total_pages"]:
        page = max(int(args["total_pages"]), 1)

    start = (page - 1) * FAVORITES_PER_PAGE
    end = start + FAVORITES_PER_PAGE

    args["favorites"] = cache.get("user_favorites:%s:%s" %
                                  (user.username, page))

    if args["favorites"] == None:
        args["favorites"] = Favorite.objects.filter(
            user=user).select_related("paste")[start:end]
        cache.set("user_favorites:%s:%s" % (user.username, page),
                  args["favorites"])

    args["pages"] = Paginator.get_pages(page, FAVORITES_PER_PAGE,
                                        args["total_favorite_count"])
    args["current_page"] = page

    return render(request, "users/profile/favorites/favorites.html", args)