def render_modal_workflow(request, html_template, js_template, template_vars=None): """" Render a response consisting of an HTML chunk and a JS onload chunk in the format required by the modal-workflow framework. """ response_keyvars = [] if html_template: html = render_to_string(html_template, template_vars or {}, request=request) response_keyvars.append("'html': %s" % json.dumps(html)) if js_template: js = render_to_string(js_template, template_vars or {}, request=request) response_keyvars.append("'onload': %s" % js) response_text = "{%s}" % ','.join(response_keyvars) return HttpResponse(response_text, content_type="text/javascript")
def edit(request, image_id, callback=None): Image = get_image_model() ImageForm = get_image_edit_form(Image) image = get_object_or_404(Image, id=image_id) if not request.is_ajax(): return HttpResponseBadRequest("Cannot POST to this view without AJAX") if not image.is_editable_by_user(request.user): raise PermissionDenied form = ImageForm(request.POST, request.FILES, instance=image, prefix='image-' + image_id) 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) return json_response({ 'success': True, 'image_id': int(image_id), }) else: return json_response({ 'success': False, 'image_id': int(image_id), 'form': render_to_string('wagtailimages/multiple/edit_form.html', { 'image': image, 'form': form, }, request=request), })
def add(request): Document = get_document_model() DocumentForm = get_document_form(Document) DocumentMultiForm = get_document_multi_form(Document) collections = permission_policy.collections_user_has_permission_for(request.user, 'add') if len(collections) > 1: collections_to_choose = collections else: # no need to show a collections chooser collections_to_choose = None 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 = DocumentForm({ 'title': request.FILES['files[]'].name, 'collection': request.POST.get('collection'), }, { 'file': request.FILES['files[]'] }, user=request.user) if form.is_valid(): # Save it doc = form.save(commit=False) doc.uploaded_by_user = request.user doc.file_size = doc.file.size doc.save() # Success! Send back an edit form for this document to the user return JsonResponse({ 'success': True, 'doc_id': int(doc.id), 'form': render_to_string('wagtaildocs/multiple/edit_form.html', { 'doc': doc, 'form': DocumentMultiForm( instance=doc, prefix='doc-%d' % doc.id, user=request.user ), }, request=request), }) else: # Validation error return JsonResponse({ '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 = DocumentForm(user=request.user) return render(request, 'wagtaildocs/multiple/add.html', { 'help_text': form.fields['file'].help_text, 'collections': collections_to_choose, })
def edit(request, doc_id, callback=None): Document = get_document_model() DocumentMultiForm = get_document_multi_form(Document) doc = get_object_or_404(Document, id=doc_id) if not request.is_ajax(): return HttpResponseBadRequest("Cannot POST to this view without AJAX") if not permission_policy.user_has_permission_for_instance(request.user, 'change', doc): raise PermissionDenied form = DocumentMultiForm(request.POST, request.FILES, instance=doc, prefix='doc-' + doc_id) if form.is_valid(): form.save() # Reindex the doc to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) return JsonResponse({ 'success': True, 'doc_id': int(doc_id), }) else: return JsonResponse({ 'success': False, 'doc_id': int(doc_id), 'form': render_to_string('wagtaildocs/multiple/edit_form.html', { 'doc': doc, 'form': form, }, request=request), })
def render(self): return render_to_string('wagtailadmin/home/pages_for_moderation.html', { 'page_revisions_for_moderation': self.page_revisions_for_moderation, }, request=self.request)
def render(self): if self.request.user.is_superuser and getattr( settings, "WAGTAIL_ENABLE_UPDATE_CHECK", True): return render_to_string( 'wagtailadmin/home/upgrade_notification.html', {}, request=self.request) else: return ""
def add(request): Document = get_document_model() DocumentForm = get_document_form(Document) DocumentMultiForm = get_document_multi_form(Document) 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 = DocumentForm({'title': request.FILES['files[]'].name}, {'file': request.FILES['files[]']}) if form.is_valid(): # Save it doc = form.save(commit=False) doc.uploaded_by_user = request.user doc.file_size = doc.file.size doc.save() # Success! Send back an edit form for this document to the user return JsonResponse({ 'success': True, 'doc_id': int(doc.id), 'form': render_to_string('wagtaildocs/multiple/edit_form.html', { 'doc': doc, 'form': DocumentMultiForm(instance=doc, prefix='doc-%d' % doc.id), }, request=request), }) else: # Validation error return JsonResponse({ '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 = DocumentForm() return render(request, 'wagtaildocs/multiple/add.html', { 'help_text': form.fields['file'].help_text, })
def render_html(self, request): return render_to_string(self.template, { 'name': self.name, 'url': self.url, 'classnames': self.classnames, 'attr_string': self.attr_string, 'label': self.label, 'active': self.is_active(request) }, request=request)
def render_html(self, request, query, current=None): return render_to_string(self.template, { 'name': self.name, 'url': self.url, 'classnames': self.classnames, 'attr_string': self.attr_string, 'label': self.label, 'active': self.is_active(request, current), 'query_string': query }, request=request)
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'], })
def edit(request, doc_id, callback=None): Document = get_document_model() DocumentMultiForm = get_document_multi_form(Document) doc = get_object_or_404(Document, id=doc_id) if not request.is_ajax(): return HttpResponseBadRequest("Cannot POST to this view without AJAX") if not permission_policy.user_has_permission_for_instance( request.user, 'change', doc): raise PermissionDenied form = DocumentMultiForm(request.POST, request.FILES, instance=doc, prefix='doc-' + doc_id, user=request.user) if form.is_valid(): form.save() # Reindex the doc to make sure all tags are indexed for backend in get_search_backends(): backend.add(doc) return JsonResponse({ 'success': True, 'doc_id': int(doc_id), }) else: return JsonResponse({ 'success': False, 'doc_id': int(doc_id), 'form': render_to_string('wagtaildocs/multiple/edit_form.html', { 'doc': doc, 'form': form, }, request=request), })
def render(self): return render_to_string('wagtailadmin/home/recent_edits.html', { 'last_edits': self.last_edits, }, request=self.request)
def render(self): return render_to_string(self.template, self.get_context(), request=self.request)
def render(self): if self.request.user.is_superuser and getattr(settings, "WAGTAIL_ENABLE_UPDATE_CHECK", True): return render_to_string('wagtailadmin/home/upgrade_notification.html', {}, request=self.request) else: return ""
def render(self, request): return render_to_string(self.template, dict(self=self, request=request), request=request)
def render(self): return render_to_string('wagtailadmin/home/site_summary.html', { 'summary_items': sorted(self.summary_items, key=lambda p: p.order), }, request=self.request)