Coverage for lino/utils/choosers.py : 58%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Copyright 2009-2015 Luc Saffre # License: BSD (see file COPYING for details)
Django model.
- Context-sensitive choices - Non-limiting choices : specify a pick list of suggestions but leave the possibility to store manually entered values - :ref:`learning_combos`
TODO: compare with `django-ajax-selects <https://github.com/crucialfelix/django-ajax-selects>`_
.. _learning_combos:
Learning Comboboxes -------------------
Choosers inspect the model, and if it defines a method `create_FOO_choice`, then the chooser will become "learning": the ComboBox will be told to accept also new values, and the server will handle these cases accordingly.
"""
FieldChooser.__init__(self, field) self.simple_values = type(field.choices[0])
"""A **chooser** holds information about the possible choices of a field.
""" #~ stored_name = None
#~ self.field = model._meta.get_field(fldname) meth, 'force_selection', self.force_selection) #~ self.context_params = meth.func_code.co_varnames[1:meth.func_code.co_argcount] #~ self.multiple = meth.multiple #~ self.context_params = meth.func_code.co_varnames[:meth.func_code.co_argcount] #~ print '20100724', meth, self.context_params #~ logger.warning("20100527 %s %s",self.context_params,meth) raise Exception( "No data element '%s' in %s " "(method %s_choices)" % ( name, self.model, field.name)) #~ if name == 'p_book': #~ print 20131012, f #~ if isinstance(f,models.ForeignKey): #~ self.context_values.append(name+"Hidden") #~ else: #~ self.context_values.append(name) #~ try: #~ except models.FieldDoesNotExist,e: #~ print e
self.choice_display_method = m
return "Chooser(%s.%s,%s)" % ( self.model.__name__, self.field.name, self.context_params)
m = getattr(obj, "create_%s_choice" % self.field.name) return m(text)
"""Calls :meth:`dd.Actor.get_data_elem` or :meth:`dd.Model.get_data_elem` or :meth:`dd.Action.get_data_elem`.
""" return self.model.get_param_elem(name)
for i, v in enumerate(args): kw[self.context_fields[i]] = v return self.get_choices(**kw)
"""Return a list of choices for this chooser, using keyword parameters as context.
""" args = [] for varname in self.context_params: args.append(context.get(varname, None)) return self.meth(*args)
""" Return a list of choices for this chooser, using a HttpRequest to build the context. """ from django.contrib.contenttypes.models import ContentType kw = {}
# 20120202 if tbl.master_field is not None: rqdata = getrqdata(request) if tbl.master is not None: master = tbl.master else: mt = rqdata.get(constants.URL_PARAM_MASTER_TYPE) try: master = ContentType.objects.get(pk=mt).model_class() except ContentType.DoesNotExist: master = None
pk = rqdata.get(constants.URL_PARAM_MASTER_PK, None) if pk and master: try: kw[tbl.master_field.name] = 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("There's no %s with primary key %r" % (master.__name__, pk))
for k, v in list(request.GET.items()): kw[str(k)] = v
# logger.info( # "20130513 get_request_choices(%r) -> %r", # tbl, kw)
for cv in self.converters: kw = cv.convert(**kw)
if tbl.known_values: kw.update(tbl.known_values)
if False: # removed 20120815 #1114 #~ ar = tbl.request(ui,request,tbl.default_action) if ar.create_kw: kw.update(ar.create_kw) if ar.known_values: kw.update(ar.known_values) if tbl.master_key: kw[tbl.master_key] = ar.master_instance #~ if tbl.known_values: #~ kw.update(tbl.known_values) return self.get_choices(**kw) # 20120918b
m = getattr(self.field, 'get_text_for_value', None) if m is not None: # e.g. lino.utils.choicelist.ChoiceListField return m(value) #~ raise NotImplementedError #~ assert not self.simple_values m = getattr(obj, "get_" + self.field.name + "_display") #~ if m is None: #~ raise Exception("") return m(value) #~ raise NotImplementedError("%s : Cannot get text for value %r" % (self.meth,value))
"used by :class:`lino.core.store`" return False return True
#~ options.setdefault('quick_insert_field',None) #~ print 20101220, args return fn(*args) 1:fn.__code__.co_argcount] #~ 20120918b wrapped.context_params = fn.func_code.co_varnames[2:fn.func_code.co_argcount] # return classmethod(wrapped) # A chooser on an action must not turn it into a classmethod
"Decorator which turns the method into a chooser."
return x
return _chooser(noop, **options) |