def image_pre_save(sender, instance, **kwargs): basetitle = instance.original_filename try: p = Photo.objects.get(title=basetitle) except Photo.DoesNotExist: p = Photo(title=basetitle) p.image = instance.file.file p.save() fld = instance.folder if fld is not None and fgs.AUTOCREATE and is_gallery_folder(fld.pretty_logical_path)[0]: try: gal = get_cached_object(Gallery, slug=slug_from_folder(fld)) if GalleryItem.objects.filter(gallery=gal, photo=p).count() == 0: GalleryItem.objects.create( gallery=gal, photo=p, order=fld.file_count ) except Gallery.DoesNotExist: pass
def setUp(self): super(TestPhoto, self).setUp() # fixtures create_photo_formats(self) # "fixtures" aka example photo # prepare image in temporary directory self.image_file_name = mkstemp(suffix=".jpg", prefix="ella-photo-tests-")[1] self.image = Image.new('RGB', (200, 100), "black") self.image.save(self.image_file_name, format="jpeg") f = open(self.image_file_name) file = ContentFile(f.read()) f.close() self.photo = Photo( title = u"Example 中文 photo", slug = u"example-photo", height = 200, width = 100, ) self.photo.image.save("bazaaah", file) self.photo.save() self.thumbnail_path = self.photo.get_thumbnail_path()
def handle(self, *args, **options): self.process_options(options) self.print_debug("Options: ") self.print_debug(options) subdir = re.sub( '[^('+re.escape(os.sep)+')]*%[^%].*', '', photos_settings.UPLOAD_TO ).strip(os.sep) from ella.photos.models import Photo storage = Photo().image.storage extensions = self.extensions or photos_settings.TYPE_EXTENSION.values() self.print_info('Accepted extensions: ' +str(extensions)) photo_extension_re = re.compile( '(%s)$' % ('|'.join([re.escape(ex) for ex in extensions])), self.extensions_ic and re.IGNORECASE or 0) # breadth-first search files = [] nodes = [subdir] while nodes: current = nodes.pop() self.print_debug("Entering directory '%s'" % current) current_dirs, current_files = storage.listdir(current) if not (current_dirs or current_files): self.print_info("Directory '%s' is empty" % current) else: nodes += [ '%s%s%s' % (current, os.sep, directory) for directory in current_dirs] for current_file in current_files: f = '%s%s%s' % (current, os.sep, current_file) is_image = bool(photo_extension_re.search(current_file)) if not is_image: self.print_info("File '%s' is not image" % f) if is_image or self.all: files.append(f) self.print_debug("Appending file '%s'" % f) self.print_debug("Leaving directory '%s'" % current) photo_files_set = set(files) db_files_set = set([photo.image.url for photo in Photo.objects.all()]) self.print_summarization(photo_files_set, db_files_set) if self.delete: self.delete_files(storage, photo_files_set -db_files_set)
def generate_photo(self, instance, time): # TODO: handle fails dir_name = Photo._meta.get_field_by_name( 'image')[0].get_directory_name() file_name = path.join(dir_name, 'screenshot-' + instance.file.token) try: makedirs(path.join(settings.MEDIA_ROOT, dir_name)) except OSError: # Directory already exists pass instance.file.create_thumb(path.join(settings.MEDIA_ROOT, file_name), time=time) photo = Photo() photo.title = "%s screenshot" % instance.title photo.slug = slugify(photo.title) photo.image = file_name size = get_img_size(path.join(settings.MEDIA_ROOT, file_name)) photo.width = size['width'] photo.height = size['height'] photo.save() instance.photo = photo
def _set_photo(self): from tempfile import mkstemp from django.core.files.base import ContentFile image_file_name = mkstemp(suffix=".jpg", prefix="ella-feeds-tests-")[1] image = Image.new('RGB', (200, 100), "black") image.save(image_file_name, format="jpeg") f = open(image_file_name) file = ContentFile(f.read()) f.close() photo = Photo( title=u"Example 中文 photo", slug=u"example-photo", height=200, width=100, ) photo.image.save("bazaaah", file) photo.save() self.publishable.photo = photo self.publishable.save()