Exemple #1
0
    def view_category(self, load_category, category, edit):
        try:
            c.category, c.posts = load_category()
            c.breadcrumbs.append({
                'title': c.category.title,
                'url': url(
                    controller='blog',
                    action='view',
                    category=c.category.slug
                )
            })

            c.editing = False
            if edit:
                if not c.user.admin:
                    abort(403)
                c.editing = True

            category_view = render('category.tpl', slacks=True)

            if request.environ['REQUEST_METHOD'] != 'POST':
                return category_view

            return self.edit_category(category_view)
        except (NoResultFound, ValueError):
            # There is no category, so our last option is that it's a
            # page.
            try:
                return render('content/%s.tpl' % (category,), slacks=True)
            except IOError:
                # There's no such page!
                abort(404)
Exemple #2
0
    def search(self):
        c.terms = request.GET.get('terms', '')
        c.results = []
        if len(c.terms) < 4:
            h.flash(
                _('Search queries must be at least 4 characters in length.'),
                'error'
            )
            redirect(url(controller='blog', action='index'))

        query = MultifieldParser(
            ['title', 'content', 'summary'],
            schema=index.schema
        ).parse(c.terms)
        results = index.searcher().search(query, limit=10)
        for result in results:
            terms = [v for k, v in query.all_terms() if k == 'content']
            url_kwargs = json.loads(result['url'])
            result['url'] = url(**url_kwargs)
            result['highlights'] = highlight(
                result['content'],
                terms,
                search.schema['content'].format.analyzer,
                ContextFragmenter(terms),
                HtmlFormatter(tagname='span', classname='highlight')
            )
            c.results.append(result)
        return render('search.tpl', slacks=True)
Exemple #3
0
    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)
Exemple #4
0
    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)
Exemple #5
0
    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)
Exemple #6
0
    def index(self, rss=False):
        """The default page is loaded, as no post has been specified."""
        @cache.beaker_cache(**config['cache_options'])
        def load_posts():
            try:
                return model.Post.all()
            except NoResultFound:
                return
        c.posts = load_posts()

        if rss:
            return self.rss(c.posts)

        return render('index.tpl', slacks=True)
Exemple #7
0
    def view_post(self, edit, edit_comment):
        c.breadcrumbs.append({
            'title': c.post.category.title,
            'url': url(
                controller='blog',
                action='view',
                category=c.post.category.slug
            )
        })
        c.post_canedit = (c.user.id == c.post.user_id)
        c.post_url = url(controller='blog', action='view', 
            category=c.post.category.slug, slug=c.post.slug
        )
        c.editing_comment = 0
        c.editing_post = False

        if edit_comment:
            try:
                comment = model.Comment.by_id(edit_comment).one()
                if not (
                    c.user.id 
                    or c.user.id == comment.user_id
                    or c.user.admin
                ):
                    abort(403)
                c.editing_comment = edit_comment
            except NoResultFound:
                abort(404)
        elif edit:
            if not c.post_canedit:
                abort(403)
            c.category_default = model.Category.first()
            c.editing_post = True

        post = render('post.tpl', slacks=True)

        if request.environ['REQUEST_METHOD'] != 'POST':
            return post

        if edit:
            return self.edit_post(post)
        elif edit_comment:
            # We're editing a comment right now.
            return self.edit_comment(comment, post=post)
        else:
            # Allow the posting of comments.
            return self.new_comment(post)
Exemple #8
0
 def document(self):
     """Render the error document"""
     resp = request.environ.get('pylons.original_response')
     c.message = resp.status
     c.code = str(resp.status_int)
     return render('error/%s.tpl' % (c.code,), slacks=True)
Exemple #9
0
    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)
Exemple #10
0
    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)
Exemple #11
0
 def document(self):
     """Render the error document"""
     resp = request.environ.get('pylons.original_response')
     c.message = resp.status
     c.code = str(resp.status_int)
     return render('error/%s.tpl' % (c.code, ), slacks=True)