def index(request): Report = get_report_model() # Get reports (filtered by user permission) reports = permission_policy.instances_user_has_any_permission_for( request.user, ['change', 'delete']) # Ordering if 'ordering' in request.GET and request.GET['ordering'] in [ 'title', '-created_at' ]: ordering = request.GET['ordering'] else: ordering = '-created_at' reports = reports.order_by(ordering) # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search reports")) if form.is_valid(): query_string = form.cleaned_data['q'] reports = reports.search(query_string) else: form = SearchForm(placeholder=_("Search reports")) # Pagination paginator, reports = paginate(request, reports) # Create response if request.is_ajax(): return render( request, 'wagtailreports/reports/results.html', { 'ordering': ordering, 'reports': reports, 'query_string': query_string, 'is_searching': bool(query_string), 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), }) else: return render( request, 'wagtailreports/reports/index.html', { 'ordering': ordering, 'reports': reports, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), })
def chooser_upload(request): Report = get_report_model() ReportForm = get_report_form(Report) if request.method == 'POST': report = Report(created_by_user=request.user) form = ReportForm(request.POST, request.FILES, instance=report, user=request.user) if form.is_valid(): form.save() # Reindex the report to make sure all tags are indexed search_index.insert_or_update_object(report) return render_modal_workflow( request, None, 'wagtailreports/chooser/report_chosen.js', {'report_json': get_report_json(report)}) else: form = ReportForm(user=request.user) reports = Report.objects.order_by('title') return render_modal_workflow(request, 'wagtailreports/chooser/chooser.html', 'wagtailreports/chooser/chooser.js', { 'reports': reports, 'uploadform': form })
def add(request): Report = get_report_model() ReportForm = get_report_form(Report) if request.method == 'POST': report = Report() form = ReportForm(request.POST, instance=report) #user=request.user if form.is_valid(): form.save() # Reindex the report to make sure all tags are indexed search_index.insert_or_update_object(report) messages.success(request, _("Report '{0}' added.").format(report.title), buttons=[ messages.button( reverse('wagtailreports:edit', args=(report.id, )), _('Edit')) ]) return redirect('wagtailreports:index') else: messages.error(request, _("The report could not be saved due to errors.")) else: form = ReportForm() return render(request, "wagtailreports/reports/add.html", { 'form': form, })
def serve(request, report_id): Report = get_report_model() report = get_object_or_404(Report, id=report_id) for fn in hooks.get_hooks('before_serve_report'): result = fn(report, request) if isinstance(result, HttpResponse): return result # Send report_served signal report_served.send(sender=Report, instance=report, request=request) try: local_path = report.file.path except NotImplementedError: local_path = None if local_path: # Use wagtail.utils.sendfile to serve the file; # this provides support for mimetypes, if-modified-since and django-sendfile backends if hasattr(settings, 'SENDFILE_BACKEND'): return sendfile(request, local_path, attachment=True, attachment_filename=report.filename) else: # Fallback to streaming backend if user hasn't specified SENDFILE_BACKEND return sendfile(request, local_path, attachment=True, attachment_filename=report.filename, backend=sendfile_streaming_backend.sendfile) else: # We are using a storage backend which does not expose filesystem paths # (e.g. storages.backends.s3boto.S3BotoStorage). # Fall back on pre-sendfile behaviour of reading the file content and serving it # as a StreamingHttpResponse wrapper = FileWrapper(report.file) response = StreamingHttpResponse( wrapper, content_type='application/octet-stream') try: response[ 'Content-Disposition'] = 'attachment; filename=%s' % report.filename except BadHeaderError: # Unicode filenames can fail on Django <1.8, Python 2 due to # https://code.djangoproject.com/ticket/20889 - try with an ASCIIfied version of the name response[ 'Content-Disposition'] = 'attachment; filename=%s' % unidecode( report.filename) # FIXME: storage backends are not guaranteed to implement 'size' response['Content-Length'] = report.file.size return response
def test_report_file_deleted_oncommit(self): with transaction.atomic(): report = get_report_model().objects.create( title="Test Image", file=get_test_image_file()) self.assertTrue(report.file.storage.exists(report.file.name)) report.delete() self.assertTrue(report.file.storage.exists(report.file.name)) self.assertFalse(report.file.storage.exists(report.file.name))
def usage(request, report_id): Report = get_report_model() report = get_object_or_404(Report, id=report_id) paginator, used_by = paginate(request, report.get_usage()) return render(request, "wagtailreports/reports/usage.html", { 'report': report, 'used_by': used_by })
def test_report_file_deleted(self): ''' this test duplicates `test_image_file_deleted_oncommit` for django 1.8 support and can be removed once django 1.8 is no longer supported ''' with transaction.atomic(): report = get_report_model().objects.create( title="Test Image", file=get_test_image_file()) self.assertTrue(report.file.storage.exists(report.file.name)) report.delete() self.assertFalse(report.file.storage.exists(report.file.name))
def expand_db_attributes(attrs, for_editor): Report = get_report_model() try: report = Report.objects.get(id=attrs['id']) if for_editor: editor_attrs = 'data-linktype="report" data-id="%d" ' % report.id else: editor_attrs = '' return '<a %shref="%s">' % (editor_attrs, escape(report.url)) except Report.DoesNotExist: return "<a>"
def chooser(request): Report = get_report_model() if permission_policy.user_has_permission(request.user, 'add'): ReportForm = get_report_form(Report) uploadform = ReportForm(user=request.user) else: uploadform = None reports = Report.objects.all() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_report_chooser_queryset'): reports = hook(reports, request) q = None if 'q' in request.GET or 'p' in request.GET: searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] reports = reports.search(q) is_searching = True else: reports = reports.order_by('-created_at') is_searching = False # Pagination paginator, reports = paginate(request, reports, per_page=10) return render(request, "wagtailreports/chooser/results.html", { 'reports': reports, 'query_string': q, 'is_searching': is_searching, }) else: searchform = SearchForm() reports = reports.order_by('-created_at') paginator, reports = paginate(request, reports, per_page=10) return render_modal_workflow( request, 'wagtailreports/chooser/chooser.html', 'wagtailreports/chooser/chooser.js', { 'reports': reports, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, })
def delete(request, report_id): Report = get_report_model() report = get_object_or_404(Report, id=report_id) if not permission_policy.user_has_permission_for_instance( request.user, 'delete', report): return permission_denied(request) if request.method == 'POST': report.delete() messages.success(request, _("Report '{0}' deleted.").format(report.title)) return redirect('wagtailreports:index') return render(request, "wagtailreports/reports/confirm_delete.html", { 'report': report, })
def edit(request, report_id): Report = get_report_model() ReportForm = get_report_form(Report) report = get_object_or_404(Report, id=report_id) # if not permission_policy.user_has_permission_for_instance(request.user, 'change', report): # return permission_denied(request) if request.method == 'POST': form = ReportForm(request.POST, request.FILES, instance=report) # user=request.user if form.is_valid(): report = form.save() # Reindex the report to make sure all tags are indexed search_index.insert_or_update_object(report) messages.success(request, _("Report '{0}' updated").format(report.title), buttons=[ messages.button( reverse('wagtailreports:edit', args=(report.id, )), _('Edit')) ]) return redirect('wagtailreports:index') else: messages.error(request, _("The report could not be saved due to errors.")) else: form = ReportForm(instance=report) #, user=request.user return render( request, "wagtailreports/reports/edit.html", { 'report': report, 'form': form, 'user_can_delete': permission_policy.user_has_permission_for_instance( request.user, 'delete', report), })
def register_report_panels_permissions(): Report = get_report_model() ReportPanel = get_report_panel_model() content_types = ContentType.objects.get_for_models(Report, ReportPanel).values() return Permission.objects.filter(content_type__in=content_types)
def test_report_model(self): cls = get_report_model() self.assertEqual('%s.%s' % (cls._meta.app_label, cls.__name__), 'tests.CustomReport')
def report_chosen(request, report_id): report = get_object_or_404(get_report_model(), id=report_id) return render_modal_workflow(request, None, 'wagtailreports/chooser/report_chosen.js', {'report_json': get_report_json(report)})
def target_model(self): from wagtailreports.models import get_report_model return get_report_model()
from __future__ import absolute_import, unicode_literals from wagtail.wagtailcore.permission_policies import ModelPermissionPolicy from wagtailreports.models import get_report_model, get_report_panel_model report_permission_policy = ModelPermissionPolicy(get_report_model()) report_panel_permission_policy = ModelPermissionPolicy( get_report_panel_model())
def __init__(self, **kwargs): super(AdminReportChooser, self).__init__(**kwargs) self.report_model = get_report_model()