Пример #1
0
def user(*args, **kwargs):
    args = current_app.config['args']
    headers = current_app.config['headers']

    response = Response()
    route_params = request.view_args
    get_params = request.args.to_dict(flat=False)
    if request.method in ['POST', 'PUT', 'DELETE']:
        body = request.json

    message = []
    data = None
    error = None
    try:
        if request.method == 'POST':
            data = User(**body)
            data.save()
            data = data.to_mongo()
        elif request.method == 'GET':
            data = [obj.to_mongo() for obj in User.objects]
        elif request.method == 'PUT':
            _id = route_params['user_id']
            data = User.objects(id=bson.ObjectId(_id))
            for attr in [
                    'first_name', 'last_name', 'email', 'password',
                    'authorization'
            ]:
                if body.get(attr) is not None:
                    setattr(data, attr, body[attr])
            data.save()
            data = data.to_mongo()
        elif request.method == 'DELETE':
            _id = route_params['user_id']
            data = User.objects(id=bson.ObjectId(_id))
            data.delete()
        elif request.method == 'OPTIONS':
            pass
        else:
            pass

    except Exception as e:
        error = AugmentedException(e).to_dict()
        LOGGER.error('', exc_info=True)

    response = jsonify(message=message, data=data, error=error)
    response = add_headers(response, headers=headers)
    LOGGER.warning(vars(response))
    return response
Пример #2
0
    def post(self):
        id = request.form.get('id')
        pw = request.form.get('pw')

        if User.objects(id=id):
            return '', 204
        else:
            User(id=id, pw=pw).save()
            return '', 201
Пример #3
0
    def post(self):
        id = str(current_identity)
        title = request.form.get('title')
        content = request.form.get('content')

        PostCol(title=title,
                content=content,
                author=User(id,
                            User.objects(id=id).first().pw)).save()

        return '', 201
Пример #4
0
    def test_create_user(self):
        """ Create user in db
        Should: return save user in db """

        user = User(username='******',
                    email="*****@*****.**",
                    password="******",
                    provider="Tutubo")
        user.save()

        fresh_user = User.objects().first()
        assert fresh_user.username == 'oli'
        assert fresh_user.email == '*****@*****.**'
        assert fresh_user.password == '123'
        assert fresh_user.provider == 'Tutubo'
Пример #5
0
    def user_oauth_login():
        body = request.get_json()
        if (not body or not OAUTH_FIELD in body.keys()):
            return error_response(400, 'Cant verify login credentials')

        try:
            idinfo = id_token.verify_oauth2_token(
                body['idToken'],
                requests.Request()) if not app.config['TESTING'] else {
                    'email': body['idToken']
                }
            email = idinfo['email']
            user = User.objects(email=email)
            if not user:
                username = email.split('@')[0]
                username = "******" + username
                photo = body['photoURL'] if 'photoURL' in body else None
                user = User(email=email,
                            profile_pic=photo,
                            username=username,
                            provider="Google").save()
            else:
                user = user[0]
                if user.is_blocked:
                    return error_response(401, "User is blocked")

            token = jwt.encode(
                {
                    'email': user.email,
                    'exp':
                    datetime.datetime.utcnow() + datetime.timedelta(days=7)
                },
                app.config['SECRET_KEY'],
                algorithm=ENCODING_ALGORITHM)
            return jsonify({
                'token': token.decode('UTF-8'),
                "user": user.serialize()
            })
        except ValueError as err:
            return error_response(401,
                                  'Cant verify Google credentials ' + str(err))
Пример #6
0
def authenticate(id, pw):
    if id and pw and User.objects(id=id, pw=pw):
        return UserModel(id=id, pw=pw)
Пример #7
0
 def get_users():
     users = jsonify(
         list(map(lambda user: user.serialize_admin(), User.objects())))
     users.status_code = 200
     return users