Skip to content

Commit

Permalink
Adding a setting on the BaseModel called 'inheritable_options' that
Browse files Browse the repository at this point in the history
controls which Meta options can be inherited.  This is all a little
janky unfortunately, might scrap all the Meta stuff in favor of a
cleaner way.
  • Loading branch information
coleifer committed Mar 21, 2011
1 parent b424e73 commit 981c291
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
13 changes: 8 additions & 5 deletions peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,13 +1043,14 @@ def lookup_value(self, lookup_type, value):

class BaseModelOptions(object):
def __init__(self, model_class, options=None):
# configurable options
options = options or {'database': database}
for k, v in options.items():
setattr(self, k, v)

self.rel_fields = {}
self.fields = {}
self.model_class = model_class

# configurable options
options = options or {}
self.database = options.get('database', database)

def get_field_by_name(self, name):
if name in self.fields:
Expand Down Expand Up @@ -1078,6 +1079,8 @@ def rel_exists(self, model):


class BaseModel(type):
inheritable_options = ['database']

def __new__(cls, name, bases, attrs):
cls = super(BaseModel, cls).__new__(cls, name, bases, attrs)

Expand All @@ -1092,7 +1095,7 @@ def __new__(cls, name, bases, attrs):
continue

for (k, v) in base_meta.__dict__.items():
if not k.startswith('_') and k not in attr_dict:
if k in cls.inheritable_options and k not in attr_dict:
attr_dict[k] = v

_meta = BaseModelOptions(cls, attr_dict)
Expand Down
20 changes: 10 additions & 10 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@ def test_inner_joins(self):

def test_outer_joins(self):
sql = SelectQuery(User).join(Blog).sql()
self.assertEqual(sql, ('SELECT t1.* FROM user AS t1 LEFT OUTER JOIN blog AS t2 ON t1.blog_id = t2.id', []))
self.assertEqual(sql, ('SELECT t1.* FROM users AS t1 LEFT OUTER JOIN blog AS t2 ON t1.blog_id = t2.id', []))

sql = SelectQuery(Blog).join(User).sql()
self.assertEqual(sql, ('SELECT t1.* FROM blog AS t1 LEFT OUTER JOIN user AS t2 ON t1.id = t2.blog_id', []))
self.assertEqual(sql, ('SELECT t1.* FROM blog AS t1 LEFT OUTER JOIN users AS t2 ON t1.id = t2.blog_id', []))


class ModelTests(BasePeeweeTestCase):
Expand Down Expand Up @@ -564,25 +564,25 @@ def test_create(self):
u = User.create(username='a')
self.assertEqual(u.username, 'a')
self.assertQueriesEqual([
('INSERT INTO user (username,active,blog_id) VALUES (?,?,?)', ['a', 0, None]),
('INSERT INTO users (username,active,blog_id) VALUES (?,?,?)', ['a', 0, None]),
])

b = Blog.create(title='b blog')
u2 = User.create(username='b', blog=b)
self.assertEqual(u2.blog, b)

self.assertQueriesEqual([
('INSERT INTO user (username,active,blog_id) VALUES (?,?,?)', ['a', 0, None]),
('INSERT INTO users (username,active,blog_id) VALUES (?,?,?)', ['a', 0, None]),
('INSERT INTO blog (title) VALUES (?)', ['b blog']),
('INSERT INTO user (username,active,blog_id) VALUES (?,?,?)', ['b', 0, b.id]),
('INSERT INTO users (username,active,blog_id) VALUES (?,?,?)', ['b', 0, b.id]),
])

def test_get_or_create(self):
u = User.get_or_create(username='a')
self.assertEqual(u.username, 'a')
self.assertQueriesEqual([
('SELECT * FROM user WHERE username = ? LIMIT 1 OFFSET 0', ['a']),
('INSERT INTO user (username,active,blog_id) VALUES (?,?,?)', ['a', 0, None]),
('SELECT * FROM users WHERE username = ? LIMIT 1 OFFSET 0', ['a']),
('INSERT INTO users (username,active,blog_id) VALUES (?,?,?)', ['a', 0, None]),
])

other_u = User.get_or_create(username='a')
Expand Down Expand Up @@ -1174,9 +1174,9 @@ def test_primary_key_index(self):

user_indexes = self.get_sorted_indexes(User)
self.assertEqual(user_indexes, [
('user_active', 0),
('user_blog_id', 0),
('user_id', 1),
('users_active', 0),
('users_blog_id', 0),
('users_id', 1),
])


Expand Down

0 comments on commit 981c291

Please sign in to comment.