def get(self, request, *args, **kwargs): self.object_list = self.get_queryset() regions = None query = request.GET.get('query') if query is not None: self.object_list = safe_queryset_and( self.object_list, self.model.objects._search(query)) regions = {} context = self.get_context_data( object_list=self.object_list, regions=regions) response = self.render_to_response(context) if query is not None: response.render() data = changed_regions(regions, ['object_list']) data['!keep'] = True # Keep modal open return HttpResponse( json.dumps(data), content_type='application/json') return response
def handle_search_form(self, request, ctx, queryset=None): """ Must return a tuple consisting of a queryset and either a HttpResponse or ``None`` """ if queryset is None: queryset = self.get_query_set(request) if self.search_form: form = self.search_form(request.GET, request=request) if not form.is_valid(): self.add_message( request, _('The search query was invalid.'), level=messages.ERROR) if request.get_full_path().endswith('?clear=1'): # No redirect loop generation raise ImproperlyConfigured( 'Search form %r does not validate after' ' clearing.' % form) return queryset, HttpResponseRedirect('?clear=1') queryset = safe_queryset_and(queryset, form.queryset(self.model)) ctx['search_form'] = form return queryset, None
def get(self, request, *args, **kwargs): """ Handles the search form and batch action handling. """ self.object_list = self.get_queryset() context = {} if self.search_form: form = self.search_form(self.request.GET, request=self.request) if not form.is_valid(): messages.error( self.request, _('The search query was invalid.')) return HttpResponseRedirect('?clear=1') self.object_list = safe_queryset_and( self.object_list, form.queryset(self.model), ) context['search_form'] = form context.update(self.get_context_data(object_list=self.object_list)) actions = self.get_batch_actions() if actions: form = BatchForm(self.request, self.object_list) form.actions = actions form.fields['action'] = forms.ChoiceField( label=_('Action'), choices=[('', '---------')] + [row[:2] for row in actions], widget=forms.HiddenInput, ) context['batch_form'] = form if form.should_process(): action = form.cleaned_data.get('action') name, title, fn = [a for a in actions if action == a[0]][0] result = fn(form.batch_queryset) if isinstance(result, HttpResponse): return result elif hasattr(result, '__iter__'): messages.success( self.request, _( '<p>Processed the following items:</p>' ' <ul><li>%s</li></ul>' ) % ( '</li><li>'.join( force_text(item) for item in result ) ) ) elif result is not None: # Not None, but cannot make sense of it either. raise TypeError('Return value %r of %s invalid.' % ( result, fn.__name__)) return redirect(self.url('list')) return self.render_to_response(context)
def _process_fields(form, request): for field in form.fields.values(): if hasattr(field, 'queryset'): model = field.queryset.model field.queryset = safe_queryset_and( field.queryset, model.objects.for_access(request.access), )
def get(self, request): self.object_list = self.get_queryset() search_form = ExpenseReportSearchForm(request.GET, request=request) if not search_form.is_valid(): messages.error(request, _("The search query was invalid.")) return redirect("zivinetz_expensereport_list") self.object_list = safe_queryset_and( self.object_list, search_form.queryset(self.model) ) return generate_expense_statistics_pdf(self.object_list)
def get_queryset(self): if self.queryset is not None: return safe_queryset_and( self.queryset, self.queryset.model._default_manager.for_access( self.request.access)) elif self.model is not None: return self.model._default_manager.for_access(self.request.access) else: raise ImproperlyConfigured( "'%s' must define 'queryset' or 'model'" % self.__class__.__name__)
def get_queryset(self): if self.queryset is not None: return safe_queryset_and( self.queryset, self.queryset.model._default_manager.for_access( self.request.access)) elif self.model is not None: return self.model._default_manager.for_access( self.request.access) else: raise ImproperlyConfigured( "'%s' must define 'queryset' or 'model'" % self.__class__.__name__)
def handle_search_form(self, request, ctx, queryset=None): """ Must return a tuple consisting of a queryset and either a HttpResponse or None """ if queryset is None: queryset = self.get_query_set(request) if self.search_form: form = self.search_form(request.GET, request=request) queryset = safe_queryset_and(queryset, form.queryset(self.model)) ctx['search_form'] = form return queryset, None
def test_safe_queryset_and(self): class AnyException(Exception): pass def _transform_nothing(queryset): raise AnyException qs1 = EmailAddress.objects.search('blub').transform( _transform_nothing).select_related() qs2 = EmailAddress.objects.distinct().reverse().select_related( 'person') qs3 = EmailAddress.objects.all() qs = safe_queryset_and(safe_queryset_and(qs1, qs2), qs3) self.assertEqual(qs._transform_fns, [_transform_nothing]) self.assertFalse(qs.query.standard_ordering) self.assertEqual(qs.query.select_related, {'person': {}}) self.assertTrue(qs.query.distinct) self.assertEqual(qs.count(), 0) self.assertRaises(AnyException, list, qs) qs = safe_queryset_and( EmailAddress.objects.select_related(), EmailAddress.objects.select_related(), ) self.assertTrue(qs.query.select_related) self.assertFalse(qs.query.distinct) qs = safe_queryset_and( EmailAddress.objects.all(), EmailAddress.objects.select_related(), ) self.assertTrue(qs.query.select_related)
def test_safe_queryset_and(self): class AnyException(Exception): pass def _transform_nothing(queryset): raise AnyException qs1 = (EmailAddress.objects.search("blub").transform( _transform_nothing).select_related()) qs2 = EmailAddress.objects.distinct().reverse().select_related( "person") qs3 = EmailAddress.objects.all() qs = safe_queryset_and(safe_queryset_and(qs1, qs2), qs3) self.assertEqual(qs._transform_fns, [_transform_nothing]) self.assertFalse(qs.query.standard_ordering) self.assertEqual(qs.query.select_related, {"person": {}}) self.assertTrue(qs.query.distinct) self.assertEqual(qs.count(), 0) self.assertRaises(AnyException, list, qs) qs = safe_queryset_and( EmailAddress.objects.select_related(), EmailAddress.objects.select_related(), ) self.assertTrue(qs.query.select_related) self.assertFalse(qs.query.distinct) qs = safe_queryset_and( EmailAddress.objects.all(), EmailAddress.objects.select_related(), ) self.assertTrue(qs.query.select_related)
def get(self, request): self.object_list = self.get_queryset() search_form = AssignmentSearchForm(request.GET, request=request) if not search_form.is_valid(): messages.error(request, _("The search query was invalid.")) return redirect("zivinetz_assignment_list") self.object_list = safe_queryset_and( self.object_list, search_form.queryset(self.model) ) pdf, response = pdf_response("phones") pdf.init_report() specification = None for assignment in self.object_list.order_by("specification", "drudge"): drudge = assignment.drudge if specification != assignment.specification: pdf.h2("%s" % assignment.specification) specification = assignment.specification pdf.table( [ ( u"%s" % drudge, drudge.user.email, u"%s - %s" % ( assignment.date_from.strftime("%d.%m.%y"), assignment.determine_date_until().strftime("%d.%m.%y"), ), ), (drudge.phone_home, drudge.phone_office, drudge.mobile), ( u"%s, %s %s" % (drudge.address, drudge.zip_code, drudge.city), "", drudge.education_occupation, ), ], (6.4 * cm, 5 * cm, 5 * cm), ) pdf.hr_mini() pdf.generate() return response
def handle_search_form(self, request, ctx, queryset=None): """ Must return a tuple consisting of a queryset and either a HttpResponse or ``None`` """ if queryset is None: queryset = self.get_query_set(request) if self.search_form: form = self.search_form(request.GET, request=request) if not form.is_valid(): self.add_message(request, _('The search query was invalid.'), level=messages.ERROR) return queryset, HttpResponseRedirect('?clear=1') queryset = safe_queryset_and(queryset, form.queryset(self.model)) ctx['search_form'] = form return queryset, None
def picker(self, request): queryset = self.get_query_set(request) regions = None query = request.GET.get('query') if query is not None: queryset = safe_queryset_and(queryset, self.model.objects._search(query)) regions = {} response = self.render( request, self.get_template(request, 'picker'), self.get_context(request, { self.template_object_list_name: queryset, 'regions': regions, })) if query is not None: data = changed_regions(regions, ['object_list']) data['!keep'] = True # Keep modal open return HttpResponse(json.dumps(data), content_type='application/json') return response
def get_query_set(self): return safe_queryset_and( super(Resource, self).get_query_set(), self.model.objects.for_access(self.request.access), )