def handle(self, *args, **options): self.stdout.write(self.style.SUCCESS("Importing images")) if not options["from_dir"].endswith("/"): options["from_dir"] = options["from_dir"] + "/" for filepath in sorted(glob(options["from_dir"] + "*-90x90.png")): with open(filepath, "rb") as image_file: name = filepath.split("/")[-1][:-10] image = Image(title=name) image.file = ImageFile(file=image_file, name=name + ".png") image.file_size = image.file.size image.file.seek(0) image._set_file_hash(image.file.read()) image.file.seek(0) # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) image.save() image.tags.add("illustration") self.stdout.write( self.style.SUCCESS(f"{image.pk},{image.title}")) self.stdout.write(self.style.SUCCESS("Importing images finished"))
def create_image(self, image_file, title, filename, tag): image = Image(title=title) image.file = ImageFile(file=image_file, name=filename) image.file_size = image.file.size image.file.seek(0) image._set_file_hash(image.file.read()) image.file.seek(0) # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) image.save() image.tags.add(tag) return image
def handle(self, *args, **options): self.stdout.write(self.style.SUCCESS("Importing images")) with open(options["speakers_csv"], "r") as file: for row in DictReader(file): if not row["filename"]: self.stdout.write( self.style.WARNING( f"====> skipping {row['post_name']}, {row['post_title']}" )) continue image_path = os.path.join(options["from_dir"], row["filename"]) with open(image_path, "rb") as image_file: image = Image(title=row["post_title"]) if row["filename"].lower().endswith(".jpg") or row[ "filename"].lower().endswith(".jpeg"): image_filename = f"{row['first_name']} {row['last_name']}.jpg" elif row["filename"].lower().endswith(".png"): image_filename = f"{row['first_name']} {row['last_name']}.png" else: raise ValueError("Unknown file format") image.file = ImageFile(file=image_file, name=image_filename) image.file_size = image.file.size image.file.seek(0) image._set_file_hash(image.file.read()) image.file.seek(0) # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) image.save() image.tags.add("speaker") speaker = Speaker.objects.get(wordpress_id=row["ID"]) speaker.photo = image speaker.save() self.stdout.write( self.style.SUCCESS(f"{image.pk},{image.title}")) self.stdout.write(self.style.SUCCESS("Importing images finished"))