def get(request, postcard_id=None, coll_id=None, format='.json'): # single postcard if postcard_id: postcard = get_object(models.Postcard, id=postcard_id) if not postcard.allowed_access(request.user): raise exceptions.APIForbidden postcard_dict = api_serialize(postcard, request) return APIResponseOK(content=postcard_dict, total=1) # multiple postcards else: base_query = models.Postcard.get_privacy_q(request) & \ Q(content_state=4) q = qstranslate.QueryTranslator(models.Postcard, ruleset, base_query) 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) postcard_arr = [] for p in objs: postcard_arr.append(api_serialize(p, request)) return APIResponseOK(content=postcard_arr, total=total, pg=pg)
def get(request, user_id=None): # Single user if user_id: u = get_object(LocastUser, id=user_id) content = api_serialize(u) return APIResponseOK(content=content, total=1) # Multiple users else: q = QueryTranslator(LocastUser, ruleset) query = request.GET.copy() objs = total = pg = None try: objs = q.filter(query) objs, total, pg = paginate(objs, request.GET) except InvalidParameterException, e: raise 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(request, coll_id=None): if coll_id: collection = get_object(Collection, id=coll_id) collection_dict = api_serialize(collection, request) return APIResponseOK(content=collection_dict, total=1) else: query = request.GET.copy() q = QueryTranslator(Collection, ruleset) try: objs = q.filter(query) except InvalidParameterException, e: raise APIBadRequest(e) objs, total, pg = paginate(objs, query) collection_arr = [] for i in objs: collection_arr.append(api_serialize(i, request)) return APIResponseOK(content=collection_arr, total=total, pg=pg)
def get(request, coll_id = None): if coll_id: collection = get_object(Collection, id=coll_id) collection_dict = api_serialize(collection, request) return APIResponseOK(content=collection_dict, total=1) else: query = request.GET.copy() q = QueryTranslator(Collection, ruleset) try: objs = q.filter(query) except InvalidParameterException, e: raise APIBadRequest(e) objs, total, pg = paginate(objs, query) collection_arr = [] for i in objs: collection_arr.append(api_serialize(i, request)) return APIResponseOK(content=collection_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)