本文整理汇总了Python中corehq.apps.hqmedia.models.CommCareImage.get_by_data方法的典型用法代码示例。如果您正苦于以下问题:Python CommCareImage.get_by_data方法的具体用法?Python CommCareImage.get_by_data怎么用?Python CommCareImage.get_by_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类corehq.apps.hqmedia.models.CommCareImage
的用法示例。
在下文中一共展示了CommCareImage.get_by_data方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_multimedia
# 需要导入模块: from corehq.apps.hqmedia.models import CommCareImage [as 别名]
# 或者: from corehq.apps.hqmedia.models.CommCareImage import get_by_data [as 别名]
def test_multimedia(self):
from corehq.apps.hqmedia.models import CommCareAudio, CommCareImage, CommCareVideo
image_path = os.path.join('corehq', 'apps', 'hqwebapp', 'static', 'hqwebapp', 'img', 'commcare-hq-logo.png')
with open(image_path, 'r') as f:
image_data = f.read()
image = CommCareImage.get_by_data(image_data)
image.attach_data(image_data, original_filename='logo.png')
image.add_domain(self.domain_name)
self.assertEqual(image_data, image.get_display_file(False))
audio_data = 'fake audio data'
audio = CommCareAudio.get_by_data(audio_data)
audio.attach_data(audio_data, original_filename='tr-la-la.mp3')
audio.add_domain(self.domain_name)
self.assertEqual(audio_data, audio.get_display_file(False))
video_data = 'fake video data'
video = CommCareVideo.get_by_data(video_data)
video.attach_data(video_data, 'kittens.mp4')
video.add_domain(self.domain_name)
self.assertEqual(video_data, video.get_display_file(False))
fakedb = self._dump_and_load([image, audio, video])
copied_image = CommCareImage.wrap(fakedb.get(image._id))
copied_audio = CommCareAudio.wrap(fakedb.get(audio._id))
copied_video = CommCareVideo.wrap(fakedb.get(video._id))
self.assertEqual(image_data, copied_image.get_display_file(False))
self.assertEqual(audio_data, copied_audio.get_display_file(False))
self.assertEqual(video_data, copied_video.get_display_file(False))
示例2: match_zipped
# 需要导入模块: from corehq.apps.hqmedia.models import CommCareImage [as 别名]
# 或者: from corehq.apps.hqmedia.models.CommCareImage import get_by_data [as 别名]
def match_zipped(self, zip, replace_existing_media=True, **kwargs):
unknown_files = []
matched_audio = []
matched_images = []
errors = []
try:
for index, path in enumerate(zip.namelist()):
path = path.strip()
filename = path.lower()
basename = os.path.basename(path.lower())
is_image = False
if not basename:
continue
if self.match_filename:
filename = basename
media = None
form_path = None
data = None
if filename in self.images:
form_path = self.image_paths[self.images.index(filename)]
data = zip.read(path)
media = CommCareImage.get_by_data(data)
is_image = True
elif filename in self.audio:
form_path = self.audio_paths[self.audio.index(filename)]
data = zip.read(path)
media = CommCareAudio.get_by_data(data)
else:
unknown_files.append(path)
if media:
try:
media.attach_data(data,
upload_path=path,
username=self.username,
replace_attachment=replace_existing_media)
media.add_domain(self.domain, owner=True)
media.update_or_add_license(self.domain,
type=kwargs.get('license', ''),
author=kwargs.get('author', ''),
attribution_notes=kwargs.get('attribution_notes', ''))
self.app.create_mapping(media, form_path)
match_map = HQMediaMapItem.format_match_map(form_path, media.doc_type, media._id, path)
if is_image:
matched_images.append(match_map)
else:
matched_audio.append(match_map)
except Exception as e:
errors.append("%s (%s)" % (e, filename))
zip.close()
except Exception as e:
logging.error(e)
errors.append(e.message)
return matched_images, matched_audio, unknown_files, errors
示例3: test_override_logo
# 需要导入模块: from corehq.apps.hqmedia.models import CommCareImage [as 别名]
# 或者: from corehq.apps.hqmedia.models.CommCareImage import get_by_data [as 别名]
def test_override_logo(self):
image_data = _get_image_data()
image = CommCareImage.get_by_data(image_data)
image.attach_data(image_data, original_filename='logo.png')
image.add_domain(self.linked_app.domain)
image.save()
self.addCleanup(image.delete)
image_path = "jr://file/commcare/logo/data/hq_logo_android_home.png"
logo_refs = {
"hq_logo_android_home": {
"humanized_content_length": "45.4 KB",
"icon_class": "fa fa-picture-o",
"image_size": "448 X 332 Pixels",
"m_id": image._id,
"media_type": "Image",
"path": "jr://file/commcare/logo/data/hq_logo_android_home.png",
"uid": "3b79a76a067baf6a23a0b6978b2fb352",
"updated": False,
"url": "/hq/multimedia/file/CommCareImage/e3c45dd61c5593fdc5d985f0b99f6199/"
},
}
self.linked_app.master = self.plain_master_app.get_id
copy = self.plain_master_app.make_build()
copy.save()
self.addCleanup(copy.delete)
self.plain_master_app.save() # increment version number
copy1 = self.plain_master_app.make_build()
copy1.is_released = True
copy1.save()
self.addCleanup(copy1.delete)
self.linked_app.version = 1
self.linked_app.linked_app_logo_refs = logo_refs
self.linked_app.create_mapping(image, image_path, save=False)
self.linked_app.save()
self.assertEqual(self.linked_app.logo_refs, {})
update_linked_app(self.linked_app, 'test_override_logos')
# fetch after update to get the new version
self.linked_app = LinkedApplication.get(self.linked_app._id)
self.assertEqual(self.plain_master_app.logo_refs, {})
self.assertEqual(self.linked_app.linked_app_logo_refs, logo_refs)
self.assertEqual(self.linked_app.logo_refs, logo_refs)
# cleanup the linked app logo properties
self.linked_app.linked_app_logo_refs = {}
self.linked_app.save()
示例4: save
# 需要导入模块: from corehq.apps.hqmedia.models import CommCareImage [as 别名]
# 或者: from corehq.apps.hqmedia.models.CommCareImage import get_by_data [as 别名]
def save(self, domain, app, username, cache_handler=None):
orig_images, orig_audio, _ = utils.get_multimedia_filenames(app)
form_images = [i.replace(utils.MULTIMEDIA_PREFIX, '').lower().strip() for i in orig_images]
form_audio = [a.replace(utils.MULTIMEDIA_PREFIX, '').lower().strip() for a in orig_audio]
if self.cleaned_data["repopulate_multimedia_map"]:
app.multimedia_map = {}
app.save()
replace_attachment = self.cleaned_data["replace_existing_media"]
zip = self.cleaned_data["zip_file"]
num_files = len(zip.namelist())
if cache_handler:
cache_handler.sync()
cache_handler.data["processed_length"] = num_files
cache_handler.save()
unknown_files = []
for index, path in enumerate(zip.namelist()):
path = path.strip()
path_lower = path.lower()
if path_lower.endswith("/") or path_lower.endswith("\\") or \
path_lower.endswith(".ds_store"):
continue
media = None
form_path = None
data = None
if path_lower in form_images:
form_path = orig_images[form_images.index(path_lower)]
data = zip.read(path)
media = CommCareImage.get_by_data(data)
elif path_lower in form_audio:
form_path = orig_audio[form_audio.index(path_lower)]
data = zip.read(path)
media = CommCareAudio.get_by_data(data)
else:
unknown_files.append(path)
if media:
media.attach_data(data,
upload_path=path,
username=username,
replace_attachment=replace_attachment)
media.add_domain(domain, owner=True)
app.create_mapping(media, form_path)
if cache_handler:
cache_handler.sync()
cache_handler.data["processed"] = index+1
cache_handler.save()
zip.close()
return {"successful": unknown_files}
示例5: match_file
# 需要导入模块: from corehq.apps.hqmedia.models import CommCareImage [as 别名]
# 或者: from corehq.apps.hqmedia.models.CommCareImage import get_by_data [as 别名]
def match_file(self, uploaded_file, replace_existing_media=True, **kwargs):
errors = []
try:
if self.specific_params and self.specific_params['media_type'][0].startswith('CommCare'):
media_class = getattr(sys.modules[__name__], self.specific_params['media_type'][0])
form_path = self.specific_params['path'][0]
replace_existing_media = self.specific_params['replace_attachment'][0]
filename = uploaded_file.name
data = uploaded_file.file.read()
media = media_class.get_by_data(data)
else:
filename = uploaded_file.name
if filename in self.images:
form_path = self.image_paths[self.images.index(filename)]
data = uploaded_file.file.read()
media = CommCareImage.get_by_data(data)
elif filename in self.audio:
form_path = self.audio_paths[self.audio.index(filename)]
data = uploaded_file.file.read()
media = CommCareAudio.get_by_data(data)
else:
uploaded_file.close()
return False, {}, errors
if media:
media.attach_data(data,
original_filename=filename,
username=self.username,
replace_attachment=replace_existing_media)
media.add_domain(self.domain, owner=True, **kwargs)
media.update_or_add_license(self.domain,
type=kwargs.get('license', ''),
author=kwargs.get('author', ''),
attribution_notes=kwargs.get('attribution_notes', ''))
self.app.create_mapping(media, form_path)
return True, HQMediaMapItem.format_match_map(form_path, media.doc_type, media._id, filename), errors
except Exception as e:
logging.error(e)
errors.append(e.message)
return False, {}, errors
示例6: setUpClass
# 需要导入模块: from corehq.apps.hqmedia.models import CommCareImage [as 别名]
# 或者: from corehq.apps.hqmedia.models.CommCareImage import get_by_data [as 别名]
def setUpClass(cls):
super(TestRemoteLinkedApps, cls).setUpClass()
image_data = _get_image_data()
cls.image = CommCareImage.get_by_data(image_data)
cls.image.attach_data(image_data, original_filename='logo.png')
cls.image.add_domain(cls.plain_master_app.domain)