Beispiel #1
0
def _create_paste(request):
    """ HTTP POST: Creates a paste, and then returns the UID for the paste """
    if request.method == 'POST':
        if request.POST.get('content') is None:
            err = {'error': 'Paste content cannot be empty'}
            return JsonResponse(err, status=422)

        paste = Paste()
        paste.content = request.POST.get('content')

        # If a title is defined by the user, use the title provided.
        # Otherwise, it will fall back to "Untitled Paste"
        if request.POST.get('title') is not None:
            paste.title = request.POST.get('title')

        # If a password is provided, encrypt the password and then store
        if request.POST.get('password') is not None:
            paste.password = hashers.make_password(request.POST.get('password'))

        # Get the IP address from the request, using the helper function.
        paste.ip_addr = _get_client_ip(request)

        paste.save()

        msg = {
            'message': 'The paste has been successfully posted',
            'uid': paste.uid,
        }
        response = JsonResponse(msg, status=200)
        response['Location'] = str(paste.uid)  # Add Paste UID to Location header
        return response
    else:
        err = {'error': 'Bad Request'}
        return JsonResponse(err, status=400)
Beispiel #2
0
def _create_paste(request):
    """ HTTP POST: Creates a paste, and then returns the UID for the paste """
    if request.method == 'POST':
        if request.POST.get('content') is None:
            err = {'error': 'Paste content cannot be empty'}
            return JsonResponse(err, status=422)

        paste = Paste()
        paste.content = request.POST.get('content')

        # If a title is defined by the user, use the title provided.
        # Otherwise, it will fall back to "Untitled Paste"
        if request.POST.get('title') is not None:
            paste.title = request.POST.get('title')

        # If a password is provided, encrypt the password and then store
        if request.POST.get('password') is not None:
            paste.password = hashers.make_password(
                request.POST.get('password'))

        # Get the IP address from the request, using the helper function.
        paste.ip_addr = _get_client_ip(request)

        paste.save()

        msg = {
            'message': 'The paste has been successfully posted',
            'uid': paste.uid,
        }
        response = JsonResponse(msg, status=200)
        response['Location'] = str(
            paste.uid)  # Add Paste UID to Location header
        return response
    else:
        err = {'error': 'Bad Request'}
        return JsonResponse(err, status=400)