def places(self, request, *args, **kwargs):
snippet = self.get_object()
lat = request.query_params['lat']
lng = request.query_params['lng']
userId = kwargs.get("id")
listPlaces = []
user = User.objects(id=userId).get()
userTypes = user.types
if not userTypes:
userTypes = ['594c36a033cbb6eed6f364e6','594c36b233cbb6eed6f364f2','594c368633cbb6eed6f364da','594c4a504362de3d8fae464f','5956fee67d2bf938c292e0bc']
places = Place.objects(Q(loc__near=[float(lat), float(lng)]))
for type in userTypes:
typeName = Category.objects(id=type).get().name
for place in places:
if typeName in place.types:
# Rating place according to twitter
twitterRating = len(Tweet.objects(text__contains= place.name))
place.twitter_rating = twitterRating
#Rating place according to user
userRating = RatedPlace.objects(Q(user_id__exact=userId) & Q(place_id__exact=place.id) )
print userId
print place.id
print userRating
if userRating:
rating = userRating.order_by('-id').first().rating
else:
rating = ''
place.user_rating = rating
#Adding places to list of recommended places
listPlaces.append(place)
# Sort the list according to twitter rated
listPlaces.sort(key=lambda x: x.twitter_rating, reverse=True)
# Sort the list according to user rated
listPlaces.sort(key=lambda x: x.user_rating, reverse=True)
serializer = PlaceSerializer(listPlaces[:20], many=True)
return Response(serializer.data)
########User choosen categories
评论列表
文章目录