def specific_post(post_id): post = Post.query.get(post_id) if request.method == "GET": return post.to_dict() elif request.method == "PUT": form = CreatePostForm() form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): form.populate_obj(post) db.session.commit() if 'images' in request.files: images = request.files.getlist('images') for image in images: if allowed_file(image.filename): image.filename = get_unique_filename(image.filename) image_url = upload_file_to_s3(image, Config.S3_BUCKET) image = Post(post_id=post.id, image_url=imgUrl) db.session.add(image) db.session.commit() return post.to_dict() return {'errors': validation_errors_to_error_messages(form.errors)} elif request.method == "DELETE": db.session.delete(post) db.session.commit() return post.to_dict() return post.to_dict()
def create_post(request): from app.forms import CreatePostForm if request.method == 'POST': title = request.POST.get('title', '').strip() or None body = request.POST.get('body', '').strip() or None categories = request.POST.get('categories', '') or None if all([title, body, categories]): from datetime import datetime try: newPost = Post.objects.create(author_id=request.user.id, title=title, body=body, date_created=datetime.now(), visible=True) except: return render(request, 'posts/create_post.html', { 'form': CreatePostForm(), 'err': 'You already used this title' }) categories = [ item.lower().strip() for item in categories.split(',') ] _create_category(categories, newPost) return redirect('display_post') else: return create_post(request) return render(request, 'posts/create_post.html', {'form': CreatePostForm()})
def user(username): u = request.args.get('u') ####### SEARCHING if u: user = User.query.filter(User.username.contains(u)) if user == None: user = User.query.filter_by(username=username).first_or_404() else: return render_template('users.html', user_list = user) else: user = User.query.filter_by(username=username).first_or_404() ######## form = CreatePostForm() if form.validate_on_submit(): post = Post(body=form.body.data, author=current_user) db.session.add(post) db.session.commit() return redirect(url_for('user', username=current_user.username)) posts = user.posts return render_template('user.html', user=user, form = form, posts = posts)
def create_post(): form = CreatePostForm() if form.validate_on_submit(): post_body = form.post.data post = Posts(body=post_body, user_id=current_user.id) db.session.add(post) db.session.commit() return redirect(url_for('user', username=current_user.username)) return render_template("create_post.html", form=form)
def create_post(): form = CreatePostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, owner=current_user) db.session.add(post) db.session.commit() return redirect(url_for('index')) return render_template('create_post.html', form=form)
def create_post(): form = CreatePostForm() if form.validate_on_submit(): title = form.title.data content = form.content.data user_id = current_user.id post = Post(title=title, content=content, user_id=user_id) db.session.add(post) db.session.commit() return redirect(url_for('posts')) return render_template("create-post.html", title='Create Post', form=form)
def create_post(): if not current_user.is_authenticated: return redirect(url_for('login')) form = CreatePostForm() if form.validate_on_submit(): post = Post(body=form.body.data,user_id=current_user.id) db.session.add(post) db.session.commit() flash('Post Was Created!') return redirect(url_for('user_page',username=current_user.username)) return render_template('create_post.html',form=form, title = 'Create Post')
def profile(): form = CreatePostForm() if form.validate_on_submit(): redirect(url_for('profile')) user = {'username': '', 'password': '', 'bio': ''} for iter_user in users: if iter_user['username'] is current_user: user = iter_user return render_template('profile.html', title='Profile - {}'.format(current_user), user=user, form=form)
def post_edit(id): post = Post.query.get(id) form = CreatePostForm() if request.method == 'POST': if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data post.date_posted = datetime.utcnow() db.session.commit() return redirect(url_for('post_details', id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('post_edit.html', title='Post Edit', form=form)
def dashboard_post_new(): form = CreatePostForm() if form.validate_on_submit(): post = Post() post.title = form.title.data post.content = form.content.data post.user_id = current_user.id db.session.add(post) db.session.commit() flash('Congratulations, you are now a registered user!', 'success') return redirect(url_for('dashboard')) return render_template('dashboard/post_new.html', title="New Post", form=form)
def createPost(username): form = CreatePostForm() if form.validate_on_submit(): p = Post(body=form.text.data, author=current_user) db.session.add(p) db.session.commit() flash('You have successfully created your post!') return redirect(url_for('profile', username=username)) elif request.method == 'GET': return render_template('create_post.html', title='Create A Post', form=form)
def create_post(): form = CreatePostForm() if request.method == 'POST': if form.validate_on_submit(): title = request.form.get('title') topic = request.form.get('topic') if request.form.get('start_event'): start_event = request.form.get('start_event') else: start_event = None if request.form.get('stop_event'): stop_event = request.form.get('stop_event') else: stop_event = None description = request.form.get('description') # if request.form.get('tags'): # tags = request.form.get('tags') # else: # tags = None # if request.files['file']: # file = request.files['file'] # if file and allowed_file(file.filename): # filename = secure_filename(file.filename) # filename = str(uuid.uuid4()) + '.' + filename.split('.')[-1] # picture_path = os.path.join(config.FOLDER_IMAGES, 'posts', filename) # print('filename: ', filename) # print('picture_path: ', picture_path) # else: # picture_path = os.path.join(config.UPLOAD_FOLDER_IMAGES, 'no_photo.jpg') post = Post( author=current_user.email, title=title, topic=topic, start=start_event, end=stop_event, description=description, # tags=tags, # picture_path=picture_path ) db.session.add(post) db.session.commit() # file.save(os.path.join(config.UPLOAD_FOLDER_IMAGES, 'posts', filename)) flash("You have successfully created post: {}.".format(title)) return redirect('/posts') return render_template('create_post.html', form=form)
def new_post(): form = CreatePostForm() if form.validate_on_submit(): title = form.title.data content = form.content.data creator_id = current_user.id post = Post(title, content, creator_id) post.save() flash('Post created successfully', 'success') return redirect(url_for('posts.post', post_id=post.id)) return render_template('posts/create_post.html', title='Create new post', form=form)
def createPost(): form = CreatePostForm() data = request.get_json(force=True) form['csrf_token'].data = request.cookies['csrf_token'] form['userId'].data = current_user.id form['photoData'].data = data['photoData'] form['caption'].data = data['caption'] form['location'].data = data['location'] if form.validate_on_submit(): newPost = Post() form.populate_obj(newPost) db.session.add(newPost) db.session.commit() return {"success": 'true'} return {'errors': validation_errors_to_error_messages(form.errors)}, 401
def forum(book_title): forum = Forum.query.filter_by(book_title=book_title).first() form = CreatePostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data) db.session.add(post) db.session.commit() flash('Created new Post: {}'.format( form.title.data)) final_form = CreatePostForm() return redirect(url_for('forum', book_title=forum.book_title)) render_template('forum.html', title='New Post', forum=forum, form=final_form) return render_template('forum.html',title='New Post', forum=forum, form=form)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.user_id != current_user.id: abort(403) form = CreatePostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data post.date_posted = dt.now() post.save() flash('Post successfully updated', 'success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('posts/update_post.html', title='Edit post', form=form, post_id=post.id)
def create_or_edit_post(request, post, template): if request.method == 'GET': form = CreatePostForm(instance=post) context = { 'form': form, 'post': post, } return render(request, f'{template}.html', context) else: old_image = post.image form = CreatePostForm(request.POST, request.FILES, instance=post) if form.is_valid(): if old_image: os.remove(old_image.path) post = form.save(commit=False) post.user = request.user.userprofile post.save() return redirect('post details', post.pk) context = { 'form': form, 'post': post, } return render(request, f'{template}.html', context)
def edit_post(request, pk): post = Post.objects.get(pk=pk) if request.method == 'GET': form = CreatePostForm(instance=post) context = { 'form': form, 'post': post, } return render(request, 'post_edit.html', context) else: old_image = post.image form = CreatePostForm(request.POST, request.FILES, instance=post) if form.is_valid(): if old_image: os.remove(old_image.path) post = form.save(commit=False) post.save() return redirect('post details', post.pk) context = { 'form': form, 'post': post, } return render(request, 'post_details.html', context)
def create_post(): form = CreatePostForm() if form.submit_create.data: Post.query.filter_by(has_photo_header=0).delete() post = Post(title=form.title.data, title_preview_text=form.preview_text.data, text=form.text.data, author=current_user.id, type=int(form.type.data), has_photo_preview=False, has_photo_header=False) db.session.add(post) db.session.commit() return render_template('create_post.html', title='Создание поста', set_photo=True, post=post) return render_template('create_post.html', title='Создание поста', form=form)
def topic(): form = CreatePostForm() return render_template('topic.html', title='Topic', posts=posts, form=form)