Esempio n. 1
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data, author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None
    return render_template('index.html', title='Home', form=form,
                           posts=posts.items, next_url=next_url,
                           prev_url=prev_url)
Esempio n. 2
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('index.html',
                           title=('Home'),
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 3
0
def index():
    """
    Responsible for display users posts and creating new posts

    :return: Landing page "Home"
    """
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    today_posts = 0
    subs_name = current_user.subscribe.name

    for post in Post.query.filter_by(author=current_user):
        if post.timestamp.date() == date.today():
            today_posts += 1

    if (subs_name == "Standard" and today_posts < 3) or (
            subs_name == "Advanced" and today_posts < 10) or subs_name == "Pro":

        form = PostForm()
        if form.validate_on_submit():
            language = guess_language(form.post.data)
            if language == 'UNKNOWN' or len(language) > 5:
                language = ''
            post = Post(body=form.post.data, author=current_user,
                        language=language)
            db.session.add(post)
            db.session.commit()
            flash(_('Your post is now live!'))
            return redirect(url_for('main.index'))

        return render_template('index.html', title=_('Home'), form=form,
                               posts=posts.items, next_url=next_url,
                               prev_url=prev_url)
    return render_template('index.html', title=_('Home'),
                           posts=posts.items, next_url=next_url,
                           prev_url=prev_url)
Esempio n. 4
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOW' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data,
                    author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('main.index'))  # 提交完文章返回首页

    posts = [  # 假对象废弃
        {
            'author': {
                'username': '******'
            },
            'body': 'Beautiful day in Portland!'
        }, {
            'author': {
                'username': '******'
            },
            'body': 'The Avengers movie was so cool!'
        }
    ]
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(  # 分页配置
        page, current_app.config['POSTS_PER_PAGE'], False)
    # print(page,posts.has_next,posts.has_prev)
    next_url = url_for('main.index',
                       page=posts.next_num) if posts.has_next else None
    prev_url = url_for('main.index',
                       page=posts.prev_num) if posts.has_prev else None
    return render_template('index.html',
                           title='Home Page',
                           posts=posts.items,
                           form=form,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 5
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        # _() mark for language translation
        flash(_('Your post is now live!'))
        # Post/Redirect/Get - avoids inserting duplicate posts
        return redirect(url_for('main.index'))
    #paginate, next and previous page links
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('index.html', title=_('Home'), form=form,
                           posts=posts.items, next_url=next_url,
                           prev_url=prev_url)
Esempio n. 6
0
def edit_file(key):
    p = Category.query.filter_by(id=key, user=current_user.id).first()
    if not p:
        flash(u'该文章不存在!', 'warning')
        abort(404)
    form = PostForm(title=p.title, text=p.content)
    if request.method == "POST":
        p.title = request.values.get("title")
        p.content = request.values.get("text")
        p.update_time = datetime.datetime.utcnow()
        filename = os.getcwd() + "/markdown/" + str(p.id) + ".pdf"
        if os.path.exists(filename):
            os.system("rm -f {} ".format(filename))
        db.session.add(p)
        db.session.commit()
        t = threading.Thread(target=work,
                             args=(str(p.id), p.content.encode("utf-8")))
        t.start()
        flash(u'保存成功!', 'success')
        return redirect(url_for('main.edit'))
    return render_template('edit.html', form=form)
Esempio n. 7
0
def addpost():
    form = PostForm()
    if form.cancel.data:
        return redirect(url_for('main.index'))
    if form.validate_on_submit():
        if form.submit.data:
            post = Post(clientname=form.clientname.data,
                        clientemail=form.clientemail.data,
                        clientphone=form.clientphone.data,
                        clientaddress=form.clientaddress.data,
                        clientzip=form.clientzip.data,
                        clientcity=form.clientcity.data,
                        clientinfo=form.clientinfo.data,
                        author=current_user)
            db.session.add(post)
            db.session.commit()
            flash('Borrower data successfully added!')
            return redirect(url_for('main.addpost'))
        else:
            return redirect(url_for('main.index'))
    return render_template('addpost.html', title='Add borrower', form=form)
Esempio n. 8
0
def make_post(chat_name):
    form = PostForm(chat_name=chat_name)
    if form.validate_on_submit():
        chat_name = form.chat_name.data

        new_post = Post(title=form.title.data,
                        body=form.body.data,
                        author=current_user)
        new_post.chat = Chat.query.filter_by(name=chat_name).first()

        if 'image' in request.files and request.files['image']:
            filename = images.save(form.image.data)
            url = images.url(filename)
            image = Image(filename=filename, url=url)
            new_post.attachment = image

        db.session.add(new_post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('main.show_chat', name=chat_name))
    return render_template('make_post.html', title="New Post", form=form)
Esempio n. 9
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash(_("Your post is now live!"))
        return redirect(url_for("main.index"))
    page = request.args.get("page", 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, myApp.config["POSTS_PER_PAGE"], False)
    next_url = url_for("main.index", page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for("main.index", page=posts.prev_num) \
        if posts.has_prev else None
    return render_template("index.html",
                           title="Home Page",
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 10
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Jūsu ieraksts tagad ir publicēts!')
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('index.html',
                           title='Sākumlapa',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 11
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.timestamp.desc()).paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index',
                       page=posts.next_num) if posts.has_next else None
    prev_url = url_for('main.index',
                       page=posts.prev_num) if posts.has_prev else None
    return render_template("index.html",
                           title='Index',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 12
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    body=form.body.data,
                    author=current_user._get_current_object())
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    query = Post.query
    pagination = query.order_by(Post.timestamp.desc()).paginate(
        page,
        per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
        error_out=False)
    posts = pagination.items
    return render_template('index.html',
                           title=u'我的博客',
                           form=form,
                           posts=posts,
                           pagination=pagination)
Esempio n. 13
0
def explore():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Ваше сообщение опубликовано')
        return redirect(url_for('main.explore'))
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.timestamp.desc()).paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.explore', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.explore', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template("chat.html",
                           title='Explore',
                           posts=posts.items,
                           form=form,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 14
0
def index():
    if current_user.is_authenticated:
        form = PostForm()
        if form.validate_on_submit():
            post = Post(body=form.post.data, author=current_user)
            db.session.add(post)
            db.session.commit()
            flash('Your post is now live!')
            return redirect(url_for('main.index'))

        page = request.args.get('page', 1, type=int)
        posts = current_user.followed_posts().paginate(page, current_app.config['POSTS_PER_PAGE'], False)
        next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None
        prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None
        usernames = db.session.query(User.username).all()
        usernames = [x[0] for x in usernames]
        return render_template('home.html', title='Home', form=form,
                               posts=posts.items, next_url=next_url,
                               prev_url=prev_url, usernames=usernames)
    else:
        return render_template('index.html', title='Welcome')
Esempio n. 15
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        # try:
        language = detect_lang(form.post.data)
        # except:
        #     language = ''
        # language = guess_language(form.post.data)
        # if language == 'UNKNOWN' or len(language) > 5:
        #     language = ''
        post = Post(body=form.post.data, author=current_user, language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Ваш пост успешно опубликован!'))
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_post().paginate(page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None
    return render_template('index.html', title=_('Домашняя страница'),  form=form, posts=posts.items,
                           next_url=next_url, prev_url=prev_url)
Esempio n. 16
0
def edit_post():
    post_form = PostForm()
    post_id = request.args.get('post')
    img_url = None

    if post_form.validate_on_submit() and 'image' in request.files:
        if post_id and Post.query.get(post_id):
            post = Post.query.get(post_id)
            post.title = post_form.title.data
            post.body = post_form.body.data
            img = Image.query.filter_by(post_id=post_id).first()
            images = save_image(request.files.getlist('image'))
            for url in images:
                img.url = url[0]
                img.url_t = url[1]
            db.session.add(img)
            db.session.add(post)
            flash('文章修改成功')
        else:
            post = Post(title=post_form.title.data,
                        body=post_form.body.data,
                        author=current_user._get_current_object())
            db.session.add(post)
            images = save_image(request.files.getlist('image'))
            for url in images:
                img = Image(url=url[0], url_t=url[1], post=post)
                db.session.add(img)
            flash('文章创建成功')

        return redirect(url_for('.index'))

    if post_id:
        post = Post.query.get_or_404(post_id)
        post_form.title.data = post.title
        post_form.body.data = post.body
        img_url = post.images.first().url_t

    return render_template('create_post.html',
                           post_form=post_form,
                           img_url=img_url)
Esempio n. 17
0
def index():
    #表单实例化
    form = PostForm()
    #如果当前用户能写文章并且提交表格成功
    if current_user.can(Permission.WRITE_ARTICLES) and \
            form.validate_on_submit():
        #post赋值
        post = Post(body=form.body.data,
                    author=current_user._get_current_object())
        #提交会话
        db.session.add(post)
        提交数据
        db.session.commit()
        #重新定向回主页
        return redirect(url_for('.index'))
    #显示关注变量定为假,如果当前用户认证,变量赋值bool值,存储cookie中show——followed字段
    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get('show_followed', ''))
    #如果为真查询国有用户关注的文章
    if show_followed:
        query = current_user.followed_posts
    #否则查询所有文章
    else:
        query = Post.query
    #分页显示,渲染页数获取
    page = request.args.get('page', 1, type=int)
    #显示某页中记录,paginate参数page(显示当前页内容)必选,查询返回pagination对象
    pagination = query.order_by(Post.timestamp.desc()).paginate(
        page,
        per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
        #超出总页显示错误
        error_out=False)
    #显示所有页的文章
    posts = pagination.items
    return render_template('index.html',
                           form=form,
                           posts=posts,
                           show_followed=show_followed,
                           pagination=pagination)
Esempio n. 18
0
def index():
    post_form = PostForm()
    empty_form = EmptyForm()

    if post_form.validate_on_submit():
        create_post(body=post_form.post.data)
        return redirect(url_for('main.index'))

    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('index.html',
                           title=_('Home'),
                           post_form=post_form,
                           empty_form=empty_form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 19
0
def senhansard(date):
    """shows the user the senate hansard for a particular date for more details please seek the definiton of hansard"""
    realdate = date
    hansard = Hansard.query.filter_by(date=date, debate_type="senate").first()
    hansards = Hansard.query.all()
    dates = []
    page = request.args.get('page', 1, type=int)
    heading = hansard.majorheading.paginate(page, 1, False)
    for date in hansards:
        dates.append(str(date.date).replace("-", "/"))
    if hansard is None:
        flash('hansard not found.')
        return redirect(url_for('main.index'))
    form1 = PostForm(identifier="FORM1")
    form2 = DateForm(identifier="FORM2")
    if form1.identifier.data == 'FORM1' and form1.validate_on_submit():
        speech = Speech.query.filter_by(exact_id=form1.hidden.data).first()
        language = guess_language(form1.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form1.post.data,
                    author=current_user,
                    speech=speech,
                    language=language)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('main.senhansard', date=realdate))
    if form2.identifier.data == 'FORM2' and form2.validate_on_submit():
        date = form2.date.data.strftime("%Y-%m-%d")
        return redirect(url_for('main.hansard', date=date))
    return render_template('hansard.html',
                           subtitle="senate debates 🗣",
                           url="/hansard/senate/" + realdate + "/extra?page=",
                           data=hansard,
                           pages=heading.pages,
                           majorheading=heading.items,
                           dates=dates,
                           form1=form1,
                           form2=form2)
Esempio n. 20
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data) # guess the language of the post, if unknown or unexpected long it is treated as unknown
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data, author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post has been published'))
        return redirect(url_for('main.index'))
    
    page = request.args.get('page', 1, type=int)

    # paginate object returns .items with the content for a page, 
    # but also properties such as has_next, has_prev and next_num and prev_num 
    posts = current_user.followed_posts().paginate(page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None

    return render_template('index.html', title=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
Esempio n. 21
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('your post has been sent to the void')
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.following_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    # pylint: disable=bad-continuation
    return render_template('index.html',
                           title='home',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 22
0
def create_post():
    form = PostForm()
    if form.validate_on_submit():
        if form.save.data:
            if form.title.data and form.content.data:
                slug = None
                if not slug:
                    slug = re.sub('[^\w]+', '-', form.title.data.lower())
                post = Post(title=form.title.data,
                            content=form.content.data,
                            slug=slug,
                            published=form.published.data,
                            author=current_user)
                db.session.add(post)
                db.session.commit()
                flash('Post saved successfully.', 'success')
                return redirect(url_for('main.detail', slug=post.slug))
            else:
                flash('Title and Content are required.', 'danger')
        else:
            redirect(url_for('main.index'))
    return render_template('create_post.html', form=form)
Esempio n. 23
0
def index():
    form = PostForm()
    base_configuration = base_navigation.query.all()
    apnews = newsPost.query.order_by(newsPost.timestamp.desc()).limit(5)
    mvnews = movenewsPost.query.order_by(movenewsPost.timestamp.desc()).limit(5)
    oanews = onearticlePost.query.order_by(onearticlePost.timestamp.desc()).limit(5)
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('index.html', title=_('Home'), form=form, apnews=apnews,mvnews=mvnews,oanews=oanews,
                           posts=posts.items, next_url=next_url,
                           prev_url=prev_url,base_configuration=base_configuration)
Esempio n. 24
0
def user(username):
    user = User.query.filter_by(username=username).first_or_404()
    page = request.args.get('page', 1, type=int)
    postform = PostForm()
    if postform.validate_on_submit():
        post = Post(body=postform.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('main.user', username=current_user.username))
    posts = Post.query.order_by(Post.timestamp.desc()).paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.user', username=user.username, page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.user', username=user.username, page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('user.html',
                           user=user,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url,
                           postform=postform)
Esempio n. 25
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)  # to handle what the language is and save to the db-table
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data, author=current_user, language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))

        # it will be GET method of request when client refresh current page, instead POST!
        return redirect(url_for('bp.index'))  # Post/Redirect/Get schema
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False
    )
    next_url = url_for('bp.index', page=posts.next_num) if posts.has_next else None
    prev_url = url_for('bp.index', page=posts.prev_num) if posts.has_prev else None
    return render_template('index.html', title='Home Page', form=form,
                           posts=posts.items, next_url=next_url,
                           prev_url=prev_url)
Esempio n. 26
0
def index():
    # API - Random
    response_random = requests.get(
        'https://www.thecocktaildb.com/api/json/v1/1/random.php')
    random_drink = response_random.json()

    response_category = requests.get(
        'https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail')
    category_drink = response_category.json()
    # print(category_drink)

    # data=data['drinks']
    # category_drink=category_drink['drinks'])

    form = PostForm()
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None

    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('main.index'))

    return render_template('index.html',
                           title="Home",
                           active_page="home",
                           posts=posts.items,
                           form=form,
                           next_url=next_url,
                           prev_url=prev_url,
                           random_drink=random_drink['drinks'],
                           category_drink=category_drink['drinks'])
Esempio n. 27
0
def index(
):  #need to inspect next parameter in login(), as hacker can put protected page address in next and it will redirect there.

    #user = {"name" : "lola"}  #not required as index.html take it from db
    #form to accept post from user
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data,
                    author=current_user)  #author is backref from user to post
        db.session.add(post)
        db.session.commit()
        print("=========post submitted===========")
        flash("Post submitted")
        return redirect(
            url_for("main.index")
        )  #why redirect here, why not stay here only as index is already rendered?
        #-> because for form submission POST request always send a web page response so if user refresh browser doesn't send same POST request to submit form.
    #read all posts from db

    posts = current_user.getAllPosts()

    return render_template("index.html", title="Home", posts=posts, form=form)
Esempio n. 28
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        # Use the guess_language package
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data,
                    author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        # This redirect is here avoids resubmitting the form on page
        # refresh. See the wiki on 'Post/Redirect/Get'.
        return redirect(url_for('main.index'))
    # Paginate results, which means to limit results to X items per page
    # instead all at once. Flask supports it, and we use it in the URL.
    page = request.args.get('page', 1, type=int)
    # arg1 = page number, starting at 1
    # arg2 = # items per page
    # arg3 = True: return 404 to client; False: return empty list
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    # Ternarys to set next and prev url link, if they exist
    # The keyword args on url_for() are kind of like React props,
    # in that the recieving template will recieve via Flask putting
    # them in the URL as query args.
    next_url = url_for('main.index',
                       page=posts.next_num) if posts.has_next else None
    prev_url = url_for('main.index',
                       page=posts.prev_num) if posts.has_prev else None
    # Render the template, substituting variables when needed.
    return render_template('index.html',
                           title=_('Home Page'),
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 29
0
def submit():
    form = PostForm()
    if form.validate_on_submit():
        if current_user.can_post():
            post = Post(
                title=form.title.data,
                url=form.url.data,
                text=form.text.data,
                author=current_user,
            )
            post.format_post(form.url.data)
            db.session.add(post)
            db.session.commit()
            flash("Congratulations, your post was published!")
            return redirect(url_for("main.post_page", post_id=post.id))
        else:
            flash(
                f"Sorry, you can only post {current_app.config['USER_POSTS_PER_DAY']} times a day"
            )
            return redirect(url_for("main.index"))

    return render_template("submit.html", title="Submit", form=form)
Esempio n. 30
0
def explore():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data, author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.timestamp.desc()).paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.explore', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.explore', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('index.html', title=_('Explore - Hikanotes'),
                           posts=posts.items, next_url=next_url,
                           prev_url=prev_url, form=form)