def post_edit(post_id): if request.method == 'GET': try: post = Post.get(Post.post_id == post_id) # todo: get not deleted except Post.DoesNotExist: abort(404) all_categories = Category.select() template = env.get_template('post/edit.html') return template.render(item=post, categories=all_categories, submit_button=u'Оновити') elif request.method == 'POST': post = Post.get(Post.post_id == post_id) post.category = post_get('category_id') post.post_text = post_get('text') post.title = post_get('title') post.draft = bool(int(post_get('draft'))) # zero int is False post.language = post_get('language') new_tags = post_get('tags') old_tags = Tag.select().join(Tag_to_Post)\ .where(Tag_to_Post.post_id == post_id) remove_tags(old_tags, new_tags, post_id) add_new_tags(new_tags, post_id) post.save() app.flash(u'Статтю успішно оновлено') redirect('/post/' + str(post_id))
def post_edit(pid): form = PostForm(request.forms) if form.validate(): s = bottle.request.environ.get('beaker.session') username =s['username'] post =Post.load(int(pid)) if post.author == username or is_admin(username): pass else: return "You do not have Permissions" post.title =form.title.data post.intro= form.intro.data post.body =form.body.data post.keywords = form.keywords.data post.edited=datetime.datetime.now() if form.draft.data==True: post.draft=1 else: post.draft=0 post.save() if len(form.tags.data)>4: tags = form.tags.data.split(",") for tag in tags: post.tags.add(tag.strip(' ')) post.save() return redirect("/posts/%s/%s" %(post.slug,post.pid)) else: return "SNAFU"
def do_post(): form = PostForm(request.forms) if form.validate(): s = bottle.request.environ.get('beaker.session') username =s['username'] slugs = [p.slug for p in Post.all()] slug = unique_slugify(form.title.data, slugs) if form.draft.data==True: draft=1 else: draft=0 post =Post.create(title=form.title.data, intro= form.intro.data, body =form.body.data, created=datetime.datetime.now(),\ edited=datetime.datetime.now(), slug=slug, draft=draft, author=username) post.save() if len(form.tags.data)>4: tags = form.tags.data.split(",") for tag in tags: post.tags.add(tag.strip(' ')) post.save() return redirect(post.get_absolute_url())
def like(post_id): try: post = Post.get(Post.post_id == post_id) post.likes += 1 post.save() except DoesNotExist: abort(404) return 'Ok'
def post_delete(post_id): try: post = Post.get(Post.post_id == post_id) post.deleted = True post.save() app.flash(u'Статтю видалено', 'success') redirect() except Post.DoesNotExist: abort(404)
def post_renew(post_id): try: post = Post.get(Post.post_id == post_id) post.date_updated = datetime.now() post.save() app.flash(u'Стаття актуалізована') redirect('/post/%s' % post_id) except DoesNotExist: abort(404)
def user_page_action(username): # handle new post post = models.Post(author=models.current_user, title=request.forms.get('post_title'), content=request.forms.get('post_content')) post.save() return user_page(username)
def post_page_action(username, title): # handle new comment on a post linked_author = models.User.objects(username=username).first() post = models.Post.objects(author=linked_author, title=title).first() comment = models.Comment(author=models.current_user, content=request.forms.get('comment_area')) post.comments.append(comment) post.save() return post_page(username, title)
def post_add(): if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/add.html') return template.render(categories=all_categories) if request.method == 'POST': post = Post.create( category=post_get('category_id'), post_text=post_get('text'), title=post_get('title'), user=app.current_user.user_id, date_posted=datetime.now(), draft=int(post_get('draft')) == 1, language=post_get('language') ) post_id = post.post_id post.save() add_new_tags(post_get('tags'), post_id) redirect('/post/' + str(post_id))
def post_view(post_id): try: # todo: rewrite with func in Post post = Post.get(Post.post_id == post_id) if post.deleted: # todo: make it visible to its creator raise DoesNotExist cu = app.current_user if post.draft: if cu is not None and cu.user_id != post.user.user_id: raise DoesNotExist if cu is None: raise DoesNotExist except DoesNotExist: abort(404) template = env.get_template('post/view.html') # post.update(views=post.views+1).execute() #classmethod! post.views += 1 post.save() # instance method! return template.render(item=post)