Пример #1
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('username',
                            help='This field cannot be blank',
                            location='json',
                            required=True)
        parser.add_argument('password',
                            help='This field cannot be blank',
                            location='json',
                            required=True)
        parser.add_argument('email',
                            help='This field cannot be blank',
                            location='json',
                            required=True)
        data = parser.parse_args()

        if not re.match(UserModel.USERNAME_FORMAT, data['username']):
            return {
                'message':
                'Username must consist of letters, numbers, hyphens and underscores'
            }

        if UserModel.find_by_username(username=data['username'],
                                      exclude_deleted=False):
            return {
                'message':
                'Username {} already exists'.format(data['username'])
            }

        if UserModel.find_by_email(email=data['email'], exclude_deleted=False):
            return {
                'message':
                'User with email {} already exists'.format(data['email'])
            }

        new_user = UserModel(
            username=data['username'],
            password=UserModel.generate_hash(data['password']),
            email=data['email'],
            deleted_at=None,
        )

        try:
            new_user.save_to_db()
            access_token = create_access_token(new_user.jwt_serialize())
            refresh_token = create_refresh_token(new_user.jwt_serialize())
            return {
                'message': 'User {} was created'.format(data['username']),
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        except:
            return {'message': 'Something went wrong'}, 400
Пример #2
0
    def run(self):
        username = input("Username: "******"Email: ")
        is_admin = input("Is admin? [y/n]: ")
        password = getpass.getpass("Password: "******"admin"] if is_admin.lower() == 'y' else []

        try:
            new_user = UserModel(
                username = username,
                email = email,
                password = UserModel.generate_hash(password)
            )
            new_user.roles = roles
            new_user.save_to_db()

            print('\n+ User created')
            print('|_ Username: {}'.format(new_user.username))
            print('|_ Email:    {}'.format(new_user.email))
            print('|_ Roles:    {}'.format(new_user.roles))
        except:
            print('User wasn\'t saved')