Пример #1
0
def postMessage(request):
	if request.POST['message'] == "":
		return index(request)

	p = Post(message = request.POST['message'])
	p.save()
	return index(request)
Пример #2
0
 def save(self):
     title = self.cleaned_data['title']
     description = self.cleaned_data['description']
     author = self.cleaned_data['author']
     post = Post(title=title, description=description, author=author)
     post.save()
     return post
Пример #3
0
def post_add():
    form = PostForm()
    form.category.choices = [(str(c.id), c.title)
                             for c in Category.query.all()]

    if form.validate_on_submit():
        tags = conformTags(form)

        post = Post(title=form.title.data,
                    content=form.content.data,
                    tags=tags,
                    cat_id=form.category.data)
        post.content = post.content.replace("$$id", str(post.id))
        db.session.add(post)
        db.session.commit()

        uploadFiles(form,
                    os.getcwd() + "/blog/static/posts/" + str(post.id) + "/")

        flash(f'Пост було опубліковано', 'success')
        return redirect(url_for('posts.post_add'))
    elif form.is_submitted():
        flash("Користувач з таким логіном вже існує!", "error")

    return render_template("add_post.html",
                           categories=Category.query.all(),
                           title="Додати пост",
                           form=form,
                           files=[])
Пример #4
0
def delete_post(slug):
    ''' Delete single or multiple posts on admin panel. '''
    post = Post.objects(slug=slug)[0]
    if request.method == 'POST':
        Post.delete(post)
        flash("Post has been deleted.")
        return redirect(url_for('posts.all_posts', sort='-created_at', page=1))
    return render_template('/admin/delete_post.html', post=post, slug=slug)
Пример #5
0
def delete_post(slug):
	''' Delete single or multiple posts on admin panel. '''
	post = Post.objects(slug=slug)[0]
	if request.method == 'POST':
		Post.delete(post)
		flash("Post has been deleted.")
		return redirect(url_for('posts.all_posts', sort='-created_at', page=1))
	return render_template('/admin/delete_post.html', post=post, slug=slug)
Пример #6
0
 def post(self):
     post = Post(
         title=request.form.get('post-title', None),
         body=request.form.get('post-body', None),
         slug=request.form.get('post-slug', None),
         category="blog",
     )
     post.save()
     return redirect(url_for('posts.detail', slug=post.slug))
Пример #7
0
def delete_selected():
	to_delete = Post.objects(mark_delete=True)
	if not to_delete:
		flash('Nein')
		return redirect(url_for('posts.all_posts', sort='-created_at', page=1))
	if request.method == 'POST':
		for post in to_delete:
			Post.delete(post)
			post.save()
		flash("{} Posts have been deleted.".format(len(to_delete)))
		return redirect(url_for('posts.all_posts', sort='-created_at', page=1))
	return render_template('/admin/delete_post.html', to_delete=to_delete)
Пример #8
0
def delete_selected():
    to_delete = Post.objects(mark_delete=True)
    if not to_delete:
        flash('Nein')
        return redirect(url_for('posts.all_posts', sort='-created_at', page=1))
    if request.method == 'POST':
        for post in to_delete:
            Post.delete(post)
            post.save()
        flash("{} Posts have been deleted.".format(len(to_delete)))
        return redirect(url_for('posts.all_posts', sort='-created_at', page=1))
    return render_template('/admin/delete_post.html', to_delete=to_delete)
Пример #9
0
def existing_slug(slug):
    ''' Checks if slug exists in dB. If so, create an
	alternative slug. Else return none.'''
    iter_num = 1
    alt_slug = None
    existing_records = Post.objects(slug=slug)
    while existing_records:
        alt_slug = slug + str(iter_num)
        existing_records = Post.objects(slug=alt_slug)
        iter_num += 1
    if alt_slug:
        return alt_slug
    return None
Пример #10
0
def existing_slug(slug):
	''' Checks if slug exists in dB. If so, create an
	alternative slug. Else return none.'''
	iter_num = 1
	alt_slug = None
	existing_records = Post.objects(slug=slug)
	while existing_records:
		alt_slug = slug + str(iter_num)
		existing_records = Post.objects(slug=alt_slug)
		iter_num += 1
	if alt_slug:
		return alt_slug
	return None
Пример #11
0
def mark_for_deletion(slugs):
    ''' Helper function for delete_selected route. Sets
	mark_delete field to True for all posts.'''
    # Unmark for deletion all previous posts
    all_posts = Post.objects()
    for p in all_posts:
        p.mark_delete = False
        p.save()
    for slug in slugs:
        if not Post.objects(slug=slug):
            continue
        post = Post.objects.get_or_404(slug=slug)
        post.mark_delete = True
        post.save()
    return
Пример #12
0
def mark_for_deletion(slugs):
	''' Helper function for delete_selected route. Sets
	mark_delete field to True for all posts.'''
	# Unmark for deletion all previous posts
	all_posts = Post.objects()
	for p in all_posts:
		p.mark_delete = False
		p.save()
	for slug in slugs:
		if not Post.objects(slug=slug):
			continue
		post = Post.objects.get_or_404(slug=slug)
		post.mark_delete = True
		post.save()
	return
Пример #13
0
def drafts(page=1, sort='-created_at'):
	''' Lists of posts in admin view that are not published, paginated and sortable.'''
	form = SearchForm(request.form)
	posts = Post.objects(published=False).order_by(sort).paginate(page, per_page=6)
	if request.method == 'POST' and form.validate_on_submit():
		redirect(url_for('posts.drafts', page=page))
	return render_template("/admin/list.html",
		                   posts=posts,
		                   form=form,
		                   cur_endpoint=request.endpoint)
Пример #14
0
def drafts(page=1, sort='-created_at'):
    ''' Lists of posts in admin view that are not published, paginated and sortable.'''
    form = SearchForm(request.form)
    posts = Post.objects(published=False).order_by(sort).paginate(page,
                                                                  per_page=6)
    if request.method == 'POST' and form.validate_on_submit():
        redirect(url_for('posts.drafts', page=page))
    return render_template("/admin/list.html",
                           posts=posts,
                           form=form,
                           cur_endpoint=request.endpoint)
Пример #15
0
def create_post(request):
    error_message = None
    is_created = False

    if request.method == 'POST':
        title = request.POST['post_title']
        description = request.POST['post_text']

        if len(title) > 0 and len(description) > 0:
            post = Post(title=title, description=description)
            post.save()
            is_created = True
        else:
            error_message = 'title or description is empty'

    context = {
        'post_created': is_created,
        'errors': error_message,
    }

    return render(request, 'posts/create_post.html', context)
Пример #16
0
def create_post():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']

        post = Post(title=title, body=body)
        db.session.add(post)
        db.session.commit()

        return redirect(url_for('posts.index'))
    form = PostForm()
    return render_template('posts/create_post.html', form=form)
Пример #17
0
def new_post():
    ''' Create new post from admin view. '''
    form = PostForm(request.form)
    if request.method == 'POST' and form.validate_on_submit():
        post_args = {}
        if Post.objects(title=form.title.data):
            flash("Post with this title already exists!")
            return render_template('/admin/new_post.html', form=form)
        if request.form.get('new_tags'):
            post_args['tags'] = process_tags(request.form.get('new_tags'))
        post_args['index_background'] = form.index_background.data
        post_args['published'] = form.published.data
        post_args['title'] = form.title.data
        post_args['author'] = form.author.data
        post_args['content'] = form.content.data
        post_args['category'] = form.category.data
        new_post = Post(**post_args)
        new_post.save()
        flash("New post, '{}', successfully created!".format(new_post.title))
        return redirect(url_for('posts.edit_post', slug=new_post.slug))
    return render_template('/admin/new_post.html', form=form)
Пример #18
0
def new_post():
	''' Create new post from admin view. '''
	form = PostForm(request.form)
	if request.method == 'POST' and form.validate_on_submit():
		post_args = {}
		if Post.objects(title=form.title.data):
			flash("Post with this title already exists!")
			return render_template('/admin/new_post.html', form=form)
		if request.form.get('new_tags'):
			post_args['tags'] = process_tags(request.form.get('new_tags'))
		post_args['index_background'] = form.index_background.data
		post_args['published'] = form.published.data
		post_args['title'] = form.title.data
		post_args['author'] = form.author.data
		post_args['content'] = form.content.data
		post_args['category'] = form.category.data
		new_post = Post(**post_args)
		new_post.save()
		flash("New post, '{}', successfully created!".format(new_post.title))
		return redirect(url_for('posts.edit_post', slug=new_post.slug))
	return render_template('/admin/new_post.html', form=form)
Пример #19
0
    def get(self, page=0):
        context = self.get_context()
        posts = Post.objects(category="blog")

        first_post = page * 3
        last_post = first_post + 3
        current_posts = posts[first_post:last_post]

        if len(posts) > last_post:
            context['older_posts'] = True
            context['next_page'] = page + 1

        context['posts'] = current_posts
        return render_template('posts/list.html', **context)
Пример #20
0
def create_post():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']

        try:
            post = Post(title=title, body=body)
            db.session.add(post)
            db.session.commit()
        except:
            current_app.logger.exception('Something wrong')

        return redirect(url_for('posts.index'))
    form = PostForm()
    return render_template('posts/create_post.html', form=form)
Пример #21
0
def post_creation():

    form = PostCreationForm()

    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)

        db.session.add(post)
        db.session.commit()

        flash('Your post has been successfully created.', 'success')
        return redirect(url_for('main.home'))

    return render_template('post_creation.htm', form=form, title='post')
Пример #22
0
def edit_post(slug):
	''' Post edit view of admin panel '''
	post = Post.objects(slug=slug)[0]
	form = PostForm(request.form, obj=post)
	if request.method == 'POST' and form.validate_on_submit():
		post.title = request.form.get('title')
		post.author = form.author.data
		if request.form.get('new_tags'):
			new_tags = process_tags(request.form['new_tags'])
			post.tags = new_tags
		post.index_background = form.index_background.data
		post.category = form.category.data
		post.published = form.published.data
		post.content = request.form.get('content')
		post.save()
		flash('Post has been edited')
		return redirect(url_for('posts.edit_post', slug=post.slug))

	return render_template("/admin/edit_post.html", form=form, slug=slug)
Пример #23
0
def edit_post(slug):
    ''' Post edit view of admin panel '''
    post = Post.objects(slug=slug)[0]
    form = PostForm(request.form, obj=post)
    if request.method == 'POST' and form.validate_on_submit():
        post.title = request.form.get('title')
        post.author = form.author.data
        if request.form.get('new_tags'):
            new_tags = process_tags(request.form['new_tags'])
            post.tags = new_tags
        post.index_background = form.index_background.data
        post.category = form.category.data
        post.published = form.published.data
        post.content = request.form.get('content')
        post.save()
        flash('Post has been edited')
        return redirect(url_for('posts.edit_post', slug=post.slug))

    return render_template("/admin/edit_post.html", form=form, slug=slug)
Пример #24
0
 def get(self):
     context = self.get_context()
     context['posts'] = Post.objects(category="blog")
     return render_template('posts/list.html', **context)
Пример #25
0
 def get(self):
     context = self.get_context()
     context['projects'] = Post.objects(category="project")
     return render_template('pages/projects.html', **context)