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 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 post_favorite(request, object): favorite = get_param(request.POST, 'favorite') if not favorite: raise APIBadRequest('Incorrect data posted. Should be favorite=true or favorite=false.') favorite = (favorite in ['true','True']) if favorite: object.favorite(request.user) else: object.unfavorite(request.user) return APIResponseOK(content={'is_favorite':object.is_favorited_by(request.user)})
def post_photo(request, postcard_id, photo_id = None): postcard = get_object(models.Postcard, id = postcard_id) if not postcard.allowed_edit(request.user): raise exceptions.APIForbidden # If there is a photo_id, posting raw photo data to a photo if photo_id: photo = get_object(models.Photo, id = photo_id) check_postcard_photo(postcard_id, photo_id) if not photo.is_author(request.user): raise exceptions.APIForbidden content_type = get_param(request.META, 'CONTENT_TYPE') mime_type = content_type.split(';')[0] if not mime_type: raise exceptions.APIBadRequest('Invalid file type!') try: photo.create_file_from_data(request.raw_post_data, mime_type) except LocastContent.InvalidMimeType: raise exceptions.APIBadRequest('Invalid file type!') try: import Image i = Image.open(photo.file.path) i.load() except IOError: raise exceptions.APIBadRequest('Corrupted image!') return APIResponseOK(content=api_serialize(photo.contentmodel)) # If there is not, posting a new photo object else: photo = photo_from_post(request, postcard_id) photo.save() return APIResponseCreated(content=api_serialize(photo.contentmodel, request), location=photo.get_api_uri())
def get_geofeatures(request): bounds_param = get_param(request.GET, 'within') query = request.GET.copy() base_query = Q() if bounds_param: del query['within'] base_query = base_query & get_polygon_bounds_query(bounds_param, 'location') # postcard within bounds base_query = models.Postcard.get_privacy_q(request) & base_query q = qstranslate.QueryTranslator(models.Postcard, ruleset, base_query) postcards = q.filter(query) postcard_arr = [] for p in postcards: if p.location: postcard_arr.append(geojson_serialize(p, p.location, request)) return APIResponseOK(content=dict(type='FeatureCollection', features=postcard_arr))