Exemplo n.º 1
0
  def save(self, commit=True):
    instance = forms.ModelForm.save(self, False)

    dir_path = self.cleaned_data.get('directory_path')
    dir_category = self.cleaned_data.get('directory_category')
    dir_date_taken = self.cleaned_data.get('date_taken')
    dir_description = self.cleaned_data.get('description')

    dir_category_ids = list(dir_category.values_list('id',flat=True))
    dir_file_list = os.listdir(dir_path)

    if not dir_date_taken:
      dir_name = os.path.basename(dir_path)
      re_pattern = '(\d{4})[-](\d{2})[-](\d{2})'
      search_in_dirname = re.search(re_pattern, dir_name)
      if search_in_dirname:
        dir_date_taken = search_in_dirname.group()

    images_added = []
    for f in dir_file_list:
      filename, file_extension = os.path.splitext(f)

      if file_extension=='.jpg':

        image_form_data = {
          'name': filename,
          'description': dir_description,
          'date_taken': dir_date_taken,
          'image_category': dir_category_ids,
        }
        image_data =  ImageFile(open(os.path.join(dir_path,f),'rb'))
        image_form_file_data = {'image': SimpleUploadedFile(filename, image_data.read())}
        image_form = ImageForm(data=image_form_data,files=image_form_file_data)

        if image_form.is_valid():
          new_dir_image = image_form.save()
          images_added.append(new_dir_image.id)

    instance.directory_related_images = images_added
    if commit:
      instance.save()

    return instance
def save_candidate_image_locally(c, url):
    current_site = Site.objects.get_current()
    img_temp = NamedTemporaryFile(delete=True)
    try:
        downloaded_image = urllib2.urlopen(url)
    except:
        print c.name, c.election
        # c.image = None
        # c.save()
        return
    d = downloaded_image.read()
    img_temp.write(d)
    img_temp.flush()
    i = ImageFile(img_temp.file)
    storage = get_storage_class()()
    data = i.read()
    extension = guess_extension(downloaded_image.info().type)
    file_name = u'candidatos/' + c.id + u'-' + c.election.slug + extension
    path = default_storage.save(file_name, i)
    
    url = u'http://' + current_site.domain + '/cache/' + file_name
    c.image = url
    c.save()
Exemplo n.º 3
0
class Coffee(models.Model):
	name = models.CharField(max_length=128)
	description = models.CharField(max_length=1024)
	one_pound_price = models.DecimalField(max_digits=5,decimal_places=2)
	two_pound_price = models.DecimalField(max_digits=5,decimal_places=2)
	five_pound_price = models.DecimalField(max_digits=5,decimal_places=2)
	image = models.ImageField(upload_to='images')
	active = models.BooleanField(default=True)

	class BadSizeException(Exception):
		pass

	def __unicode__(self):
		return self.name
	def __str__(self):
		return self.__unicode__()

	def value(self, pounds, quantity):
		if pounds == 1:
			return quantity * self.one_pound_price
		elif pounds == 2:
			return quantity * self.two_pound_price
		elif pounds == 5:
			return quantity * self.five_pound_price
		else:
			raise Coffee.BadSizeException

	def set_image(self, image_data):
		iof = BytesIO(image_data)
		self.image = ImageFile(iof)
		self.image.name = os.path.join('images', '%s.png' % self.name)

	def image_data_url(self):
		self.image.open()
		b64 = b64encode(self.image.read()).decode('utf-8')
		self.image.close()
		return 'data:image/png;base64,%s' % b64
Exemplo n.º 4
0
def mce_upload(request):
    if request.method == 'POST':
        try:
            print 'image'
            file = request.FILES['image']
            print 'convert'
            image = ImageFile(file)
            print 'path'
            path = default_storage.save('article_images/%s' % image.name, ContentFile(image.read()))
            print 'data'
            data = {
                'message': 'success',
                'path': os.path.join(settings.MEDIA_URL, path),
                'width': image.width,
                'height': image.height
            }
        except IOError:
            data = {
                'message': 'error'
            }
    else:
        data = {
            'message': 'fail'
        }
    return createJSONResponse(data)