Do we have Plugin solid form upgraded code for python 3 ?

60 views
Skip to first unread message

Rahul

unread,
Mar 23, 2022, 7:43:15 AM3/23/22
to web2py-users
Hi All,
         I am migrating my python 2.7 project to py 3.7 - I am facing issues with plugin solid form conversion to py3 code. My code uses this plugin a lot. Rest all code works fine except the plugin part - While using it I am getting the error - I tried web recommendations but to no avail - Please suggest ! 

<class 'TypeError'> 'float' object cannot be interpreted as an integerVersion
web2py™
Version 2.22.3-stable+timestamp.2022.02.15.15.14.38
Python
Python 3.7.5: C:\Python37\python.exe (prefix: C:\Python37)Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

Traceback (most recent call last):
File "E:\web2py_src\web2py\gluon\restricted.py", line 219, in restricted
exec(ccode, environment)
File "E:/web2py_src/web2py/applications/artpic/controllers/default.py", line 9599, in <module>
File "E:\web2py_src\web2py\gluon\globals.py", line 430, in <lambda>
self._caller = lambda f: f()
File "E:/web2py_src/web2py/applications/artpic/controllers/default.py", line 6200, in contact
form = SOLIDFORM(db.contactus, fields=fields, submit_button='Submit')
File "E:\web2py_src\web2py\applications\artpic\modules\plugin_solidform.py", line 59, in __init__
for row_no, inner in enumerate(self.structured_fields):
TypeError: 'float' object cannot be interpreted as an integer


Attached is the file

also here is the code -


# -*- coding: utf-8 -*-
# This plugins is licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php#
#Authors: Kenji Hosoda <hos...@s-cubism.jp>
from gluon import *
import copy
from math import trunc, ceil

class SOLIDFORM(SQLFORM):
    def __init__(self, *args, **kwds):
        self.structured_fields = kwds.get('fields')
        self.showid = kwds.get('showid', True)
        table = args and args[0] or kwds['table']
        if not self.structured_fields:
            self.structured_fields = [f.name for f in table if self._is_show(f)]
        else:
            self.structured_fields = copy.copy(self.structured_fields)
        _precedent_row_len = 1

        include_id = False
        flat_fields = []
        max_row_lines = 1
        for i, inner in enumerate(self.structured_fields):
            if type(inner) in (list, tuple):
                _inner = []
                for field in inner:
                    if field == 'id':
                        include_id = True
                        self.showid = True
                    if field and self._is_show(table[field]):
                        flat_fields.append(field)
                        _inner.append(field)
                self.structured_fields[i] = _inner
                max_row_lines = max(len(_inner), max_row_lines)
                _precedent_row_len = len(_inner)
            elif inner:
                if inner == 'id':
                    include_id = True
                    self.showid = True
                if self._is_show(table[inner]):
                    flat_fields.append(inner)
                else:
                    self.structured_fields[i] = None
            else:
                self.structured_fields[i] = [inner for i in range(_precedent_row_len)]
        self.structured_fields = [e for e in self.structured_fields if e or e is None]
        row_spans = dict((e, 1) for e in flat_fields)
        col_spans = dict((e, 1) for e in flat_fields)
        row_lines = [[] for i in range(max_row_lines)]
        for row_no, inner in enumerate(self.structured_fields):
            if type(inner) in (list, tuple):
                num_lines = trunc (len(inner))
                colspan = max_row_lines / num_lines
                extra_colspan = max_row_lines % num_lines
                for i in range((max_row_lines - extra_colspan) / colspan):
                    field = inner[i]
                    if field:
                        col_spans[field] = colspan
                        row_lines[i * colspan].append(field)
                    else:
                        for _row_no in reversed(list(range(row_no))):
                            try:
                                row_spans[self.structured_fields[_row_no][i]] += 1
                                break
                            except KeyError:
                                pass
                if extra_colspan:
                    for line_no in range(max_row_lines - extra_colspan, max_row_lines):
                        row_lines[line_no].append(None)
            else:
                field = inner
                col_spans[field] = max_row_lines
                row_lines[0].append(field)

        self.row_spans = row_spans
        self.col_spans = col_spans
        self.max_row_lines = max_row_lines
        self.flat_fields = flat_fields
        self.row_lines = row_lines
        kwds['fields'] = copy.copy(flat_fields)
        if not self.structured_fields or flat_fields[0] == 'id':
            self.ignore_first = False
        else:
            self.ignore_first = include_id
        self.readonly = kwds.get('readonly', False)

        kwds['showid'] = self.showid
        SQLFORM.__init__(self, *args, **kwds)

    def _is_show(self, fieldobj):
        return (fieldobj.writable or fieldobj.readable) and (fieldobj.type != 'id' or
                                                            (self.showid and fieldobj.readable))

    def createform(self, xfields):
        table_inner = []
        if self.showid and self.record and (not self.readonly or self.ignore_first):
            if not self.ignore_first:
                id, a, b, c = xfields[0]
                tr_inner = []
                tr_inner.append(TD(a, _class='w2p_fl'))
                tr_inner.append(TD(b, _class='w2p_fw', _colspan=(2 * self.max_row_lines) - 1))
                table_inner.append(TR(*tr_inner))
            xfields = xfields[1:]

        n = len(self.flat_fields)
        xfield_dict = dict([(x, y) for x, y in zip(self.flat_fields, xfields[:n])])

        row_lines = copy.copy(self.row_lines)
        for i in range(max(len(line) for line in row_lines) * self.max_row_lines):
            if i >= len(row_lines[0]):
                break
            tr_inner = []
            for j, row_line in enumerate(row_lines):
                if i < len(row_line):
                    field = row_line[i]
                    if field is None:
                        tr_inner.append(TD())
                    elif field is False:
                        pass
                    else:
                        row_span = self.row_spans[field]
                        if row_span > 1:
                            for k in range(1, row_span):
                                row_lines[j].insert(i + k, False)
                        col_span = self.col_spans[field]
                        if col_span > 1:
                            for k in range(1, col_span):
                                row_lines[j + k].insert(i, False)
                        tr_inner += self.create_td(xfield_dict[field], row_span, col_span)
            table_inner.append(TR(*tr_inner))

            for id, a, b, c in xfields[n:]:
                td_b = self.field_parent[id] = TD(b, _class='w2p_fw',
                                                    _colspan=(2 * self.max_row_lines) - 1)
                tr_inner = []
                tr_inner.append(TD(a, _class='w2p_fl'))
                tr_inner.append(td_b)
                table_inner.append(TR(*tr_inner))

            return TABLE(*table_inner)

    def create_td(self, xfield, row_span, col_span):
        id, a, b, c = xfield
        if self.formstyle == 'table3cols':
            td_b = self.field_parent[id] = TD(
            b,
            c and not self.readonly and DIV(c, _class='notation') or '',  # do not show when readonly
            _class='w2p_fw',
            _rowspan=row_span,
            _colspan=2 * col_span - 1)

            return (TD(a, _class='w2p_fl',
                        _rowspan=row_span,
                         _colspan=1), td_b)

        else:
            raise RuntimeError('formstyle not supported')

    @staticmethod
    def factory(*fields, **attributes):
        table_name = attributes.get('table_name', 'no_table')
        if 'table_name' in attributes:
            del attributes['table_name']

        flat_fields = []
        structured_fields = []
        for inner in fields:
            if type(inner) in (list, tuple):
                _inner = []
                for field in inner:
                    _inner.append(field.name)
                    if field:
                        flat_fields.append(field)
                structured_fields.append(_inner)

            elif inner:
                flat_fields.append(inner)
                structured_fields.append(inner.name)

        return SOLIDFORM(DAL(None).define_table(table_name, *flat_fields),
                        fields=structured_fields, **attributes)

    @staticmethod
    def formstyle(id, a, td_b, c):
        if c:
            td_b.components = td_b.components + [DIV(c, _class='notation')]
            return TR(TD(a, _class='w2p_fl'), td_b, _id=id)

Any help or pointers would be greatly appreciated. Do we have upgraded version of plugin solid form ? 

Regards,

Rahul
plugin_solidform.py

Rahul

unread,
Mar 24, 2022, 4:47:40 AM3/24/22
to web2py-users
anyone has any ideas ? 

Massimiliano

unread,
Mar 24, 2022, 6:21:01 AM3/24/22
to web...@googlegroups.com
This could be one problem:

for i in range((max_row_lines - extra_colspan) / colspan):

there are many of this. 

range take an integer, but a division in python3 return a float...




--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/5c335ab5-cb50-474e-b3a5-189cd0dc3776n%40googlegroups.com.


--
Massimiliano

Dave S

unread,
Mar 24, 2022, 7:21:46 AM3/24/22
to web2py-users
On Thursday, March 24, 2022 at 3:21:01 AM UTC-7 Massimiliano wrote:
This could be one problem:

for i in range((max_row_lines - extra_colspan) / colspan):

there are many of this. 

range take an integer, but a division in python3 return a float...


Interesting, but why doesn't the exception occur in the quoted line with the range statement?

The stack trace suggests it occurs after the for loop completes.

(I' also dizzy from the number of places where self.structured fields get redone by enumerating itself)

/dps

Rahul

unread,
Mar 26, 2022, 2:14:07 AM3/26/22
to web2py-users
yes I used some things like trunc or ceil to fix it however the error persists on the same line. Pretty weird. Also used what was suggested like // 
but did not work for me. I want to get this fixed soon. However, 
 If its not possible to fix it "quickly" perhaps someone can suggest me some plugin to manage fields in the layout for proper UI orientation. 

All suggestions are welcome

Regards,
Rahul

Rahul

unread,
Apr 2, 2022, 4:58:59 AM4/2/22
to web2py-users
Do we have alternatives to this feature  ? 

Rahul

Dave S

unread,
Apr 3, 2022, 1:37:05 PM4/3/22
to web2py-users
On Friday, March 25, 2022 at 11:14:07 PM UTC-7 Rahul wrote:
yes I used some things like trunc or ceil to fix it however the error persists on the same line. Pretty weird. Also used what was suggested like // 
but did not work for me. I want to get this fixed soon. However, 
 If its not possible to fix it "quickly" perhaps someone can suggest me some plugin to manage fields in the layout for proper UI orientation. 

All suggestions are welcome

Regards,
Rahul



Have you examined the type of structured_fields and it's components at the time of the exception?  I would add some debug prints just before the for statement.

/dps

Rahul

unread,
Apr 4, 2022, 6:34:40 AM4/4/22
to web2py-users
Hey All,
           This is finally fixed. I spent some time debugging on it today. The fix was actually very simple, all I had to do was to convert some values to integer. Here is the final fixed version on plugin_solidform.py. For all the folks who want to use this awesome plugin with python 3.x.x can give it a try. 

working.png

Thanks snide.. and all


Regards,

Rahul

plugin_solidform.py
Reply all
Reply to author
Forward
0 new messages