Hi all,
-- By using API calculating Distance between two Zip codes(post codes) But Here is my question. When user loading page always running API is not good coding so, I want to save that into one table and show that data into classifieds page( items listings). So, Here We are calculating distance between logged in user zip code and Ad posted by user Zip code. In classifieds page we have number of ads so, How can I achieve this Can any one help me to achieve this Thank you.
```
def classifieds(request):
global dict_time, distance_var, list_names_qury
try:
dict_time = {}
email = request.session.get('email')
list_names_qury = Wishlist_cl.objects.filter(email=email,
itegration="Classifieds").values_list('name',
flat=True).count()
print("list_names_qury")
print(list_names_qury)
# Here we are displaying the classified ads "order by date". The ads will be sorted by latest date.
classifieds = classifieds.objects.filter(class_status='1').order_by('-added_date')
cl_list = list(classifieds)
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
qury_list = list(classifieds)
# 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",
{'msg_count': msg_count_cl(email),
'qury_list': qury_list,
'list_names_qury': Classifieds_list_names_count(email),
'wishlist_names': wishlist_names_Classifieds(email),
"error": error, 'banners': bestDeals(),
'form': HomeForm(), 'fullname': fullname(email),
"Classifieds": classified_p,
# distance parsing to classifieds page.
"distance": classifieds_dist(email),
'name': first_last_initial(email),
'time': settings.SESSION_IDLE_TIMEOUT,
'id': request.session.get('id'),
'some_date': dict_time,
})
return render(request, "classifieds.html", {'msg_count': msg_count_cl(email),
'list_names_qury': Classifieds_list_names_count(email),
'wishlist_names': wishlist_names_Classifieds(email),
"Classifieds": classified_p, 'qury_list': qury_list,
# distance parsing to classifieds page.
"distance": classifieds_dist(email),
'id': request.session.get('id'),
'some_date': dict_time,
})
def classifieds(request):
global dict_time, distance_var, list_names_qury
try:
dict_time = {}
email = request.session.get('email')
list_names_qury = Wishlist_cl.objects.filter(email=email,
itegration="Classifieds").values_list('name',
flat=True).count()
print("list_names_qury")
print(list_names_qury)
# Here we are displaying the classified ads "order by date". The ads will be sorted by latest date.
classifieds = classifieds.objects.filter(class_status='1').order_by('-added_date')
cl_list = list(classifieds)
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
qury_list = list(classifieds)
# 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",
{'msg_count': msg_count_cl(email),
'qury_list': qury_list,
'list_names_qury': Classifieds_list_names_count(email),
'wishlist_names': wishlist_names_Classifieds(email),
"error": error, 'banners': bestDeals(),
'form': HomeForm(), 'fullname': fullname(email),
"Classifieds": classified_p,
# distance parsing to classifieds page.
"distance": classifieds_dist(email),
'name': first_last_initial(email),
'time': settings.SESSION_IDLE_TIMEOUT,
'id': request.session.get('id'),
'some_date': dict_time,
})
return render(request, "classifieds.html", {'msg_count': msg_count_cl(email),
'list_names_qury': Classifieds_list_names_count(email),
'wishlist_names': wishlist_names_Classifieds(email),
"Classifieds": classified_p, 'qury_list': qury_list,
# distance parsing to classifieds page.
"distance": classifieds_dist(email),
'id': request.session.get('id'),
'some_date': dict_time,
})
```
```
def classifieds_dist(email):
global frm, km
l1 = []
dict_distance = {}
qury = customer.objects.filter(email=email).first()
if qury:
# From above qury variable we are getting zip of customer.
frm = qury.Zip
# importing json package to calculate the distance
url = "some url"
headers = {
'Authorization': "some key",
'User-Agent': "some agent",
'Accept': "*/*",
'Cache-Control': "no-cache",
'Postman-Token': "some token",
'Host': "maps.googleapis.com",
'Accept-Encoding': "gzip, deflate",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
classifieds = 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 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": "some key"}
# 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)
return dict_distance
global frm, km
l1 = []
dict_distance = {}
qury = customer.objects.filter(email=email).first()
if qury:
# From above qury variable we are getting zip of customer.
frm = qury.Zip
# importing json package to calculate the distance
url = "some url"
headers = {
'Authorization': "some key",
'User-Agent': "some agent",
'Accept': "*/*",
'Cache-Control': "no-cache",
'Postman-Token': "some token",
'Host': "maps.googleapis.com",
'Accept-Encoding': "gzip, deflate",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
classifieds = 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 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": "some key"}
# 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)
return dict_distance
```
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMSz6bkup%3DCGyg%3DYYj3AkRTNgjxbQLGkZiUQ%2BMZMQKdJ1VEx-w%40mail.gmail.com.
No comments:
Post a Comment