[sqlalchemy] Re: A question about best practices after the Object Relational Tutorial

2009-02-17 Thread Ants Aasma

I have written a descriptor for implementing hashed storage of
passwords. I added some documentation and added it to the wiki as a
recipe:

http://www.sqlalchemy.org/trac/wiki/UsageRecipes/HashProperty

It doesn't depend on anything in SQLAlchemy and works with any object
persistence framework. It could probably use some better null handling
and error reporting, but should be usable as is.

Ants
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: A question about best practices after the Object Relational Tutorial

2009-02-16 Thread Jessica Meyer

Thank you for your replies.

With helper code I mean:

I want the architecture of my program so:

  - There's a database
  - Only SQLAlchemy talks to the database
  - There is a function for every task that SQLAlchemy does

So, I want to do something like this in the end: addCustomer
(login='mylogin', password='secret', ...) and the function
addCustomer checks input, creates a hash of the password and so on..
And that helper function then uses the SQLAlchemy API. I mean, I think
you're all doing this too, since nobody wants to call the SQLalchemy
API every time again and again.

Thanks
Jess

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: A question about best practices after the Object Relational Tutorial

2009-02-16 Thread Randall Smith

I would put the addCustomer method on something other than the customer 
object, like the thing you're adding the customer to.  Or bettery yet, 
you'd probably be just appending the customer.  I think for your 
specific example, you should handle customer creation stuff (login + 
password) in __init__.

class Customer(object):

 def __init__(self, login, secret):
 self.password = secret
 self.login = login

 # from Michael's example
 def _set_password(self, pw):
 self._password = sha1(pw)

 def _get_password(self):
 return self._password

 # other helper functions.

 def activate(self):
 self.status = 'a'

 def inactivate(self):
 self.status = 'i'

 def annoyOnThePhone(self):
 dialAsteriskAndAnnoy(self.phone_number)

 def sendMonthlyBill(self):
 # stuff
 pass


customer = Customer('thelogin', 'thesecret')
store.customers.append(customer)
customer.activate()

--Randall

Jessica Meyer wrote:
 Thank you for your replies.
 
 With helper code I mean:
 
 I want the architecture of my program so:
 
   - There's a database
   - Only SQLAlchemy talks to the database
   - There is a function for every task that SQLAlchemy does
 
 So, I want to do something like this in the end: addCustomer
 (login='mylogin', password='secret', ...) and the function
 addCustomer checks input, creates a hash of the password and so on..
 And that helper function then uses the SQLAlchemy API. I mean, I think
 you're all doing this too, since nobody wants to call the SQLalchemy
 API every time again and again.
 
 Thanks
 Jess


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: A question about best practices after the Object Relational Tutorial

2009-02-15 Thread az

 I know how to query the database, and to add a new database entry.
 But I really wanna know where my helper code goes. My idea is that
 there is a function addCustomer() in the Customer class, but I
 tried this out and it didnt work. Any suggestions or examples how
 to proceed?
if in the class, it ought to be a classmethod, alternative constructor 
of sorts. 
helper code - when would be that executed? 
if at creation, but not at load-from-db, just put in the __init__...

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: A question about best practices after the Object Relational Tutorial

2009-02-15 Thread Michael Bayer

for passwords I usually use a descriptor:

def _set_password(self, pw):
 self._password = sha1(pw)

def _get_password(self):
return self._password

password = property(_get_password, _set_password)

the mapper would map '_password' to customer_table.c.password.   Above  
is assuming one-way encryption for the password which is typical.

the other way to do it would be to use a TypeDecorator.

On Feb 15, 2009, at 5:57 AM, Jessica Meyer wrote:


 Hi there

 I just completed the Object Relational Tutorial [1] and want to use
 that knowledge to start a little project of mine.

 So I have a customer table in my database, and I want a helper
 function that converts the password to a SHA1 hash and performs other
 things. This is just a simple example and I wanted to ask you guys how
 I should do this.

 So my table just looks like this:

 customer_table = Table(
'customer', metadata,
Column('id', Integer, primary_key=True),
Column('login', Unicode(25), nullable=False),
Column('password', Unicode(40), nullable=False),
Column('firstname', Unicode(40), nullable=False),
Column('lastname', Unicode(40), nullable=False),
Column('street', Unicode(40)),
Column('zip', Unicode(10)),
Column('city', Unicode(40)),
Column('country_id', Integer, ForeignKey('country.id')),
Column('phonebusiness', Unicode(20)),
Column('phoneprivate', Unicode(20)),
Column('phonemobile', Unicode(20)),
)

 Then follows a class definition Customer, from what I've learnt in
 the tutorial:

 class Customer(object):
def __init__(self, login, password, firstname, lastname, street,
 plz, city):
self.login = login
self.password = password
self.firstname = firstname
self.lastname = lastname
self.street = street
self.zip = zip
self.city = city
self.phonebusiness = phonebusiness
self.phoneprivate = phoneprivate
self.phonemobile = phonemobile

def __repr__(self):
return Customer('%s', '%s') % (self.login, self.password)

 I know how to query the database, and to add a new database entry. But
 I really wanna know where my helper code goes. My idea is that there
 is a function addCustomer() in the Customer class, but I tried this
 out and it didnt work. Any suggestions or examples how to proceed?

 Thanks!

 [1] http://www.sqlalchemy.org/docs/05/ormtutorial.html

 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---