0

I'm setting up a Views that save data from an API into my database on a button click and I'm having a hard time trying to figure out how to limit the size of the requests response for product descriptions in the following way:

If the lenght of a description is higher than 2000, delete some of the letters at the end of it until it reaches the limit of 2000, but don't remove it completely from the request.

As of now, what I've been able to achieve is to completely remove the product infos if the lenght is higher than 2000 as you can see below.

My django views function:

def api_data(request):
    if request.GET.get('mybtn'):  # to improve, == 'something':
        resp_1 = requests.get(
            "https://www.test-headout.com/api/public/v1/product/listing/list-by/city?language=fr&cityCode=PARIS&limit=5000&currencyCode=CAD",
            headers={
                "Headout-Auth": HEADOUT_TEST_API_KEY
            })
        resp_1_data = resp_1.json()
        base_url_2 = "https://www.test-headout.com/api/public/v1/product/get/"

        for item in resp_1_data['items']:
            # concat ID to the URL string
            url = '{}{}'.format(base_url_2, item['id'] + '?language=fr')

            # make the HTTP request
            resp_2 = requests.get(
                url,
                headers={
                    "Headout-Auth": HEADOUT_TEST_API_KEY
                })
            resp_2_data = resp_2.json()

            if len(resp_2_data['contentListHtml'][0]['html']) < 2000: #represent the description of a product
                Product.objects.get_or_create(
                    title=item['name'],
                    destination=item['city']['name'],
                    description=resp_2_data['contentListHtml'][0]['html'],
                    link=item['canonicalUrl'],
                    image=item['image']['url']
                )

    return render(request, "form.html")

But I remove way to much rows by doing this so I would like to know how can I fix this?

Please help.

locq
  • 301
  • 1
  • 5
  • 22
  • see https://stackoverflow.com/questions/3486384/output-first-100-characters-in-a-string I think you just want to slice the contentListHtml `description=resp_2_data['contentListHtml'][0]['html'][:2000]` – AMG May 31 '19 at 00:33

1 Answers1

2

Instead of using a conditional statement, you can use the slice operator to specify the character limit for the description. Using this approach you can refactor the relevant part of your code:

resp_2_data = resp_2.json()
Product.objects.get_or_create(title=item['name'],
                              destination=item['city']['name'],
                              description=resp_2_data['contentListHtml'][0]['html'][0:2000],
                              ....
                              )
Juan Carlos Ramirez
  • 2,054
  • 1
  • 7
  • 22