def setUp(self): # make temp folder because we want to delete files after test # and this code do that automatically settings.MEDIA_ROOT = tempfile.mkdtemp() self.user = User.objects.create_superuser('test_user', '', 'testing') self.tags = [ Tag.objects.create(value='django'), Tag.objects.create(value='python'), Tag.objects.create(value='test'), Tag.objects.create(value='app'), ] with open('./handypackages/test_requirements/test_upload_image.jpg', 'rb') as image: test_image = SimpleUploadedFile('test_upload_image.jpg', image.read(), content_type='image/jpeg') image = Image(owner=self.user, file=test_image, original_filename='product_image_file', name='test_product_image') image.save() product = Product(title='product test title', text='product test', slug='product-test-title', image=image, price=250000) product.save() product.tags.add(*self.tags) self.product = product
def copy_url_to_filer(instance, url_field_name, filer_field_nane): url = getattr(instance, url_field_name) resp = requests.get(url) base_filename = url.rsplit('/', 1)[1] base_path = '__'.join(url.rsplit('/', 3)[1:]) user = User.objects.get(username='******') folder, _ = Folder.objects.get_or_create(name='cloudinary_to_s3', owner=user) prefix = settings.FILER_STORAGES['public']['main']['UPLOAD_TO_PREFIX'] filename = '{0}/c2s/{1}'.format(prefix, base_path) image = Image.objects.filter(file=filename).first() if image is None: cf = ContentFile(resp.content) image = Image( owner=user, original_filename = base_filename, folder=folder) image.file = image.file.storages['public'].save(filename, cf) image.save() setattr(instance, filer_field_nane, image) instance.save() image.icons
def copy_url_to_filer(instance, url_field_name, filer_field_nane): url = getattr(instance, url_field_name) resp = requests.get(url) base_filename = url.rsplit('/', 1)[1] base_path = '__'.join(url.rsplit('/', 3)[1:]) user = User.objects.get(username='******') folder, _ = Folder.objects.get_or_create(name='cloudinary_to_s3', owner=user) prefix = settings.FILER_STORAGES['public']['main']['UPLOAD_TO_PREFIX'] filename = '{0}/c2s/{1}'.format(prefix, base_path) image = Image.objects.filter(file=filename).first() if image is None: cf = ContentFile(resp.content) image = Image(owner=user, original_filename=base_filename, folder=folder) image.file = image.file.storages['public'].save(filename, cf) image.save() setattr(instance, filer_field_nane, image) instance.save() image.icons
def setUp(self): # make temp folder because we want to delete files after test # and this code do that automatically settings.MEDIA_ROOT = tempfile.mkdtemp() self.user = User.objects.create_superuser("test_user", "", "testing") self.tags = [ Tag.objects.create(value="django"), Tag.objects.create(value="python"), Tag.objects.create(value="test"), Tag.objects.create(value="app"), ] with open("./handypackages/test_requirements/test_upload_image.jpg", 'rb') as image: test_image = SimpleUploadedFile("test_upload_image.jpg", image.read(), content_type="image/jpeg") image = Image(owner=self.user, file=test_image, original_filename="blog_image_file", name='test_blog_image') image.save() blog = Blog(title='blog test title', text='blog test', slug='blog-test-title', image=image, publish_time=timezone.now()) blog.save() blog.tags.add(*self.tags) self.blog = blog
def extract_images(self, post): """ Finds direct image links. Creates filer Image objects and extracts links """ soup = BeautifulSoup(post) links = soup.findAll("img") internal_uploads_dir = '{}/wp-content/uploads'.format(self.base_url) images = [] for link in links: try: href = link['src'] except KeyError: # Link has no href continue if internal_uploads_dir in href: if not Image.matches_file_type(href, None, None): # File is not an image continue image = self.download_and_save(href) images.append(image) # Remove link from content, replace with placeholder link.replaceWith(self.image_placeholder) # Re-write all internal links - GH: #3 if self.site.domain in href: uri = href.split(self.site.domain)[1] link['href'] = uri return str(soup), images
def save(self, commit=True): cleaned_data = self.cleaned_data instance = super().save(commit=False) instance.set_password(self.cleaned_data.get('password')) instance.save() client = Customer(user=instance) address = cleaned_data.get('address') if address: client.address = address avatar = cleaned_data.get('avatar') if avatar: file = Image(file=avatar, owner=instance, name=avatar.name, original_filename=avatar.name) file.save() instance.image = file client.save() instance.save() return instance
def save(self, *args, **kwargs): if self.cleaned_data['image']: img = Image() img.file = self.cleaned_data['image'] img.name = img.file.name img.save() self.instance.image = img return super(WithImage, self).save(*args, **kwargs)