def update_post(request): post_form = PostForm(request.POST, request = request) if post_form.is_valid(): response = post_form.update() else: response["code"] = settings.APP_CODE["FORM ERROR"] response["errors"] = post_form.errors return HttpResponse(simplejson.dumps(response))
def new_post_form(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('') else: form = PostForm() return render(request, "new_post.html", {'form' : form})
def create_post(request): if request.method == 'GET': form = PostForm() return render_to_response('posts/post.html', {'form':form}, context_instance=RequestContext(request)) if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save() return redirect(reverse_lazy('cms:posts_index')) else: return render_to_response('posts/post.html', {'form':form}, context_instance=RequestContext(request))
def update_post(request, post_id): post = Post.objects.get(id=post_id) if request.method == 'GET': form = PostForm(instance=post) return render_to_response('posts/post.html', {'editing':True, 'form':form}, context_instance=RequestContext(request)) if request.method == 'POST': form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save() return redirect(reverse_lazy('cms:posts_index')) else: return render_to_response('posts/post.html', {'editing':True, 'form':form}, context_instance=RequestContext(request))
def new_post(): user = records.find_one({'_id': current_user._id}) form = PostForm() if (user['role'] == 'author'): if form.validate_on_submit(): mongo.db.post.insert({ "author": user, "title": form.title.data, "date_posted": datetime.datetime.now(), "content": form.content.data }) flash(f'Your post has been created!', 'success') return redirect(url_for('home')) else: abort(403) return render_template('create_post.html', legend='New Post', title='New Post', form=form)
def edit_post(request, post_id=None): # Get the post post = None image_id = None if post_id: post = get_object_or_404(Post, id=post_id) if post.image: image_id = post.image.id if request.method == 'POST': # If the form has been submitted... if post: postform = PostForm(request.POST, request.FILES, prefix='post', instance=post) else: postform = PostForm(request.POST, request.FILES, prefix='post') # check that the image form has been completed (look for a file path) if request.FILES.has_key('image-image'): imageform = process_image_form(request, image_id=image_id) else: imageform = None if postform.is_valid(): # All validation rules pass post = postform.save(commit=False) if imageform: post.image = imageform.cleaned_data['image'] else: post.image = None if not post_id: name = slugify(post.title)[:50] try: Post.objects.get(name=name) count = 0 while True: count += 1 newname = name[:(50 - 1 - len(str(count)))] + '-' + str(count) try: Post.objects.get(name=newname) except Post.DoesNotExist: name = newname except Post.DoesNotExist: pass post.name = name if not post.author: post.author = request.user post.save() return HttpResponseRedirect(post.get_absolute_url()) else: if not post_id: postform = PostForm(prefix='post') # An unbound form imageform = ImageForm(initial=get_initial_data(request), prefix='image') # An unbound form else: post = get_object_or_404(Post, id=post_id) postform = PostForm(instance=post, prefix='post') if not post.image: imageform = ImageForm(initial=get_initial_data(request), prefix='image') # An unbound form else: imageform = ImageForm(post.image, initial=get_initial_data(request), prefix='image') if post_id: action = reverse('edit-post', args=(), kwargs={'post_id': post.id}) else: action = reverse('new-post') return render_to_response('cms/edit_post.html', {'postform': postform, 'imageform': imageform, 'required': REQUIRED_FIELDS, 'action': action}, context_instance=RequestContext(request))
def update_post(_id): form = PostForm() post = mongo.db.post.find_one({'_id': ObjectId(_id)}) user = records.find_one({'_id': current_user._id}) if post['author']['username'] != user['username']: abort(403) if form.validate_on_submit(): mongo.db.post.update_one( {'_id': ObjectId(_id)}, {'$set': { "title": form.title.data, "content": form.content.data }}) flash('Your post has been updated!', 'success') return redirect(url_for('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', legend='Update Post', form=form, post=post)
def add_post(request): post_form = PostForm(initial = {"post_id" : 0}) tag_form = TagSuggestionForm() response = reply_object() if request.method == 'POST': post_form = PostForm(request.POST, request = request) if post_form.is_valid(): if post_form.cleaned_data["object_id"] == 0 or not \ post_form.cleaned_data["object_id"]: response = post_form.save() else: response = post_form.update() else: response["code"] = settings.APP_CODE["FORM ERROR"] response["errors"] = post_form.errors return HttpResponse(simplejson.dumps(response)) return render_to_response("cms/post.html", context_instance = RequestContext(request, {"post_form" : post_form, "save_button_text" : "Save", "tag_form" : tag_form, "base_url" : settings.BASE_URL_PATH }))
def edit_post(request, post_id=None): post = None image_id = None hasimage = False # If post_id specified we are editing an existing post. if post_id: post = get_object_or_404(Post, id=post_id) if not post.can_user_modify(request.user): raise PermissionDenied if post.image: image_id = post.image.id # If the form has been submitted... if request.method == 'POST': # Get a postform if post: postform = PostForm(request.user, request.POST, request.FILES, prefix='post', instance=post) else: postform = PostForm(request.user, request.POST, request.FILES, prefix='post') # Get any attached image if (post and post.image) or request.FILES.has_key('image-image'): imageform = process_image_form(request, image_id=image_id) else: imageform = None # All validation rules pass # Via the post form, this also ensures the listing specified is allowed # and prevents users from posting to lists they don't have permission for if postform.is_valid(): post = postform.save(commit=False) if imageform and imageform.is_valid(): post.image = imageform.cleaned_data['image'] else: post.image = None if not post_id: name = slugify(post.title)[:50] post.name = get_first_available_label(Post, name, 'name') if not post.author: post.author = request.user post.save() return HttpResponseRedirect(post.get_absolute_url()) else: if not post_id: # unbound forms postform = PostForm(request.user, prefix='post') imageform = ImageForm(initial=get_initial_image_data(request), prefix='image') else: postform = PostForm(request.user, instance=post, prefix='post') if not post.image: # unbound form imageform = ImageForm(initial=get_initial_image_data(request), prefix='image') else: imageform = ImageForm(instance=post.image, initial=get_initial_image_data(request), prefix='image') hasimage = True if post_id: action = reverse('edit-post', args=(), kwargs={'post_id': post.id}) else: action = reverse('new-post') return render_to_response('cms/edit_post.html', { 'postform': postform, 'imageform': imageform, 'required': REQUIRED_FIELDS, 'action': action, 'hasimage': hasimage}, context_instance=RequestContext(request))
def edit_post(request, post_id=None): # Get the post post = None image_id = None if post_id: post = get_object_or_404(Post, id=post_id) if post.image: image_id = post.image.id if request.method == 'POST': # If the form has been submitted... if post: postform = PostForm(request.POST, request.FILES, prefix='post', instance=post) else: postform = PostForm(request.POST, request.FILES, prefix='post') # check that the image form has been completed (look for a file path) if request.FILES.has_key('image-image'): imageform = process_image_form(request, image_id=image_id) else: imageform = None if postform.is_valid(): # All validation rules pass post = postform.save(commit=False) if imageform: post.image = imageform.cleaned_data['image'] else: post.image = None if not post_id: name = slugify(post.title)[:50] try: Post.objects.get(name=name) count = 0 while True: count += 1 newname = name[:(50 - 1 - len(str(count)))] + '-' + str(count) try: Post.objects.get(name=newname) except Post.DoesNotExist: name = newname except Post.DoesNotExist: pass post.name = name if not post.author: post.author = request.user post.save() return HttpResponseRedirect(post.get_absolute_url()) else: if not post_id: postform = PostForm(prefix='post') # An unbound form imageform = ImageForm(initial=get_initial_data(request), prefix='image') # An unbound form else: post = get_object_or_404(Post, id=post_id) postform = PostForm(instance=post, prefix='post') if not post.image: imageform = ImageForm(initial=get_initial_data(request), prefix='image') # An unbound form else: imageform = ImageForm(post.image, initial=get_initial_data(request), prefix='image') if post_id: action = reverse('edit-post', args=(), kwargs={'post_id': post.id}) else: action = reverse('new-post') return render_to_response('cms/edit_post.html', { 'postform': postform, 'imageform': imageform, 'required': REQUIRED_FIELDS, 'action': action }, context_instance=RequestContext(request))