def handle(self, *args, **options): json_url = options['json_url'][0] response = requests.get(json_url) place_info = response.json() place = Place.objects.get_or_create( title=place_info['title'], defaults={ 'short_description': place_info['description_short'], 'long_description': place_info['description_long'], 'lat': place_info['coordinates']['lat'], 'lon': place_info['coordinates']['lng'], })[0] if os.path.exists(os.path.join(BASE_DIR, 'media')): for image in place.images.all(): os.remove(os.path.join(BASE_DIR, image.image.path)) place.images.all().delete() for image_number, image_url in enumerate(place_info['imgs']): filename = image_url.split('/')[-1] response = requests.get(image_url) content = ContentFile(response.content) image_object = Image() image_object.image.save(filename, content, save=True) image_object.place = place image_object.number = image_number + 1 image_object.save()
def handle(self, *args, **options): for url in options['urls']: response = requests.get(url) response.raise_for_status() payload = response.json() place, created = Place.objects.get_or_create( title=payload['title'], defaults={ 'description_short': payload['description_short'], 'description_long': payload['description_long'], 'lat': payload['coordinates']['lat'], 'lon': payload['coordinates']['lng'] }) for index, img_url in enumerate(payload['imgs']): img_response = requests.get(img_url) img_response.raise_for_status() content_file = ContentFile(img_response.content) new_image = Image(place=place, position=index) new_image.img.save(parse_img_name(img_url), content_file, save=False) new_image.save() self.stdout.write( self.style.SUCCESS(f'Successfully loaded url "{url}"'))
def handle(self, *args, **options): for link in options['link']: r = requests.get(link) data = r.json() new_place = Place.objects.create( title=data['title'], description_short='description_short', description_long=data['description_long'], lng=data['coordinates']['lng'], lat=data['coordinates']['lat']) for img_url in data['imgs']: name = urlparse(img_url).path.split('/')[-1] img_content = ContentFile(requests.get(img_url).content) new_img = Image(place=new_place) new_img.img.save(name, img_content, save=True) new_img.save()
def setup_db(self): first_image = Image() first_image.image = 'test1.jpg' first_image.title = 'image 1' first_image.description = 'caption 1' first_image.alt = 'alt 1' first_image.attribution = 'attribution 1' first_image.license = 'license 1' first_image.save() first_place = Place.objects.create(title="place 1", description="description 1", location=Point(-34.0001, -108.1234)) first_place.images.add(first_image) Place.objects.create(title="place 2", description="description 2")
def handle(self, *args, **options): for link in options['link']: r = requests.get(link) r.raise_for_status() json_file = r.json() new_place, created = Place.objects.get_or_create( title=json_file['title'], short_description=json_file['description_short'], long_description=json_file['description_long'], lng=json_file['coordinates']['lng'], lat=json_file['coordinates']['lat']) for img_url in json_file['imgs']: name = urlparse(img_url).path.split('/')[-1] requests.get(img_url).raise_for_status() img_content = ContentFile(requests.get(img_url).content) new_img = Image(place=new_place) new_img.img.save(name, img_content, save=True) new_img.save()
def handle(self, *args, **options): url = options.get("url") response = requests.get(url, allow_redirects=False) response.raise_for_status() place_data = response.json() place, created = Place.objects.get_or_create( title=place_data["title"], defaults={ 'description_short': place_data["description_short"], 'description_long': place_data["description_long"], 'lng': place_data["coordinates"]["lng"], 'lat': place_data["coordinates"]["lat"] }) for img_number, img in enumerate(place_data["imgs"], start=1): response = requests.get(img, allow_redirects=False) response.raise_for_status() content = ContentFile(response.content) filename = img.split('/')[-1] new_image = Image(place=place, position=img_number) new_image.picture.save(filename, content, save=True) new_image.save()
def handle(self, *args, **options): url = options['url'][0] response = requests.get(url) decoded_response = response.json() new_place, status = Place.objects.get_or_create( title=decoded_response['title'], description_short=decoded_response['description_short'], description_long=decoded_response['description_long'], lon=decoded_response['coordinates']['lng'], lat=decoded_response['coordinates']['lat'], ) for num, image in enumerate(decoded_response['imgs'], start=1): new_image = Image(place=new_place, position=num) new_image.save() image_name = image.split('/')[-1] response = requests.get(image, verify=False) response.raise_for_status() i = BytesIO(response.content) new_image.image.save(image_name, i, save=True)
def append_image_to_place(image_name, image_file, place): image_entry = Image(place=place) image_entry.save() image_entry.file.save(image_name, image_file, save=True)