def handle_uploaded_photo(f): if f.name == '': raise NotImplemented photo = Photo(author="simon", description="what so ever") photo.file.new_file() for chunk in f.chunks(): photo.file.write(chunk) #destination.write(chunk) #destination.close() photo.file.close() photo.file.content_type="image/jpeg" photo.save()
def store_photo_to_db(file, author, description, content_type): ''' Store uploaded photo into GridFS (mongoDB) ''' if file.name == '': raise ValueError r = Restraunt.objects().first() photo = Photo(author=author, description=description) photo.file.new_file() for chunk in file.chunks(): photo.file.write(chunk) photo.file.content_type=content_type photo.file.close() photo.save() r.photos.append(photo) r.save() return photo
def serve_photo_by_id(request, photo_id): if request.method == 'GET': photo = Photo.objects(id=ObjectId(photo_id)).first() response = HttpResponse(photo.file.read()) response['Content-Type'] = photo.file.content_type return response