Example #1
0
def user(username, page=1):
    userController = UserController()
    user = userController.query_byname(username=username)
    if user == None:
        flash('User:'******'not found')
        return redirect('/')

    # posts = [{'author':user,'body':'Test post #1'},{'author':user,'body':'Test post #2'}]
    # posts = current_user.followed_posts()
    followed_posts_results = current_user.followed_posts(page=page)
    # posts =current_user.show_all_posts()
    # posts = current_user.show_all_posts()

    posts = followed_posts_results['posts']

    has_pre_page = followed_posts_results['has_pre_page']
    pre_page_num = followed_posts_results['pre_page_num']
    has_next_page = followed_posts_results['has_next_page']
    next_page_num = followed_posts_results['next_page_num']

    return render_template('user.html',
                           user=user,
                           posts=posts,
                           has_pre_page=has_pre_page, pre_page_num=pre_page_num,
                           has_next_page=has_next_page, next_page_num=next_page_num
                           )
Example #2
0
def login():
    #如果已经LOGIN,重定向到首页
    if current_user.is_authenticated:
        return redirect('/')
    form = LoginForm()

    if form.validate_on_submit():
        # flash('username="******", password='******'/')

        else:
            flash('username or password is wrong')
            return redirect('auth/login')

    else:
        template_name = 'login.html'
        return render_template(template_name, title='Sign In', form=form)
Example #3
0
def index(page=1):
    user = current_user
    form = PostForm()

    if form.validate_on_submit():
        post_body = form.post.data
        user_id = user.id
        nickname = user.nickname
        userController = UserController()
        userController.addpost(user_id=user_id,
                               nickname=nickname,
                               post_body=post_body)
        return redirect(url_for('index'))

    # get posts
    followed_posts_results = current_user.followed_posts(page=page)
    posts = followed_posts_results['posts']
    has_pre_page = followed_posts_results['has_pre_page']
    pre_page_num = followed_posts_results['pre_page_num']
    has_next_page = followed_posts_results['has_next_page']
    next_page_num = followed_posts_results['next_page_num']

    template_naeme = 'posts.html'
    return render_template(template_naeme,
                           title='Posts',
                           posts=posts,
                           user=user,
                           form=form,
                           has_next_page=has_next_page,
                           next_page_num=next_page_num,
                           has_pre_page=has_pre_page,
                           pre_page_num=pre_page_num)
Example #4
0
def before_request():
    # current_user
    if current_user.is_authenticated:
        userController = UserController()

        current_user.last_seen = userController.set_timpstamp()

        userController.update(current_user)
        g.search_form = SearchForm()
Example #5
0
def delete(id):
    userController = UserController()
    post = userController.search_post_byId(id=id)
    if post == None:
        flash('Post not found.')
        return redirect(url_for('index'))
    if post.author.id != current_user.id:
        flash('You cannot delete this post.')
        return redirect(url_for('index'))

    # 删除Post
    userController.delete_post(post)
    flash('Your post has been deleted.')

    return redirect(url_for('index'))
Example #6
0
def unfollow(username):
    userController = UserController()
    user = userController.query_byname(username)
    if user is None:
        flash('User %s not found.' % username)
        return redirect(url_for('index'))
    if user == current_user:
        flash('You can\'t unfollow yourself!')
        return redirect(url_for('user', username=username))
    u = current_user.unfollow(user)
    if u is None:
        flash('Cannot unfollow ' + username + '.')
        return redirect(url_for('user', username=username))
    flash('You have stopped following ' + username + '.')
    return redirect(url_for('user', username=username))
Example #7
0
def search_results(query):
    results = search_by_text(query)
    post_ids = []
    for item in results:
        post_ids.append(item['post_id'])

    if len(post_ids) == 0:
        return redirect(url_for('index'))
    else:
        userController = UserController()
        posts = userController.search_posts(post_ids=post_ids)
        template_name = 'search_results.html'
        return render_template(template_name,
                               title='Search Results',
                               posts=posts,
                               query=query)
Example #8
0
def follow(username):
    userController = UserController()
    user = userController.query_byname(username)
    if user is None:
        flash('User %s not found.' % username)
        return redirect(url_for('index'))
    if user == current_user:
        flash('You can\'t follow yourself!')
        return redirect(url_for('user', username=username))
    u = current_user.follow(user)
    if u is None:
        flash('Cannot follow ' + username + '.')
        return redirect(url_for('user', username=username))
    flash('You are now following ' + username + '!')
    follower_notification(user, current_user)

    return redirect(url_for('user', username=username))
Example #9
0
def newuser():
    form = NewUserForm()
    if form.validate_on_submit():
        username = form.username.data
        userController = UserController()
        user = userController.query_byname(username)
        if user:
            flash(_('The username is signned up, please another username!'))
            render_template('newuser.html',title='Sign Up',form=form)
        else:
            password = form.password.data
            nickname = form.nickname.data
            description = form.description.data
            user = userController.add(username=username,password=password,nickname=nickname,description=description)
            login_user(user)
            return redirect(url_for('user',username=username))


    return render_template('newuser.html',title='Sign Up',form=form)
Example #10
0
def search_results(query):
    print("query:", query)
    results = search_by_text(query)
    print('results:', results)
    post_ids = []

    for item in results:
        post_ids.append(item['post_id'])

    if len(post_ids) == 0:
        flash('Word: '+query + ' is not matched!')
        return redirect(url_for('index'))

    else:
        userController = UserController()
        posts = userController.search_posts(post_ids=post_ids)


        template_name = 'search_results.html'

        return render_template(template_name, title='Search Resluts', posts=posts, query=query)
Example #11
0
def login():
    if current_user.is_authenticated:
        return redirect('/')

    form = LoginForm()
    if form.validate_on_submit():
        username = form.username.data
        password = form.password.data
        user_controller = UserController()
        user = user_controller.query(username=username, password=password)
        print(user)

        if user:
            login_user(user)
            return redirect('/')
        else:
            flash('username or password is worry')
            return redirect('/auth/login')
    else:
        template_naeme = 'login.html'
        return render_template(template_naeme, title='Sign In', form=form)
Example #12
0
def user(username,page=1):
    userController = UserController()
    user = userController.query_byname(username=username)

    if user == None:
        flash('User ' + username + ' not found.')
        return redirect('/')
    #getposts
    followed_posts_results = current_user.followed_posts(page=page)
    posts = followed_posts_results['posts']
    has_pre_page = followed_posts_results['has_pre_page']
    pre_page_num = followed_posts_results['pre_page_num']
    has_next_page = followed_posts_results['has_next_page']
    next_page_num = followed_posts_results['next_page_num']


    template_naeme = 'user.html'

    return render_template(template_naeme, title='Posts', posts=posts,
                           user=user, has_next_page=has_next_page,
                           next_page_num=next_page_num, has_pre_page=has_pre_page,
                           pre_page_num=pre_page_num)
Example #13
0
def index(page=1):
    '''
    user = {'nickname':'Miguel'}  #fake user
    template_name ='index.html'
    return render_template(template_name,title='Home',user=user)
    '''
    userController = UserController()
    form = PostForm()
    user = current_user
    if form.validate_on_submit():
        post_body = form.post.data
        user_id = user.id
        user_nickname = user.nickname
        # userController = UserController()
        userController.addpost(user_id=user_id, nickname=user_nickname, post_body=post_body)
        flash('Your post is now living!')

        return redirect(url_for('index'))

    # fakearray of posts
    '''posts=[{'author':{'nickname':'John'},'body':'Beautiful day in Portland!'},
           {'author':{'nickname':'Susan'},'body':'The Avengers movie was so coolbbbbbb!!'}]'''

    followed_posts_results = current_user.followed_posts(page=page)
    # posts =current_user.show_all_posts()
    # posts = current_user.show_all_posts()
    posts = followed_posts_results['posts']

    has_pre_page = followed_posts_results['has_pre_page']
    pre_page_num = followed_posts_results['pre_page_num']
    has_next_page = followed_posts_results['has_next_page']
    next_page_num = followed_posts_results['next_page_num']

    template_name = 'posts.html'

    return render_template(template_name, title='Home', user=user, posts=posts, form=form,
                           has_pre_page=has_pre_page, pre_page_num=pre_page_num,
                           has_next_page=has_next_page, next_page_num=next_page_num)
Example #14
0
def follow(username):
    userController = UserController()

    user = userController.query_byname(username)

    if user is None:
        flash('User %s is not found!' % username)
        return redirect(url_for('index'))

    if user == current_user:
        flash('You can\'t follow yourself!')
        return redirect(url_for('user', username=username))

    u = current_user.follow(user)
    if u is None:
        flash('Can not follow' + username + '.')
        return redirect(url_for('user', username=username))

    # session.add(u)
    # session.commit()

    flash('You are now following' + username + '!')
    return redirect(url_for('user', username=username))
Example #15
0
 def test_get_imgpath(self):
     userController = UserController()
     user = userController.query_byname('test2')
     img_path = user.get_imgpath()
     self.assertEqual(img_path, '../static/resources/default.jpg')
Example #16
0
def before_request():
    if current_user.is_authenticated:
        current_user.last_seen = datetime.utcnow()
        userController = UserController()
        userController.update(current_user)
        g.search_form = SearchForm()
Example #17
0
def user_load(user_id):
    user_controller = UserController()
    user = user_controller.query_byId(user_id=int(user_id))
    return user