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 post_comments_flag(request, cast_id, comment_id): cast = get_object(models.Cast, id=cast_id) comment = comment_api.check_comment(cast, comment_id) comment.flag(request.user) return APIResponseOK(content='success')
def post_media_content(request, cast_id, media_id): get_object(models.Cast, cast_id) media = get_object(models.Media, media_id) if not media.author == request.user: raise exceptions.APIForbidden content_type = get_param(request.META, 'CONTENT_TYPE') mime_type = content_type.split(';')[0] # Form upload if mime_type == 'multipart/form-data': # multipart/form-data; boundary=----pluploadboundaryp15s131vjl18tdnlk1sj11min19p42 file = get_param(request.FILES, 'file') if not file: #TODO: perhaps if there is no file, and there is an error, delete the original? raise exceptions.APIBadRequest('Error uploading file!') media.content.file.save(file.name, file, save=True) else: media.content.create_file_from_data(request.body, mime_type) # media is the generic holder, media.content is the specific # content model (ImageMedia, VideoMedia etc.). media.content.content_state = models.Media.STATE_COMPLETE media.content.save() return APIResponseOK(content=api_serialize(media, request))
def get(request, fmt='.json', index=None): # adds a description field automatically based on the verbose name of a model for url in index: if 'description' not in url: if 'model' in url: url['description'] = str( url['model']._meta.verbose_name_plural.title()) else: url['description'] = url['view'] urls = map( lambda url: { 'description': url['description'], 'uri': reverse(url['view']) }, index) site = get_current_site(request) resp_index = { 'openlocast_version': __version__, 'site_name': site.name, 'resources': urls } if '.json' == fmt: return APIResponseOK(content=resp_index)
def get_media_content(request, cast_id, media_id): media = get_object(models.Media, media_id) cast = check_cast_media(media_id, cast_id) if not cast.allowed_access(request.user): raise exceptions.APIForbidden media_dict = api_serialize(media, request) return APIResponseOK(content=media_dict, total=1)
def post_favorite(request, coll_id): coll = get_object(Collection, id=coll_id) favorite = get_param(request.POST, 'favorite') if favorite: coll.favorite(request.user) else: coll.unfavorite(request.user) return APIResponseOK(content='success')
def delete_media_content(request, cast_id, media_id): media = get_object(models.Media, media_id) cast = check_cast_media(media_id, cast_id) if not cast.allowed_edit(request.user): raise exceptions.APIForbidden media.delete() return APIResponseOK(content='success')
def get_geofeature(request, cast_id): cast = get_object(models.Cast, id=cast_id) if not cast.allowed_access(request.user): raise exceptions.APIForbidden if cast.location: geojson = geojson_serialize(cast, cast.location, request) return APIResponseOK(content=geojson) else: raise exceptions.APINotFound('No location')
def get_media(request, cast_id): cast = get_object(models.Cast, cast_id) if not cast.allowed_access(request.user): raise exceptions.APIForbidden media_dicts = [] for m in cast.media_set.all(): media_dicts.append(api_serialize(m, request)) return APIResponseOK(content=media_dicts)
def delete(request, cast_id, coll_id=None): if coll_id: check_collection(cast_id, coll_id) cast = get_object(models.Cast, cast_id) if not cast.allowed_edit(request.user): raise exceptions.APIForbidden cast.delete() return APIResponseOK(content='success')
def put(request, cast_id, coll_id=None): if coll_id: check_collection(cast_id, coll_id) cast = get_object(models.Cast, cast_id) if not cast.allowed_edit(request.user): raise exceptions.APIForbidden cast = cast_from_post(request, cast) cast.save() return APIResponseOK(content=api_serialize(cast, request))
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)
def post_flag(request, cast_id): cast = get_object(models.Cast, id=cast_id) cast.flag(request.user) return APIResponseOK(content='success')
def get_me(request): return APIResponseOK(content=api_serialize(request.user))