Beispiel #1
0
    def get_request_choices(self, request, tbl):
        """
        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:
            if tbl.master is not None:
                master = tbl.master
            else:
                rqdata = getrqdata(request)
                mt = rqdata.get(constants.URL_PARAM_MASTER_TYPE)
                try:
                    master = ContentType.objects.get(pk=mt).model_class()
                except ContentType.DoesNotExist:
                    master = None

            rqdata = getrqdata(request)
            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 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
Beispiel #2
0
    def get_request_choices(self, ar, tbl):
        """
        Return a list of choices for this chooser,
        using a HttpRequest to build the context.
        """
        kw = {
            "ar": ar,
        }
        # kw = {}

        # ba = tbl.get_url_action(tbl.default_elem_action_name)
        # 20120202
        if tbl.master_field is not None:
            from django.contrib.contenttypes.models import ContentType
            rqdata = getrqdata(ar.request)
            if tbl.master is not None:
                master = tbl.master
            else:
                mt = rqdata.get(constants.URL_PARAM_MASTER_TYPE)
                # ContentType = rt.models.contenttypes.ContentType
                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:
                    raise Exception("There's no %s with primary key %r" %
                                    (master.__name__, pk))

        for k, v in list(ar.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
Beispiel #3
0
    def parse_params(self, request, **kw):
        data = getrqdata(request)
        # print(20160329, data)
        # assert 'pv' in data
        pv = data.getlist(self.url_param) #'fv', 'pv', post[fn] post[fv][fn]
        # logger.info("20120221 ParameterStore.parse_params(%s) --> %s",self.url_param,pv)

        def parse(sf, form_value):
            if form_value == '' and not sf.field.empty_strings_allowed:
                return sf.form2obj_default
                # When a field has been posted with empty string, we
                # don't want it to get the field's default value
                # because otherwise checkboxes with default value True
                # can never be unset.  charfields have
                # empty_strings_allowed e.g. id field may be empty.
                # But don't do this for other cases.
            else:
                return sf.parse_form_value(form_value, None)

        if len(pv) > 0:
            if len(self.param_fields) != len(pv):
                raise Exception(
                    "%s expects a list of %d values but got %d: %s" % (
                        self, len(self.param_fields), len(pv), pv))
            for i, f in enumerate(self.param_fields):
                kw[f.field.name] = parse(f, pv[i])
        elif self.url_param == "fv":
            # try to get data from dict style in main body of request
            for i, f in enumerate(self.param_fields):
                if f.name + "Hidden" in data:
                    kw[f.name] = parse(f, data[f.name + "Hidden"])
                elif f.name in data:
                    kw[f.name] = parse(f, data[f.name])
        # print(20160329, kw)
        return kw
Beispiel #4
0
    def parse_params(self, request, **kw):
        data = getrqdata(request)
        pv = data.getlist(self.url_param)
        #~ logger.info("20120221 ParameterStore.parse_params(%s) --> %s",self.url_param,pv)

        def parse(sf, form_value):
            if form_value == '' and not sf.field.empty_strings_allowed:
                return sf.form2obj_default
                # When a field has been posted with empty string, we
                # don't want it to get the field's default value
                # because otherwise checkboxes with default value True
                # can never be unset.  charfields have
                # empty_strings_allowed e.g. id field may be empty.
                # But don't do this for other cases.
            else:
                return sf.parse_form_value(form_value, None)

        if len(pv) > 0:
            if len(self.param_fields) != len(pv):
                raise Exception(
                    "%s expects a list of %d values but got %d: %s" % (
                        self, len(self.param_fields), len(pv), pv))
            for i, f in enumerate(self.param_fields):
                kw[f.field.name] = parse(f, pv[i])
        return kw
Beispiel #5
0
    def parse_params(self, request, **kw):
        data = getrqdata(request)
        # print(20160329, data)
        # assert 'pv' in data
        pv = data.getlist(self.url_param)

        # logger.info("20120221 ParameterStore.parse_params(%s) --> %s",self.url_param,pv)

        def parse(sf, form_value):
            if form_value == '' and not sf.field.empty_strings_allowed:
                return sf.form2obj_default
                # When a field has been posted with empty string, we
                # don't want it to get the field's default value
                # because otherwise checkboxes with default value True
                # can never be unset.  charfields have
                # empty_strings_allowed e.g. id field may be empty.
                # But don't do this for other cases.
            else:
                return sf.parse_form_value(form_value, None)

        if len(pv) > 0:
            if len(self.param_fields) != len(pv):
                raise Exception(
                    "%s expects a list of %d values but got %d: %s" %
                    (self, len(self.param_fields), len(pv), pv))
            for i, f in enumerate(self.param_fields):
                kw[f.field.name] = parse(f, pv[i])
        # print(20160329, kw)
        return kw
Beispiel #6
0
    def __init__(self, request=None, parent=None, is_on_main_actor=True, **kw):
        self.request = request
        self.response = dict()
        if request is not None:
            rqdata = getrqdata(request)
            kw = self.parse_req(request, rqdata, **kw)
        if parent is not None:
            self._confirm_answer = parent._confirm_answer
            for k in inheritable_attrs:
                if k in kw:
                    if kw[k] is None:
                        raise Exception("%s : %s=None" % (kw, k))
                else:
                    kw[k] = getattr(parent, k)
            kv = kw.setdefault('known_values', {})
            kv.update(parent.known_values)
            # kw.setdefault('user', parent.user)
            # kw.setdefault('subst_user', parent.subst_user)
            # kw.setdefault('renderer', parent.renderer)
            # kw.setdefault('requesting_panel', parent.requesting_panel)
            # if not parent.is_on_main_actor or parent.actor != kw.get('actor', None):
            if not parent.is_on_main_actor:
                is_on_main_actor = False
            elif parent.actor is not None and parent.actor is not self.actor:
                is_on_main_actor = False
            # is_on_main_actor = False
        self.is_on_main_actor = is_on_main_actor

        self.setup(**kw)
Beispiel #7
0
    def get_request_choices(self, ar, tbl):
        """
        Return a list of choices for this chooser,
        using a HttpRequest to build the context.
        """
        kw = {
            "ar": ar,
        }
        # kw = {}

        # ba = tbl.get_url_action(tbl.default_elem_action_name)
        # 20120202
        if tbl.master_field is not None:
            rqdata = getrqdata(ar.request)
            if tbl.master is not None:
                master = tbl.master
            else:
                mt = rqdata.get(constants.URL_PARAM_MASTER_TYPE)
                ContentType = rt.models.contenttypes.ContentType
                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:
                    raise Exception("There's no %s with primary key %r" %
                                    (master.__name__, pk))

        for k, v in list(ar.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
Beispiel #8
0
    def parse_params(self, request, **kw):
        data = getrqdata(request)
        # print(20160329, data)
        # assert 'pv' in data
        pv = data.getlist(self.url_param)  #'fv', 'pv', post[fn] post[fv][fn]

        # logger.info("20120221 ParameterStore.parse_params(%s) --> %s",self.url_param,pv)

        def parse(sf, form_value):
            if form_value == '' and not sf.field.empty_strings_allowed:
                return sf.form2obj_default
                # When a field has been posted with empty string, we
                # don't want it to get the field's default value
                # because otherwise checkboxes with default value True
                # can never be unset.  charfields have
                # empty_strings_allowed e.g. id field may be empty.
                # But don't do this for other cases.
            elif form_value in (constants.CHOICES_BLANK_FILTER_VALUE,
                                constants.CHOICES_NOT_BLANK_FILTER_VALUE):
                return form_value
            else:
                return sf.parse_form_value(form_value, None)

        if len(pv) > 0:
            if len(self.param_fields) != len(pv):
                raise Exception(
                    "%s expects a list of %d values but got %d: %s" %
                    (self, len(self.param_fields), len(pv), pv))
            for i, f in enumerate(self.param_fields):
                # kw[f.field.name] = parse(f, pv[i])
                kw[f.name] = parse(
                    f, pv[i])  # 20200423 support remote fields in parameters
        elif self.url_param == "fv":
            # try to get data from dict style in main body of request
            for i, f in enumerate(self.param_fields):
                if f.name + "Hidden" in data:
                    kw[f.name] = parse(f, data[f.name + "Hidden"])
                elif f.name in data:
                    kw[f.name] = parse(f, data[f.name])
        # print(20160329, kw)
        return kw
Beispiel #9
0
 def __init__(self, request=None, parent=None, **kw):
     self.request = request
     self.response = dict()
     if request is not None:
         rqdata = getrqdata(request)
         kw = self.parse_req(request, rqdata, **kw)
     if parent is not None:
         for k in inheritable_attrs:
             if k in kw:
                 if kw[k] is None:
                     raise Exception("%s : %s=None" % (kw, k))
             else:
                 kw[k] = getattr(parent, k)
         kv = kw.setdefault('known_values', {})
         kv.update(parent.known_values)
         # kw.setdefault('user', parent.user)
         # kw.setdefault('subst_user', parent.subst_user)
         # kw.setdefault('renderer', parent.renderer)
         # kw.setdefault('requesting_panel', parent.requesting_panel)
         
     self.setup(**kw)
Beispiel #10
0
    def __init__(self, request=None, parent=None, **kw):
        self.request = request
        self.response = dict()
        if request is not None:
            rqdata = getrqdata(request)
            kw = self.parse_req(request, rqdata, **kw)
        if parent is not None:
            for k in inheritable_attrs:
                if k in kw:
                    if kw[k] is None:
                        raise Exception("%s : %s=None" % (kw, k))
                else:
                    kw[k] = getattr(parent, k)
            kv = kw.setdefault('known_values', {})
            kv.update(parent.known_values)
            # kw.setdefault('user', parent.user)
            # kw.setdefault('subst_user', parent.subst_user)
            # kw.setdefault('renderer', parent.renderer)
            # kw.setdefault('requesting_panel', parent.requesting_panel)

        self.setup(**kw)
Beispiel #11
0
        def getit():

            if ar.request is None:
                columns = None
            else:
                data = getrqdata(ar.request)
                columns = [
                    str(x) for x in data.getlist(constants.URL_PARAM_COLUMNS)
                ]
            if columns:
                all_widths = data.getlist(constants.URL_PARAM_WIDTHS)
                hiddens = [(x == 'true')
                           for x in data.getlist(constants.URL_PARAM_HIDDENS)]
                fields = []
                widths = []
                ah = ar.actor.get_handle()
                for i, cn in enumerate(columns):
                    col = None
                    for e in ah.list_layout.main.columns:
                        if e.name == cn:
                            col = e
                            break
                    if col is None:
                        raise Exception("No column named %r in %s" %
                                        (cn, ar.ah.list_layout.main.columns))
                    if not hiddens[i]:
                        fields.append(col)
                        widths.append(int(all_widths[i]))
            else:
                if column_names:
                    ll = ColumnsLayout(column_names, datasource=ar.actor)
                    lh = ll.get_layout_handle(settings.SITE.kernel.default_ui)
                    columns = lh.main.columns
                    columns = [e for e in columns if not e.hidden]
                else:
                    ah = ar.actor.get_request_handle(ar)

                    columns = ah.list_layout.main.columns
                    # print(20160530, ah, columns, ah.list_layout.main)

                # render them so that babelfields in hidden_languages
                # get hidden:
                for e in columns:
                    e.value = e.ext_options()
                    # try:
                    #     e.value = e.ext_options()
                    # except AttributeError as ex:
                    #     raise AttributeError("20160529 %s : %s" % (e, ex))
                #
                columns = [
                    e for e in columns if not e.value.get('hidden', False)
                ]

                columns = [e for e in columns if not e.hidden]

                # if str(ar.actor) == "isip.ExamPolicies":
                # from lino.modlib.extjs.elems import is_hidden_babel_field
                # print("20180103", [c.name for c in columns])
                # print("20180103", [c.field for c in columns])
                # print("20180103", [c.value['hidden'] for c in columns])
                # print("20180103", [
                #     is_hidden_babel_field(c.field) for c in columns])
                # print("20180103", [
                #     getattr(c.field, '_babel_language', None)
                #     for c in columns])
                widths = [
                    "%d" % (col.width or col.preferred_width)
                    for col in columns
                ]
                # print("20180831 {}".format(widths))
                # ~ 20130415 widths = ["%d%%" % (col.width or col.preferred_width) for col in columns]
                # ~ fields = [col.field._lino_atomizer for col in columns]
                fields = columns

            headers = [column_header(col) for col in fields]

            # if str(ar.actor).endswith("DailySlave"):
            #     print("20181022", fields[0].field.verbose_name)

            oh = ar.actor.override_column_headers(ar)
            if oh:
                for i, e in enumerate(columns):
                    header = oh.get(e.name, None)
                    if header is not None:
                        headers[i] = header
                # ~ print 20120507, oh, headers

            return fields, headers, widths
Beispiel #12
0
        def getit():

            if ar.request is None:
                columns = None
            else:
                data = getrqdata(ar.request)
                columns = [
                    str(x) for x in data.getlist(constants.URL_PARAM_COLUMNS)]
            if columns:
                all_widths = data.getlist(constants.URL_PARAM_WIDTHS)
                hiddens = [(x == 'true') for x in data.getlist(
                    constants.URL_PARAM_HIDDENS)]
                fields = []
                widths = []
                ah = ar.actor.get_handle()
                for i, cn in enumerate(columns):
                    col = None
                    for e in ah.list_layout.main.columns:
                        if e.name == cn:
                            col = e
                            break
                    if col is None:
                        raise Exception("No column named %r in %s" %
                                        (cn, ar.ah.list_layout.main.columns))
                    if not hiddens[i]:
                        fields.append(col)
                        widths.append(int(all_widths[i]))
            else:
                if column_names:
                    from lino.core.layouts import ColumnsLayout
                    ll = ColumnsLayout(column_names, datasource=ar.actor)
                    lh = ll.get_layout_handle(settings.SITE.kernel.default_ui)
                    columns = lh.main.columns
                    columns = [e for e in columns if not e.hidden]
                else:
                    ah = ar.actor.get_request_handle(ar)
                    columns = ah.list_layout.main.columns

                # render them so that babelfields in hidden_languages
                # get hidden:
                for e in columns:
                    e.value = e.ext_options()
                #
                columns = [e for e in columns if not
                           e.value.get('hidden', False)]

                columns = [e for e in columns if not e.hidden]

                widths = ["%d" % (col.width or col.preferred_width)
                          for col in columns]
                #~ 20130415 widths = ["%d%%" % (col.width or col.preferred_width) for col in columns]
                #~ fields = [col.field._lino_atomizer for col in columns]
                fields = columns

            headers = [column_header(col) for col in fields]

            oh = ar.actor.override_column_headers(ar)
            if oh:
                for i, e in enumerate(columns):
                    header = oh.get(e.name, None)
                    if header is not None:
                        headers[i] = header
                #~ print 20120507, oh, headers

            return fields, headers, widths
Beispiel #13
0
        def getit():

            if ar.request is None:
                columns = None
            else:
                data = getrqdata(ar.request)
                columns = [
                    six.text_type(x) for x in
                    data.getlist(constants.URL_PARAM_COLUMNS)]
            if columns:
                all_widths = data.getlist(constants.URL_PARAM_WIDTHS)
                hiddens = [(x == 'true') for x in data.getlist(
                    constants.URL_PARAM_HIDDENS)]
                fields = []
                widths = []
                ah = ar.actor.get_handle()
                for i, cn in enumerate(columns):
                    col = None
                    for e in ah.list_layout.main.columns:
                        if e.name == cn:
                            col = e
                            break
                    if col is None:
                        raise Exception("No column named %r in %s" %
                                        (cn, ar.ah.list_layout.main.columns))
                    if not hiddens[i]:
                        fields.append(col)
                        widths.append(int(all_widths[i]))
            else:
                if column_names:
                    ll = ColumnsLayout(column_names, datasource=ar.actor)
                    lh = ll.get_layout_handle(settings.SITE.kernel.default_ui)
                    columns = lh.main.columns
                    columns = [e for e in columns if not e.hidden]
                else:
                    ah = ar.actor.get_request_handle(ar)
                    
                    columns = ah.list_layout.main.columns
                    # print(20160530, ah, columns, ah.list_layout.main)

                # render them so that babelfields in hidden_languages
                # get hidden:
                for e in columns:
                    e.value = e.ext_options()
                    # try:
                    #     e.value = e.ext_options()
                    # except AttributeError as ex:
                    #     raise AttributeError("20160529 %s : %s" % (e, ex))
                #
                columns = [e for e in columns if not
                           e.value.get('hidden', False)]

                columns = [e for e in columns if not e.hidden]

                widths = ["%d" % (col.width or col.preferred_width)
                          for col in columns]
                #~ 20130415 widths = ["%d%%" % (col.width or col.preferred_width) for col in columns]
                #~ fields = [col.field._lino_atomizer for col in columns]
                fields = columns

            headers = [column_header(col) for col in fields]

            oh = ar.actor.override_column_headers(ar)
            if oh:
                for i, e in enumerate(columns):
                    header = oh.get(e.name, None)
                    if header is not None:
                        headers[i] = header
                #~ print 20120507, oh, headers

            return fields, headers, widths
Beispiel #14
0
        def getit():

            if ar.request is None:
                columns = None
            else:
                data = getrqdata(ar.request)
                columns = [
                    six.text_type(x) for x in
                    data.getlist(constants.URL_PARAM_COLUMNS)]
            if columns:
                all_widths = data.getlist(constants.URL_PARAM_WIDTHS)
                hiddens = [(x == 'true') for x in data.getlist(
                    constants.URL_PARAM_HIDDENS)]
                fields = []
                widths = []
                ah = ar.actor.get_handle()
                for i, cn in enumerate(columns):
                    col = None
                    for e in ah.list_layout.main.columns:
                        if e.name == cn:
                            col = e
                            break
                    if col is None:
                        raise Exception("No column named %r in %s" %
                                        (cn, ar.ah.list_layout.main.columns))
                    if not hiddens[i]:
                        fields.append(col)
                        widths.append(int(all_widths[i]))
            else:
                if column_names:
                    ll = ColumnsLayout(column_names, datasource=ar.actor)
                    lh = ll.get_layout_handle(settings.SITE.kernel.default_ui)
                    columns = lh.main.columns
                    columns = [e for e in columns if not e.hidden]
                else:
                    ah = ar.actor.get_request_handle(ar)
                    
                    columns = ah.list_layout.main.columns
                    # print(20160530, ah, columns, ah.list_layout.main)

                # render them so that babelfields in hidden_languages
                # get hidden:
                for e in columns:
                    e.value = e.ext_options()
                    # try:
                    #     e.value = e.ext_options()
                    # except AttributeError as ex:
                    #     raise AttributeError("20160529 %s : %s" % (e, ex))
                #
                columns = [e for e in columns if not
                           e.value.get('hidden', False)]

                columns = [e for e in columns if not e.hidden]

                # if str(ar.actor) == "isip.ExamPolicies":
                    # from lino.modlib.extjs.elems import is_hidden_babel_field
                    # print("20180103", [c.name for c in columns])
                    # print("20180103", [c.field for c in columns])
                    # print("20180103", [c.value['hidden'] for c in columns])
                    # print("20180103", [
                    #     is_hidden_babel_field(c.field) for c in columns])
                    # print("20180103", [
                    #     getattr(c.field, '_babel_language', None)
                    #     for c in columns])
                widths = ["%d" % (col.width or col.preferred_width)
                          for col in columns]
                # print("20180831 {}".format(widths))
                #~ 20130415 widths = ["%d%%" % (col.width or col.preferred_width) for col in columns]
                #~ fields = [col.field._lino_atomizer for col in columns]
                fields = columns

            headers = [column_header(col) for col in fields]

            # if str(ar.actor).endswith("DailyPlanner"):
            #     print("20181022", fields[0].field.verbose_name)

            oh = ar.actor.override_column_headers(ar)
            if oh:
                for i, e in enumerate(columns):
                    header = oh.get(e.name, None)
                    if header is not None:
                        headers[i] = header
                #~ print 20120507, oh, headers

            return fields, headers, widths