views.py 文件源码

python
阅读 54 收藏 0 点赞 0 评论 0

项目:semillas_platform 作者: Semillas 项目源码 文件源码
def get_queryset(self):
        queryset = Service.objects.all()
        #Order all the services by distance to the requester user location
        if 'lat' in self.request.query_params and 'lon' in self.request.query_params:
            # Brings lat and lon in request parameters
            ref_location = Point(float(self.request.query_params['lon']),float(self.request.query_params['lat']),srid=4326)
            if not self.request.user.is_anonymous():
                # If user is logged in, save his location
                self.request.user.location = ref_location
                self.request.user.save()
        elif not self.request.user.is_anonymous and (self.request.user.location is not None):
            # User has a location previously saved
            ref_location = self.request.user.location
        else:
            # if no location at all
            geoip = GeoIP2()
            ip = self.request.META['REMOTE_ADDR']
            try:
                ref_location = Point(geoip.lon_lat(ip),srid=4326)
            except AddressNotFoundError:
                ref_location = Point((-3.8196228, 40.4378698), srid=4326) # Madrid
            if not self.request.user.is_anonymous:
                self.request.user.location = ref_location
                self.request.user.save()
        return queryset.annotate(dist = Distance('author__location', ref_location)).order_by('dist')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号