本文整理汇总了Python中wagtail.wagtailimages.forms.get_image_form函数的典型用法代码示例。如果您正苦于以下问题:Python get_image_form函数的具体用法?Python get_image_form怎么用?Python get_image_form使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_image_form函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: edit
def edit(request, image_id):
Image = get_image_model()
ImageForm = get_image_form(Image)
image = get_object_or_404(Image, id=image_id)
if not image.is_editable_by_user(request.user):
raise PermissionDenied
if request.POST:
original_file = image.file
form = ImageForm(request.POST, request.FILES, instance=image)
if form.is_valid():
if 'file' in form.changed_data:
# if providing a new image file, delete the old one and all renditions.
# NB Doing this via original_file.delete() clears the file field,
# which definitely isn't what we want...
original_file.storage.delete(original_file.name)
image.renditions.all().delete()
# Set new image file size
image.file_size = image.file.size
form.save()
# Reindex the image to make sure all tags are indexed
for backend in get_search_backends():
backend.add(image)
messages.success(request, _("Image '{0}' updated.").format(image.title), buttons=[
messages.button(reverse('wagtailimages:edit', args=(image.id,)), _('Edit again'))
])
return redirect('wagtailimages:index')
else:
messages.error(request, _("The image could not be saved due to errors."))
else:
form = ImageForm(instance=image)
# Check if we should enable the frontend url generator
try:
reverse('wagtailimages_serve', args=('foo', '1', 'bar'))
url_generator_enabled = True
except NoReverseMatch:
url_generator_enabled = False
if image.is_stored_locally():
# Give error if image file doesn't exist
if not os.path.isfile(image.file.path):
messages.error(request, _(
"The source image file could not be found. Please change the source or delete the image."
).format(image.title), buttons=[
messages.button(reverse('wagtailimages:delete', args=(image.id,)), _('Delete'))
])
return render(request, "wagtailimages/images/edit.html", {
'image': image,
'form': form,
'url_generator_enabled': url_generator_enabled,
'filesize': image.get_file_size(),
})
示例2: add
def add(request):
ImageModel = get_image_model()
ImageForm = get_image_form(ImageModel)
if request.method == "POST":
image = ImageModel(uploaded_by_user=request.user)
form = ImageForm(request.POST, request.FILES, instance=image, user=request.user)
if form.is_valid():
# Set image file size
image.file_size = image.file.size
form.save()
# Reindex the image to make sure all tags are indexed
search_index.insert_or_update_object(image)
messages.success(
request,
_("Image '{0}' added.").format(image.title),
buttons=[messages.button(reverse("wagtailimages:edit", args=(image.id,)), _("Edit"))],
)
return redirect("wagtailimages:index")
else:
messages.error(request, _("The image could not be created due to errors."))
else:
form = ImageForm(user=request.user)
return render(request, "wagtailimages/images/add.html", {"form": form})
示例3: add
def add(request):
ImageForm = get_image_form()
ImageModel = get_image_model()
if request.POST:
image = ImageModel(uploaded_by_user=request.user)
form = ImageForm(request.POST, request.FILES, instance=image)
if form.is_valid():
form.save()
# Reindex the image to make sure all tags are indexed
for backend in get_search_backends():
backend.add(image)
messages.success(request, _("Image '{0}' added.").format(image.title))
return redirect('wagtailimages_index')
else:
messages.error(request, _("The image could not be created due to errors."))
else:
form = ImageForm()
return render(request, "wagtailimages/images/add.html", {
'form': form,
'max_filesize': MAX_UPLOAD_SIZE,
})
示例4: test_custom_image_model_without_admin_form_fields_raises_warning
def test_custom_image_model_without_admin_form_fields_raises_warning(self):
self.reset_warning_registry()
with warnings.catch_warnings(record=True) as raw_warnings:
form = get_image_form(CustomImageWithoutAdminFormFields)
# Check that a RemovedInWagtail12Warning has been triggered
# Ignore any ResourceWarnings. TODO: remove this when we've stopped ResourceWarnings from happening...
try:
clean_warnings = [w for w in raw_warnings if not issubclass(w.category, ResourceWarning)]
except NameError: # ResourceWarning only exists on Python >= 3.2
clean_warnings = raw_warnings
self.assertEqual(len(clean_warnings), 1)
self.assertTrue(issubclass(clean_warnings[-1].category, RemovedInWagtail12Warning))
self.assertTrue("Add admin_form_fields = (tuple of field names) to CustomImageWithoutAdminFormFields" in str(clean_warnings[-1].message))
# All fields, including the not editable one should be on the form
self.assertEqual(list(form.base_fields.keys()), [
'title',
'file',
'focal_point_x',
'focal_point_y',
'focal_point_width',
'focal_point_height',
'caption',
'not_editable_field',
'tags',
])
示例5: test_fields
def test_fields(self):
form = get_image_form(Image)
self.assertEqual(
list(form.base_fields.keys()),
["title", "file", "tags", "focal_point_x", "focal_point_y", "focal_point_width", "focal_point_height"],
)
示例6: edit
def edit(request, image_id):
Image = get_image_model()
ImageForm = get_image_form()
image = get_object_or_404(Image, id=image_id)
if not image.is_editable_by_user(request.user):
raise PermissionDenied
if request.POST:
original_file = image.file
form = ImageForm(request.POST, request.FILES, instance=image)
if form.is_valid():
if "file" in form.changed_data:
# if providing a new image file, delete the old one and all renditions.
# NB Doing this via original_file.delete() clears the file field,
# which definitely isn't what we want...
original_file.storage.delete(original_file.name)
image.renditions.all().delete()
form.save()
messages.success(request, _("Image '{0}' updated.").format(image.title))
return redirect("wagtailimages_index")
else:
messages.error(request, _("The image could not be saved due to errors."))
else:
form = ImageForm(instance=image)
return render(request, "wagtailimages/images/edit.html", {"image": image, "form": form})
示例7: add
def add(request):
ImageModel = get_image_model()
ImageForm = get_image_form(ImageModel)
if request.POST:
image = ImageModel(uploaded_by_user=request.user)
form = ImageForm(request.POST, request.FILES, instance=image)
if form.is_valid():
# Set image file size
image.file_size = image.file.size
form.save()
# Reindex the image to make sure all tags are indexed
for backend in get_search_backends():
backend.add(image)
messages.success(request, _("Image '{0}' added.").format(image.title), buttons=[
messages.button(reverse('wagtailimages:edit', args=(image.id,)), _('Edit'))
])
return redirect('wagtailimages:index')
else:
messages.error(request, _("The image could not be created due to errors."))
else:
form = ImageForm()
return render(request, "wagtailimages/images/add.html", {
'form': form,
})
示例8: test_custom_image_model_without_admin_form_fields_raises_warning
def test_custom_image_model_without_admin_form_fields_raises_warning(self):
self.reset_warning_registry()
with warnings.catch_warnings(record=True) as w:
form = get_image_form(CustomImageWithoutAdminFormFields)
# Check that a RemovedInWagtail12Warning has been triggered
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, RemovedInWagtail12Warning))
self.assertTrue(
"Add admin_form_fields = (tuple of field names) to CustomImageWithoutAdminFormFields"
in str(w[-1].message)
)
# All fields, including the not editable one should be on the form
self.assertEqual(
list(form.base_fields.keys()),
[
"title",
"file",
"focal_point_x",
"focal_point_y",
"focal_point_width",
"focal_point_height",
"caption",
"not_editable_field",
"tags",
],
)
示例9: chooser_upload
def chooser_upload(request):
Image = get_image_model()
ImageForm = get_image_form()
if request.POST:
image = Image(uploaded_by_user=request.user)
form = ImageForm(request.POST, request.FILES, instance=image)
if form.is_valid():
form.save()
if request.GET.get("select_format"):
form = ImageInsertionForm(initial={"alt_text": image.default_alt_text})
return render_modal_workflow(
request,
"wagtailimages/chooser/select_format.html",
"wagtailimages/chooser/select_format.js",
{"image": image, "form": form},
)
else:
# not specifying a format; return the image details now
return render_modal_workflow(
request, None, "wagtailimages/chooser/image_chosen.js", {"image_json": get_image_json(image)}
)
else:
form = ImageForm()
images = Image.objects.order_by("title")
return render_modal_workflow(
request,
"wagtailimages/chooser/chooser.html",
"wagtailimages/chooser/chooser.js",
{"images": images, "uploadform": form},
)
示例10: chooser_upload
def chooser_upload(request):
Image = get_image_model()
ImageForm = get_image_form()
if request.POST:
image = Image(uploaded_by_user=request.user)
form = ImageForm(request.POST, request.FILES, instance=image)
if form.is_valid():
form.save()
if request.GET.get('select_format'):
form = ImageInsertionForm(initial={'alt_text': image.default_alt_text})
return render_modal_workflow(
request, 'wagtailimages/chooser/select_format.html', 'wagtailimages/chooser/select_format.js',
{'image': image, 'form': form}
)
else:
# not specifying a format; return the image details now
return render_modal_workflow(
request, None, 'wagtailimages/chooser/image_chosen.js',
{'image_json': get_image_json(image)}
)
else:
form = ImageForm()
images = Image.objects.order_by('title')
return render_modal_workflow(
request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js',
{'images': images, 'uploadform': form}
)
示例11: test_fields
def test_fields(self):
form = get_image_form(Image)
self.assertEqual(list(form.base_fields.keys()), [
'title',
'file',
'tags',
'focal_point_x',
'focal_point_y',
'focal_point_width',
'focal_point_height',
])
示例12: test_focal_point_fields
def test_focal_point_fields(self):
form = get_image_form(WagtailImage)
self.assertIsInstance(form.base_fields['focal_point_x'], forms.IntegerField)
self.assertIsInstance(form.base_fields['focal_point_y'], forms.IntegerField)
self.assertIsInstance(form.base_fields['focal_point_width'], forms.IntegerField)
self.assertIsInstance(form.base_fields['focal_point_height'], forms.IntegerField)
self.assertIsInstance(form.base_fields['focal_point_x'].widget, forms.HiddenInput)
self.assertIsInstance(form.base_fields['focal_point_y'].widget, forms.HiddenInput)
self.assertIsInstance(form.base_fields['focal_point_width'].widget, forms.HiddenInput)
self.assertIsInstance(form.base_fields['focal_point_height'].widget, forms.HiddenInput)
示例13: add
def add(request):
Image = get_image_model()
ImageForm = get_image_form(Image)
if request.method == 'POST':
if not request.is_ajax():
return HttpResponseBadRequest("Cannot POST to this view without AJAX")
if not request.FILES:
return HttpResponseBadRequest("Must upload a file")
# Build a form for validation
form = ImageForm({
'title': request.FILES['files[]'].name,
}, {
'file': request.FILES['files[]'],
})
if form.is_valid():
# Save it
image = form.save(commit=False)
image.uploaded_by_user = request.user
image.file_size = image.file.size
image.save()
# Success! Send back an edit form for this image to the user
return json_response({
'success': True,
'image_id': int(image.id),
'form': render_to_string('wagtailimages/multiple/edit_form.html', {
'image': image,
'form': get_image_edit_form(Image)(instance=image, prefix='image-%d' % image.id),
}, request=request),
})
else:
# Validation error
return json_response({
'success': False,
# https://github.com/django/django/blob/stable/1.6.x/django/forms/util.py#L45
'error_message': '\n'.join(['\n'.join([force_text(i) for i in v]) for k, v in form.errors.items()]),
})
else:
form = ImageForm()
return render(request, 'wagtailimages/multiple/add.html', {
'max_filesize': form.fields['file'].max_upload_size,
'help_text': form.fields['file'].help_text,
'allowed_extensions': ALLOWED_EXTENSIONS,
'error_max_file_size': form.fields['file'].error_messages['file_too_large_unknown_size'],
'error_accepted_file_types': form.fields['file'].error_messages['invalid_image'],
})
示例14: chooser
def chooser(request):
Image = get_image_model()
if request.user.has_perm('wagtailimages.add_image'):
ImageForm = get_image_form(Image)
uploadform = ImageForm()
else:
uploadform = None
images = Image.objects.order_by('-created_at')
q = None
if 'q' in request.GET or 'p' in request.GET or 'tag' in request.GET:
# this request is triggered from search, pagination or 'popular tags';
# we will just render the results.html fragment
searchform = SearchForm(request.GET)
if searchform.is_valid():
q = searchform.cleaned_data['q']
images = images.search(q)
is_searching = True
else:
is_searching = False
tag_name = request.GET.get('tag')
if tag_name:
images = images.filter(tags__name=tag_name)
# Pagination
paginator, images = paginate(request, images, per_page=12)
return render(request, "wagtailimages/chooser/results.html", {
'images': images,
'is_searching': is_searching,
'query_string': q,
'will_select_format': request.GET.get('select_format')
})
else:
searchform = SearchForm()
paginator, images = paginate(request, images, per_page=12)
return render_modal_workflow(request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', {
'images': images,
'uploadform': uploadform,
'searchform': searchform,
'is_searching': False,
'query_string': q,
'will_select_format': request.GET.get('select_format'),
'popular_tags': Image.popular_tags(),
})
示例15: test_admin_form_fields_attribute
def test_admin_form_fields_attribute(self):
form = get_image_form(CustomImage)
self.assertEqual(list(form.base_fields.keys()), [
'title',
'file',
'tags',
'focal_point_x',
'focal_point_y',
'focal_point_width',
'focal_point_height',
'caption',
])