pombreda / formalchemy

Automatically exported from code.google.com/p/formalchemy
MIT License
0 stars 0 forks source link

Editing time record gives TypeError #148

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Using this code:

1. model/__init__.py :

"""The application's model objects"""
from sensor.model.meta import Session, Base
from sqlalchemy import orm, Table, Column
from sqlalchemy import Integer, Time

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    Session.configure(bind=engine)

    orm.mapper(Timer, timr)

timr = Table('timer', Base.metadata,
        Column('id_timer', Integer, primary_key=True),
        Column('ora', Time, unique=True),
    )

class Timer(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __unicode__(self):
        return self.ora

    __str__ = __unicode__

2. websetup.py

import pylons.test

from sensor.config.environment import load_environment
from sensor.model.meta import Session, Base
from sensor import model

import datetime

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup sensor here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    Base.metadata.create_all(bind=Session.bind)

    #add schedule every 6 hrs
    for h in (0, 6, 12, 18):
        timer = model.Timer(
            ora=datetime.time(h)
            )
        Session.add(timer)

    Session.commit()

3. setup-app, serve, go to http://127.0.0.1:5000/admin/Timer/models and try to 
edit a record

What is the expected output? What do you see instead?

Debug reports this:

TypeError: coercing to Unicode: need string or buffer, datetime.time found

Module formalchemy.ext.pylons.controller:350 in edit          view
>>  return self.render(format=format, fs=fs, action='edit', id=id)
Module formalchemy.ext.pylons.controller:117 in render          view
>>  breadcrumb=self.breadcrumb(**kwargs),
Module formalchemy.ext.pylons.controller:98 in breadcrumb          view
>>  items.append((model_url(self.member_name, id=id), u'%s' % fs.model))
TypeError: coercing to Unicode: need string or buffer, datetime.time found

What version of the product are you using? On what operating system?

In [2]: formalchemy.__version__ 
Out[2]: '1.3.5'

In [4]: pylons.__version__ 
Out[4]: '1.0'

In [6]: sqlalchemy.__version__ 
Out[6]: '0.6.4'

Please provide any additional information below.

I need a little admin section to CRUD schedule times and a few other things.

Little sidenotes:
- Hour 00:00:00 is rendered as an empty string

- Would be nice to set 00 as default for hh, mm, ss if not selected (i.e. if I 
just set hh as '3' I mean 03:00:00 and I don't get "Invalid Time")

Thanks for your support

Original issue reported on code.google.com by neur...@gmail.com on 12 Oct 2010 at 9:16

GoogleCodeExporter commented 9 years ago
I guess that self.ora return a non-unicode string. You should try:

def __unicode__(self):
    return unicode(str(self.ora))

Original comment by gael.pas...@gmail.com on 12 Oct 2010 at 9:43

GoogleCodeExporter commented 9 years ago
*Thanks, that did the trick!*

Now I'm stuck on another thing: I have a table I NEED to insert primary_key by 
hand but admin controller does not show it.

     Product 
+---------------+
| serial | desc |
+---------------+
|   1245 | item | 
|   1657 | item | 
|   9999 | item | 
...

product = Table('product', Base.metadata,
        Column('serial', Integer, primary_key=True, autoincrement=False),
        Column('desc', Unicode(40))
    )

I'm using sqlite in development and I read autoincrement is disabled but in 
production I'll switch to mySQL which I can't install on this machine.

Will it work with mySQL and let me insert/edit serial or it's not expected to 
work?

I wish I'll not iscover this thing only at deployment...

Sorry if I hijack my own issue ^^

Thanks again

Original comment by neur...@gmail.com on 12 Oct 2010 at 10:09

GoogleCodeExporter commented 9 years ago
As I remember you can use pk=True to allow to edit primary keys: 
http://docs.formalchemy.org/forms.html#formalchemy.forms.FieldSet.configure

Original comment by gael.pas...@gmail.com on 12 Oct 2010 at 10:23

GoogleCodeExporter commented 9 years ago
That worked too, thanks a lot!

P.S. 00:00:00 keeps on being shown as an empty string... not a problem anyway.

Thanks for all your answers.

Original comment by neur...@gmail.com on 12 Oct 2010 at 10:47

GoogleCodeExporter commented 9 years ago
P.P.S.: this my be a (little) bug:

if I edit by hand my primary_key (that's the product serial, let's say from 11 
to 22) after hitting save I'm redirected to ...Product/models/11 that, of 
course returns, a 404.

Cheers

Original comment by neur...@gmail.com on 12 Oct 2010 at 10:51

GoogleCodeExporter commented 9 years ago
Can you fill an other issue for this with an explicit title ? thanks

Original comment by gael.pas...@gmail.com on 12 Oct 2010 at 10:54

GoogleCodeExporter commented 9 years ago
Done,

about my first post: I also have found that editing / adding an already 
existing time to the table (declared with unique=True) I get an unhandled 
IntegrityError.

IntegrityError: (IntegrityError) column ora is not unique u'INSERT INTO timer 
(ora) VALUES (?)' ('06:00:00.000000',)

Should I add this issue too?
I'm asking it before since I don't want to seem a nutcracker... ^^

Thank you

Original comment by neur...@gmail.com on 12 Oct 2010 at 11:37