Hi all,

We are building website, Here I have written functionality for classifieds
page.
It is loading to slow because of We are prompting distance calculation in
classifieds page. By using distance API matrix based on logged in user zip
code and individual Ad zip codes from query set(database).

```
    def classifieds(request):
        global dict_time
        try:
            dict_time = {}
            email = request.session.get('email')

            # Here we are displaying the classified ads "order by date".
The ads will be sorted by latest date.
            classifieds =
vk_classifieds.objects.filter(class_status='1').order_by('-added_date')

            count =
vk_classifieds.objects.filter(class_status='1').order_by('-added_date').count()
            for i in classifieds:
                # diff_time is a child method. By passing 'i' as object in
diff_time.
                difrnc_date = diff_time(i)
                dict_time[i.id] = difrnc_date

            # Pagination for classifieds page.
            # classified = random.sample(list(classifieds), k=count)
            # print("classified = ", str(classified))
            # By default first page
            page = request.GET.get('page', 1)
            # print("page = ", str(page))
            # Per page setting 40 objects.
            paginator = Paginator(list(classifieds), 40)
            # print("paginator = ", str(paginator))
            classified_p = paginator.page(page)
            # print(classified_p)
        except PageNotAnInteger:
            classified_p = paginator.page(1)
        except EmptyPage:
            classified_p = paginator.page(paginator.num_pages)
        except Exception as e:
            logging.error(e)
            return render(request, "classifieds.html",
                          {"Classifieds": classified_p,
 # distance calculation
                           "distance": classifieds_dist(email),
                           'some_date': dict_time,
                           })
        return render(request, "classifieds.html", {"Classifieds":
classified_p,
                                                    "distance":
classifieds_dist(email),
                                                    'some_date': dict_time,
                                                    })
```

```

def classifieds_dist(email):
    global frm, km
    dict_distance = {}

    qury = vk_customer.objects.filter(email=email).first()

    # From above qury variable we are getting zip of customer.
    frm = qury.Zip
    # importing json package to calculate the distance
    url = "
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial";
    headers = {
        'Authorization': "Bearer somevalue",
        'User-Agent': "some value",
        'Accept': "*/*",
        'Cache-Control': "no-cache",
        'Postman-Token': "some value",
        'Host': "maps.googleapis.com",
        'Accept-Encoding': "gzip, deflate",
        'Connection': "keep-alive",
        'cache-control': "no-cache"
    }
    classifieds =
vk_classifieds.objects.filter(class_status='1').order_by('-added_date')
    for i in classifieds:
        # while a user login through his login email we capture his
complete detail into an variable which we given as "qury"
        # and after the details are stored into the variable from there we
will filter his starting and destination point as zipcode

        # After login his/her based on email we are filtering in
vk_customer table. Then storing in qury variable.

        # This zip is getting from vk_classifieds(model in models.py) table.
        # 'i' attribute is getting from classifieds() function.
        to = i.zip

        origin_list = [to]
        desination_list = [frm]
        # here we used api for calculating the source and destination point
        querystring = {"origins": origin_list, "destinations":
desination_list, "departure_time": "now",
                       "key": "AIzaSyDhlCiMAEEfoYhkPcOyP0PLqpHsVMmYEXM"}
        # here we are passing these headers to the api

        # we are capturing the response in variable called response
        response = requests.request("GET", url, headers=headers,
params=querystring)
        jsondata = response.text
        obj = json.loads(jsondata)
        list = obj['rows']
        if list:
            a = list[0].get('elements')
            obj2 = a[0].get("distance")
            if obj2 is None:
                km = "None"
            else:
                km = obj2["text"]
            dict_distance[i.id] = km
            l1.append(i.id)
            print("id = ", str(i.id))
            print("km = ", str(km))
    return dict_distance


```

Because of this loading time of classifieds page is to slow. Please help me
to solve this issue.

Thanks
~Salima

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMSz6bn7j5vsyya9%3Dg4wQBQSyiAEkc8%3DSqm_5QOtthUBWnmZFA%40mail.gmail.com.
  • [no subject] Salima Begum
    • Re: Chetan Ganji
      • Re: Alam Khazi

Reply via email to