def save(self):
        staff = self.obj or Staff()
        # do not use populate_obj here, session.add will not work
        staff.full_name = self.full_name.data
        staff.title = self.title.data
        email = self.user.email.data

        if staff.user is None:
            try:
                staff.user = User.query.filter_by(email=email).one()
            except NoResultFound:
                staff.user = User(email=email,
                                  recover_token=str(uuid4()),
                                  recover_time=datetime.now())
                send_activation_mail(staff.user.email,
                                     staff.user.recover_token)
        else:
            staff.user.email = email

        if self.user.is_superuser:
            staff.user.is_superuser = self.user.is_superuser.data

        if staff.id is None:
            db.session.add(staff)
        db.session.commit()
        return staff
Exemplo n.º 2
0
def create_user(ctx):
    email = click.prompt('Enter email', type=str)
    while not validate_email(email):
        email = click.prompt('Invalid email. Enter another email', type=str)
    password = click.prompt('Enter password', type=str, hide_input=True)
    confirm = click.prompt('Enter password again', type=str, hide_input=True)

    if password == confirm:
        app = ctx.obj['app']
        with app.app_context():
            user = User(email=email)
            user.set_password(password)
            db.session.add(user)
            staff = Staff(user=user, full_name='')
            db.session.add(staff)
            db.session.commit()
            click.echo('User has been created')
    else:
        click.echo('Passwords differ')