Hello,

I tried to create a model using tg-admin sql create, but I have error message 
below. I manage to make the model work in pure python (not using turbogears), 
but when I use turbogears I can not manage to make it work.

Could anyone help?


Kind regards,

Helene

/Documents/currency/cur4$ tg-admin sql create
/usr/lib/python2.5/site-packages/CherryPy-2.3.0-py2.5.egg/cherrypy/lib/profiler.py:54:
 UserWarning: Your installation of Python doesn't have a profile module. If 
you're on Debian, you can apt-get python2.4-profiler from non-free in a 
separate step. See http://www.cherrypy.org/wiki/ProfilingOnDebian for details.
  warnings.warn(msg)
Creating tables at sqlite:///devdata.sqlite
Traceback (most recent call last):
  File "/usr/local/bin/tg-admin", line 8, in <module>
    load_entry_point('TurboGears==1.0.4.4', 'console_scripts', 'tg-admin')()
  File 
"/usr/lib/python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg/turbogears/command/base.py",
 line 369, in main
    command.run()
  File 
"/usr/lib/python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg/turbogears/command/base.py",
 line 97, in run
    sacommand(command, sys.argv)
  File "<string>", line 5, in sacommand
  File 
"/usr/lib/python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg/turbogears/command/sacommand.py",
 line 32, in create
    get_model()
  File 
"/usr/lib/python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg/turbogears/util.py",
 line 131, in get_model
    package = __import__(package_name, {}, {}, ["model"])
  File "/home/francois/Documents/currency/cur4/cur4/model.py", line 63, in 
<module>
    Column('date', Date),
NameError: name 'Date' is not defined
franc...@spv:~/Documents/currency/cur4$ tg-admin sql create
/usr/lib/python2.5/site-packages/CherryPy-2.3.0-py2.5.egg/cherrypy/lib/profiler.py:54:
 UserWarning: Your installation of Python doesn't have a profile module. If 
you're on Debian, you can apt-get python2.4-profiler from non-free in a 
separate step. See http://www.cherrypy.org/wiki/ProfilingOnDebian for details.
  warnings.warn(msg)
Creating tables at sqlite:///devdata.sqlite
Traceback (most recent call last):
  File "/usr/local/bin/tg-admin", line 8, in <module>
    load_entry_point('TurboGears==1.0.4.4', 'console_scripts', 'tg-admin')()
  File 
"/usr/lib/python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg/turbogears/command/base.py",
 line 369, in main
    command.run()
  File 
"/usr/lib/python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg/turbogears/command/base.py",
 line 97, in run
    sacommand(command, sys.argv)
  File "<string>", line 5, in sacommand
  File 
"/usr/lib/python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg/turbogears/command/sacommand.py",
 line 32, in create
    get_model()
  File 
"/usr/lib/python2.5/site-packages/TurboGears-1.0.4.4-py2.5.egg/turbogears/util.py",
 line 131, in get_model
    package = __import__(package_name, {}, {}, ["model"])
  File "/home/francois/Documents/currency/cur4/cur4/model.py", line 64, in 
<module>
    Column('date', Date),
NameError: name 'Date' is not defined


Model.py:
----------


from datetime import datetime
import pkg_resources
pkg_resources.require("SQLAlchemy>=0.3.10")
from turbogears.database import metadata, mapper
# import some basic SQLAlchemy classes for declaring the data model
# (see http://www.sqlalchemy.org/docs/04/ormtutorial.html)
from sqlalchemy import Table, Column, ForeignKey
from sqlalchemy.orm import relation
# import some datatypes for table columns from SQLAlchemy
# (see http://www.sqlalchemy.org/docs/04/types.html for more)
from sqlalchemy import String, Unicode, Integer, DateTime
from turbogears import identity



# your data tables

# your_table = Table('yourtable', metadata,
#     Column('my_id', Integer, primary_key=True)
# )


# your model classes



##########################################################################################################
#                                                                               
                         #
#   Definition of Currency class and currency_table                             
                         #
#                                                                               
                         #
##########################################################################################################


currency_table = Table('currencies', metadata,
        Column('id', Integer, primary_key=True),
        Column('code', Unicode(3), unique=True),  #  ISO definition of currency 
in 3 letters
        Column('name', Unicode(100)),
        )

class Currency(object):
    """
    The currency table is mapped on this class.
    List all currencies available.
    """
    def __init__(self, code, name):
        self.code = code
        self.name = name
        
    def __repr__(self):
        return '(%s, %s)' % (repr(self.code), repr(self.name))


        
##########################################################################################################
#                                                                               
                         #
#   Definition of Currency rates ECB and currency rates ECB table               
                         #
#                                                                               
                         #
##########################################################################################################
        

currency_rates_ecb_table = Table('currency_rates_ecb', metadata,
    Column('id', Integer, primary_key=True),
    Column('currency_id', Integer, ForeignKey('currencies.id')),
    Column('date', Date),
    Column('fxrate', DECIMAL),
    )

class CurrencyRateECB(object):
    """
    the currency rates table is mapped on this class
    For each currency, list all the daily rates.
    """
    def __init__(self, date, fxrate):
        self.date = date
        self.fxrate = fxrate
    def __repr__(self):
        #return repr(self.fxrate)
        return '(%s, %s)' % (repr(self.date), repr(self.fxrate))
 

       
##########################################################################################################
#                                                                               
                         #
#   Mappers                                                                     
                         #
#                                                                               
                         #
##########################################################################################################
         
        
        
mapper(Currency, currency_table, properties={
    'currency_rates_ecb':relation(CurrencyRateECB, 
order_by=[currency_rates_ecb_table.c.date], backref=backref('currency'))
    })

mapper(CurrencyRateECB, currency_rates_ecb_table)




# the identity schema

visits_table = Table('visit', metadata,
    Column('visit_key', String(40), primary_key=True),
    Column('created', DateTime, nullable=False, default=datetime.now),
    Column('expiry', DateTime)
)

visit_identity_table = Table('visit_identity', metadata,
    Column('visit_key', String(40), primary_key=True),
    Column('user_id', Integer, ForeignKey('tg_user.user_id'), index=True)
)

groups_table = Table('tg_group', metadata,
    Column('group_id', Integer, primary_key=True),
    Column('group_name', Unicode(16), unique=True),
    Column('display_name', Unicode(255)),
    Column('created', DateTime, default=datetime.now)
)

users_table = Table('tg_user', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('user_name', Unicode(16), unique=True),
    Column('email_address', Unicode(255), unique=True),
    Column('display_name', Unicode(255)),
    Column('password', Unicode(40)),
    Column('created', DateTime, default=datetime.now)
)

permissions_table = Table('permission', metadata,
    Column('permission_id', Integer, primary_key=True),
    Column('permission_name', Unicode(16), unique=True),
    Column('description', Unicode(255))
)

user_group_table = Table('user_group', metadata,
    Column('user_id', Integer, ForeignKey('tg_user.user_id',
        onupdate='CASCADE', ondelete='CASCADE')),
    Column('group_id', Integer, ForeignKey('tg_group.group_id',
        onupdate='CASCADE', ondelete='CASCADE'))
)

group_permission_table = Table('group_permission', metadata,
    Column('group_id', Integer, ForeignKey('tg_group.group_id',
        onupdate='CASCADE', ondelete='CASCADE')),
    Column('permission_id', Integer, ForeignKey('permission.permission_id',
        onupdate='CASCADE', ondelete='CASCADE'))
)


# the identity model


class Visit(object):
    """
    A visit to your site
    """
    def lookup_visit(cls, visit_key):
        return cls.query.get(visit_key)
    lookup_visit = classmethod(lookup_visit)


class VisitIdentity(object):
    """
    A Visit that is link to a User object
    """
    pass


class Group(object):
    """
    An ultra-simple group definition.
    """
    pass


class User(object):
    """
    Reasonably basic User definition.
    Probably would want additional attributes.
    """

    def permissions(self):
        p = set()
        for g in self.groups:
            p |= set(g.permissions)
        return p
    permissions = property(permissions)

    def by_email_address(cls, email):
        """
        A class method that can be used to search users
        based on their email addresses since it is unique.
        """
        return cls.query.filter_by(email_address=email).first()
    by_email_address = classmethod(by_email_address)

    def by_user_name(cls, username):
        """
        A class method that permits to search users
        based on their user_name attribute.
        """
        return cls.query.filter_by(user_name=username).first()
    by_user_name = classmethod(by_user_name)

    def _set_password(self, password):
        """
        encrypts password on the fly using the encryption
        algo defined in the configuration
        """
        self._password = identity.encrypt_password(password)

    def _get_password(self):
        """
        returns password
        """
        return self._password

    password = property(_get_password, _set_password)


class Permission(object):
    """
    A relationship that determines what each Group can do
    """
    pass


# set up mappers between identity tables and classes

mapper(Visit, visits_table)

mapper(VisitIdentity, visit_identity_table,
        properties=dict(users=relation(User, backref='visit_identity')))

mapper(User, users_table,
        properties=dict(_password=users_table.c.password))

mapper(Group, groups_table,
        properties=dict(users=relation(User,
                secondary=user_group_table, backref='groups')))

mapper(Permission, permissions_table,
        properties=dict(groups=relation(Group,
                secondary=group_permission_table, backref='permissions')))


      
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to