def test_generate_img_id(self): for i in xrange(50): self.assertIsNotNone(re.match(r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}$', generate_img_id('simple1'))) for i in xrange(50): self.assertIsNotNone(re.match(r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}\.png$', generate_img_id('simple1', ext='png'))) for i in xrange(50): self.assertIsNotNone(re.match(r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}_tst$', generate_img_id('simple1', label='tst'))) for i in xrange(50): self.assertIsNotNone(re.match(r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}_tst\.png$', generate_img_id('simple1', ext='png', label='tst'))) for i in xrange(50): self.assertIsNotNone(re.match(r'^simple1:__t_[a-z0-9]{8,11}_[a-z0-9]{4}$', generate_img_id('simple1', tmp=True))) for i in xrange(50): self.assertIsNotNone(re.match(r'^simple1:__t_[a-z0-9]{8,11}_[a-z0-9]{4}\.png$', generate_img_id('simple1', tmp=True, ext='png'))) for i in xrange(50): self.assertIsNotNone(re.match(r'^simple1:__t_[a-z0-9]{8,11}_[a-z0-9]{4}_tst$', generate_img_id('simple1', tmp=True, label='tst'))) for i in xrange(50): self.assertIsNotNone(re.match(r'^simple1:__t_[a-z0-9]{8,11}_[a-z0-9]{4}_tst\.png$', generate_img_id('simple1', tmp=True, label='tst', ext='png'))) self.assertIsNotNone(re.match( r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}_ts_ABC159\-q_qweyuoopzts_ABC159\-q_qweyuoopzts_ABC159\-q_qweyuo$', generate_img_id('simple1', label=('ts_/ABC159-q___q(w..e#y%uo&op*?z' * 5)) ))
def test_generate_img_id(self): for i in xrange(50): self.assertIsNotNone( re.match(r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}$', generate_img_id('simple1'))) for i in xrange(50): self.assertIsNotNone( re.match(r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}\.png$', generate_img_id('simple1', ext='png'))) for i in xrange(50): self.assertIsNotNone( re.match(r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}_tst$', generate_img_id('simple1', label='tst'))) for i in xrange(50): self.assertIsNotNone( re.match(r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}_tst\.png$', generate_img_id('simple1', ext='png', label='tst'))) for i in xrange(50): self.assertIsNotNone( re.match(r'^simple1:__t_[a-z0-9]{8,11}_[a-z0-9]{4}$', generate_img_id('simple1', tmp=True))) for i in xrange(50): self.assertIsNotNone( re.match(r'^simple1:__t_[a-z0-9]{8,11}_[a-z0-9]{4}\.png$', generate_img_id('simple1', tmp=True, ext='png'))) for i in xrange(50): self.assertIsNotNone( re.match(r'^simple1:__t_[a-z0-9]{8,11}_[a-z0-9]{4}_tst$', generate_img_id('simple1', tmp=True, label='tst'))) for i in xrange(50): self.assertIsNotNone( re.match( r'^simple1:__t_[a-z0-9]{8,11}_[a-z0-9]{4}_tst\.png$', generate_img_id('simple1', tmp=True, label='tst', ext='png'))) self.assertIsNotNone( re.match( r'^simple1:[a-z0-9]{8,11}_[a-z0-9]{4}_ts_ABC159\-q_qweyuoopzts_ABC159\-q_qweyuoopzts_ABC159\-q_qweyuo$', generate_img_id('simple1', label=('ts_/ABC159-q___q(w..e#y%uo&op*?z' * 5))))
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)
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, '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'],
def test_img_id_has_tmp_prefix(self): img_id = generate_img_id('none', ext='png', label='zz', tmp=False) img_id_tmp = generate_img_id('none', ext='png', label='zz', tmp=True) self.assertTrue(img_id_has_tmp_prefix(img_id_tmp)) self.assertFalse(img_id_has_tmp_prefix(img_id))
def test_get_files_by_img_id_file_is_not_exists(self): r = get_files_by_img_id(generate_img_id('simple0')) self.assertIsNone(r)
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"],