def new_post(self): """Writing posts.""" if not c.user.admin: abort(403) c.category_default = model.Category.first() post_write = render('post_write.tpl', slacks=True) if request.environ['REQUEST_METHOD'] != 'POST': return post_write try: form = PostForm().to_python(request.POST) category_id = self.set_category(form) slug = self.set_slug(form) post = model.Post( title=form['title'], category_id=category_id, content=form['content'], user_id=c.user.id, slug=slug, summary=form.get('summary', '') ) model.session.add(post) model.session.commit() redirect_url = url(controller='blog', action='view', category=post.category.slug, slug=post.slug ) redirect(redirect_url) except validators.Invalid, e: return h.htmlfill(e, form=post_write)
def edit_post(self, post): try: form = PostForms().to_python(request.POST) if form['delete']: model.Comment.by_post(c.post.id).delete() model.Post.by_id(c.post.id).delete() model.session.commit() h.flash(_('Post deleted successfully.'), 'success') redirect_url = url(controller='blog', action='index') else: category_id = self.set_category(form) c.post.title = form['title'] c.post.category_id = category_id c.post.content = form['content'] c.post.user_id = c.user.id c.post.summary = form.get('summary', '') if form['slug'] != c.post.slug: c.post.slug = self.set_slug(form) model.session.commit() h.flash(_('Post edited successfully.'), 'success') redirect_url = url( controller='blog', action='view', category=c.post.category.slug, slug=c.post.slug ) redirect(redirect_url) except validators.Invalid, e: return h.htmlfill(e, form=post)
def login(self): login = render('account/login.tpl', slacks=True) if request.environ['REQUEST_METHOD'] != 'POST': return login try: form = LoginForm().to_python(request.POST) except validators.Invalid, e: return h.htmlfill(e, form=login)
def new_comment(self, post_form): remote_ip = h.getip() try: if c.user.id: form = CommentUserForm().to_python(request.POST) # Users do not have to pass any specific information. comment_kwargs = {'user_id': c.user.id} else: form = CommentForm().to_python(request.POST) comment_kwargs = { 'email': form['email'], 'url': form['url'], 'name': form['name'] } except validators.Invalid, e: return h.htmlfill(e, form=post_form)
def edit_comment(self, comment, post): try: form = CommentUserForm().to_python(request.POST) if form['delete']: model.Comment.by_id(comment.id).delete() c.post.comments_count -= 1 model.session.commit() h.flash(_('Comment deleted successfully.'), 'success') redirect_url = c.post_url else: comment.content = form['comment'] model.session.commit() h.flash(_('Comment edited successfully.'), 'success') redirect_url = '%s#comment-%d' % (c.post_url, comment.id) redirect(redirect_url) except validators.Invalid, e: return h.htmlfill(e, form=post)
def edit_category(self, category_view): try: form = CategoryForm().to_python(request.POST) if form['delete']: posts = model.Post.by_category(c.category.id) for post in posts.all(): model.Comment.by_post(post.id).delete() posts.delete() model.Category.by_id(c.category.id).delete() model.session.commit() h.flash(_('Category deleted successfully.'), 'success') redirect_url = url(controller='blog', action='index') else: c.category.title = form['title'] model.session.commit() h.flash(_('Category edited successfully.'), 'success') redirect_url = url( controller='blog', action='view', category=c.category.slug ) redirect(redirect_url) except validators.Invalid, e: return h.htmlfill(e, form=category_view)
def profile(self, id='', edit=''): @cache.beaker_cache(**config['cache_options']) def get_user(): return model.User.by_id(id).one() @cache.beaker_cache(**config['cache_options']) def get_users(): return model.User.all() if not id: try: c.profiles = get_users() except NoResultFound: c.profiles = [] return render('account/profiles.tpl', slacks=True) try: c.profile = get_user() except NoResultFound: abort(404) c.breadcrumbs.append({ 'title': _('Users List'), 'url': url(controller='account', action='profile') }) c.canedit = (c.user.admin or c.user.id == c.profile.id) c.editing = False if edit: if c.canedit: c.editing = True c.breadcrumbs.append({ 'title': _('Editing'), 'url': '' }) else: abort(403) sorted(c.profile.comments, reverse=True) try: c.country = self.geoip.country_code_by_addr(c.profile.ip).lower() except AttributeError: c.country = '' c.comments = c.profile.comments[:5] c.comments_count = len(c.profile.comments) c.posts_count = len(c.profile.posts) profile_page = render('account/profile.tpl', slacks=True) if not request.environ['REQUEST_METHOD'] == 'POST': return profile_page try: form = ProfileForm().to_python(request.POST) # Only administrators can delete users. if form['delete'] and c.user.admin: # Delete all posts, comments and finally the profile for this # user if checkbox is ticked. model.Post.by_user(c.profile.id).delete() model.Comment.by_user(c.profile.id).delete() model.User.by_id(c.profile.id).delete() model.session.commit() h.flash(_('User has been deleted.'), 'success') redirect_url = url(controller='blog', action='index') else: if form['name'] != c.profile.name: try: model.User.by_name(form['name']).one() h.flash(_('Username Taken'), 'error') except NoResultFound: c.profile.name = form['name'] c.profile.email = form['email'] c.profile.identifier = form['identifier'] c.profile.website = form['website'] model.session.commit() h.flash(_('Profile Updated'), 'success') redirect_url = url( controller='account', action='profile', id=c.profile.id ) redirect(redirect_url) except validators.Invalid, e: return h.htmlfill(e, profile_page)
def profile(self, id='', edit=''): @cache.beaker_cache(**config['cache_options']) def get_user(): return model.User.by_id(id).one() @cache.beaker_cache(**config['cache_options']) def get_users(): return model.User.all() if not id: try: c.profiles = get_users() except NoResultFound: c.profiles = [] return render('account/profiles.tpl', slacks=True) try: c.profile = get_user() except NoResultFound: abort(404) c.breadcrumbs.append({ 'title': _('Users List'), 'url': url(controller='account', action='profile') }) c.canedit = (c.user.admin or c.user.id == c.profile.id) c.editing = False if edit: if c.canedit: c.editing = True c.breadcrumbs.append({'title': _('Editing'), 'url': ''}) else: abort(403) sorted(c.profile.comments, reverse=True) try: c.country = self.geoip.country_code_by_addr(c.profile.ip).lower() except AttributeError: c.country = '' c.comments = c.profile.comments[:5] c.comments_count = len(c.profile.comments) c.posts_count = len(c.profile.posts) profile_page = render('account/profile.tpl', slacks=True) if not request.environ['REQUEST_METHOD'] == 'POST': return profile_page try: form = ProfileForm().to_python(request.POST) # Only administrators can delete users. if form['delete'] and c.user.admin: # Delete all posts, comments and finally the profile for this # user if checkbox is ticked. model.Post.by_user(c.profile.id).delete() model.Comment.by_user(c.profile.id).delete() model.User.by_id(c.profile.id).delete() model.session.commit() h.flash(_('User has been deleted.'), 'success') redirect_url = url(controller='blog', action='index') else: if form['name'] != c.profile.name: try: model.User.by_name(form['name']).one() h.flash(_('Username Taken'), 'error') except NoResultFound: c.profile.name = form['name'] c.profile.email = form['email'] c.profile.identifier = form['identifier'] c.profile.website = form['website'] model.session.commit() h.flash(_('Profile Updated'), 'success') redirect_url = url(controller='account', action='profile', id=c.profile.id) redirect(redirect_url) except validators.Invalid, e: return h.htmlfill(e, profile_page)