Exemplo n.º 1
0
 def test_check(self):
     self.assertTrue(is_image(self.img_jpeg, ("JpEg", "PnG", "GIF")))
     self.assertFalse(is_image(self.img_jpeg, ("PnG", "GIF")))
     uf = UploadedFile(
         file=self.img_jpeg, name="test.jpeg", content_type="empty", size=len(self.img_jpeg.getvalue())
     )
     self.assertTrue(is_image(uf, ("jpeg",), set_content_type=False))
     self.assertEqual(uf.content_type, "empty")
     is_image(uf, ("jpeg",))
     self.assertEqual(uf.content_type, "image/jpeg")
Exemplo n.º 2
0
 def test_check(self):
     self.assertTrue(is_image(self.img_jpeg, ('JpEg', 'PnG', 'GIF')))
     self.assertFalse(is_image(self.img_jpeg, ('PnG', 'GIF')))
     uf = UploadedFile(file=self.img_jpeg,
                       name='test.jpeg',
                       content_type='empty',
                       size=len(self.img_jpeg.getvalue()))
     self.assertTrue(is_image(uf, ('jpeg', ), set_content_type=False))
     self.assertEqual(uf.content_type, 'empty')
     is_image(uf, ('jpeg', ))
     self.assertEqual(uf.content_type, 'image/jpeg')
Exemplo n.º 3
0
def upload_from_fs(fn, profile=None, label=None):
    """
    Saves image from fn with TMP prefix and returns img_id.
    """
    if not os.path.isfile(fn):
        raise ValueError('File is not exists: {}'.format(fn))
    if profile is None:
        profile = 'default'
    conf = get_profile_configs(profile)
    with open(fn, 'rb') as f:
        if not is_image(f, types=conf['TYPES']):
            msg = (('Format of uploaded file "%(name)s" is not allowed. '
                    'Allowed formats is: %(formats)s.') %
                   {'name': fn, 'formats': ', '.join(map(lambda t: t.upper(), conf['TYPES']))})
            raise RuntimeError(msg)
        t = adjust_image(f, max_size=conf['MAX_SIZE'], new_format=conf['FORMAT'],
                         jpeg_quality=conf['JPEG_QUALITY'], fill=conf['FILL'],
                         stretch=conf['STRETCH'], return_new_image=True)
        img_id = generate_img_id(profile, ext=image_get_format(f), label=label, tmp=True)
        relative_path = get_relative_path_from_img_id(img_id)
        full_path = media_path(relative_path)
        save_file(t, full_path)
        for v_conf in conf['VARIANTS']:
            v_label = v_conf['LABEL']
            if not v_label:
                v_label = get_variant_label(v_conf)
            v_t = adjust_image(t, max_size=v_conf['MAX_SIZE'], new_format=v_conf['FORMAT'],
                               jpeg_quality=v_conf['JPEG_QUALITY'], fill=v_conf['FILL'],
                               stretch=v_conf['STRETCH'], return_new_image=True)
            v_relative_path = get_relative_path_from_img_id(img_id, variant_label=v_label,
                                                            ext=image_get_format(v_t))
            v_full_path = media_path(v_relative_path)
            save_file(v_t, v_full_path)
        return img_id
Exemplo n.º 4
0
def upload_from_fileobject(f, profile=None, label=None):
    """
    Saves image from f with TMP prefix and returns img_id.
    """
    if profile is None:
        profile = 'default'
    conf = get_profile_configs(profile)
    f.seek(0)
    if not is_image(f, types=conf['TYPES']):
        msg = (
            ('Format of uploaded file is not allowed. '
             'Allowed formats is: %(formats)s.') % {
                 'formats': ', '.join(map(lambda t: t.upper(), conf['TYPES']))
             })
        raise RuntimeError(msg)
    return _custom_upload(f, profile, label, conf)
Exemplo n.º 5
0
def upload_from_fs(fn, profile=None, label=None):
    """
    Saves image from fn with TMP prefix and returns img_id.
    """
    if not os.path.isfile(fn):
        raise ValueError('File is not exists: {}'.format(fn))
    if profile is None:
        profile = 'default'
    conf = get_profile_configs(profile)
    with open(fn, 'rb') as f:
        if not is_image(f, types=conf['TYPES']):
            msg = (
                ('Format of uploaded file "%(name)s" is not allowed. '
                 'Allowed formats is: %(formats)s.') % {
                     'name': fn,
                     'formats': ', '.join(
                         map(lambda t: t.upper(), conf['TYPES']))
                 })
            raise RuntimeError(msg)
        return _custom_upload(f, profile, label, conf)
Exemplo n.º 6
0
 if request.method != 'POST':
     return HttpResponseNotAllowed(('POST',))
 result = {'uploaded': [], 'errors': []}
 files = request.FILES.getlist('images[]')
 if not files:
     result['errors'].append(unicode(ERROR_MESSAGES['no_uploaded_files']))
     return send_json(result)
 try:
     profile = request.POST.get('profile', 'default')
     conf = get_profile_configs(profile)
 except ValueError, e:
     result['errors'].append(unicode(e))
     return send_json(result)
 for i in xrange(min(len(files), dju_settings.DJU_IMG_UPLOAD_MAX_FILES)):
     f = files[i]
     if not is_image(f, types=conf['TYPES']):
         result['errors'].append(
             unicode(ERROR_MESSAGES['wrong_file_format']) %
             {'name': f.name, 'formats': ', '.join(map(lambda t: t.upper(), conf['TYPES']))}
         )
         continue
     adjust_image(f, max_size=conf['MAX_SIZE'], new_format=conf['FORMAT'],
                  jpeg_quality=conf['JPEG_QUALITY'], fill=conf['FILL'], stretch=conf['STRETCH'])
     img_id = generate_img_id(profile, ext=image_get_format(f),
                              label=request.POST.get('label'), tmp=True)
     relative_path = get_relative_path_from_img_id(img_id)
     full_path = media_path(relative_path)
     save_file(f, full_path)
     data = {
         'url': settings.MEDIA_URL + relative_path,
         'rel_url': relative_path,
Exemplo n.º 7
0
 if request.method != "POST":
     return HttpResponseNotAllowed(("POST",))
 result = {"uploaded": [], "errors": []}
 files = request.FILES.getlist("images[]")
 if not files:
     result["errors"].append(unicode(ERROR_MESSAGES["no_uploaded_files"]))
     return send_json(result)
 try:
     profile = request.POST.get("profile", "default")
     conf = get_profile_configs(profile)
 except ValueError, e:
     result["errors"].append(unicode(e))
     return send_json(result)
 for i in xrange(min(len(files), dju_settings.DJU_IMG_UPLOAD_MAX_FILES)):
     f = files[i]
     if not is_image(f, types=conf["TYPES"]):
         result["errors"].append(
             unicode(ERROR_MESSAGES["wrong_file_format"])
             % {"name": f.name, "formats": ", ".join(map(lambda t: t.upper(), conf["TYPES"]))}
         )
         continue
     adjust_image(
         f,
         max_size=conf["MAX_SIZE"],
         new_format=conf["FORMAT"],
         jpeg_quality=conf["JPEG_QUALITY"],
         fill=conf["FILL"],
         stretch=conf["STRETCH"],
     )
     img_id = generate_img_id(profile, ext=image_get_format(f), label=request.POST.get("label"), tmp=True)
     relative_path = get_relative_path_from_img_id(img_id)