Example #1
0
 def clean_text(self):
     """
     Check that the user hasn't uploaded too many pastes
     """
     if Limiter.is_limit_reached(self.request, Limiter.PASTE_UPLOAD):
         action_limit = Limiter.get_action_limit(self.request, Limiter.PASTE_UPLOAD)
         
         raise forms.ValidationError("You can only upload %s pastes every %s." % (action_limit, format_timespan(settings.MAX_PASTE_UPLOADS_PERIOD)))
 
     return self.cleaned_data.get("text")
Example #2
0
def edit_paste(request, char_id):
    """
    Edit the paste
    """
    if not request.user.is_authenticated():
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "not_logged_in"})
    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/edit_paste/show_error.html", {"reason": "not_found"}, status=404)
    except ObjectDoesNotExist:
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "not_found"})
    
    if paste.user != request.user and not request.user.is_staff:
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "not_owner"})
    
    if paste.is_expired():
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "expired"})
    if paste.is_removed():
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "removed"})
    
    if paste.hidden:
        visibility = "hidden"
    else:
        visibility = "public"
    
    edit_form = EditPasteForm(request.POST or None, request=request,
                                                    initial={"title": paste.title,
                                                             "visibility": visibility,
                                                             "syntax_highlighting": paste.format})
    
    if edit_form.is_valid():
        paste_data = edit_form.cleaned_data
        
        paste.update_paste(title=paste_data["title"],
                           text=paste_data["text"],
                           visibility=paste_data["visibility"],
                           format=paste_data["syntax_highlighting"],
                           encrypted=paste_data["encrypted"],
                           note=paste_data["note"])
        
        Limiter.increase_action_count(request, Limiter.PASTE_EDIT)
        
        return redirect("show_paste", char_id=char_id)
    
    paste_text = paste.get_text(formatted=False)
    
    return render(request, "pastes/edit_paste/edit_paste.html", {"paste": paste,
                                                                 "paste_text": paste_text,
                                                                 
                                                                 "form": edit_form})
Example #3
0
def edit_paste(request, char_id):
    """
    Edit the paste
    """
    if not request.user.is_authenticated():
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "not_logged_in"})
    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/edit_paste/show_error.html", {"reason": "not_found"}, status=404)
    except ObjectDoesNotExist:
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "not_found"})
    
    if paste.user != request.user and not request.user.is_staff:
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "not_owner"})
    
    if paste.is_expired():
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "expired"})
    if paste.is_removed():
        return render(request, "pastes/edit_paste/edit_error.html", {"reason": "removed"})
    
    if paste.hidden:
        visibility = "hidden"
    else:
        visibility = "public"
    
    edit_form = EditPasteForm(request.POST or None, request=request,
                                                    initial={"title": paste.title,
                                                             "visibility": visibility,
                                                             "syntax_highlighting": paste.format})
    
    if edit_form.is_valid():
        paste_data = edit_form.cleaned_data
        
        paste.update_paste(title=paste_data["title"],
                           text=paste_data["text"],
                           visibility=paste_data["visibility"],
                           format=paste_data["syntax_highlighting"],
                           encrypted=paste_data["encrypted"],
                           note=paste_data["note"])
        
        Limiter.increase_action_count(request, Limiter.PASTE_EDIT)
        
        return redirect("show_paste", char_id=char_id)
    
    paste_text = paste.get_text(formatted=False)
    
    return render(request, "pastes/edit_paste/edit_paste.html", {"paste": paste,
                                                                 "paste_text": paste_text,
                                                                 
                                                                 "form": edit_form})
Example #4
0
    def clean_text(self):
        """
        Check that the user hasn't uploaded too many pastes
        """
        if Limiter.is_limit_reached(self.request, Limiter.PASTE_UPLOAD):
            action_limit = Limiter.get_action_limit(self.request, Limiter.PASTE_UPLOAD)

            raise forms.ValidationError(
                "You can only upload %s pastes every %s."
                % (action_limit, format_timespan(settings.MAX_PASTE_UPLOADS_PERIOD))
            )

        return self.cleaned_data.get("text")
Example #5
0
def home(request):
    """
    Display the index page with the form to submit a paste, as well as the most recent
    pastes
    """
    paste_form = SubmitPasteForm(request.POST or None, request=request)

    latest_pastes = cache.get("home_latest_pastes")

    if latest_pastes == None:
        latest_pastes = Paste.objects.get_pastes(include_expired=False,
                                                 include_hidden=False,
                                                 count=15)
        cache.set("home_latest_pastes", latest_pastes, 5)

    languages = highlighting.settings.LANGUAGES

    if paste_form.is_valid():
        paste_data = paste_form.cleaned_data

        user = None
        if request.user.is_authenticated():
            user = request.user

        paste = Paste()

        char_id = paste.add_paste(title=paste_data["title"],
                                  user=user,
                                  text=paste_data["text"],
                                  expiration=paste_data["expiration"],
                                  visibility=paste_data["visibility"],
                                  format=paste_data["syntax_highlighting"],
                                  encrypted=paste_data["encrypted"])

        Limiter.increase_action_count(request, Limiter.PASTE_UPLOAD)

        # Redirect to the newly created paste
        return redirect("show_paste", char_id=char_id)

    return render(request, "home/home.html", {
        "form": paste_form,
        "latest_pastes": latest_pastes,
        "languages": languages
    })
Example #6
0
def home(request):
    """
    Display the index page with the form to submit a paste, as well as the most recent
    pastes
    """
    paste_form = SubmitPasteForm(request.POST or None, request=request)
    
    latest_pastes = cache.get("home_latest_pastes")
    
    if latest_pastes == None:
        latest_pastes = Paste.objects.get_pastes(include_expired=False, include_hidden=False,
                                                 count=15)
        cache.set("home_latest_pastes", latest_pastes, 5)
    
    languages = highlighting.settings.LANGUAGES
    
    if paste_form.is_valid():
        paste_data = paste_form.cleaned_data
        
        user = None
        if request.user.is_authenticated():
            user = request.user
        
        paste = Paste()
        
        char_id = paste.add_paste(title=paste_data["title"],
                                  user=user,
                                  text=paste_data["text"],
                                  expiration=paste_data["expiration"],
                                  visibility=paste_data["visibility"],
                                  format=paste_data["syntax_highlighting"],
                                  encrypted=paste_data["encrypted"])
        
        Limiter.increase_action_count(request, Limiter.PASTE_UPLOAD)
        
        # Redirect to the newly created paste
        return redirect("show_paste", char_id=char_id)
    
    return render(request, "home/home.html", {"form": paste_form,
                                              "latest_pastes": latest_pastes,
                                              "languages": languages })
Example #7
0
def add_comment(request):
    """
    Adds a comment to a paste
    """
    response = {"status": "success", "data": {}}

    if "char_id" in request.POST:
        char_id = request.POST["char_id"]
    else:
        response["status"] = "fail"
        response["data"][
            "message"] = "Paste ID was not provided (POST parameter 'char_id')"
        return HttpResponse(json.dumps(response), status=422)

    if "text" not in request.POST:
        response["status"] = "fail"
        response["data"][
            "message"] = "Comment text was not provided (POST parameter 'text')"
        return HttpResponse(json.dumps(response), status=422)

    try:
        paste = Paste.objects.get(char_id=char_id)
    except ObjectDoesNotExist:
        response["status"] = "fail"
        response["data"]["message"] = "The paste couldn't be found."
        return HttpResponse(json.dumps(response), status=422)

    if not request.user.is_authenticated() or not request.user.is_active:
        response["status"] = "fail"
        response["data"]["message"] = "You are not logged in."
        return HttpResponse(json.dumps(response), status=422)

    if Limiter.is_limit_reached(request, Limiter.COMMENT):
        response["status"] = "fail"
        response["data"][
            "message"] = "You can only post %s comments every %s." % (
                Limiter.get_action_limit(request, Limiter.COMMENT),
                format_timespan(settings.MAX_COMMENTS_PERIOD))
        return HttpResponse(json.dumps(response))

    submit_form = SubmitCommentForm(request.POST or None)

    if submit_form.is_valid():
        comment_data = submit_form.cleaned_data

        comment = Comment(text=comment_data["text"],
                          user=request.user,
                          paste=paste)
        comment.save()

        Limiter.increase_action_count(request, Limiter.COMMENT)

        total_comment_count = Comment.objects.filter(paste=paste).count()

        response["data"]["comments"] = queryset_to_list(Comment.objects.filter(paste=paste) \
                                                                       .select_related("user") \
                                                                       [0:Comment.COMMENTS_PER_PAGE],
                                                                       fields=["id", "text", "submitted", "edited", "user__username=username"])
        response["data"]["page"] = 0
        response["data"]["pages"] = math.ceil(
            float(total_comment_count) / float(Comment.COMMENTS_PER_PAGE))

        if response["data"]["pages"] == 0:
            response["data"]["pages"] = 1

        response["data"]["total_comment_count"] = total_comment_count
    else:
        response["status"] = "fail"
        response["data"]["message"] = "Provided text wasn't valid."

    return HttpResponse(json.dumps(response))
Example #8
0
def add_comment(request):
    """
    Adds a comment to a paste
    """
    response = {"status": "success",
                "data": {}}
    
    if "char_id" in request.POST:
        char_id = request.POST["char_id"]
    else:
        response["status"] = "fail"
        response["data"]["message"] = "Paste ID was not provided (POST parameter 'char_id')"
        return HttpResponse(json.dumps(response), status=422)
    
    if "text" not in request.POST:
        response["status"] = "fail"
        response["data"]["message"] = "Comment text was not provided (POST parameter 'text')"
        return HttpResponse(json.dumps(response), status=422)
    
    try:
        paste = Paste.objects.get(char_id=char_id)
    except ObjectDoesNotExist:
        response["status"] = "fail"
        response["data"]["message"] = "The paste couldn't be found."
        return HttpResponse(json.dumps(response), status=422)
    
    if not request.user.is_authenticated() or not request.user.is_active:
        response["status"] = "fail"
        response["data"]["message"] = "You are not logged in."
        return HttpResponse(json.dumps(response), status=422)
    
    if Limiter.is_limit_reached(request, Limiter.COMMENT):
        response["status"] = "fail"
        response["data"]["message"] = "You can only post %s comments every %s." % (Limiter.get_action_limit(request, Limiter.COMMENT), format_timespan(settings.MAX_COMMENTS_PERIOD))
        return HttpResponse(json.dumps(response))
    
    submit_form = SubmitCommentForm(request.POST or None)
        
    if submit_form.is_valid():
        comment_data = submit_form.cleaned_data
        
        comment = Comment(text=comment_data["text"], 
                          user=request.user,
                          paste=paste)
        comment.save()
        
        Limiter.increase_action_count(request, Limiter.COMMENT)
        
        total_comment_count = Comment.objects.filter(paste=paste).count()
        
        response["data"]["comments"] = queryset_to_list(Comment.objects.filter(paste=paste) \
                                                                       .select_related("user") \
                                                                       [0:Comment.COMMENTS_PER_PAGE],
                                                                       fields=["id", "text", "submitted", "edited", "user__username=username"])
        response["data"]["page"] = 0
        response["data"]["pages"] = math.ceil(float(total_comment_count) / float(Comment.COMMENTS_PER_PAGE))
        
        if response["data"]["pages"] == 0:
            response["data"]["pages"] = 1
            
        response["data"]["total_comment_count"] = total_comment_count
    else:
        response["status"] = "fail"
        response["data"]["message"] = "Provided text wasn't valid."
        
    return HttpResponse(json.dumps(response))