コード例 #1
0
ファイル: views.py プロジェクト: jod35/food-tracker-app
def create_user():
    data = request.get_json()

    if data['username']:
        username = data['username']

    if data['email']:
        email = data['email']

    if data['password']:
        password = data['password']

    new_user = User(
        username=username,
        email=email,
    )

    new_user.set_password(password)

    user_schema = UserSchema()

    user = user_schema.dump(new_user)

    return make_response(
        jsonify({
            "message": "Resource added successfully",
            "success": True
        }), 201)
コード例 #2
0
    def setUp(self):
        self.app = app
        self.ctx = self.app.app_context()
        self.ctx.push()
        db.drop_all()
        db.create_all()

        # Creating the 1st user. This user will issue a request
        user = User(username=self.default_username)
        user.set_password_hash(self.default_password)
        db.session.add(user)
        db.session.commit()
        self.client = TestClient(self.app, user.generate_auth_token(), '')

        # Create a request for 1st user
        request_data = {
            'meal_type': 'Vietnamese',
            'meal_time': 'Dinner',
            'location_string': 'San Francisco'
        }
        rv, json = self.client.post(API_VERSION + '/requests/',
                                    data=request_data)
        self.request_location = rv.headers['Location']

        # Create the 2nd user. This user will make proposal for the request by
        # 1st user
        user_2 = User(username='******')
        user_2.set_password_hash('123456')
        db.session.add(user_2)
        db.session.commit()
        self.client = TestClient(self.app, user_2.generate_auth_token(), '')
コード例 #3
0
 def setUp(self):
     self.app = app
     self.ctx = self.app.app_context()
     self.ctx.push()
     db.drop_all()
     db.create_all()
     user = User(username=self.default_username)
     user.set_password_hash(self.default_password)
     db.session.add(user)
     db.session.commit()
     self.client = TestClient(self.app, user.generate_auth_token(), '')
コード例 #4
0
def add_user():
    try:
        username = request.json['username']
        if User.query.filter_by(username=username).first():
            raise ValidationError('Username already existed!')
    except KeyError as e:
        raise ValidationError('Invalid user info: missing ' + e.args[0])
    user = User()
    user.import_data(request.json)
    db.session.add(user)
    db.session.commit()

    return {}, 201, {'Location': user.get_url()}
コード例 #5
0
def client():
    test_client = app.test_client()
    db.drop_all()
    db.create_all()
    user = User(username='******')
    user.set_password_hash('123456')
    db.session.add(user)
    db.session.commit()

    yield test_client
    """
		tearDown code
	"""
    db.session.remove()
    db.drop_all()
コード例 #6
0
def oauth_callback(provider):
    if provider not in app.config['OAUTH_CREDENTIALS']:
        response = jsonify({
            'status': 401,
            'error': 'provider not found',
            'message': '{0} is not supported'.format(provider)
        })
        response.status_code = 401

        return response

    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()

    if username is not None:
        user_data = {
            'username': username,
            'email': email,
            'photo_url': '',
            'password': os.urandom(24)
        }

        tmp_user = User.query.filter_by(username=user_data['username']).first()

        if tmp_user is None:
            new_user = User()
            new_user.import_data(user_data)
            db.session.add(new_user)
            db.session.commit()
            tmp_user = new_user

        return jsonify({'token': tmp_user.generate_auth_token()})

    response = jsonify({
        'status': 401,
        'error': 'access denied',
        'message': 'user does not authorized access'
    })
    response.status_code = 401

    return response
コード例 #7
0
def test_password_hashing():
    with app.app_context():
        db.create_all()

        new_user = User(username='******', email='*****@*****.**')

        password = '******'

        new_user.set_password(password)

        new_user.save()

        assert new_user.check_password('password') == True
コード例 #8
0
def add_users():
    """Register new user.
    获取产品
    添加用户
    添加到推荐人列表
    添加购买列表
    """
    form = RegisterForm(request.form)
    if form.validate_on_submit():
        if current_user.is_admin or current_user.is_center:

            tuijianren = User.query.filter_by(
                phone=form.tuijianren.data).first()
            if not tuijianren:
                flash('已注册失败.推荐人不存在,请填写正确的手机号。', 'danger')
                return dict(form=form)
            baodan = User.query.filter_by(phone=form.baodan.data).first()
            if not tuijianren:
                flash('已注册失败.报单中心不存在,请填写正确的手机号。', 'danger')
                return dict(form=form)

            product = Products.query.get_or_404(int(form.products.data))
            user = User.create(username=form.user.data,
                               password=form.password.data,
                               phone=form.user.data,
                               active=False,
                               parent_center=baodan.id)
            ProductsBuys.create(
                user=user,
                product=product,
            )
            Recommend.create(recommender_id=tuijianren.id,
                             recommends_id=user.id)
            flash('已注册成功.', 'success')
        else:
            flash('已注册失败.您不是报单中心。', 'danger')
        return redirect(url_for('public.home'))
    else:
        flash_errors(form)
    return dict(form=form)
コード例 #9
0
def test_user_creation():

    with app.app_context():
        db.create_all()

        username = '******'
        email = '*****@*****.**'
        password = '******'

        new_user = User(username=username, email=email)

        new_user.set_password(password)

        new_user.save()

        user = User.query.filter_by(username=username).first()

        assert user.username == username
コード例 #10
0
ファイル: views.py プロジェクト: jod35/FS-blog
def create_new_user():
    """    Create user endpoint    ---    parameters:- in: bodyname: bodyschema:id: UserSignuprequired:- username- password- emailproperties:username:type: stringdescription: Unique username of the userdefault: "Johndoe"password:type: stringdescription: Password of the user
    127default: "somethingstrong"email:type: stringdescription: email of the userdefault: "*****@*****.**"responses:201:description: User successfully createdschema:id: UserSignUpSchemaproperties:code:type: string422:description: Invalid input argumentsschema:id: invalidInputproperties:code:type: stringmessage:type: string"""
    data = request.get_json()

    new_user = User(username=data['username'],
                    email=data['email'],
                    passwd_hash=data['passwd_hash'])
    new_user.hash_password(new_user.passwd_hash)

    new_user.create()

    user_schema = UserOutputSchema()

    user = user_schema.dump(new_user)
    print(user)

    return make_response(
        jsonify({
            "message": "User Resource Created Successfully",
            "Success": True,
            "user": user
        }))
コード例 #11
0
def verify_auth_token(token, unused_password):
    g.user = User.verify_auth_token(token)

    return g.user is not None
コード例 #12
0
def add_user(username, password):
    user = User(username=username)
    user.set_password_hash(password)
    db.session.add(user)
    db.session.commit()
    print "Added {0}!".format(user.username)
コード例 #13
0
from main.models.users import User
from main.utils.database import db
from main import app
from main.config import TestConfig

app.config.from_object(TestConfig)
db.init_app(app)

with app.app_context():
    db.create_all()

    new_user = User(username='******', email='*****@*****.**')

    new_user.set_password('testpassword')

    new_user.save()


def test_password_reset():

    old_password = '******'

    new_password = '******'

    new_user.reset_password(old_password, new_password)

    assert new_user.check_password('newpassword') == True
コード例 #14
0
ファイル: __init__.py プロジェクト: anaf007/t923
def load_user(user_id):
    """Load user by ID."""
    return User.get_by_id(int(user_id))