def create_post(request): form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.written_by = request.user post.save() return redirect('/account')
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 post_edit(request,pk): try: if request.session['username']: post = get_object_or_404(Post,pk=pk) username= Register.objects.get(username=request.session['username']) if username==post.author: if request.method =="POST": form = PostForm(request.POST,instance=post) if form.is_valid(): post = form.save(commit = False) #add Post author post.published_date(timezone.now()) post.save() return redirect('blogapp.views.post_detail',pk=post.pk) else: form = PostForm(instance=post) return render(request,'blogapp/post_edit.html',{'form':form}) else: return HttpResponseRedirect('/error/') except: return HttpResponseRedirect('/login/')
def add_post(request): if request.method == 'GET': category_list = Category.objects.all() post_list = Post.objects.all() return render(request, 'admin/post-add.html', { 'category_list': category_list, 'post_list': post_list }) validate_post = PostForm(request.POST) errors = {} if validate_post.is_valid(): new_post = validate_post.save(commit=False) if 'image' not in request.FILES: errors['image'] = 'Please upload Image' return JsonResponse(errors) if request.FILES['image']: new_post.image = request.FILES['image'] new_post.save() photos = request.FILES.getlist('photos') for p in photos: img = Gal_Image.objects.create(image=p) new_post.photos.add(img) data = {"data": 'Post created successfully', "error": False} return JsonResponse(data) if 'image' not in request.FILES: validate_post.errors['image'] = 'Please upload Image' return JsonResponse(validate_post.errors)
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 post_new(request): form = PostForm(request.POST or None) if request.method == 'POST': if form.is_valid(): message = form.save(commit=False) message.author = request.user message.save() return redirect('post_detail', pk=message.pk) else: return render(request, 'blog/post_edit.html', {'form': form})
def create(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=True) print(post) return redirect('blogapp:post_index') context = {'form': PostForm()} template_name = 'blogapp/post_create.html' return render(request, template_name, context)
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 post_edit(request, pk): post = get_object_or_404(Post, pk=pk) form = PostForm(request.POST or None, instance=post) if request.method == "POST": if form.is_valid(): post = form.save(commit=False) post.author = request.user post.save() return redirect('post_detail', pk=post.pk) else: return render(request, 'blog/post_edit.html', {'form': form})
def edit_post(request, post_id): post_instance = Post.objects.get(pk=post_id) form = PostForm(request.POST or None, instance=post_instance) if form.is_valid(): post = form.save(commit=False) post.written_by = request.user post.save() return redirect('/account') return render(request, 'body/edit_post.html', { 'form': form, 'post_id': post_id })
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 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 create(request): if request.POST: form = PostForm(request.POST) if form.is_valid(): post = form.save() post.author = request.user.username post.save() return HttpResponseRedirect('/') else: form = PostForm() args = {} args.update(csrf(request)) args['form'] = form args['user'] = request.user return render_to_response('create_post.html', args)
def post_add(): #获取session if 'uid' in session: user = User.query.filter_by(id=session['uid']).first() form = PostForm() if request.method == "POST": if form.validate(): print form.data #添加数据 user = Post(author=user,title=form.data['title'], content=form.data['content']) db.session.add(user) db.session.commit() return redirect(url_for('index')) return render_template('post_add.html', form=form)
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 post_new(request): if request.session["username"]: if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = Register.objects.get(username=request.session["username"]) post.published_date = timezone.now() post.save() return redirect("blogapp.views.post_detail", pk=post.pk) else: form = PostForm() else: return HttpResponseRedirect("/login/") return render(request, "blogapp/post_edit.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 account(request): if request.user.is_authenticated(): form = PostForm() args = {} args['posts'] = request.user.written_by.all() args['form'] = form return render(request, 'body/account.html', args) else: return redirect("/login")
def post_edit(request, pk): if request.session["username"]: post = get_object_or_404(Post, pk=pk) if request.method == "POST": form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save(commit=False) # add Post author post.published_date(timezone.now()) post.save() return redirect("blogapp.views.post_detail", pk=post.pk) else: form = PostForm(instance=post) return render(request, "blogapp/post_edit.html", {"form": form}) else: return HttpResponseRedirect("/login/")
def post_new(request): try: if request.session['username']: if request.method=="POST": form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author= Register.objects.get(username=request.session['username']) post.published_date = timezone.now() post.save() return redirect('blogapp.views.post_detail',pk=post.pk) else: form = PostForm() return render(request,'blogapp/post_edit.html',{'form':form}) except: return HttpResponseRedirect('/login/')
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 edit(request,pk): context = RequestContext(request) try: p = Post.objects.get(id=pk) except Post.DoesNotExist: ## 读取分类,如果不存在,则引发错误,并404 raise Http404 if request.method == "POST": forms = PostForm(instance=p,data=request.POST) if forms.is_valid(): forms.save() # 文章更新,我们也更新 cache posts = Post.objects.all() cache.set("posts",posts) categories = Category.objects.all() cache.set("categories",categories) links = FriendLink.objects.all() cache.set("links",links) tags = Tag.objects.all() cache.set("tags",tags) return HttpResponseRedirect(reverse('post_default',kwargs={'pk':pk})) else: messages.info(request,forms.errors) return HttpResponseRedirect(reverse("edit",kwargs={'pk':pk})) else: forms = PostForm(instance=p) tags = Tag.objects.all() categories = Category.objects.all() return render_to_response('blogapp/post_forms.html',{ "forms":forms, 'tags':tags, 'post':p, "categories": categories, 'action':'edit', },context)
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 edit_post(request, pk): if request.method == "GET": post = Post.objects.get(pk=pk) category_list = Category.objects.all() post_list = Post.objects.all() return render(request, 'admin/post-edit.html', { 'post': post, 'post_list': post_list, 'category_list': category_list }) p = Post.objects.get(pk=pk) validate_post = PostForm(request.POST, instance=p) errors = {} if validate_post.is_valid(): new_post = validate_post.save(commit=False) if 'image' in request.FILES: image_path = p.image.url try: os.remove(image_path) except Exception: pass new_post.image = request.FILES['image'] new_post.save() photos = request.FILES.getlist('photos') for p in photos: img = Gal_Image.objects.create(image=p) new_post.photos.add(img) return JsonResponse({ "data": 'Post edited successfully', "error": False }) for k in validate_post.errors: errors[k] = validate_post.errors[k][0] return JsonResponse(errors)
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 add(request): context = RequestContext(request) add_instance = Post(author=request.user) if request.method == "POST": forms = PostForm(instance=add_instance,data=request.POST) if forms.is_valid(): forms.save() # 添加文章之后,我们更新 cache posts = Post.objects.all() cache.set("posts",posts) categories = Category.objects.all() cache.set("categories",categories) links = FriendLink.objects.all() cache.set("links",links) tags = Tag.objects.all() cache.set("tags",tags) return HttpResponseRedirect(reverse("index")) else: messages.info(request,forms.errors) return HttpResponseRedirect(reverse("add")) else: forms = PostForm(instance=add_instance) tags = Tag.objects.all() categories = Category.objects.all() return render_to_response('blogapp/post_forms.html',{ "forms":forms, "tags":tags, "categories": categories, 'action':'add', },context)
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)
def edit(request, post_id): post = get_object_or_404(Post, pk=post_id) if request.method == 'POST': form = PostForm(request.POST, instance=post) if form.is_valid(): form.save(commit=True) return redirect('blogapp:post_index') context = {'form': PostForm(instance=post)} template_name = 'blogapp/post_edit.html' return render(request, template_name, context)