示例#1
0
    def to_parent(self, ctx, cls, inst, parent, name, **kwargs):
        fti = cls.get_flat_type_info(cls)

        id_name = id_type = id_str = None
        if self.id_field is not None:
            id_name = self.id_field
            id_type = fti[id_name]

        text_name = self.text_field
        text_type = fti[text_name]
        text_str = "[Unknown]"

        if inst is not None:
            if id_name is not None:
                id_val = getattr(inst, id_name)
                id_str = self.to_unicode(id_type, id_val)

            text_val = getattr(inst, text_name)
            text_str = self.to_unicode(text_type, text_val)

        a_kwargs = {}
        if id_str is not None:
            tn = cls.get_type_name()
            tn_url = camel_case_to_uscore(tn)
            a_kwargs['href'] = tn_url + "?" + urlencode({'id': id_str})

        parent.write(E.a(text_str, **a_kwargs))

        if self.hidden_fields is not None:
            for key in self.hidden_fields:
                self._gen_input_hidden(fti[key], getattr(inst, key, None),
                                    parent, self.hier_delim.join((name, key)))
示例#2
0
            def to_parent(self, ctx, cls, inst, parent, name, **kwargs):
                s = self.to_unicode(cls._type_info['query'], inst.query)
                q = urlencode({"q": s})

                parent.write(
                    E.a("Search %s" % inst.query,
                        href="{}?{}".format(inst.uri, q)))
示例#3
0
文件: form.py 项目: merveunlu/neurons
    def to_parent(self, ctx, cls, inst, parent, name, **kwargs):
        fti, id_str, text_str = self._prep_inst(cls, inst)

        a_kwargs = {}
        if id_str is not None:
            tn = cls.get_type_name()
            tn_url = camel_case_to_uscore(tn)
            a_kwargs['href'] = tn_url + "?" + urlencode({self.id_field: id_str})

        parent.write(E.a(text_str, **a_kwargs))

        if self.hidden_fields is not None:
            for key in self.hidden_fields:
                self._gen_input_hidden(fti[key], getattr(inst, key, None),
                                      parent, self.hier_delim.join((name, key)))
示例#4
0
    def complex_model_to_parent(self, ctx, cls, inst, parent, name, **kwargs):
        fti = cls.get_flat_type_info(cls)
        id_str, text_str = self._prep_inst(cls, inst, fti)

        attrib = {}
        label_div_attrib = {}

        label = None
        if self.label:
            label = self._gen_label_for(ctx, cls, name, "")
            label_div_attrib = {
                'class': '%s %s' % (
                        self.DEFAULT_ANCHOR_WRAPPER_CLASS, cls.get_type_name())
            }

        if id_str != "":
            tn_url = self.url
            if tn_url is None:
                tn = cls.get_type_name()
                tn_url = camel_case_to_uscore(tn)
            attrib['href'] = tn_url + "?" + urlencode({self.id_field: id_str})

            a_tag = E.a(text_str, **attrib)
            if self.label:
                parent.write(E.div(label, a_tag, **label_div_attrib))
            else:
                parent.write(a_tag)

            self._write_hidden_fields(ctx, cls, inst, parent, name, fti, **kwargs)

        elif self.empty_widget is not None:
            if self.label:
                with parent.element('div', attrib=label_div_attrib):
                    parent.write(label)
                    self.empty_widget \
                        .to_parent(ctx, cls, inst, parent, name, **kwargs)
            else:
                self.empty_widget \
                    .to_parent(ctx, cls, inst, parent, name, **kwargs)
示例#5
0
文件: table.py 项目: mahdi-b/spyne
    def _gen_row(self, ctx, cls, inst, parent, name, from_arr=False,
                                                    array_index=None, **kwargs):

        # because HtmlForm* protocols don't use the global null handler, it's
        # possible for null values to reach here.
        if inst is None:
            return

        logger.debug("Generate row for %r", cls)

        with parent.element('tr'):
            for k, v in self.sort_fields(cls):
                attr = self.get_cls_attrs(v)
                if attr.exc:
                    logger.debug("\tExclude table cell %r type %r for %r",
                                                                      k, v, cls)
                    continue

                try:
                    sub_value = getattr(inst, k, None)
                except:  # e.g. SQLAlchemy could throw NoSuchColumnError
                    sub_value = None

                sub_name = attr.sub_name
                if sub_name is None:
                    sub_name = k

                if self.hier_delim is not None:
                    if array_index is None:
                        sub_name = "%s%s%s" % (name, self.hier_delim, sub_name)
                    else:
                        sub_name = "%s[%d]%s%s" % (name, array_index,
                                                     self.hier_delim, sub_name)

                logger.debug("\tGenerate table cell %r type %r for %r",
                                                               sub_name, v, cls)

                td_attrs = {}
                if self.field_name_attr is not None:
                    td_attrs[self.field_name_attr] = attr.sub_name or k
                if attr.hidden:
                    td_attrs['style'] = 'display:None'

                with parent.element('td', td_attrs):
                    ret = self.to_parent(ctx, v, sub_value, parent,
                                              sub_name, from_arr=from_arr,
                                              array_index=array_index, **kwargs)

                    if isgenerator(ret):
                        try:
                            while True:
                                sv2 = (yield)
                                ret.send(sv2)
                        except Break as b:
                            try:
                                ret.throw(b)
                            except StopIteration:
                                pass

            m = cls.Attributes.methods
            if m is not None and len(m) > 0:
                td_attrs = {}

                with parent.element('td', td_attrs):
                    first = True
                    mrpc_delim = html.fromstring(" | ").text

                    for mn, md in self._methods(cls, inst):
                        if first:
                            first = False
                        else:
                            parent.write(mrpc_delim)

                        pd = { }
                        for k, v in self.sort_fields(cls):
                            if getattr(v.Attributes, 'primary_key', None):
                                r = self.to_unicode(v, getattr(inst, k, None))
                                if r is not None:
                                    pd[k] = r

                        params = urlencode(pd)

                        mdid2key = ctx.app.interface.method_descriptor_id_to_key
                        href = mdid2key[id(md)].rsplit("}",1)[-1]
                        text = md.translate(ctx.locale,
                                                  md.in_message.get_type_name())
                        parent.write(E.a(text, href="%s?%s" % (href, params)))

            logger.debug("Generate row for %r done.", cls)
            self.extend_data_row(ctx, cls, inst, parent, name,
                                              array_index=array_index, **kwargs)
示例#6
0
文件: table.py 项目: simudream/spyne
    def _gen_row(self,
                 ctx,
                 cls,
                 inst,
                 parent,
                 name,
                 from_arr=False,
                 array_index=None,
                 **kwargs):

        # because HtmlForm* protocols don't use the global null handler, it's
        # possible for null values to reach here.
        if inst is None:
            return

        print("Generate row for", cls)

        with parent.element('tr'):
            for k, v in cls.get_flat_type_info(cls).items():
                attr = get_cls_attrs(self, v)
                if attr.exc:
                    print("\tExclude table cell %r type %r" % (k, v), "for",
                          cls)
                    continue
                if not attr.get('read', True):
                    continue

                print("\tGenerate table cell %r type %r" % (k, v), "for", cls)

                try:
                    sub_value = getattr(inst, k, None)
                except:  # e.g. SQLAlchemy could throw NoSuchColumnError
                    sub_value = None

                sub_name = attr.sub_name
                if sub_name is None:
                    sub_name = k
                if self.hier_delim is not None:
                    if array_index is None:
                        sub_name = "%s%s%s" % (name, self.hier_delim, sub_name)
                    else:
                        sub_name = "%s[%d]%s%s" % (name, array_index,
                                                   self.hier_delim, sub_name)

                td_attrs = {}
                if self.field_name_attr is not None:
                    td_attrs[self.field_name_attr] = attr.sub_name or k

                with parent.element('td', td_attrs):
                    if attr.href is not None:
                        try:
                            attrib = {'href': attr.href % sub_value}
                        except:
                            attrib = {'href': attr.href}

                        with parent.element('a', attrib=attrib):
                            ret = self.to_parent(ctx,
                                                 v,
                                                 sub_value,
                                                 parent,
                                                 sub_name,
                                                 from_arr=from_arr,
                                                 array_index=array_index,
                                                 **kwargs)
                    else:
                        ret = self.to_parent(ctx,
                                             v,
                                             sub_value,
                                             parent,
                                             sub_name,
                                             from_arr=from_arr,
                                             array_index=array_index,
                                             **kwargs)

                    if isgenerator(ret):
                        try:
                            while True:
                                sv2 = (yield)
                                ret.send(sv2)
                        except Break as b:
                            try:
                                ret.throw(b)
                            except StopIteration:
                                pass

            m = cls.Attributes.methods
            if m is not None and len(m) > 0:
                with parent.element('td'):
                    first = True
                    mrpc_delim = html.fromstring(" | ").text

                    for mn, md in self._methods(cls, inst):
                        if first:
                            first = False
                        else:
                            parent.write(mrpc_delim)

                        pd = {}
                        for k, v in cls.get_flat_type_info(cls).items():
                            if getattr(v.Attributes, 'primary_key', None):
                                r = self.to_unicode(v, getattr(inst, k, None))
                                if r is not None:
                                    pd[k] = r

                        params = urlencode(pd)

                        mdid2key = ctx.app.interface.method_descriptor_id_to_key
                        href = mdid2key[id(md)].rsplit("}", 1)[-1]
                        text = md.translate(ctx.locale,
                                            md.in_message.get_type_name())
                        parent.write(E.a(text, href="%s?%s" % (href, params)))

            print("Generate row for %r done." % cls)
            self.extend_data_row(ctx,
                                 cls,
                                 inst,
                                 parent,
                                 name,
                                 array_index=array_index,
                                 **kwargs)
示例#7
0
    def _gen_row(self,
                 ctx,
                 cls,
                 inst,
                 parent,
                 name,
                 from_arr=False,
                 array_index=None,
                 **kwargs):

        # because HtmlForm* protocols don't use the global null handler, it's
        # possible for null values to reach here.
        if inst is None:
            return

        logger.debug("Generate row for %r", cls)

        mrpc_delim_elt = ''
        if self.mrpc_delim_text is not None:
            mrpc_delim_elt = E.span(self.mrpc_delim_text,
                                    **{'class': 'mrpc-delimiter'})
            mrpc_delim_elt.tail = ' '

        with parent.element('tr'):
            for k, v in self.sort_fields(cls):
                attr = self.get_cls_attrs(v)
                if attr.exc:
                    logger.debug("\tExclude table cell %r type %r for %r", k,
                                 v, cls)
                    continue

                try:
                    sub_value = getattr(inst, k, None)
                except:  # e.g. SQLAlchemy could throw NoSuchColumnError
                    sub_value = None

                sub_name = attr.sub_name
                if sub_name is None:
                    sub_name = k

                if self.hier_delim is not None:
                    if array_index is None:
                        sub_name = "%s%s%s" % (name, self.hier_delim, sub_name)
                    else:
                        sub_name = "%s[%d]%s%s" % (name, array_index,
                                                   self.hier_delim, sub_name)

                logger.debug("\tGenerate table cell %r type %r for %r",
                             sub_name, v, cls)

                td_attrs = {}
                if self.field_name_attr is not None:
                    td_attrs[self.field_name_attr] = attr.sub_name or k
                if attr.hidden:
                    td_attrs['style'] = 'display:None'

                with parent.element('td', td_attrs):
                    ret = self.to_parent(ctx,
                                         v,
                                         sub_value,
                                         parent,
                                         sub_name,
                                         from_arr=from_arr,
                                         array_index=array_index,
                                         **kwargs)
                    if isgenerator(ret):
                        try:
                            while True:
                                sv2 = (yield)
                                ret.send(sv2)
                        except Break as b:
                            try:
                                ret.throw(b)
                            except StopIteration:
                                pass

            m = cls.Attributes.methods
            if m is not None and len(m) > 0:
                td_attrs = {'class': 'mrpc-cell'}

                with parent.element('td', td_attrs):
                    first = True

                    for mn, md in self._methods(cls, inst):
                        if first:
                            first = False
                        elif mrpc_delim_elt is not None:
                            parent.write(" ")
                            parent.write(mrpc_delim_elt)

                        pd = {}
                        for k, v in self.sort_fields(cls):
                            if getattr(v.Attributes, 'primary_key', None):
                                r = self.to_unicode(v, getattr(inst, k, None))
                                if r is not None:
                                    pd[k] = r

                        params = urlencode(pd)

                        mdid2key = ctx.app.interface.method_descriptor_id_to_key
                        href = mdid2key[id(md)].rsplit("}", 1)[-1]
                        text = md.translate(ctx.locale,
                                            md.in_message.get_type_name())
                        parent.write(
                            E.a(text,
                                href="%s?%s" % (href, params),
                                **{'class': 'mrpc-operation'}))

            logger.debug("Generate row for %r done.", cls)
            self.extend_data_row(ctx,
                                 cls,
                                 inst,
                                 parent,
                                 name,
                                 array_index=array_index,
                                 **kwargs)
示例#8
0
            def to_parent(self, ctx, cls, inst, parent, name, **kwargs):
                s = self.to_unicode(cls._type_info['query'], inst.query)
                q = urlencode({"q": s})

                parent.write(E.a("Search %s" % inst.query,
                                              href="{}?{}".format(inst.uri, q)))