示例#1
0
 def assertUploadedFilesExist(self, response_data):
     for item in response_data['uploaded']:
         path = media_path(item['rel_url'])
         self.assertTrue(os.path.isfile(path))
         for var_data in item['variants'].values():
             var_path = media_path(var_data['rel_url'])
             self.assertTrue(os.path.isfile(var_path))
示例#2
0
 def assertUploadedFilesNotExist(self, response_data):
     for item in response_data['uploaded']:
         path = media_path(item['rel_url'])
         self.assertFalse(os.path.isfile(path))
         for var_data in item['variants'].values():
             var_path = media_path(var_data['rel_url'])
             self.assertFalse(os.path.isfile(var_path))
示例#3
0
 def test_get_files_by_img_id_removed_variants_ext(self):
     r = self.client.post(self.upload_url, {
         'images[]': [
             get_img_file(create_test_image(1000, 1000)),
             get_img_file(create_test_image(900, 900)),
             get_img_file(create_test_image(800, 800)),
             get_img_file(create_test_image(700, 700)),
         ],
         'profile': 'simple0',
         'label': 'world0',
     })
     self.assertEqual(r.status_code, 200)
     d = self.get_json(r)
     self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES)
     self.assertEqual(len(d['errors']), 0)
     self.assertUploadedFilesExist(d)
     for item in d['uploaded']:
         # for i in xrange(len(item['variants'])):
         for var_data in item['variants'].values():
             # remove ext for all variants files
             rel_url = var_data['rel_url']
             rel_url_new = os.path.splitext(rel_url)[0]
             os.rename(media_path(rel_url), media_path(rel_url_new))
             var_data['rel_url'] = rel_url_new
         r = get_files_by_img_id(item['img_id'])
         self.assertEqual(r['main'], item['rel_url'])
         self.assertEqual(len(item['variants']), len(r['variants']))
         for var_label, var_data in item['variants'].iteritems():
             self.assertEqual(r['variants'][var_label], var_data['rel_url'])
示例#4
0
 def test_get_files_by_img_id_removed_variants_ext(self):
     r = self.client.post(
         self.upload_url, {
             'images[]': [
                 get_img_file(create_test_image(1000, 1000)),
                 get_img_file(create_test_image(900, 900)),
                 get_img_file(create_test_image(800, 800)),
                 get_img_file(create_test_image(700, 700)),
             ],
             'profile':
             'simple0',
             'label':
             'world0',
         })
     self.assertEqual(r.status_code, 200)
     d = self.get_json(r)
     self.assertEqual(len(d['uploaded']),
                      dju_settings.DJU_IMG_UPLOAD_MAX_FILES)
     self.assertEqual(len(d['errors']), 0)
     self.assertUploadedFilesExist(d)
     for item in d['uploaded']:
         # for i in xrange(len(item['variants'])):
         for var_data in item['variants'].values():
             # remove ext for all variants files
             rel_url = var_data['rel_url']
             rel_url_new = os.path.splitext(rel_url)[0]
             os.rename(media_path(rel_url), media_path(rel_url_new))
             var_data['rel_url'] = rel_url_new
         r = get_files_by_img_id(item['img_id'])
         self.assertEqual(r['main'], item['rel_url'])
         self.assertEqual(len(item['variants']), len(r['variants']))
         for var_label, var_data in item['variants'].iteritems():
             self.assertEqual(r['variants'][var_label], var_data['rel_url'])
示例#5
0
 def test_get_relative_path_from_img_id_with_create_dirs(self):
     self.assertEqual(
         get_relative_path_from_img_id(
             'default:__t_abcde1234_ab12_myname.jpeg', create_dirs=True),
         'upload-img/common/34/__t_abcde1234_ab12_myname__m_93c87b2a66.jpeg'
     )
     self.assertTrue(os.path.isdir(media_path('upload-img/common/34')))
示例#6
0
 def test_get_files_by_img_id_with_invalid_hash_and_filename_pattern(self):
     r = self.client.post(self.upload_url, {
         'images[]': [
             get_img_file(create_test_image(1000, 1000)),
             get_img_file(create_test_image(900, 900)),
             get_img_file(create_test_image(800, 800)),
             get_img_file(create_test_image(700, 700)),
         ],
         'profile': 'simple0',
         'label': 'world0',
     })
     self.assertEqual(r.status_code, 200)
     d = self.get_json(r)
     self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES)
     self.assertEqual(len(d['errors']), 0)
     self.assertUploadedFilesExist(d)
     for item in d['uploaded']:
         for var_data in item['variants'].values():
             # add file with invalid hash
             rel_url = var_data['rel_url']
             rel_url_new = re.sub(
                 r'({suf})[a-z0-9]{{hs}}(_.+)'.replace(
                     '{suf}', dju_settings.DJU_IMG_UPLOAD_VARIANT_SUFFIX
                 ).replace(
                     '{hs}', str(HASH_SIZE)
                 ),
                 r'\1{h}\2'.replace('{h}', 'z' * HASH_SIZE),
                 rel_url
             )
             shutil.copy(media_path(rel_url), media_path(rel_url_new))
             # add file with invalid filename pattern
             rel_url = var_data['rel_url']
             rel_url_new = re.sub(
                 r'({suf})[a-z0-9]{{hs}}(_.+)'.replace(
                     '{suf}', dju_settings.DJU_IMG_UPLOAD_VARIANT_SUFFIX
                 ).replace(
                     '{hs}', str(HASH_SIZE)
                 ),
                 r'\1{h}\2'.replace('{h}', 'z' * (HASH_SIZE + 1)),
                 rel_url
             )
             shutil.copy(media_path(rel_url), media_path(rel_url_new))
         r = get_files_by_img_id(item['img_id'])
         self.assertEqual(r['main'], item['rel_url'])
         self.assertEqual(len(item['variants']), len(r['variants']))
         for var_label, var_data in item['variants'].iteritems():
             self.assertEqual(r['variants'][var_label], var_data['rel_url'])
示例#7
0
 def test_get_files_by_img_id_with_invalid_hash_and_filename_pattern(self):
     r = self.client.post(
         self.upload_url, {
             'images[]': [
                 get_img_file(create_test_image(1000, 1000)),
                 get_img_file(create_test_image(900, 900)),
                 get_img_file(create_test_image(800, 800)),
                 get_img_file(create_test_image(700, 700)),
             ],
             'profile':
             'simple0',
             'label':
             'world0',
         })
     self.assertEqual(r.status_code, 200)
     d = self.get_json(r)
     self.assertEqual(len(d['uploaded']),
                      dju_settings.DJU_IMG_UPLOAD_MAX_FILES)
     self.assertEqual(len(d['errors']), 0)
     self.assertUploadedFilesExist(d)
     for item in d['uploaded']:
         for var_data in item['variants'].values():
             # add file with invalid hash
             rel_url = var_data['rel_url']
             rel_url_new = re.sub(
                 r'({suf})[a-z0-9]{{hs}}(_.+)'.replace(
                     '{suf}',
                     dju_settings.DJU_IMG_UPLOAD_VARIANT_SUFFIX).replace(
                         '{hs}', str(HASH_SIZE)),
                 r'\1{h}\2'.replace('{h}', 'z' * HASH_SIZE), rel_url)
             shutil.copy(media_path(rel_url), media_path(rel_url_new))
             # add file with invalid filename pattern
             rel_url = var_data['rel_url']
             rel_url_new = re.sub(
                 r'({suf})[a-z0-9]{{hs}}(_.+)'.replace(
                     '{suf}',
                     dju_settings.DJU_IMG_UPLOAD_VARIANT_SUFFIX).replace(
                         '{hs}', str(HASH_SIZE)),
                 r'\1{h}\2'.replace('{h}', 'z' * (HASH_SIZE + 1)), rel_url)
             shutil.copy(media_path(rel_url), media_path(rel_url_new))
         r = get_files_by_img_id(item['img_id'])
         self.assertEqual(r['main'], item['rel_url'])
         self.assertEqual(len(item['variants']), len(r['variants']))
         for var_label, var_data in item['variants'].iteritems():
             self.assertEqual(r['variants'][var_label], var_data['rel_url'])
示例#8
0
 def test_save_file(self):
     f = get_img_file(create_test_image(1000, 1000))
     img_id = generate_img_id('simple1', ext=image_get_format(f), label='test-save-file')
     relative_path = get_relative_path_from_img_id(img_id)
     full_path = media_path(relative_path)
     save_file(f, full_path)
     file_exists = os.path.exists(full_path)
     self.assertTrue(file_exists)
     if file_exists:
         f.seek(0)
         h1 = hashlib.md5(f.read()).hexdigest()
         h2 = hashlib.md5(open(full_path, 'rb').read()).hexdigest()
         self.assertEqual(h1, h2)
示例#9
0
 def test_save_file(self):
     f = get_img_file(create_test_image(1000, 1000))
     img_id = generate_img_id('simple1',
                              ext=image_get_format(f),
                              label='test-save-file')
     relative_path = get_relative_path_from_img_id(img_id)
     full_path = media_path(relative_path)
     save_file(f, full_path)
     file_exists = os.path.exists(full_path)
     self.assertTrue(file_exists)
     if file_exists:
         f.seek(0)
         h1 = hashlib.md5(f.read()).hexdigest()
         h2 = hashlib.md5(open(full_path, 'rb').read()).hexdigest()
         self.assertEqual(h1, h2)
示例#10
0
def clean_media_dir():
    for fn in glob.glob(media_path('*')):
        if os.path.isdir(fn):
            shutil.rmtree(fn)
        else:
            os.remove(fn)
示例#11
0
def clean_media_dir():
    for fn in glob.glob(media_path('*')):
        if os.path.isdir(fn):
            shutil.rmtree(fn)
        else:
            os.remove(fn)
示例#12
0
     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,
         'img_id': img_id,
         'variants': {},
     }
     for v_conf in conf['VARIANTS']:
         label = v_conf['LABEL']
         if not label:
             label = get_variant_label(v_conf)
         v_f = adjust_image(f, 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=label,
示例#13
0
 def test_get_relative_path_from_img_id_with_create_dirs(self):
     self.assertEqual(
         get_relative_path_from_img_id('default:__t_abcde1234_ab12_myname.jpeg', create_dirs=True),
         'upload-img/common/34/__t_abcde1234_ab12_myname__m_93c87b2a66.jpeg'
     )
     self.assertTrue(os.path.isdir(media_path('upload-img/common/34')))
示例#14
0
     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, "img_id": img_id, "variants": {}}
 for v_conf in conf["VARIANTS"]:
     label = v_conf["LABEL"]
     if not label:
         label = get_variant_label(v_conf)
     v_f = adjust_image(
         f,
         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,
     )