def put(self, request, app_label=None, actor=None): ui = settings.SITE.ui rpt = requested_actor(app_label, actor) PUT = http.QueryDict(request.body) # raw_post_data before Django 1.4 gc = dict( widths=[int(x) for x in PUT.getlist(ext_requests.URL_PARAM_WIDTHS)], columns=[str(x) for x in PUT.getlist(ext_requests.URL_PARAM_COLUMNS)], hiddens=[(x == 'true') for x in PUT.getlist(ext_requests.URL_PARAM_HIDDENS)], #~ hidden_cols=[str(x) for x in PUT.getlist('hidden_cols')], ) filter = PUT.get('filter', None) if filter is not None: filter = json.loads(filter) gc['filters'] = [ext_requests.dict2kw(flt) for flt in filter] name = PUT.get('name', None) if name is None: name = ext_elems.DEFAULT_GC_NAME else: name = int(name) gc.update(label=PUT.get('label', "Standard")) try: msg = rpt.save_grid_config(name, gc) except IOError, e: msg = _("Error while saving GC for %(table)s: %(error)s") % dict( table=rpt, error=e) return settings.SITE.ui.error(None, msg, alert=True)
def put(self, request, app_label=None, actor=None): rpt = requested_actor(app_label, actor) PUT = http.QueryDict(request.body) # raw_post_data before Django 1.4 gc = dict( widths=[int(x) for x in PUT.getlist(constants.URL_PARAM_WIDTHS)], columns=[str(x) for x in PUT.getlist(constants.URL_PARAM_COLUMNS)], hiddens=[(x == 'true') for x in PUT.getlist(constants.URL_PARAM_HIDDENS)], #~ hidden_cols=[str(x) for x in PUT.getlist('hidden_cols')], ) filter = PUT.get('filter', None) if filter is not None: filter = json.loads(filter) gc['filters'] = [constants.dict2kw(flt) for flt in filter] name = PUT.get('name', None) if name is None: name = constants.DEFAULT_GC_NAME else: name = int(name) gc.update(label=PUT.get('label', "Standard")) try: msg = rpt.save_grid_config(name, gc) except IOError as e: msg = _("Error while saving GC for %(table)s: %(error)s") % dict( table=rpt, error=e) return settings.SITE.kernel.error(None, msg, alert=True) #~ logger.info(msg) settings.SITE.kernel.extjs_renderer.build_site_cache(True) return settings.SITE.kernel.success(msg)
def parse_req(self, request, rqdata, **kw): """Parse the incoming HttpRequest and translate it into keyword arguments to be used by :meth:`setup`. The `mt` url param is parsed only when needed. Usually it is not needed because the `master_class` is constant and known per actor. But there are exceptions: - `master` is `ContentType` - `master` is some abstract model - `master` is not a subclass of Model, e.g. :class:`lino_xl.lib.polls.models.AnswersByResponse`, a virtual table which defines :meth:`get_row_by_pk <lino.core.actors.Actor.get_row_by_pk>`. """ # logger.info("20120723 %s.parse_req() %s", self.actor, rqdata) # ~ rh = self.ah if "master_instance" not in kw: kw.update(master_mt=rqdata.get(constants.URL_PARAM_MASTER_TYPE, None), master_mk=rqdata.get(constants.URL_PARAM_MASTER_PK, None)) if settings.SITE.use_filterRow: exclude = dict() for f in self.ah.store.fields: if f.field: filterOption = rqdata.get('filter[%s_filterOption]' % f.field.name) if filterOption == 'empty': kw[f.field.name + "__isnull"] = True elif filterOption == 'notempty': kw[f.field.name + "__isnull"] = False else: filterValue = rqdata.get('filter[%s]' % f.field.name) if filterValue: if not filterOption: filterOption = 'contains' if filterOption == 'contains': kw[f.field.name + "__icontains"] = filterValue elif filterOption == 'doesnotcontain': exclude[f.field.name + "__icontains"] = filterValue else: print("unknown filterOption %r" % filterOption) if len(exclude): kw.update(exclude=exclude) if settings.SITE.use_gridfilters: filter = rqdata.get(constants.URL_PARAM_GRIDFILTER, None) if filter is not None: filter = json.loads(filter) kw['gridfilters'] = [constants.dict2kw(flt) for flt in filter] kw = ActionRequest.parse_req(self, request, rqdata, **kw) # ~ kw.update(self.report.known_values) # ~ for fieldname, default in self.report.known_values.items(): # ~ v = request.REQUEST.get(fieldname,None) # ~ if v is not None: # ~ kw[fieldname] = v quick_search = rqdata.get(constants.URL_PARAM_FILTER, None) if quick_search: kw.update(quick_search=quick_search) sort = rqdata.get(constants.URL_PARAM_SORT, None) if sort: sortfld = self.actor.get_data_elem(sort) if isinstance(sortfld, FakeField): sort = sortfld.sortable_by # sort might be None when user asked to sort a virtual # field without sortable_by. else: sort = [sort] if sort is not None: def si(k): if k[0] == '-': return k[1:] else: return '-' + k sort_dir = rqdata.get(constants.URL_PARAM_SORTDIR, 'ASC') if sort_dir == 'DESC': sort = [si(k) for k in sort] # sort = ['-' + k for k in sort] # print("20171123", sort) kw.update(order_by=sort) try: offset = rqdata.get(constants.URL_PARAM_START, None) if offset: kw.update(offset=int(offset)) limit = rqdata.get(constants.URL_PARAM_LIMIT, self.actor.preview_limit) if limit: kw.update(limit=int(limit)) except ValueError: # Example: invalid literal for int() with base 10: # 'fdpkvcnrfdybhur' raise SuspiciousOperation("Invalid value for limit or offset") kw = self.actor.parse_req(request, rqdata, **kw) # print("20171123 %s.parse_req() --> %s" % (self, kw)) return kw
if filterValue: if not filterOption: filterOption = 'contains' if filterOption == 'contains': kw[f.field.name + "__icontains"] = filterValue elif filterOption == 'doesnotcontain': exclude[f.field.name + "__icontains"] = filterValue else: print "unknown filterOption %r" % filterOption if len(exclude): kw.update(exclude=exclude) if settings.SITE.use_gridfilters: filter = rqdata.get(constants.URL_PARAM_GRIDFILTER,None) if filter is not None: filter = json.loads(filter) kw['gridfilters'] = [constants.dict2kw(flt) for flt in filter] kw = ActionRequest.parse_req(self,request,rqdata,**kw) #~ raise Exception("20120121 %s.parse_req(%s)" % (self,kw)) #~ kw.update(self.report.known_values) #~ for fieldname, default in self.report.known_values.items(): #~ v = request.REQUEST.get(fieldname,None) #~ if v is not None: #~ kw[fieldname] = v quick_search = rqdata.get(constants.URL_PARAM_FILTER,None) if quick_search: kw.update(quick_search=quick_search) sort = rqdata.get(constants.URL_PARAM_SORT,None)
def parse_req(self, request, rqdata, **kw): """Parse the incoming HttpRequest and translate it into keyword arguments to be used by :meth:`setup`. The `mt` url param is parsed only when needed. Usually it is not needed because the `master_class` is constant and known per actor. But there are exceptions: - `master` is `ContentType` - `master` is some abstract model - `master` is not a subclass of Model, e.g. :class:`lino.modlib.polls.models.AnswersByResponse`, a virtual table which defines :meth:`get_row_by_pk <lino.core.actors.Actor.get_row_by_pk>`. """ #~ logger.info("20120723 %s.parse_req()",self.actor) #~ rh = self.ah master = kw.get('master', self.actor.master) if master is not None: if not isinstance(master, type): raise Exception("20150216 not a type: %r" % master) if issubclass(master, models.Model) and ( master is ContentType or master._meta.abstract): mt = rqdata.get(constants.URL_PARAM_MASTER_TYPE) try: master = kw['master'] = ContentType.objects.get( pk=mt).model_class() except ContentType.DoesNotExist: pass # master is None if 'master_instance' not in kw: pk = rqdata.get(constants.URL_PARAM_MASTER_PK, None) #~ print '20100406a', self.actor,URL_PARAM_MASTER_PK,"=",pk #~ if pk in ('', '-99999'): if pk == '': pk = None if pk is None: kw['master_instance'] = None else: mi = self.actor.get_master_instance(self, master, pk) if mi is None: raise ObjectDoesNotExist( "Invalid master key {0} for {1}".format( pk, self.actor)) kw['master_instance'] = mi # ~ print '20100212', self #, kw['master_instance'] #~ print '20100406b', self.actor,kw if settings.SITE.use_filterRow: exclude = dict() for f in self.ah.store.fields: if f.field: filterOption = rqdata.get( 'filter[%s_filterOption]' % f.field.name) if filterOption == 'empty': kw[f.field.name + "__isnull"] = True elif filterOption == 'notempty': kw[f.field.name + "__isnull"] = False else: filterValue = rqdata.get('filter[%s]' % f.field.name) if filterValue: if not filterOption: filterOption = 'contains' if filterOption == 'contains': kw[f.field.name + "__icontains"] = filterValue elif filterOption == 'doesnotcontain': exclude[f.field.name + "__icontains"] = filterValue else: print "unknown filterOption %r" % filterOption if len(exclude): kw.update(exclude=exclude) if settings.SITE.use_gridfilters: filter = rqdata.get(constants.URL_PARAM_GRIDFILTER, None) if filter is not None: filter = json.loads(filter) kw['gridfilters'] = [constants.dict2kw(flt) for flt in filter] kw = ActionRequest.parse_req(self, request, rqdata, **kw) #~ raise Exception("20120121 %s.parse_req(%s)" % (self,kw)) #~ kw.update(self.report.known_values) #~ for fieldname, default in self.report.known_values.items(): #~ v = request.REQUEST.get(fieldname,None) #~ if v is not None: #~ kw[fieldname] = v quick_search = rqdata.get(constants.URL_PARAM_FILTER, None) if quick_search: kw.update(quick_search=quick_search) sort = rqdata.get(constants.URL_PARAM_SORT, None) if sort: #~ self.sort_column = sort sort_dir = rqdata.get(constants.URL_PARAM_SORTDIR, 'ASC') if sort_dir == 'DESC': sort = '-' + sort #~ self.sort_direction = 'DESC' kw.update(order_by=[sort]) offset = rqdata.get(constants.URL_PARAM_START, None) if offset: kw.update(offset=int(offset)) #~ limit = rqdata.get(constants.URL_PARAM_LIMIT,None) limit = rqdata.get(constants.URL_PARAM_LIMIT, self.actor.preview_limit) if limit: kw.update(limit=int(limit)) return self.actor.parse_req(request, rqdata, **kw)
def parse_req(self, request, rqdata, **kw): """Parse the incoming HttpRequest and translate it into keyword arguments to be used by :meth:`setup`. The `mt` url param is parsed only when needed. Usually it is not needed because the `master_class` is constant and known per actor. But there are exceptions: - `master` is `ContentType` - `master` is some abstract model - `master` is not a subclass of Model, e.g. :class:`lino.modlib.polls.models.AnswersByResponse`, a virtual table which defines :meth:`get_row_by_pk <lino.core.actors.Actor.get_row_by_pk>`. """ #~ logger.info("20120723 %s.parse_req()",self.actor) #~ rh = self.ah master = kw.get('master', self.actor.master) if master is not None: if not isinstance(master, type): raise Exception("20150216 not a type: %r" % master) if issubclass(master, models.Model) and (master is ContentType or master._meta.abstract): mt = rqdata.get(constants.URL_PARAM_MASTER_TYPE) try: master = kw['master'] = ContentType.objects.get( pk=mt).model_class() except ContentType.DoesNotExist: pass # master is None if 'master_instance' not in kw: pk = rqdata.get(constants.URL_PARAM_MASTER_PK, None) #~ print '20100406a', self.actor,URL_PARAM_MASTER_PK,"=",pk #~ if pk in ('', '-99999'): if pk == '': pk = None if pk is None: kw['master_instance'] = None else: mi = self.actor.get_master_instance(self, master, pk) if mi is None: raise ObjectDoesNotExist( "Invalid master key {0} for {1}".format( pk, self.actor)) kw['master_instance'] = mi # ~ print '20100212', self #, kw['master_instance'] #~ print '20100406b', self.actor,kw if settings.SITE.use_filterRow: exclude = dict() for f in self.ah.store.fields: if f.field: filterOption = rqdata.get('filter[%s_filterOption]' % f.field.name) if filterOption == 'empty': kw[f.field.name + "__isnull"] = True elif filterOption == 'notempty': kw[f.field.name + "__isnull"] = False else: filterValue = rqdata.get('filter[%s]' % f.field.name) if filterValue: if not filterOption: filterOption = 'contains' if filterOption == 'contains': kw[f.field.name + "__icontains"] = filterValue elif filterOption == 'doesnotcontain': exclude[f.field.name + "__icontains"] = filterValue else: print "unknown filterOption %r" % filterOption if len(exclude): kw.update(exclude=exclude) if settings.SITE.use_gridfilters: filter = rqdata.get(constants.URL_PARAM_GRIDFILTER, None) if filter is not None: filter = json.loads(filter) kw['gridfilters'] = [constants.dict2kw(flt) for flt in filter] kw = ActionRequest.parse_req(self, request, rqdata, **kw) #~ raise Exception("20120121 %s.parse_req(%s)" % (self,kw)) #~ kw.update(self.report.known_values) #~ for fieldname, default in self.report.known_values.items(): #~ v = request.REQUEST.get(fieldname,None) #~ if v is not None: #~ kw[fieldname] = v quick_search = rqdata.get(constants.URL_PARAM_FILTER, None) if quick_search: kw.update(quick_search=quick_search) sort = rqdata.get(constants.URL_PARAM_SORT, None) if sort: #~ self.sort_column = sort sort_dir = rqdata.get(constants.URL_PARAM_SORTDIR, 'ASC') if sort_dir == 'DESC': sort = '-' + sort #~ self.sort_direction = 'DESC' kw.update(order_by=[sort]) offset = rqdata.get(constants.URL_PARAM_START, None) if offset: kw.update(offset=int(offset)) #~ limit = rqdata.get(constants.URL_PARAM_LIMIT,None) limit = rqdata.get(constants.URL_PARAM_LIMIT, self.actor.preview_limit) if limit: kw.update(limit=int(limit)) return self.actor.parse_req(request, rqdata, **kw)
def parse_req(self, request, rqdata, **kw): #~ logger.info("20120723 %s.parse_req()",self.actor) #~ rh = self.ah master = kw.get('master', self.actor.master) if master is not None: """ If `master` is `ContentType` or some abstract model, then """ #~ if master is ContentType or master is models.Model: if master is ContentType or master._meta.abstract: mt = rqdata.get(constants.URL_PARAM_MASTER_TYPE) try: master = kw['master'] = ContentType.objects.get( pk=mt).model_class() except ContentType.DoesNotExist: pass # master is None if not 'master_instance' in kw: pk = rqdata.get(constants.URL_PARAM_MASTER_PK, None) #~ print '20100406a', self.actor,URL_PARAM_MASTER_PK,"=",pk #~ if pk in ('', '-99999'): if pk == '': pk = None if pk is None: kw['master_instance'] = None else: try: kw['master_instance'] = master.objects.get(pk=pk) except ValueError: raise Exception( "Invalid primary key %r for %s", pk, master.__name__) except master.DoesNotExist: # todo: ReportRequest should become a subclass of # Dialog and this exception should call dlg.error() raise Exception( "%s : There's no %s with primary key %r" % (self.actor, master.__name__, pk)) # ~ print '20100212', self #, kw['master_instance'] #~ print '20100406b', self.actor,kw if settings.SITE.use_filterRow: exclude = dict() for f in self.ah.store.fields: if f.field: filterOption = rqdata.get( 'filter[%s_filterOption]' % f.field.name) if filterOption == 'empty': kw[f.field.name + "__isnull"] = True elif filterOption == 'notempty': kw[f.field.name + "__isnull"] = False else: filterValue = rqdata.get('filter[%s]' % f.field.name) if filterValue: if not filterOption: filterOption = 'contains' if filterOption == 'contains': kw[f.field.name + "__icontains"] = filterValue elif filterOption == 'doesnotcontain': exclude[f.field.name + "__icontains"] = filterValue else: print "unknown filterOption %r" % filterOption if len(exclude): kw.update(exclude=exclude) if settings.SITE.use_gridfilters: filter = rqdata.get(constants.URL_PARAM_GRIDFILTER, None) if filter is not None: filter = json.loads(filter) kw['gridfilters'] = [constants.dict2kw(flt) for flt in filter] kw = ActionRequest.parse_req(self, request, rqdata, **kw) #~ raise Exception("20120121 %s.parse_req(%s)" % (self,kw)) #~ kw.update(self.report.known_values) #~ for fieldname, default in self.report.known_values.items(): #~ v = request.REQUEST.get(fieldname,None) #~ if v is not None: #~ kw[fieldname] = v quick_search = rqdata.get(constants.URL_PARAM_FILTER, None) if quick_search: kw.update(quick_search=quick_search) sort = rqdata.get(constants.URL_PARAM_SORT, None) if sort: #~ self.sort_column = sort sort_dir = rqdata.get(constants.URL_PARAM_SORTDIR, 'ASC') if sort_dir == 'DESC': sort = '-' + sort #~ self.sort_direction = 'DESC' kw.update(order_by=[sort]) offset = rqdata.get(constants.URL_PARAM_START, None) if offset: kw.update(offset=int(offset)) #~ limit = rqdata.get(constants.URL_PARAM_LIMIT,None) limit = rqdata.get(constants.URL_PARAM_LIMIT, self.actor.preview_limit) if limit: kw.update(limit=int(limit)) return self.actor.parse_req(request, rqdata, **kw)
def parse_req(self, request, rqdata, **kw): """Parse the incoming HttpRequest and translate it into keyword arguments to be used by :meth:`setup`. The `mt` url param is parsed only when needed. Usually it is not needed because the `master_class` is constant and known per actor. But there are exceptions: - `master` is `ContentType` - `master` is some abstract model - `master` is not a subclass of Model, e.g. :class:`lino_xl.lib.polls.models.AnswersByResponse`, a virtual table which defines :meth:`get_row_by_pk <lino.core.actors.Actor.get_row_by_pk>`. """ # logger.info("20120723 %s.parse_req() %s", self.actor, rqdata) #~ rh = self.ah master = kw.get('master', self.actor.master) if master is not None: if not isinstance(master, type): raise Exception("20150216 not a type: %r" % master) if settings.SITE.is_installed('contenttypes'): from django.contrib.contenttypes.models import ContentType if issubclass(master, models.Model) and ( master is ContentType or master._meta.abstract): mt = rqdata.get(constants.URL_PARAM_MASTER_TYPE) try: master = kw['master'] = ContentType.objects.get( pk=mt).model_class() except ContentType.DoesNotExist: pass # master is None if 'master_instance' not in kw: pk = rqdata.get(constants.URL_PARAM_MASTER_PK, None) #~ print '20100406a', self.actor,URL_PARAM_MASTER_PK,"=",pk #~ if pk in ('', '-99999'): if pk == '': pk = None if pk is None: kw['master_instance'] = None else: mi = self.actor.get_master_instance(self, master, pk) if mi is None: raise ObjectDoesNotExist( "Invalid master key {0} for {1}".format( pk, self.actor)) kw['master_instance'] = mi # ~ print '20100212', self #, kw['master_instance'] #~ print '20100406b', self.actor,kw if settings.SITE.use_filterRow: exclude = dict() for f in self.ah.store.fields: if f.field: filterOption = rqdata.get( 'filter[%s_filterOption]' % f.field.name) if filterOption == 'empty': kw[f.field.name + "__isnull"] = True elif filterOption == 'notempty': kw[f.field.name + "__isnull"] = False else: filterValue = rqdata.get('filter[%s]' % f.field.name) if filterValue: if not filterOption: filterOption = 'contains' if filterOption == 'contains': kw[f.field.name + "__icontains"] = filterValue elif filterOption == 'doesnotcontain': exclude[f.field.name + "__icontains"] = filterValue else: print("unknown filterOption %r" % filterOption) if len(exclude): kw.update(exclude=exclude) if settings.SITE.use_gridfilters: filter = rqdata.get(constants.URL_PARAM_GRIDFILTER, None) if filter is not None: filter = json.loads(filter) kw['gridfilters'] = [constants.dict2kw(flt) for flt in filter] kw = ActionRequest.parse_req(self, request, rqdata, **kw) #~ kw.update(self.report.known_values) #~ for fieldname, default in self.report.known_values.items(): #~ v = request.REQUEST.get(fieldname,None) #~ if v is not None: #~ kw[fieldname] = v quick_search = rqdata.get(constants.URL_PARAM_FILTER, None) if quick_search: kw.update(quick_search=quick_search) sort = rqdata.get(constants.URL_PARAM_SORT, None) if sort: sortfld = self.actor.get_data_elem(sort) if isinstance(sortfld, FakeField): sort = sortfld.sortable_by # sort might be None when user asked to sort a virtual # field without sortable_by. else: sort = [sort] if sort is not None: def si(k): if k[0] == '-': return k[1:] else: return '-' + k sort_dir = rqdata.get(constants.URL_PARAM_SORTDIR, 'ASC') if sort_dir == 'DESC': sort = [si(k) for k in sort] # sort = ['-' + k for k in sort] # print("20171123", sort) kw.update(order_by=sort) try: offset = rqdata.get(constants.URL_PARAM_START, None) if offset: kw.update(offset=int(offset)) limit = rqdata.get( constants.URL_PARAM_LIMIT, self.actor.preview_limit) if limit: kw.update(limit=int(limit)) except ValueError: # Example: invalid literal for int() with base 10: # 'fdpkvcnrfdybhur' raise SuspiciousOperation("Invalid value for limit or offset") kw = self.actor.parse_req(request, rqdata, **kw) # print("20171123 %s.parse_req() --> %s" % (self, kw)) return kw