#825: [PATCH] fix saprovider + finalize abstraction
--------------------------------+-------------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: anonymous
Type: defect | Status: new
Priority: normal | Milestone: 1.0b1
Component: TurboGears | Version: 0.9a5
Severity: normal | Resolution:
Keywords: |
--------------------------------+-------------------------------------------
Comment (by [EMAIL PROTECTED]):
instead of giving patches, i'll just comment what i did, as some of this
may have been taken care of by recent patches:
soprovider / saprovider
* rename 'visit_class' to visit2identity_class as it is a lookup table
for visit2identity
soprovider / saprovider / app.cfg / model.py
* i renamed visit_identity to visit2identity. i also changed the
namespace to be part of so/sa provider instead of visit. this is because
there is no standard naming scheme in tg for database tables. association
tables tend to either be named CamelCase classa_classb or classa2classb.
We can't do camelcase in the db as there are different backends, and
either several of the legacy tables have _ in them already or there are
the 'tg_' prepended tables that kind of suggest joins, so i opted for the
explict description
soprovider
on a successful validation, there was no logging of the link. saprovider
had it in this spot, so i added it:
{{{
+ log.info( "associating user (%s) with visit (%s)",
user.user_name,
+ visit_key )
}}}
saprovider
* the imports on the top of the file were way out of sync. i made them
more in line with soprovider
* i also added "from turbogears.util import load_class" as in
soprovider, removed the _load_class function within the class, and
replaced calls to _load_class with load_class
* the SqlAlchemyIdentity class has this: ( note, its in there 2x )
{{{
visit= TG_VisitIdentity.get_by( visit_key= self.visit_key )
}}}
that should really be importing the dynamic class as in soprovider
{{{
visit= visit2identity_class.get_by( visit_key= self.visit_key )
}}}
* the SqlAlchemyIdentityProvider class has the same issue
* create_provider_model
* this depends on activemapper. activemapper doesn't work right
now. i just made the class return early - otherwise the app will break
* this also calls "TG_VisitIdentity.table.create()" directly, it
should be calling the global class
{{{
- TG_VisitIdentity.table.create()
+ visit2identity_class.table.create()
}}}
* the linking of the user to the visit on successful validation also
has this issue
* this class also has a ton of TG_ tables defined in it. i deleted them
all , as they're alread in model.py
savisit.py
this is pretty straightforward -- i took out the activemapper
functionality, and replaced it with the monkeypatch
{{{
-class TG_Visit(ActiveMapper):
- class mapping:
- visit_key= column( String(40), primary_key=True )
- created= column( DateTime, default=datetime.now )
- expiry= column( DateTime )
+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(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 )
}}}
model.py
i fixed the imports for the database store that is chosen - it gives you
the right stuff for sqlobject, but its broke for sqlalchemy
{{{
+
+#if $identity != "sqlalchemy"
from sqlobject import *
from turbogears.database import PackageHub
+#end if
#if $identity == "sqlobject"
from turbogears import identity
#end if
+#if $identity != "sqlalchemy"
hub = PackageHub("${package}")
__connection__ = hub
# class YourDataClass(SQLObject):
# pass
+#end if
+#if $identity == "sqlalchemy"
+from sqlalchemy import *
+from sqlalchemy.ext.activemapper import *
+from turbogears.database import PackageEngine
+engine = __engine__ = PackageEngine("${package}")
+#end if
}}}
app.cfg
* i removed visit.soprovider.model , and renamed it
identity.soprovider.model.visit2identity -- as its not part of visit, its
part of identity. The namespace was misleading.
* "# identity.provider='${identity}'" is a commented line, showing the
default provider -- i set it to not be a comment line if sqlalchemy is
chosen, as tg will use sqlobject if sqlalchemy is commented out
* i set 'identity.on=False' for /static and /favicon by default, as that
just kills the server with superfluous db calls
The big this was this -- abstracting the login form:
i'm not showing all of the code here, as you will def. have other ideas on
this, I'll just show how i did it:
app.cfg
{{{
+# The names of the fields on your user table that will be used for
identity purpose
+# user_id must be a primary key, user_name and password can be named
anything
+# The default values are those used by the base TG class installs (>=
0.9a5)
+# identity.db.user_id="user_id"
+# identity.db.user_name="user_name"
+# identity.db.password="password"
}}}
then in saprovider, i just renamed a few accessors
* i set some variables and used them for lookups
{{{
- user= user_class.get_by( user_name=user_name )
+ _dbfield_user_id =
turbogears.config.get('identity.db.user_id','user_id')
+ _dbfield_user_name =
turbogears.config.get('identity.db.user_name','user_name')
+ _dbfield_password =
turbogears.config.get('identity.db.password','password')
+
+ user= user_class.get_by( **{ _dbfield_user_name : user_name } )
- if user.password!=self.encrypt_password(password):
+ if getattr(user,_dbfield_password) !=
self.encrypt_password(password):
+ _dbfield_user_name =
turbogears.config.get('identity.db.user_name','user_name')
+ return getattr(self.user,_dbfield_user_name)
}}}
* in _get_user_name i replaced
{{{
- link.user_id= user.user_id
+ link.user_id= getattr(user,_dbfield_user_id)
}}}
everything i did used the tg 0.9a5 stock values as defaults - so people
who upgrade and don't specify a new option won't notice a thing
--
Ticket URL: <http://trac.turbogears.org/turbogears/ticket/825>
TurboGears <http://www.turbogears.org/>
TurboGears front-to-back web development
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Tickets" 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-tickets
-~----------~----~----~----~------~----~------~--~---