Esempio n. 1
0
    def get(self, page_id="", operation=""):
        t_values = {}
        logging.info("PageManager get: page_id = %s, operation = %s" % (page_id, operation))

        # find current_post based on page_id
        if page_id:
            current_post = Entry.get_by_id(long(page_id))
            if current_post:
                logging.info("find post %s from post id %s" % (page_id, current_post.title))
                if operation == "edit":
                    t_values['current_post'] = current_post
                elif operation == "publish":
                    current_post.is_external_page = True
                    current_post.put()
                    t_values['alert_message'] = "Post %s has been changed to public" % (current_post.title)
                elif operation == "unpublish":
                    current_post.is_external_page = False
                    current_post.put()
                    t_values['alert_message'] = "Post %s has been changed to private" % (current_post.title)
                elif operation == "delete":
                    current_post.delete()
                    t_values['alert_message'] = "Post %s has been changed to deleted" % (current_post.title)

        # show all posts
        posts = Entry.all().filter("entrytype =", 'page')
        t_values['posts'] = posts
        return self.response.out.write(render_template("pages.html", t_values, "", True))
Esempio n. 2
0
    def post(self):
        # add new post or edit existed post
        t_values = {}
        current_post_id = self.request.POST["current_post_id"]
        post_title = self.request.POST["blog_title"]
        post_slug = get_safe_slug(self.request.POST["blog_slug"])
        post_content = self.request.POST["blog_content"]
        # find category
        blog_category_id = self.request.POST["blog_category_id"]
        post_category = Category.get_by_id(long(blog_category_id))
        if post_category:
            logging.info("find category %s for id %s" % (post_category.name, blog_category_id))
        else:
            logging.error("category id %s can't be located" % (blog_category_id))

        if current_post_id:
            logging.info("PostManager: post : edit post current_post_id = %s" % (current_post_id))
            # update existed post
            post = Entry.get_by_id(long(current_post_id))
            if post:
                t_values['alert_message'] = "Post %s has been updated!" % (post.title)
                post.title = post_title
                post.slug = post_slug
                post.content = post_content
                post.entrytype = "post"
                # update category count if this post is public
                if post.is_external_page and post.category != post_category:
                    if post.category and (post.category.entrycount > 0):
                        post.category.entrycount -= 1
                        post.category.put()
                    post_category.entrycount += 1
                    post.category.put()
                post.category = post_category
                post.put()
        else:
            logging.info("PostManager: post : new post title %s" % (self.request.POST['blog_title']))
            # create new post
            post = Entry()
            post.title = post_title
            post.slug = post_slug
            post.content = post_content
            post.entrytype = 'post'
            post.category = post_category
            # save as public or private?
            operation = self.request.POST["submit_action"]
            if operation == "save_publish":
                post.is_external_page = True
                # update category count
                post.category.entrycount += 1
                post.category.put()
            else:  # "save" operation
                post.is_external_page = False
            # save the post
            post.put()
            t_values['alert_message'] = "Post %s has been created!" % (post.title)

        # show all posts
        posts = Entry.all().filter("entrytype =", 'post')
        t_values['posts'] = posts
        return self.response.out.write(render_template("posts.html", t_values, "", True))
Esempio n. 3
0
 def post(self, user_id, feed_id, entry_id):
     """Updates a specific entry"""
     current_user = utils.get_current_user()
     
     if not current_user:
         self.error(403)
         return
     
     user = User.get_by_id(int(user_id))
     
     if user == None:
         self.error(404)
         return
     
     if current_user.key() != user.key():
         self.error(401)
         return
     
     feed = InputFeed.get_by_id(int(feed_id), parent=user)
     
     if feed == None:
         self.error(404)
         return
     
     entry = Entry.get_by_id(int(entry_id), parent=feed)
     
     published = self.request.get('published')
     
     if published != None:
         entry.published = bool(int(published))
         entry.save()
     
     self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
     self.response.headers['Access-Control-Allow-Origin'] = '*'
     self.response.out.write(json.dumps(entry.to_struct()))
Esempio n. 4
0
    def get(self, post_id="", operation=""):
        t_values = {}
        logging.info("PostManager get: post_id = %s, operation = %s" % (post_id, operation))

        # find current_post based on post_id
        if post_id:
            current_post = Entry.get_by_id(long(post_id))
            if current_post:
                logging.info("find post %s from post id %s" % (post_id, current_post.title))
                if operation == "edit":
                    t_values['current_post'] = current_post
                elif operation == "publish":
                    if not current_post.is_external_page:
                        current_post.category.entrycount += 1
                        current_post.category.put()
                        current_post.is_external_page = True
                        current_post.put()
                        t_values['alert_message'] = "Post %s has been changed to public" % (current_post.title)
                    else:
                        t_values['alert_message'] = "Post %s was public already" % (current_post.title)
                elif operation == "unpublish":
                    if current_post.is_external_page:
                        current_post.category.entrycount -= 1
                        current_post.category.put()
                        current_post.is_external_page = False
                        current_post.put()
                        t_values['alert_message'] = "Post %s has been changed to private" % (current_post.title)
                    else:
                        t_values['alert_message'] = "Post %s was private already" % (current_post.title)
                elif operation == "delete":
                    if current_post.is_external_page:
                        current_post.category.entrycount -= 1
                        current_post.category.put()
                    current_post.delete()
                    t_values['alert_message'] = "Post %s has been changed to deleted" % (current_post.title)

        # show all posts
        posts = Entry.all().filter("entrytype =", 'post')
        t_values['posts'] = posts

        # load all categories
        categories = Category.all().order("name")
        t_values['categories'] = categories

        return self.response.out.write(render_template("posts.html", t_values, "", True))
Esempio n. 5
0
    def post(self, feed_id, entry_id, tag_title):
        """ Create or modify tag """
        current_user = utils.get_current_user()
        
        if not current_user:
            logging.info('no user logged in')
            self.error(403)
            return

        logging.info(feed_id)
        feed = InputFeed.get_by_id(int(feed_id), parent=current_user)

        if not feed:
            logging.info('no feed found')
            self.error(404)
            return

        entry = Entry.get_by_id(int(entry_id), parent=feed)

        if not entry:
            logging.info('no entry found')
            self.error(404)
            return

        if not tag_title:
            logging.info('no tag_title provided found')
            self.error(400)
            return

        tag_title = tag_title.decode('utf-8')
        tag = Tag.all().ancestor(current_user).filter('title_lower =', tag_title.lower()).get()

        if not tag:
            logging.info('tag not found, creating new')

            tag = Tag(
                parent=current_user,
                title=tag_title,
                title_lower=tag_title.lower(),
            )

        tag.tag_entry(entry)
        self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.out.write('ok')
Esempio n. 6
0
    def delete(self, feed_id, entry_id, tag_title):
        """ Delete tag """
        current_user = utils.get_current_user()
        
        tag_title = tag_title.decode('utf-8')
        
        if not current_user:
            logging.info('no user logged in')
            self.error(403)
            return

        logging.info(feed_id)
        feed = InputFeed.get_by_id(int(feed_id), parent=current_user)

        if not feed:
            logging.info('no feed found')
            self.error(404)
            return

        entry = Entry.get_by_id(int(entry_id), parent=feed)

        if not entry:
            logging.info('no entry found')
            self.error(404)
            return

        if not tag_title:
            logging.info('no tag_title provided found')
            self.error(400)
            return

        tag = Tag.all().ancestor(current_user).filter('title_lower =', tag_title.lower()).get()

        if not tag:
            logging.info('no tag found')
            self.error(404)
            return

        entry.tags.remove(tag.key())
        entry.save()
        self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.out.write('ok')
Esempio n. 7
0
    def post(self):
        t_values = {}
        # add new post or edit existed post
        current_post_id = self.request.POST["current_post_id"]
        if current_post_id:
            logging.info("PageManager: post : current_post_id = %s" % (current_post_id))
            # update existed post
            post = Entry.get_by_id(long(current_post_id))
            if post:
                t_values['alert_message'] = "Post %s has been updated!" % (post.title)
                post.title = self.request.POST["blog_title"]
                post.slug = get_safe_slug(self.request.POST["blog_slug"])
                post.content = self.request.POST["blog_content"]
                post.entrytype = 'page'
                post.put()

        else:
            logging.info("PageManager: post : new post title %s" % (self.request.POST['blog_title']))
            # create new post
            post = Entry()
            post.title = self.request.POST["blog_title"]
            post.slug = get_safe_slug(self.request.POST["blog_slug"])
            post.content = self.request.POST["blog_content"]
            # post.categories = self.request.POST["blog_categories"]
            operation = self.request.POST["submit_action"]
            logging.info("operation = %s" % (operation))
            if operation == "save_publish":
                post.is_external_page = True
            else:  # "save" operation
                post.is_external_page = False
            post.entrytype = 'page'
            post.put()

            t_values['alert_message'] = "Page %s: %s has been created!" % (post.title, post.entrytype)

        # show all posts
        posts = Entry.all().filter("entrytype =", 'page')
        t_values['posts'] = posts
        return self.response.out.write(render_template("pages.html", t_values, "", True))
Esempio n. 8
0
    def post(self, post_slug=""):
        if post_slug:
            t_values = {}

            post_id = self.request.POST['post_id']
            post = Entry.get_by_id(long(post_id))
            if post:
                # ok, we find the post, try to add comment to this post
                logging.warning("find one post with post_id %s" % (post_id))
                t_values['post'] = post
                # dump(post)

                # check google recaptcha, these two fileds might not exist due to connection to reCAPTCHA
                recaptcha_challenge_field = self.request.POST.get('recaptcha_challenge_field', "")
                recaptcha_response_field = self.request.POST.get('recaptcha_response_field', "")
                remote_ip = self.request.environ['REMOTE_ADDR']
                private_key = "6LdwFdISAAAAAOYRK7ls3O-kXPTnYDEstrLM2MRo"
                antispam_flag = False
                try:
                    result = submit(recaptcha_challenge_field, recaptcha_response_field, private_key, remote_ip)
                    logging.info("google recaptcha %s, %s" % (result.is_valid, result.error_code))
                    if result.is_valid:
                        antispam_flag = True
                except:
                    e = sys.exc_info()[0]
                    logging.info(e)

                # create comment for this post
                if antispam_flag:
                    logging.info("PostManager - add comment")
                    comm_author = self.request.POST['author']
                    comm_email = self.request.POST['email']
                    comm_weburl = self.request.POST['weburl']
                    comm_content = self.request.POST['comment']
                    comment_ip = self.request.environ['REMOTE_ADDR']
                    comm = Comment(entry=post, author=comm_author, email=comm_email, weburl=comm_weburl, content=comm_content, ip=comment_ip)
                    comm.put()
                    t_values['alert_message'] = "Thanks %s for your comment!" % (comm_author)
                else:
                    logging.warning("comment ignored because antispam failed")
                    t_values['alert_message'] = "Sorry, your comment was ignored because of reCAPTCHA failure!"

                # find all comments
                comments = Comment.all().filter("entry =", post).order("date")
                logging.info("PostHandler, post, find %d comments" % (comments.count()))
                if post_id:
                    # only update commentcount when new comment is added
                    post.commentcount = comments.count()
                    post.put()
                t_values['comments'] = comments
            else:
                logging.warning("post_id %s does not exist" % (post_id))

            links = Link.all().order("date")
            t_values['links'] = links

            categories = Category.all()
            t_values['categories'] = categories
    
            pages = Entry.all().filter("is_external_page =", True).filter("entrytype =", 'page').order("date")
            t_values['pages'] = pages

            return self.response.out.write(render_template("post.html", t_values, "basic", False))
        else:
            self.redirect(uri_for("weblog.index"))