Exemple #1
0
def fixture_add_user(username=u'chris',
                     password=u'toast',
                     privileges=[],
                     wants_comment_notification=True):
    # Reuse existing user or create a new one
    test_user = LocalUser.query.filter(LocalUser.username == username).first()
    if test_user is None:
        test_user = LocalUser()
    test_user.username = username
    test_user.email = username + u'@example.com'
    if password is not None:
        test_user.pw_hash = gen_password_hash(password)
    test_user.wants_comment_notification = wants_comment_notification
    for privilege in privileges:
        query = Privilege.query.filter(Privilege.privilege_name == privilege)
        if query.count():
            test_user.all_privileges.append(query.one())

    test_user.save()

    # Reload - The `with_polymorphic` needs to be there to eagerly load
    # the attributes on the LocalUser as this can't be done post detachment.
    user_query = LocalUser.query.with_polymorphic(LocalUser)
    test_user = user_query.filter(LocalUser.username == username).first()

    # ... and detach from session:
    Session.expunge(test_user)

    return test_user
Exemple #2
0
def create_basic_user(form):
    user = LocalUser()
    user.username = form.username.data
    user.email = form.email.data
    user.save()
    return user