def get_geofeatures(request): cache_key = _generate_cache_key(request) cache_val = cache.get(cache_key, cache_control.GEOFEATURES_CACHE_GROUP) if cache_val: return APIResponseOK(content=cache_val) bounds_param = get_param(request.GET, 'within') query = request.GET.copy() if bounds_param: pnts = bounds_param.split(',') bbox = (float(pnts[0]), float(pnts[1]), float(pnts[2]), float(pnts[3])) poly = Polygon.from_bbox(bbox) poly.set_srid(4326) del query['within'] base_query = Q() if bounds_param: base_query = base_query & Q(location__within=poly) # cast within bounds cast_base_query = models.Cast.get_privacy_q(request) & base_query q = qstranslate.QueryTranslator(models.Cast, CastAPI.ruleset, cast_base_query) try: casts = q.filter(query).select_related('author').prefetch_related('media_set').prefetch_related('tags') except qstranslate.InvalidParameterException, e: raise exceptions.APIBadRequest(e.message)
def get(request, event_id=None): if event_id: event = get_object(Event, id=event_id) if not event.allowed_access(request.user): raise exceptions.APIForbidden event_dict = api_serialize(event, request) return APIResponseOK(content=event_dict, total=1) else: q = qstranslate.QueryTranslator(Event, EventAPI.ruleset) query = request.GET.copy() objs = total = pg = None try: objs = q.filter(query) objs, total, pg = paginate(objs, query) except qstranslate.InvalidParameterException, e: raise exceptions.APIBadRequest(e) event_arr = [] for m in objs: event_arr.append(api_serialize(m, request)) return APIResponseOK(content=event_arr, total=total, pg=pg)
def get(request, user_id=None): # Single user if user_id: u = get_object(TravelsUser, id=user_id) content = api_serialize(u) return APIResponseOK(content=content, total=1) # Multiple users else: q = qstranslate.QueryTranslator(TravelsUser, UserAPI.ruleset) query = request.GET.copy() objs = total = pg = None try: objs = q.filter(query) objs, total, pg = paginate(objs, request.GET) except qstranslate.InvalidParameterException, e: raise exceptions.APIBadRequest(e.message) user_arr = [] for m in objs: user_arr.append(api_serialize(m, request)) return APIResponseOK(content=user_arr, total=total, pg=pg)
def get_geofeatures(request): bounds_param = get_param(request.GET, 'within') query = request.GET.copy() if bounds_param: pnts = bounds_param.split(',') bbox = (float(pnts[0]), float(pnts[1]), float(pnts[2]), float(pnts[3])) poly = Polygon.from_bbox(bbox) poly.set_srid(4326) del query['within'] base_query = Q() if bounds_param: base_query = base_query & Q(location__within=poly) # cast within bounds cast_base_query = models.Cast.get_privacy_q(request) & base_query q = qstranslate.QueryTranslator(models.Cast, CastAPI.ruleset, cast_base_query) casts = q.filter(query) cast_arr = [] for c in casts: if c.location: cast_arr.append(geojson_serialize(c, c.location, request)) #event within bounds events = models.Event.objects.filter(base_query) event_arr = [] for e in events: if e.location: event_arr.append(geojson_serialize(e, e.location, request)) # itinerary intersects bounds if bounds_param: base_query = Q(path__intersects = poly) itins = models.Itinerary.objects.filter(base_query) itin_arr = [] for i in itins: if i.path: itin_arr.append(geojson_serialize(i, i.path, request)) features_dict = {} features_dict['casts'] = dict(type='FeatureCollection', features=cast_arr) features_dict['events'] = dict(type='FeatureCollection', features=event_arr) features_dict['itineraries'] = dict(type='FeatureCollection', features=itin_arr) dict(casts = cast_arr, events = event_arr, itineraries = itin_arr) return APIResponseOK(content=features_dict)
def get_urgency_rank(request, itin_id=None): query = request.GET.copy() base_query = Q() cast_base_query = models.Cast.get_privacy_q(request) & base_query q = qstranslate.QueryTranslator(models.Cast, CastAPI.ruleset, cast_base_query) try: pre_query = q.filter(query).select_related('author').prefetch_related('media_set').prefetch_related('tags').prefetch_related('itinerary_set').annotate(urgency_score=Sum('tags__urgency_score')).filter(urgency_score__gt=0) if itin_id: pre_query = pre_query.filter(itinerary__id=itin_id) casts = pre_query.order_by('-urgency_score')[:10] except qstranslate.InvalidParameterException, e: raise exceptions.APIBadRequest(e.message)
def get(request, itin_id=None): if itin_id: itinerary = get_object(Itinerary, id=itin_id) itinerary_dict = api_serialize(itinerary, request) return APIResponseOK(content=itinerary_dict, total=1) else: query = request.GET.copy() q = qstranslate.QueryTranslator(Itinerary, ItineraryAPI.ruleset) try: objs = q.filter(query) except qstranslate.InvalidParameterException, e: raise exceptions.APIBadRequest(e) objs, total, pg = paginate(objs, query) itinerary_arr = [] for i in objs: itinerary_arr.append(api_serialize(i, request)) return APIResponseOK(content=itinerary_arr, total=total, pg=pg)
def get(request, cast_id=None, coll_id=None, format='.json'): # single cast if cast_id: if coll_id: check_collection(cast_id, coll_id) cast = get_object(models.Cast, id=cast_id) if not cast.allowed_access(request.user): raise exceptions.APIForbidden if format == '.json': cast_dict = api_serialize(cast, request) return APIResponseOK(content=cast_dict, total=1) if format == '.html': is_flagged = cast.is_flagged_by(request.user) is_favorited = cast.is_favorited_by(request.user) allowed_edit = cast.allowed_edit(request.user) content = render_to_string('ajax/cast_frontpage.django.html', locals(), context_instance = RequestContext(request)) resp = HttpResponse(content=content) return resp else: raise exceptions.APIBadRequest('Invalid response format') # multiple casts else: base_query = models.Cast.get_privacy_q(request) if coll_id: get_object(models.Collection, id=coll_id) base_query = base_query & Q(collection=coll_id) q = qstranslate.QueryTranslator(models.Cast, ruleset, base_query) query = request.GET.copy() # Need to do some magic to order by popularity, so remove from # the query that will be sent to qstranslate popularity_order = False if 'orderby' in query and query['orderby'] == 'popularity': popularity_order = True del query['orderby'] objs = total = pg = None try: objs = q.filter(query) # popularity magic! if popularity_order: objs = objs.annotate(popularity=Count('favorited_by')).order_by('-popularity') objs, total, pg = paginate(objs, request.GET) except qstranslate.InvalidParameterException, e: raise exceptions.APIBadRequest(e.message) cast_arr = [] for c in objs: cast_arr.append(api_serialize(c, request)) return APIResponseOK(content=cast_arr, total=total, pg=pg)
def get_geofeatures(request): bounds_param = get_param(request.GET, 'within') base_query = Q() if bounds_param: pnts = bounds_param.split(',') bbox = (float(pnts[0]), float(pnts[1]), float(pnts[2]), float(pnts[3])) poly = Polygon.from_bbox(bbox) poly.set_srid(4326) del query['within'] base_query = base_query & Q(location__within=poly) # PUBLIC cast within bounds cast_base_query = Q(privacy__lt=3) & base_query q = qstranslate.QueryTranslator(Cast, cast_ruleset, cast_base_query) public_casts = q.filter(request.GET).order_by('-created') cache_key = request_cache_key(request, ignore_params=['_']) cast_arr = get_cache(cache_key, cache_group=CAST_GEO_GROUP) if not cast_arr: cast_arr = [] for c in public_casts: if c.location: cast_arr.append(geojson_serialize(c, c.location, request)) set_cache(cache_key, cast_arr, cache_group=CAST_GEO_GROUP) # casts that ONLY THIS user can see. Superusers can see all casts, logged in users can see any casts they authored. if request.user.is_authenticated(): if request.user.is_superuser: cast_base_query = Q(privacy=3) & base_query else: cast_base_query = Q(privacy=3) & Q( author=request.user) & base_query q = qstranslate.QueryTranslator(Cast, cast_ruleset, cast_base_query) user_casts = q.filter(request.GET).order_by('-created') for c in user_casts: if c.location: cast_arr.append(geojson_serialize(c, c.location, request)) # collection intersects bounds if bounds_param: base_query = Q(path__intersects=poly) colls = Collection.objects.filter(base_query).order_by('-created') coll_arr = [] for i in colls: # only add collections if they have a path if i.path: coll_arr.append(geojson_serialize(i, i.path, request)) features_dict = {} features_dict['casts'] = dict(type='FeatureCollection', features=cast_arr) features_dict['collections'] = dict(type='FeatureCollection', features=coll_arr) return APIResponseOK(content=features_dict)