How to avoid wrapper classes? - I have both: `User(object)` and `User(StdModel)`

26 views
Skip to first unread message

Alec Taylor

unread,
Jan 22, 2014, 1:24:05 AM1/22/14
to python...@googlegroups.com
How do I avoid wrapper classes? - Here is a fully working; self-contained example using only Python's stdlib and python-stdnet:

from stdnet.odm import SymbolField, CharField, StdModel, Router
from uuid import uuid4
from hashlib import sha512


class EmailField(SymbolField):
    def to_python(self, value, backend=None):
        value = super(EmailField, self).to_python(value, backend)
        assert '@' in value
        return value


class UserWrapper(object):
    def __init__(self, models_obj):
        self.models_obj = models_obj

    def login(self, email, password):
        found_user = self.models_obj.user.filter(email=email)

        if not found_user:
            pass
        elif found_user[0].password == sha512(found_user[0].salt + password).hexdigest():
            return {'successful_login': True}
        return {'successful_login': False}

    def register(self, email, password):
        salt = uuid4().hex
        hashed_password = sha512(salt + password).hexdigest()
        self.models_obj.user.new(email=email, password=hashed_password, salt=salt)

        return {'successful_registration': True}


class User(StdModel):
    email = EmailField(primary_key=True, unique=True)
    password = CharField()
    salt = CharField()


if __name__ == '__main__':
    models = Router('redis://localhost:6379?db=3')
    models.register(User)
    user_wrapper_obj = UserWrapper(models_obj=models)
    email_and_pass = 'f...@bar.com', 'passwd'
    assert user_wrapper_obj.register(*email_and_pass) == {'successful_registration': True}
    assert user_wrapper_obj.login(*email_and_pass) == {'successful_login': True}

lsbardel

unread,
Jan 22, 2014, 8:38:32 AM1/22/14
to python...@googlegroups.com

On Wednesday, January 22, 2014 6:24:05 AM UTC, Alec Taylor wrote:
How do I avoid wrapper classes? - Here is a fully working; self-contained example using only Python's stdlib and python-stdnet:

 
Use a custom manager

Something along these lines
from stdnet import odm

class UserManager(odm.Manager):

    def create_user(self, email, password):
        salt = uuid4().hex
        hashed_password = sha512(salt + password).hexdigest()
        return self.new(email=email, password=hashed_password, salt=salt)


class User(StdModel):
    email = EmailField(primary_key=True, unique=True)
    password = CharField()
    salt = CharField()

    manager_class = UserManager



if __name__ == '__main__':
    models = odm.Router('redis://localhost:6379?db=3')
    models.register(User)
    user1 = modesl.user.create_user('e...1@foo.com', 'passw1')
    user2 = modesl.user.create_user('e...2@foo.com', 'passw2')


Alec Taylor

unread,
Jan 22, 2014, 6:23:59 PM1/22/14
to python...@googlegroups.com
Thanks, I will do that :)
    user1 = modesl.user.create_user('e...1...@foo.com', 'passw1')
    user2 = modesl.user.create_user('e...2...@foo.com', 'passw2')


Reply all
Reply to author
Forward
0 new messages