示例#1
0
 def post(self):
   name = urllib.quote(self.request.get('name').encode('utf8'))
   category = Category.all().filter('name',name).get()
   if category is not None:
     self.response.out.write('category: %s has already existed' % (name))
   else:
     category = Category()
     category.name = name
     category.put()
     categories = Category.all()
     template_values = {
       'categories': categories,
     }
     self.generate('../templates/admin.html', template_values)
示例#2
0
 def post(self):
     name = urllib.quote(self.request.get('name').encode('utf8'))
     category = Category.all().filter('name', name).get()
     if category is not None:
         self.response.out.write('category: %s has already existed' %
                                 (name))
     else:
         category = Category()
         category.name = name
         category.put()
         categories = Category.all()
         template_values = {
             'categories': categories,
         }
         self.generate('../templates/admin.html', template_values)
示例#3
0
    def get(self):

        urls = []

        def addurl(loc, lastmod=None, changefreq=None, priority=None):
            url_info = {
                'location': loc,
                'lastmod': lastmod,
                'changefreq': changefreq,
                'priority': priority,
            }
            urls.append(url_info)

        addurl(g_blog.baseurl, changefreq='daily', priority=1)

        entries = Entry.all().filter('published =', True).order('-date').fetch(g_blog.sitemap_entries)

        for item in entries:
            loc = '%s/%s' % (g_blog.baseurl, item.link)
            addurl(loc, item.date, 'daily', 0.9)

        if g_blog.sitemap_include_category:
            cats = Category.all()
            for cat in cats:
                loc = '%s/category/%s' % (g_blog.baseurl, cat.slug)
                addurl(loc, None, 'weekly', 0.8)

        if g_blog.sitemap_include_tag:
            tags = Tag.all()
            for tag in tags:
                loc = '%s/tag/%s' % (g_blog.baseurl, urlencode(tag.tag))
                addurl(loc, None, 'weekly', 0.8)

        self.response.headers['Content-Type'] = 'text/xml; charset=utf-8'
        self.render2('views/sitemap.xml', {'urlset': urls})
示例#4
0
文件: cache.py 项目: fly2014/XBLOG
	def update_basic_info(
		update_categories=False,
		update_tags=False,
		update_links=False,
		update_comments=False,
		update_archives=False,
		update_pages=False):

		from model import Entry,Archive,Comment,Category,Tag,Link
		basic_info = ObjCache.get(is_basicinfo=True)
		if basic_info is not None:
			info = ObjCache.get_cache_value(basic_info.cache_key)
			if update_pages:
				info['menu_pages'] = Entry.all().filter('entrytype =','page')\
							.filter('published =',True)\
							.filter('entry_parent =',0)\
							.order('menu_order').fetch(limit=1000)
			if update_archives:
				info['archives'] = Archive.all().order('-year').order('-month').fetch(12)
			if update_comments:
				info['recent_comments'] = Comment.all().order('-date').fetch(5)
			if update_links:
				info['blogroll'] = Link.all().filter('linktype =','blogroll').fetch(limit=1000)
			if update_tags:
				info['alltags'] = Tag.all().order('-tagcount').fetch(limit=100)
			if update_categories:
				info['categories'] = Category.all().fetch(limit=1000)

			logging.debug('basic_info updated')
			basic_info.update(info)
示例#5
0
    def get(self, page_slug=""):
        if page_slug:
            t_values = {}

            posts = Entry.all().filter("is_external_page =", True).filter("entrytype =", 'page').filter("slug =", page_slug)
            if posts.count() == 1:
                logging.warning("find one page with slug=%s" % (page_slug))
                posts = posts.fetch(limit=1)
                post = posts[0]
                t_values['post'] = post
                # dump(post)

                # find all comments
                comments = Comment.all().filter("entry =", post).order("date")
                t_values['comments'] = comments
            else:
                logging.warning("%d entries share the same slug %s" % (posts.count(), page_slug))

            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("page.html", t_values, "basic", False))
        else:
            self.redirect(uri_for("weblog.index"))
示例#6
0
    def get(self):
        t_values = {}
        logging.info("CategoryManager: get")

        # find all categories
        cates = Category.all().order("name")
        t_values["categories"] = cates
        return self.response.out.write(render_template("categories.html", t_values, "", True))
示例#7
0
    def get(self, page="1", cate_slug=""):
        t_values = {}
        page = int(page)
        logging.info("IndexHandler - get: page = %d, cate_slug = %s" % (page, cate_slug))

        # find all entries by order
        query = Entry.all().filter("is_external_page =", True).filter("entrytype =", 'post').order("-date")
        # add category filter?
        if cate_slug:
            cates = Category.all().filter("slug =", cate_slug)
            if cates:
                query = query.filter("category =", cates[0])

        # pagination
        total_posts = query.count()
        q_limit = Configuration["posts_per_page"]
        q_offset = (page - 1) * Configuration["posts_per_page"]
        logging.info("limit = %d, offset = %d" % (q_limit, q_offset))

        # get entries
        entries = query.fetch(limit=q_limit, offset=q_offset)
        t_values['entries'] = entries

        # show entries for debug purpose
        # for entry in entries:
        #     logging.info("entry title: %s, public = %s, cate = %s" % (entry.title, entry.is_external_page, entry.category.name))

        logging.info("total posts = %d, current_page = %d, posts_per_page = %d" % (total_posts, page, Configuration['posts_per_page']))
        t_values['navlist'] = generateNavList(total_posts, page, Configuration["posts_per_page"])
        # logging.info(t_values['navlist'])

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

        # find all categories
        categories = Category.all()
        t_values['categories'] = categories

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

        # show index page
        return self.response.out.write(render_template("index.html", t_values, "basic", False))
示例#8
0
 def create_category(category_name):
     """ Create a category in datastore """
     query = Category.all()
     query.filter("description =", category_name)
     ret = query.get()
     if ret is None:
         ret = Category()
         ret.description = category_name
         ret.put()
     return ret
示例#9
0
def getCategoryLists():
  key_ = "category_lists"
  categories = memcache.get(key_)
  if categories is not None:
    return categories
  else:
    categories_ = Category.all()
    categories = [x for x in categories_]
    if not memcache.add(key_, categories, 3600):
      logging.error("Memcache set failed.")
    return categories
示例#10
0
    def get(self):
        # find stats for this blog
        stats = {}
        stats['posts'] = Entry.all().filter("entrytype =", "post").filter("is_external_page =", True).count()
        stats['pages'] = Entry.all().filter("entrytype =", "page").filter("is_external_page =", True).count()
        stats['comments'] = Comment.all().count()
        stats['categories'] = Category.all().count()
        stats['links'] = Link.all().count()

        t_values = {}
        t_values['stats'] = stats
        return self.response.out.write(render_template("index.html", t_values, "", True))
示例#11
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))
示例#12
0
 def get(self):
   categories = Category.all()
   cache_stats = memcache.get_stats()
   oldest_item_age = int(cache_stats['oldest_item_age'])
   format_time = str(oldest_item_age/3600)+":"
   oldest_item_age = oldest_item_age%3600
   format_time += str(oldest_item_age/60)+":"
   oldest_item_age = oldest_item_age%60
   format_time += str(oldest_item_age/60)
   cache_stats['oldest_item_age'] = format_time
   ua_list = UserAgent.all().fetch(500)
   ua_list = sorted(ua_list,key = lambda x:x.count,reverse = True)
   visitor_counter = Visitor.all().count()
   template_values = {
     'cache_stats': cache_stats,
     'ua_list': ua_list,
     'visitor_counter': visitor_counter,
   }
   self.generate('../templates/admin.html', template_values)
示例#13
0
 def get(self):
     categories = Category.all()
     cache_stats = memcache.get_stats()
     oldest_item_age = int(cache_stats['oldest_item_age'])
     format_time = str(oldest_item_age / 3600) + ":"
     oldest_item_age = oldest_item_age % 3600
     format_time += str(oldest_item_age / 60) + ":"
     oldest_item_age = oldest_item_age % 60
     format_time += str(oldest_item_age / 60)
     cache_stats['oldest_item_age'] = format_time
     ua_list = UserAgent.all().fetch(500)
     ua_list = sorted(ua_list, key=lambda x: x.count, reverse=True)
     visitor_counter = Visitor.all().count()
     template_values = {
         'cache_stats': cache_stats,
         'ua_list': ua_list,
         'visitor_counter': visitor_counter,
     }
     self.generate('../templates/admin.html', template_values)
示例#14
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"))