BACKGROUND: [[[ This is a continuation of the thread begun by Kevin's 
post to sqlalchemy-users, archived at 
http://permalink.gmane.org/gmane.comp.python.sqlalchemy.user/2452
To summarize: Kevin checked in support for SQLAlchemy 0.2 today -- an 
implementation quite different from the one that's been in a patch in 
trac. It's Good Stuff, but doesn't update the SA visit or identity 
providers, or the quickstart templates. ]]]

I've built new version of my patch fixing the SQLAlchemy identity and 
visit providers for use with r1600 which withstands slightly more 
scrutiny than the old one. Next up is a patch to ActiveMapper to add 
implicit creation of a surrogate key when no primary key is explicitly 
defined, though that may not happen 'till tomorrow. I still don't have 
anything by way of updates for the quickstart templates -- but if we 
want to demonstrate the implicit primary key creation, then that might 
as well wait 'till the other patch is finished anyhow.

(I don't like the use of tg_user.user_id at all, btw -- would much 
rather change it to tg_user.id; anyone object?).


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Index: turbogears/identity/saprovider.py
===================================================================
--- turbogears/identity/saprovider.py	(revision 1602)
+++ turbogears/identity/saprovider.py	(working copy)
@@ -7,16 +7,19 @@
 from datetime import *
 from sqlalchemy import *
 from sqlalchemy.ext.activemapper import *
+import sqlalchemy.ext.activemapper as activemapper
 
 import logging
 log = logging.getLogger("turbogears.identity.saprovider")
 
 from turbogears import identity
+from turbogears.database import metadata
 
-__engine__= turbogears.database.PackageEngine( 'turbogears.identity' )
-
 from turbogears.util import load_class
 
+turbogears.database.bind_meta_data()
+activemapper.metadata = turbogears.database.metadata
+
 try:
     set, frozenset
 except NameError:
@@ -182,7 +185,6 @@
             link= visit_class( visit_key=visit_key, user_id=user.user_id )
         else:
             link.user_id= user.user_id
-        link.commit()
         return SqlAlchemyIdentity( visit_key, user )
             
     def load_identity( self, visit_key ):
@@ -208,78 +210,3 @@
         '''
         return SqlAlchemyIdentity( None )
         
-
-user_group = Table("tg_user_group", __engine__, 
-                      Column("user_id", Integer,
-                              ForeignKey("tg_user.user_id"),
-                              primary_key = True),
-                      Column("group_id", Integer,
-                              ForeignKey("tg_group.group_id"),
-                              primary_key = True))
-                      
-group_permission = Table("tg_group_permission", __engine__,
-                            Column("group_id", Integer,
-                                    ForeignKey("tg_group.group_id"),
-                                    primary_key = True),
-                            Column("permission_id", Integer,
-                                ForeignKey("permission.permission_id"),
-                                    primary_key = True))
-
-
-class VisitIdentity(ActiveMapper):
-    class mapping:
-        __table__ = "tg_visit_identity"
-        visit_key = column(String, # foreign_key = "visit.visit_key",
-                          primary_key = True)
-        user_id = column(Integer, foreign_key = "tg_user.user_id", index = True)
-
-
-class Group(ActiveMapper):
-    """
-    An ultra-simple group definition.
-    """
-    class mapping:
-        __table__ = "tg_group"
-        group_id = column(Integer, primary_key = True)
-        group_name = column(Unicode(16), unique = True)
-        display_name = column(Unicode(255))
-        created = column(DateTime, default = datetime.now)
-
-        #users = many_to_many("User", user_group, backref = "groups")
-        #permissions = many_to_many("Permission", group_permission,
-        #                           backref = "groups")
-
-
-class User(ActiveMapper):
-    """
-    Reasonably basic User definition. Probably would want additional attributes.
-    """
-    class mapping:
-        __table__ = "tg_user"
-        user_id = column(Integer, primary_key = True)
-        user_name = column(Unicode(16), unique = True)
-        email_address = column(Unicode(255), unique = True)
-        display_name = column(Unicode(255))
-        password = column(Unicode(40))
-        created = column(DateTime, default = datetime.now)
-
-        groups = many_to_many("Group", user_group, backref = "users")
-
-    @property
-    def permissions(self):
-        perms = set()
-        for g in self.groups:
-            perms = perms | set(g.permissions)
-        return perms
-        
-
-class Permission(ActiveMapper):
-    class mapping:
-        __table__ = "tg_permission"
-        permission_id = column(Integer, primary_key = True)
-        permission_name = column(Unicode(16), unique = True)
-        description = column(Unicode(255))
-        
-        groups = many_to_many("Group", group_permission,
-                              backref = "permissions")
-
Index: turbogears/visit/savisit.py
===================================================================
--- turbogears/visit/savisit.py	(revision 1602)
+++ turbogears/visit/savisit.py	(working copy)
@@ -1,15 +1,16 @@
 import turbogears
 from datetime import *
+from turbogears.database import metadata
 from turbogears.visit.api import BaseVisitManager, Visit
+import sqlalchemy
 from sqlalchemy import *
 from sqlalchemy.ext.activemapper import *
 
 import logging
 log = logging.getLogger("turbogears.identity.savisit")
 
-__engine__= turbogears.database.PackageEngine( 'turbogears.visit' )
+turbogears.database.bind_meta_data()
 
-
 class SqlAlchemyVisitManager(BaseVisitManager):
     def __init__(self, timeout):
         super(SqlAlchemyVisitManager,self).__init__( timeout )
@@ -17,13 +18,13 @@
     def create_model(self):
         try:
             TG_Visit.table.create()
-            objectstore.commit()
-        except SQLError:
+        except sqlalchemy.exceptions.SQLError:
             pass
+        except sqlalchemy.exceptions.ArgumentError:
+            pass
             
     def new_visit_with_key(self, visit_key):
         visit= TG_Visit( visit_key=visit_key, expiry=datetime.now()+self.timeout )
-        visit.commit()
         return Visit( visit_key, True )
         
     def visit_for_key(self, visit_key):
@@ -42,32 +43,27 @@
     def update_queued_visits(self, queue):
         try:
             table= TG_Visit.table
+            if not hasattr(table.metadata, 'engine') or table.metadata.engine is not None:
+                turbogears.database.bind_meta_data()
             # Now update each of the visits with the most recent expiry
             for visit_key,expiry in queue.items():
                 log.info( "updating visit (%s) to expire at %s", visit_key,
                           expiry )
                 table.update( table.c.visit_key==visit_key,
                                   values={'expiry': expiry} ).execute()
-            objectstore.commit()
         except:
             objectstore.clear()
             raise
             
 
-tbl__tgvisit = Table('tg_visit', __engine__,
-    Column('visit_key', String(40) , primary_key=True),
-    Column('created', DateTime  , nullable = False ),
-    Column('expiry', DateTime ),
-)
+class TG_Visit(ActiveMapper):
+    class mapping:
+        __table__ = 'tg_visit'
+        visit_key = column(String(40), primary_key=True)
+        created = column(DateTime, nullable=False, default=datetime.now)
+        expiry = column(DateTime)
 
-
-class TG_Visit(object):
-    table = tbl__tgvisit
-
     def lookup_visit( cls, visit_key ):
         return TG_Visit.get( visit_key );
     lookup_visit= classmethod(lookup_visit)
 
-assign_mapper( TG_Visit , tbl__tgvisit )
-
-

Reply via email to