예제 #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")
예제 #2
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")
예제 #3
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))
예제 #4
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))