Esempio n. 1
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.body.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.body.data,
                    author=current_user,
                    language=language)
        post.save()
        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_num = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None
    # projects = current_user.followed_projects().all()
    return render_template('index.html',
                           title='Home',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_num=prev_num)
Esempio n. 2
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,
                    name=form.name.data,
                    height=form.height.data,
                    weight=form.weight.data,
                    bmi=form.bmi.data,
                    bmireport=form.bmireport.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. 3
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = detect(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(_l('Ваше сообщение было добавлено'))
        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
    all_users = User.query.all()
    return render_template('index.html',
                           title='Главная',
                           posts=posts.items,
                           form=form,
                           next_url=next_url,
                           prev_url=prev_url,
                           all_users=all_users)
Esempio n. 4
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. 5
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        # Not implemeted language translation
        language = ''
        post = Post(body=form.post.data,
                    author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is live now'))
        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. 6
0
def index():
    if current_user.is_anonymous:
        return redirect(url_for('auth.login'))
    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'))
    # posts = current_user.followed_posts().all()

    # 分页
    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'
                           , current_user=current_user
                           , posts=posts.items
                           , form=form
                           , next_url=next_url
                           , prev_url=prev_url
                           )
Esempio n. 7
0
def project(project_id):
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.body.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        project = Project.query.filter_by(id=project_id).first_or_404()
        post = Post(body=form.body.data, author=current_user, commented_project=project, language=language)
        post.save()
        flash('Your post is now live!')
        return redirect(url_for('project.project', project_id=project_id))
    page = request.args.get('page', 1, type=int)
    project = Project.query.filter_by(id=project_id).first_or_404()
    user = User.query.filter_by(username=project.creator.username).first()
    mymap = Map(
    identifier="view-map",
    lat=project.site.lat,
    lng=project.site.lng,
    markers=[(project.site.lat, project.site.lng)],
    style="height:400px;width:100%;margin:0;",
    fit_markers_to_bounds = True
    )
    posts = project.posts.order_by(Post.timestamp.desc()).paginate(page, current_app.config['POSTS_PER_PAGE'], False)
    images = project.images.all()
    next_url = url_for('project.project', username=user.username, page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('project.project', username=user.username, page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('project/project.html', user=user, project=project, form=form, posts=posts.items, next_url=next_url, prev_url=prev_url, mymap=mymap, images=images)
Esempio n. 8
0
def edit_post(id, ver):
    if ver and ver > 1:
        abort(404)
    nom = Nom.query.get_or_404(id)
    author = current_user._get_current_object()
    post = Post.query.filter_by(nom=nom, author=author).first()
    if post is None:
        post = Post(keyword=nom.keyword, story="", nom=nom, author=author)
        db.session.add(post)

    form = PostForm()
    if form.validate_on_submit():
        if post.keyword != form.keyword.data or \
                post.story != form.story.data:
            post.keyword_backup = post.keyword
            post.keyword = form.keyword.data
            post.story_backup = post.story
            post.story = form.story.data 
            post.shared = form.shared.data
            db.session.add(post)
        elif post.shared != form.shared.data:
            post.shared = form.shared.data
            db.session.add(post)
        cache.delete_memoized(user)
        return redirect(url_for('.nom_view', id=nom.id))

    form.keyword.data = post.keyword_backup if ver else post.keyword
    form.story.data = post.story_backup if ver else post.story
    form.shared.data = post.shared

    return render_template('main/nom_edit.html',
                           nom=nom, form=form, ver=ver)
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'))
    # posts = current_user.followed_posts().all()
    # user = {'username': '******'}
    # 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)
    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. 10
0
def quote_post(post_id):
    post = Post.query.filter_by(id=post_id).first()
    if post is None:
        flash('post {} not found.'.format(post_id))
        return redirect(url_for('main.index'))

    thread = Post.query.filter_by(id=post_id).first(
    ).thread  # todo - user should be able to quote a post into any thread, not just the same
    #  thread as the original post
    if thread is None:
        flash('thread for post {} not found.'.format(post_id))
        return redirect(url_for('main.index'))

    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user, thread=thread)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        anchor = 'p' + str(post.id)
        return redirect(
            url_for('main.thread',
                    thread_id=thread.id,
                    page=thread.last_page(),
                    _anchor=anchor))
    elif request.method == 'GET':
        body = '[quote,name={},time={},post_id={}]{}[/quote]'.format(
            post.author.username, post.timestamp, post.id, post.body)
        form.post.data = body
    return render_template('make_post.html',
                           title='Quote Post',
                           form=form,
                           thread=thread)
Esempio n. 11
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        # 自动监测提交的post内容是什么语言
        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) # 如果没有page参数,则默认page变量为1

    # paginate是分页查询的方法
    # False表示如果未查询到,则返回空列表,如果改成True,如果未查询到,则返回404
    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

    # posts现在是一个paginate对象,所以需要调用它的items成员,才是筛选出来的数据列表
    # 如果使用的是posts = current_user.followed_posts().all(),则只能一次性提取所有的动态,
    # 当动态量比较大时,会很耗时,all()返回的就是一个列表,没有items成员
    return render_template('index.html', title=_('Home Page'), form=form, posts=posts.items,
                            next_url=next_url, prev_url=prev_url)
Esempio n. 12
0
def index():
    # user = User.query.filter_by(username=current_user.username).first_or_404()
    form = PostForm()
    page = request.args.get('page', 1, type=int)
    if form.validate_on_submit():
        print("Body: ", form.post.data, "Author: ", current_user, "and: ",
              current_user.username)
        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=page))

    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="Main Page",
                           posts=posts.items,
                           form=form,
                           next_url=next_url,
                           prev_url=prev_url,
                           page=page)
Esempio n. 13
0
def index():
    form = PostForm()
    if (current_user.can(Permission.WRITE_ARTICLES)
            and form.validate_on_submit()):
        post = Post(question=form.question.data,
                    answer=form.answer.data,
                    author=current_user._get_current_object())
        db.session.add(post)
        for hour in hours:
            task = Task(post=post,
                        start_timestamp=datetime.utcnow() +
                        timedelta(hours=hour))
            db.session.add(task)

        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    pagination = Post.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,
                           pagination=pagination)
Esempio n. 14
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        if request.form["avatar-url"]:
            avatar_url = request.form["avatar-url"]
            print('!!!!!!1', avatar_url)
            post = Post(body=form.post.data,
                        title=form.title.data,
                        file_url=avatar_url,
                        author=current_user)
        else:
            post = Post(body=form.post.data,
                        title=form.title.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)
    # return value from a paginate() call is an
    # object of a Pagination class from Flask-SQLAlchemy
    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. 15
0
def explore():
    if current_user.hindi != True:
        form = PostForm()
    else:
        form = PostFormHINDI()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = 'en'
        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.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    
    if current_user.hindi != True:
        return render_template('index.html', title=_('Explore') ,
                            posts=posts.items, next_url=next_url,
                            prev_url=prev_url,dm=current_user.darkmode,verified=current_user.verified,form=form,index=False)
    else:
        return render_template('Hindi/index.html',title=_('Explore'),
                            posts=posts.items, next_url=next_url,
                            prev_url=prev_url,dm=current_user.darkmode,verified=current_user.verified,form=form)
Esempio n. 16
0
def upload_post():
    post_form = PostForm()
    print('headers', current_app.config.get('WTF_CSRF_FIELD_NAME'))
    print('validete', post_form.validate(), post_form.errors)
    if post_form.validate_on_submit():
        return jsonify({'success': 'upload post successfully'})
    return jsonify({'error': 'fail to validete'})
Esempio n. 17
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!')
        # Standard practice is to respond to POST with
        # a redirect. This prevents refresh from re-submitting
        # the form.
        # See Post/Redirect/Get pattern.
        return redirect(url_for('main.index'))

    # Paginated posts
    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

    # Render index page
    return render_template('index.html', title='Home', form=form,
        posts=posts.items, prev_url=prev_url, next_url=next_url)
Esempio n. 18
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()
        if (str(form.post.data) == "Analyze"):
            return redirect(url_for('main.results'))
        flash(_('Message envoyé !'))
        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=_('Accueil'),
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 19
0
def index():
    form = PostForm()
    if current_user.can(
            Permission.WRITE_ARTICLES) and form.validate_on_submit():
        post = Post(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)
    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
    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. 20
0
def explore():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UKNOWN' 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.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("forum.html",
                           title='Explore',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Esempio n. 21
0
def write_articles():
    form = PostForm(user=current_user)
    if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit():
        kind = PostKind.query.filter_by(name=form.kind.data).first()
        post = Post(title=form.title.data, outline=form.outline.data, body=form.body.data,
                    author=current_user._get_current_object(), kind=kind)
        db.session.add(post)
        return redirect(url_for('.index'))
    return render_template('write_articles.html', form=form)
Esempio n. 22
0
def post():
    form = PostForm()
    otherpost = Post.query.all()
    if form.validate_on_submit():
        post = Post(post=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
    return render_template('post.html', title=_('Post'), form=form, otherpost = otherpost)
Esempio n. 23
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=form.author.data)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('main.index'))
    posts = Post.query.order_by(Post.timestamp.desc())
    return render_template('index.html', form=form, posts=posts)
Esempio n. 24
0
def add_post():
    form = PostForm()
    error = None
    if form.validate_on_submit():
        title = form.title.data
        if postService.is_unique_title(title):
            postService.add(title, form.body.data, current_user)
            return redirect(url_for('main.posts'))
        error = 'Title must be unique'
    return render_template('add_post.html', form=form, error=error)
Esempio n. 25
0
def post():
    form1 = PostForm()
    form2 = PlantFormDropDown()
    form2.garden.choices = [(g.id, g.name) for g in current_user.gardens]
    if form1.validate_on_submit():
        post = Post(body=form1.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('main.index'))
Esempio n. 26
0
def add_post():
    form = PostForm()
    if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit():
        post = Post(body=form.body.data, title=form.title.data,
                    author=current_user._get_current_object())
        post.addTag(form.tag.data)
        db.session.add(post)
        flash("发布成功!")
        return redirect(url_for('.post'))
    return render_template("auth/add_post.html", form=form)
Esempio n. 27
0
def user(username):
    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('/index')
    user = User.query.filter_by(username=username).first_or_404()
    posts = Post.query.order_by(Post.timestamp.desc()).all()
    return render_template('user.html',form=form, user=user, posts=posts)
Esempio n. 28
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        part = CarPart(name=form.name.data, body=form.body.data)
        db.session.add(part)
        db.session.commit()
        flash("Your Post Is Live!")
        return redirect(url_for("main.index"))

    parts = CarPart.query.all()
    return render_template("index.html", title="Home", form=form, parts=parts)
Esempio n. 29
0
File: views.py Progetto: WES6/boke
def index():
    form = PostForm()
    if current_user.can(Permission.WRITE) and form.validate_on_submit():
        post = Post(body=form.body.data,
                    author=current_user._get_current_object())
        db.session.add(post)
        db.session.commit()
        flash("Submission of success !")
        return redirect(url_for('.index'))
    posts = Post.query.order_by(Post.timestamp.desc()).all()
    return render_template('index.html', form=form, posts=posts)
Esempio n. 30
0
def create_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.body.data,
                    author=current_user,
                    title=form.title.data)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('main.index'))
    return render_template('create_post.html', title='create', form=form)
Esempio n. 31
0
def index():
  form = PostForm()
  if form.validate_on_submit():
    post = Post(body=form.popst.data, author=current_user)
    db.session.add(post)
    db.session.commit()
    return redirect(url_for('index'))
  
  all = [{ 'name': user.username } 
    for user in User.query.all()]

  return jsonify(all)
Esempio n. 32
0
def edit(id):
    post = Post.query.get_or_404(id)
    if current_user != post.author and not current_user.can(Permission.ADMINISTER):
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        flash('The post has been update')
        return redirect(url_for('post', id=post.id))
    form.body.data = post.body
    return render_template('edit_post.html', form=form)
Esempio n. 33
0
def post():
    if not current_user.get_admin():
        return redirect(url_for('index'))
    form = PostForm()
    if form.validate_on_submit():
        stage = int(form.data.stage)
        users = mongo.db.users
        user = users.find_one({'name': form.username.data})
        user['projects'][stage]['passed'] = True
        users.save(user)
        return redirect(url_for('profile', username=form.username.data))
    return abort(404)
Esempio n. 34
0
def edit_post(id):
    post = Post.query.get_or_404(id)
    if current_user != post.user and not current_user.is_administer():
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        flash("博文修改完毕")
        return redirect(url_for("main.post", id=id))
    form.body.data = post.body
    return render_template("edit_post.html", form=form)
Esempio n. 35
0
def new_post():
    form = PostForm()
    post = Post()
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.author = current_user
        db.session.add(post)
        db.session.commit()
        tag_ids = form.tags.data
        for tag_id in tag_ids:
            post_tags = PostTags(post_id=post.id, tag_id=tag_id)
            db.session.add(post_tags)
        flash('文章已发布.')
        return redirect(url_for('.post', title=post.url_title))
    return render_template('edit_post.html', form=form, is_new=True)
Esempio n. 36
0
def edit_post(id: int):
    post = Post.query.get(id)
    if post is None:
        post = Post(userid=current_user.id)
    elif post not in current_user.posts:
        return redirect(url_for('main.post', id=id))
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('main.post', id=id))
    form.title.data = post.title
    form.content.data = post.content
    return render_template('edit_post.html', form=form)
Esempio n. 37
0
def index():
    form = PostForm()
    if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit():
        post = Post(body=form.body.data, author=current_user._get_current_object())
        db.session.add(post)
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    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
    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, pagination=pagination, show_followed=show_followed)
Esempio n. 38
0
def edit(id):
    post = Post.query.get_or_404(id)
    if current_user != post.author and not current_user.can(Permission.ADMINISTER):
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.last_modified = datetime.utcnow()
        # print("传过来的标签有:%s" % form.tag.data)
        post.updateTag(form.tag.data)
        db.session.add(post)
        flash("The post has been Update!")
        return redirect(url_for('.post'))
    form.title.data = post.title
    form.body.data = post.body
    form.tag.data = post.getTagByString()
    return render_template('auth/edit_post.html', form=form)
Esempio n. 39
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = detect_lang(form.post.data)['lang']
        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. 40
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. 41
0
def edit_post(title):
    post = Post.query.filter_by(url_title=title).first()
    if not post:
        abort(404)
    if current_user != post.author:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        db.session.add(post)
        for tag in post.tags:
            db.session.delete(tag)
        tag_ids = form.tags.data
        for tag_id in tag_ids:
            post_tags = PostTags(post_id=post.id, tag_id=tag_id)
            db.session.add(post_tags)
        flash('文章已更新.')
        return redirect(url_for('.post', title=post.url_title))
    form.title.data = post.title
    form.tags.data = [tag.id for tag in post.tags]
    form.body.data = post.body
    return render_template('edit_post.html', form=form, is_new=False)
Esempio n. 42
0
def index():
    form = PostForm()
    if current_user.can(Permission.WRITE_ARTICLES) and \
            form.validate_on_submit():
        post = Post(title=form.title.data, content=form.content.data, user=current_user)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('main.index'))

    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get('show_followed', ''))
    if show_followed:
        query = current_user.following_posts
    else:
        query = Post.query
    page = request.args.get('page', 1, type=int)
    pagination = query.order_by(Post.timestamp.desc()).paginate(page,
                                                                per_page=current_app.config[
                                                                             'FLASK_PER_PAGE'] or 10,
                                                                error_out=False)
    posts = pagination.items
    return render_template('index.html', form=form, posts=posts, show_followed=show_followed, pagination=pagination,
                           Permission=Permission)
Esempio n. 43
0
from flask import render_template, abort, flash, redirect, url_for, request, current_app