I can manually add objects to sqlite, but when I try to add them
programtically I get this error:
AttributeError: class NoDefault has no attribute 'encode'
My model.py is as follows
from datetime import datetime
from turbogears.database import PackageHub
from sqlobject import *
from turbogears import identity
hub = PackageHub('getstuffdone')
__connection__ = hub
STATUS_NOT_STARTED = 1
STATUS_ON_SCHEDULE = 2
STATUS_BEHIND_SCHEDULE = 3
STATUS_COMPLETE = 4
status_codes = { STATUS_NOT_STARTED: 'Not Started',
STATUS_ON_SCHEDULE: 'On Schedule',
STATUS_BEHIND_SCHEDULE: 'Behind
Schedule',
STATUS_COMPLETE: 'Complete' }
# class YourDataClass(SQLObject):
# pass
# identity models.
class Visit(SQLObject):
"""
A visit to your site
"""
class sqlmeta:
table = 'visit'
visit_key = StringCol(length=40, alternateID=True,
alternateMethodName='by_visit_key')
created = DateTimeCol(default=datetime.now)
expiry = DateTimeCol()
def lookup_visit(cls, visit_key):
try:
return cls.by_visit_key(visit_key)
except SQLObjectNotFound:
return None
lookup_visit = classmethod(lookup_visit)
class VisitIdentity(SQLObject):
"""
A Visit that is link to a User object
"""
visit_key = StringCol(length=40, alternateID=True,
alternateMethodName='by_visit_key')
user_id = IntCol()
class Group(SQLObject):
"""
An ultra-simple group definition.
"""
# names like "Group", "Order" and "User" are reserved words in SQL
# so we set the name to something safe for SQL
class sqlmeta:
table = 'tg_group'
group_name = UnicodeCol(length=16, alternateID=True,
alternateMethodName='by_group_name')
display_name = UnicodeCol(length=255)
created = DateTimeCol(default=datetime.now)
# collection of all users belonging to this group
users = RelatedJoin('User', intermediateTable='user_group',
joinColumn='group_id', otherColumn='user_id')
# collection of all permissions for this group
permissions = RelatedJoin('Permission', joinColumn='group_id',
intermediateTable='group_permission',
otherColumn='permission_id')
class User(SQLObject):
"""
Reasonably basic User definition.
Probably would want additional attributes.
"""
# names like "Group", "Order" and "User" are reserved words in SQL
# so we set the name to something safe for SQL
class sqlmeta:
table = 'tg_user'
user_name = UnicodeCol(length=16, alternateID=True,
alternateMethodName='by_user_name')
email_address = UnicodeCol(length=255, alternateID=True,
alternateMethodName='by_email_address')
display_name = UnicodeCol(length=255)
password = UnicodeCol(length=40)
created = DateTimeCol(default=datetime.now)
# groups this user belongs to
groups = RelatedJoin('Group', intermediateTable='user_group',
joinColumn='user_id', otherColumn='group_id')
def _get_permissions(self):
perms = set()
for g in self.groups:
perms = perms | set(g.permissions)
return perms
def _set_password(self, cleartext_password):
"Runs cleartext_password through the hash algorithm before
saving."
password_hash = identity.encrypt_password(cleartext_password)
self._SO_set_password(password_hash)
def set_password_raw(self, password):
"Saves the password as-is to the database."
self._SO_set_password(password)
class Permission(SQLObject):
"""
A relationship that determines what each Group can do
"""
permission_name = UnicodeCol(length=16, alternateID=True,
alternateMethodName='by_permission_name')
description = UnicodeCol(length=255)
groups = RelatedJoin('Group',
intermediateTable='group_permission',
joinColumn='permission_id',
otherColumn='group_id')
class Company(SQLObject):
name = UnicodeCol(length=100)
address1 = UnicodeCol(length=100)
address2 = UnicodeCol(length=100)
city = UnicodeCol(length=50)
state = UnicodeCol(length=3)
zip = UnicodeCol(length=10)
phone1 = UnicodeCol(length=20)
phone2 = UnicodeCol(length=20)
fax = UnicodeCol(length=20)
The controller.py is
from turbogears import controllers, expose, flash
# from model import *
from turbogears import controllers, expose, validate, redirect,
widgets, validators, error_handler, identity
import turbogears
from cherrypy import request, response
# from getstuffdone import json
# import logging
# log = logging.getLogger("getstuffdone.controllers")
from model import *
class company_fields(widgets.WidgetsList):
name = widgets.TextField(validator=validators.NotEmpty)
address1 = widgets.TextField(validator=validators.NotEmpty)
address1 = widgets.TextField(validator=validators.NotEmpty)
city = widgets.TextField(validator=validators.NotEmpty)
state = widgets.TextField(validator=validators.NotEmpty)
zip = widgets.TextField(validator=validators.NotEmpty)
phone1 = widgets.TextField(validator=validators.NotEmpty)
phone2 = widgets.TextField(validator=validators.NotEmpty)
fax = widgets.TextField(validator=validators.NotEmpty)
company_form = widgets.TableForm(fields=company_fields(),
submit_text="Save Company")
class Root(controllers.RootController):
@expose(template="getstuffdone.templates.welcome")
# @identity.require(identity.in_group("admin"))
def index(self):
import time
# log.debug("Happy TurboGears Controller Responding For Duty")
flash("Your application is now running")
return dict(now=time.ctime())
@expose(template="getstuffdone.templates.login")
def login(self, forward_url=None, previous_url=None, *args, **kw):
if not identity.current.anonymous \
and identity.was_login_attempted() \
and not identity.get_identity_errors():
raise redirect(forward_url)
forward_url=None
previous_url= request.path
if identity.was_login_attempted():
msg=_("The credentials you supplied were not correct or "
"did not grant access to this resource.")
elif identity.get_identity_errors():
msg=_("You must provide your credentials before accessing
"
"this resource.")
else:
msg=_("Please log in.")
forward_url= request.headers.get("Referer", "/")
response.status=403
return dict(message=msg, previous_url=previous_url,
logging_in=True,
original_parameters=request.params,
forward_url=forward_url)
@expose()
def logout(self):
identity.current.logout()
raise redirect("/")
@expose(template="getstuffdone.templates.company")
def company(self, *args, **kwargs):
c=Company.select()
return dict(companies=c)
@expose(template="getstuffdone.templates.companyinfo")
def companyinfo(self, company_id):
c=Company.get(company_id)
return dict(company=c)
@expose(template="getstuffdone.templates.form")
def companychange(self, *args, **kwargs):
print 'hi1'
if args and args[0] == "add":
c = ""
submit_action= "/save_company/0"
if args and args[0] == "edit":
print 'hi2'
from sqlobject import SQLObjectNotFound
try:
c = Company.get(args[1])
values = kwargs
except SQLObjectNotFound:
values = ""
flash("That's not a valid Company, do you want " +
"to add one now?")
submit_action = "/save_company/%s" %args[1]
return dict(form=company_form,
values=c,
action=submit_action)
@expose()
@error_handler(company)
@validate(form=company_form)
def save_company(self, ID, **kwargs):
print 'save_company(): ID=',ID, 'kwargs=', kwargs
from sqlobject import SQLObjectNotFound
try:
c=Company.get(ID)
c.set(**kwargs)
except SQLObjectNotFound:
Company(**kwargs)
raise redirect("/company")
the full output from the command prompt is
save_company(): ID= 0 kwargs= {'city': u'Here', 'fax': u'5555555555',
'name': u'Test', 'zip': u'67565', 'phone2': u'5555555555', 'address1':
u'100 Main Street', 'phone1': u'5555555555', 'state': u'NV'}
2007-09-26 23:56:46,158 cherrypy.msg INFO HTTP: Page handler: <bound
method Root.save_company of <getstuffdone.controllers.Root object at
0x0152A690>>
Traceback (most recent call last):
File "c:\python25\lib\site-packages\cherrypy-2.2.1-py2.5.egg\cherrypy
\_cphttptools.py", line 105, in _run
self.main()
File "c:\python25\lib\site-packages\cherrypy-2.2.1-py2.5.egg\cherrypy
\_cphttptools.py", line 254, in main
body = page_handler(*virtual_path, **self.params)
File "<string>", line 3, in save_company
File "c:\python25\lib\site-packages\TurboGears-1.0.4b1-py2.5.egg
\turbogears\controllers.py", line 344, in expose
*args, **kw)
File "<string>", line 5, in run_with_transaction
File "c:\python25\lib\site-packages\TurboGears-1.0.4b1-py2.5.egg
\turbogears\database.py", line 316, in so_rwt
retval = func(*args, **kw)
File "<string>", line 5, in _expose
File "c:\python25\lib\site-packages\TurboGears-1.0.4b1-py2.5.egg
\turbogears\controllers.py", line 359, in <lambda>
mapping, fragment, args, kw)))
File "c:\python25\lib\site-packages\TurboGears-1.0.4b1-py2.5.egg
\turbogears\controllers.py", line 386, in _execute_func
output = errorhandling.try_call(func, *args, **kw)
File "c:\python25\lib\site-packages\TurboGears-1.0.4b1-py2.5.egg
\turbogears\errorhandling.py", line 72, in try_call
return func(self, *args, **kw)
File "<string>", line 3, in save_company
File "c:\python25\lib\site-packages\TurboGears-1.0.4b1-py2.5.egg
\turbogears\controllers.py", line 181, in validate
return errorhandling.run_with_errors(errors, func, *args, **kw)
File "c:\python25\lib\site-packages\TurboGears-1.0.4b1-py2.5.egg
\turbogears\errorhandling.py", line 110, in run_with_errors
return func(self, *args, **kw)
File "E:\TG\getstuffdone\getstuffdone\controllers.py", line 107, in
save_company
Company(**kwargs)
File "c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg
\sqlobject\declarative.py", line 98, in _wrapper
return fn(self, *args, **kwargs)
File "c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg
\sqlobject\main.py", line 1218, in __init__
self._create(id, **kw)
File "c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg
\sqlobject\main.py", line 1246, in _create
self.set(**kw)
File "c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg
\sqlobject\main.py", line 1093, in set
kw[name] = dbValue = from_python(value, self._SO_validatorState)
File "c:\python25\lib\site-packages\SQLObject-0.9.1-py2.5.egg
\sqlobject\col.py", line 560, in from_python
return value.encode(self.db_encoding)
AttributeError: class NoDefault has no attribute 'encode'
I'm not sure what I'm doing wrong, please help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---