def pre_save(self, model_instance, add): value = super(CloudinaryField, self).pre_save(model_instance, add) if isinstance(value, UploadedFile): options = {"type": self.type, "resource_type": self.resource_type} options.update(self.upload_options_with_filename(model_instance, value.name)) instance_value = uploader.upload_resource(value, **options) setattr(model_instance, self.attname, instance_value) return self.get_prep_value(instance_value) else: return value
def pre_save(self, model_instance, add): value = super(CloudinaryField, self).pre_save(model_instance, add) if isinstance(value, UploadedFile): options = {"type": self.type, "resource_type": self.resource_type} options.update(self.options) instance_value = uploader.upload_resource(value, **options) setattr(model_instance, self.attname, instance_value) if self.width_field: setattr(model_instance, self.width_field, instance_value.metadata.get('width')) if self.height_field: setattr(model_instance, self.height_field, instance_value.metadata.get('height')) return self.get_prep_value(instance_value) else: return value
def init_class(cls, storage): with open(cls.upload_file, 'rb') as fp: storage.resource = uploader.upload_resource( fp, type=cls.type, resource_type=cls.resource_type, folder='data/now', use_filename=True, unique_filename=True, overwrite=True) storage.file = CloudinaryFieldFile(storage.resource, checksum=cls.resource_checksum) yield uploader.destroy(storage.resource.public_id, type=cls.type, resource_type=cls.resource_type, invalidate=True)
def scrapImage(name, string="brand logo"): fName = name.replace(" ", "").replace("&", "").replace("?", "").replace( "-", "").replace(".", "").replace("\n", "").replace(";", "") name = name.split() for i in string.split(): name.append(i) link = "https://www.google.com/search?q={}&tbm=isch".format('+'.join(name)) r = requests.get(link) soup = BeautifulSoup(r.content, 'html.parser') try: mainSoup = soup.find_all("table", {"class": "GpQGbf"})[0] images = mainSoup.find_all("img") link = images[0].get('src') except Exception as E: print(E) print(fName) return None img = requests.get(link) image = img.content image = uploader.upload_resource(image, folder="Vocal4Local/{}".format(fName)) return image.metadata["secure_url"]