On Jan 21, 11:33 am, Jan Koprowski <[email protected]> wrote:
> @Paweł Stradomski
> University of Technology in Gdańsk
>
> Ok. I understood this. But - i must wrote this methods. Simple CRUD
> isn't something bad. SQLAlchemy could support something like this:
>
> class User(SQLAlchemy.CRUD):
> pass
>
> and give optionaly something like in django. This will be nice. Now i
> do for example something like this.
>
> def __new__(cls, *args, **kwargs):
> if 'username' in kwargs.keys():
> uid = getpwnam(kwargs.get('username')).pw_uid
> if 'uid' in kwargs.keys():
> uid = kwargs.get('uid')
> # @todo - poprawic czytelnosc
> if uid:
> if meta.Session.query(Informations).get(uid) == None:
> return object.__new__(cls, *args, **kwargs)
> else:
> return meta.Session.query(Informations).get(uid)
> else:
> return object.__new__(cls, *args, **kwargs)
>
> IMHO this could be standard for all classes (why not). I understood
> this SQLAlchemy forces you to write your own class methods wrap
> meta.Session - but this isn't cool :P because i waste my time :P
there is absolutely no reason in the world you are "forced" to write
class methods, except for the fact that you want that particular
pattern. However, if you want that pattern, its utterly absurd to
believe you have to create those methods individually for every
class. They should be implemented on a base class of your choosing:
Session = scoped_session(sessionmaker())
class MyActiveRecordBase(object):
session = Session
def __new__(cls, *args, **kwargs):
if args and self.Session.query(cls).get(args) == None:
return object.__new__(cls, *args, **kwargs)
else:
return object.__new__(cls, *args, **kwargs)
def save(self):
Session.add(self)
Session.flush()
def delete(self):
Session.delete(self)
Session.flush()
also please be aware of the declarative extension at
http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html in
case you find SQLA's Table construct similarly unappealing.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" 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/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---