Пример #1
0
    def index(self):
        response = {'error': {'message': 'Username does not exist.'}}

        username = request.args.get('username', default=None, type=str)

        if username is None:
            return jsonify(response), 400

        user = User.find_by_identity(username)

        if user is None:
            return jsonify(response), 404

        editable = False
        if username == current_user.username:
            editable = True

        facts = Fact.query.filter_by(user_id=user.id).order_by(
            Fact.created_on.desc())

        response = {
            'data': facts_schema.dump(facts).data,
            'editable': editable
        }

        return jsonify(response), 200
Пример #2
0
    def post(self):
        json_data = request.get_json()

        if not json_data:
            response = {'error': 'Invalid input'}

            return jsonify(response), 400

        try:
            data = auth_schema.load(json_data)
        except ValidationError as err:
            response = {'error': err.messages}

            return jsonify(response), 422

        user = User.find_by_identity(data['identity'])

        if user and user.authenticated(password=data['password']):
            access_token = create_access_token(identity=user.username)

            response = jsonify({'data': {'access_token': access_token}})

            # Set the JWTs and the CSRF double submit protection cookies.
            set_access_cookies(response, access_token)

            return response, 200

        response = jsonify(
            {'error': {
                'message': 'Invalid identity or password'
            }})

        return response, 401
Пример #3
0
def seed():
    """
    Seed the database with an initial user.

    :return: User instance
    """
    if User.find_by_identity(app.config['SEED_USER_EMAIL']) is not None:
        return None

    params = {
        'email': app.config['SEED_USER_EMAIL'],
        'username': app.config['SEED_USER_USERNAME'],
        'password': app.config['SEED_USER_PASSWORD']
    }

    return User(**params).save()
Пример #4
0
def ensure_unique_identity(data):
    user = User.find_by_identity(data)

    if user:
        raise ValidationError('{0} already exists'.format(data))

    return data
Пример #5
0
def users():
    """
    Generate fake users.
    """
    random_emails = []
    data = []

    click.echo('Working...')

    # Ensure we get about 20 unique random emails.
    for i in range(0, 19):
        random_emails.append(fake.email())

    random_emails.append(app.config['SEED_USER_EMAIL'])
    random_emails = list(set(random_emails))

    while True:
        if len(random_emails) == 0:
            break

        created_on = fake.date_time_between(start_date='-1y',
                                            end_date='now').strftime('%s')
        created_on = datetime.utcfromtimestamp(
            float(created_on)).strftime('%Y-%m-%dT%H:%M:%S Z')

        email = random_emails.pop()

        random_trail = str(int(round((random.random() * 1000))))
        username = fake.first_name() + random_trail

        params = {
            'created_on': created_on,
            'updated_on': created_on,
            'email': email,
            'username': username.lower(),
            'password': User.encrypt_password('password')
        }

        # Ensure the seeded user has the seeded config settings.
        if email == app.config['SEED_USER_EMAIL']:
            params['username'] = app.config['SEED_USER_USERNAME']
            password = User.encrypt_password(app.config['SEED_USER_PASSWORD'])
            params['password'] = password

        data.append(params)

    return _bulk_insert(User, data, 'users')
Пример #6
0
    def post(self):
        json_data = request.get_json()

        if not json_data:
            response = jsonify({'error': 'Invalid input'})

            return response, 400

        data, errors = registration_schema.load(json_data)

        if errors:
            response = {'error': errors}

            return jsonify(response), 422

        user = User()
        user.email = data.get('email')
        user.username = data.get('username')
        user.password = User.encrypt_password(data.get('password'))
        user.save()

        return jsonify(data), 200