def user_model(database):
"""Fixture that provides a simple User model."""
class User(Model):
email = peewee.CharField(max_length=100, null=False, unique=True)
password = PasswordField(null=False, iterations=4)
active = peewee.BooleanField(null=False, default=True)
@classmethod
def base_query(cls):
# Only query for active Users
return super(User, cls).base_query().where(cls.active == True)
try:
@pre_save(sender=User)
def validate_email(sender, instance, created):
# Ensure that the email is valid.
assert '@' in instance.email
@post_save(sender=User)
def send_welcome_email(sender, instance, created):
# Send the user an email when they are created initially.
if created:
print('Sending welcome email to {}'.format(instance.email))
except ValueError:
# This gets hit because you can't connect an event listener more than
# once and for some reason this fixture is sticking around.
pass
User.create_table(True)
yield User
评论列表
文章目录