def customer_chatting(): session['current_path'] = request.path form = PostForm() username = session.get("USERNAME") if not session.get("USERNAME") is None: if form.validate_on_submit(): body = form.postbody.data user_in_db = Customer.query.filter( Customer.username == session.get("USERNAME")).first() post = Post(body=body, author=user_in_db, name=session.get("USERNAME")) db.session.add(post) db.session.commit() return redirect(url_for('customer_chatting')) else: user_in_db = Customer.query.filter( Customer.username == session.get("USERNAME")).first() prev_posts = Post.query.filter(Post.user_id == user_in_db.id).all() # print("Checking for user: {} with id: {}".format(user_in_db.username, user_in_db.id)) return render_template('customer_chatting.html', title='Message', username=username, user_in_db=user_in_db, prev_posts=prev_posts, form=form, language=language[render_languages()]) else: flash("User needs to either login or signup first") return redirect(url_for('login'))
def chatting_detail(): session['current_path'] = request.path form = PostForm() username = session.get("USERNAME") if not session.get("USERNAME") is None: employee_in_db = Employee.query.filter( Employee.username == session.get("USERNAME")).first() user = {'city': employee_in_db.key} user_id = request.args.get("id") user_in_db = Customer.query.filter(Customer.id == user_id).first() if form.validate_on_submit(): body = form.postbody.data if user_in_db: name = 'Employee_' + str(employee_in_db.id) post = Post(body=body, author=user_in_db, name=name) db.session.add(post) db.session.commit() return redirect(url_for('employee_chatting')) else: return render_template('chatting_detail.html', user=user, username=username, title='Message', customer=user_in_db, form=form, language=language[render_languages()]) else: flash("User needs to either login or signup first") return redirect(url_for('login'))
def newpost(): form = PostForm() 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('Post has been submited', 'success') return redirect(url_for('posts')) return render_template('newpost.html', title='New post', form=form)
def new_post(): form = PostForm() 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('The post was created successfully', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form)
def update(post_id): post = Post.query.get_or_404(post_id) if post.author == current_user: form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('update.html',title='Update',form=form)
def new_post(): form = PostForm() 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('Twój post został utworzony!', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form, legend='Nowy post')
def new_post(): """Make new blog post.""" form = PostForm() if form.validate_on_submit(): new_post = Post( title=form.title.data, text=form.text.data, publish_date=datetime.datetime.now(), ) db.session.add(new_post) db.session.commit() return redirect(url_for('.home')) return render_template('new.html', form=form)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Twój post został zaktualizowany!', 'success') return redirect(url_for('post', post_id=post_id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('create_post.html', title='Update Post', form=form, legend='Zaktualizuj post')
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('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('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)
def new_post(): form = PostForm() if form.validate_on_submit(): post_collection = mongo.db.posts now_user = user_collection.find_one({'username': current_user.username}) created_post = post_collection.insert_one({ 'title': form.title.data, 'content': form.content.data, 'author': now_user['_id'], 'date_posted': datetime.utcnow() }) user_collection.update_one({'username': current_user.username}, { '$push': { 'posts': created_post.inserted_id } }) flash('Your post has been successfully created.', 'success') return redirect(url_for('home')) return render_template('new_post.html', title = 'New Post', form = form, legend = 'New Post')
def update_post(post_id): posts_collection = mongo.db.posts now_post = posts_collection.find_one({'_id': ObjectId(post_id)}) now_post_author = user_collection.find_one({'_id': ObjectId(now_post['author'])}) current_post_author = User(now_post_author['username'], now_post_author['email'], now_post_author['image']) now_post['author'] = now_post_author if current_post_author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): posts_collection.find_and_modify({'_id': ObjectId(post_id)}, { '$set': { 'title': form.title.data, 'content': form.content.data } }) flash('You post has been updated.', 'success') return redirect(url_for('post', post_id = post_id)) elif request.method == 'GET': form.title.data = now_post['title'] form.content.data = now_post['content'] return render_template('new_post.html', title = now_post['title'], form = form, legend = 'Update Post')
def edit_post(id): """Edit existing blog post.""" post = Post.query.get_or_404(id) permission = Permission(UserNeed(post.user.id)) if permission.can() or admin_permission.can(): form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.text = form.text.data post.publish_date = datetime.datetime.now() db.session.commit() return redirect(url_for('.post', post_id=post.id)) form.text.data = post.text return render_template('edit.html', form=form, post=post) abort(403)