Example #1
0
def users():

    s = get_db_session()

    if request.method == 'POST':

        sub = (s.query(Subscription).filter(
            Subscription.subscriber_id == g.user.id).filter(
                Subscription.target_id == request.form['sub']))

        if sub.first():
            sub.delete()
            s.commit()
        else:
            print('create')
            s.add(
                Subscription(subscriber_id=g.user.id,
                             target_id=request.form['sub']))
            s.commit()

    users = (s.query(User).filter(User.id != g.user.id).all())

    subs = get_users_subs(g.user.id)

    users_dict = {}
    for u in users:
        users_dict[u.id] = {
            'id': u.id,
            'subscribed': u.id in subs,
            'nick': u.nick,
        }
    print(subs)
    print(users_dict)

    return render_template('blog/users.html', users=users_dict)
Example #2
0
def root():

    s = get_db_session()

    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = 'Title required'
        elif not body:
            error = 'Post has no content'

        if error is None:
            s.add(Post(author_id=g.user.id, title=title, body=body))
            s.commit()
            flash('Post added successfully')
        else:
            flash(error)

    posts = (s.query(Post, User).join(
        Subscription, Post.author_id == Subscription.target_id).join(
            User, Post.author_id == User.id).filter(
                Subscription.subscriber_id == g.user.id).all())

    return render_template('blog/blog.html', posts=posts)
Example #3
0
def register():
    if request.method == 'POST':
        nick = request.form['nick']
        username = request.form['username']
        password = request.form['password']
        s = get_db_session()
        error = None

        if not nick:
            error = 'Nick required'
        elif not username:
            error = 'Username required'
        elif not password:
            error = 'Passwd required'
        elif s.query(User).filter(User.nick == nick).all():
            error = 'Nick {} is already taken.'.format(nick)
        elif s.query(User).filter(User.username == username).all():
            error = 'User {} is already registered.'.format(username)

        if error is None:
            password = generate_password_hash(password)
            s.add(User(nick=nick, username=username, password=password))
            s.commit()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
Example #4
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        s = get_db_session()
        g.user = s.query(User).filter(User.id == user_id).one()
Example #5
0
def test_register(client, app):
    assert client.get('/auth/register').status_code == 200
    response = client.post('/auth/register',
                           data={
                               'nick': 'a',
                               'username': '******',
                               'password': '******'
                           })
    assert 'http://localhost/auth/login' == response.headers['Location']

    with app.app_context():
        s = get_db_session()
        assert len(s.query(User).filter(User.username == 'a').all()) == 1
Example #6
0
def create_test_data():
    s = get_db_session()

    s.add(
        User(
            username='******',
            password=
            '******',
            nick='test_nick'))
    s.add(
        User(
            username='******',
            password=
            '******',
            nick='test2'))
    s.add(Subscription(subscriber_id=1, target_id=2))
    s.add(
        Post(title='my title',
             body='my post content\nand some more',
             author_id=2))

    s.commit()
Example #7
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        s = get_db_session()
        error = None
        try:
            user = s.query(User).filter(User.username == username).one()
        except NoResultFound:
            user = None

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user.password, password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()
            session['user_id'] = user.id
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')
Example #8
0
def get_users_subs(user_id):
    s = get_db_session()
    subs = (s.query(
        Subscription.target_id).filter(Subscription.subscriber_id == user_id))
    return [s.target_id for s in subs]