Esempio n. 1
0
def show_paste(request, char_id, raw=False, download=False, version=None):
    """
    Show the paste, possibly as raw text or as a download
    """
    # If paste has expired, show the ordinary "paste not found" page
    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)
        
        version_number = version
        
        if version == None:
            version = paste.version
            
        paste_version = cache.get("paste_version:%s:%s" % (char_id, version))
        
        if paste_version == None:
            paste_version = PasteVersion.objects.get(paste=paste, version=version)
            cache.set("paste_version:%s:%s" % (char_id, version), paste_version)
    except ObjectDoesNotExist:
        cache.set("paste:%s" % char_id, False)
        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)
        
    if raw:
        text = paste.get_text(formatted=False, version=version)
        response = HttpResponse(text, content_type='text/plain')
        return response
    elif download:
        text = paste.get_text(formatted=False, version=version)
        response = HttpResponse(text, content_type='application/octet-stream')
        response["Content-Disposition"] = 'attachment; filename="%s.txt"' % char_id
        return response
    else:
        # Display the paste as normal
        paste_favorited = False
        
        if request.user.is_authenticated():
            paste_favorited = Favorite.has_user_favorited_paste(request.user, paste)
            
        # Add a hit to this paste if the hit is an unique (1 hit = 1 IP address once per 24 hours)
        ip_address = get_real_ip(request)
        
        if ip_address != None:
            paste_hits = paste.add_hit(ip_address)
        else:
            paste_hits = paste.get_hit_count()
            
        comment_count = cache.get("paste_comment_count:%s" % char_id)
        
        if comment_count == None:
            comment_count = Comment.objects.filter(paste=paste).count()
            cache.set("paste_comment_count:%s" % char_id, comment_count)
        
        paste_text = paste.get_text(version=version)
            
        return render(request, "pastes/show_paste/show_paste.html", {"paste": paste,
                                                                     "paste_version": paste_version,
                                                                     "paste_text": paste_text,
                                                                     
                                                                     "version_number": version_number,
                                                                     
                                                                     "paste_favorited": paste_favorited,
                                                                     "paste_hits": paste_hits,
                                                                     
                                                                     "comment_count": comment_count})
Esempio n. 2
0
def show_paste(request, char_id, raw=False, download=False, version=None):
    """
    Show the paste, possibly as raw text or as a download
    """
    # If paste has expired, show the ordinary "paste not found" page
    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)
        
        if version == None:
            version = paste.version
            
        paste_version = cache.get("paste_version:%s:%s" % (char_id, version))
        
        if paste_version == None:
            paste_version = PasteVersion.objects.get(paste=paste, version=version)
            cache.set("paste_version:%s:%s" % (char_id, version), paste_version)
    except ObjectDoesNotExist:
        cache.set("paste:%s" % char_id, False)
        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)
        
    if raw:
        text = paste.get_text(formatted=False, version=version)
        response = HttpResponse(text, content_type='text/plain')
        return response
    elif download:
        text = paste.get_text(formatted=False, version=version)
        response = HttpResponse(text, content_type='application/octet-stream')
        response["Content-Disposition"] = 'attachment; filename="%s.txt"' % char_id
        return response
    else:
        # Display the paste as normal
        paste_favorited = False
        
        if request.user.is_authenticated():
            paste_favorited = Favorite.has_user_favorited_paste(request.user, paste)
            
        # Add a hit to this paste if the hit is an unique (1 hit = 1 IP address once per 24 hours)
        ip_address = get_real_ip(request)
        
        if ip_address != None:
            paste_hits = paste.add_hit(ip_address)
        else:
            paste_hits = paste.get_hit_count()
            
        comment_count = cache.get("paste_comment_count:%s" % char_id)
        
        if comment_count == None:
            comment_count = Comment.objects.filter(paste=paste).count()
            cache.set("paste_comment_count:%s" % char_id, comment_count)
        
        paste_text = paste.get_text(version=version)
            
        return render(request, "pastes/show_paste/show_paste.html", {"paste": paste,
                                                                     "paste_version": paste_version,
                                                                     "paste_text": paste_text,
                                                                     
                                                                     "version": version,
                                                                     
                                                                     "paste_favorited": paste_favorited,
                                                                     "paste_hits": paste_hits,
                                                                     
                                                                     "comment_count": comment_count})