示例#1
0
	def get(self):
		urls = []
		host = 'http://blog-pj.appspot.com'

		urls.append(Url(host + '/archive', 'monthly', '1.00')) 
		urls.append(Url(host + '/guestbook', 'monthly', '1.00')) 
		urls.append(Url(host + '/about', 'monthly', '1.00')) 
		urls.append(Url(host + '/feed', 'monthly', '1.00')) 

		cats = Category.all()
		for cat in cats:
			urls.append(Url(host + '/category/' + cat.name, 'monthly', '0.8'))
		urls.append(Url(host + '/category/unsorted', 'monthly', '0.8'))

		tags = Tag.all()
		for tag in tags:
			urls.append(Url(host + '/tag/' + tag.name, 'monthly', '0.8'))

		arcs = Archive.all()
		for arc in arcs:
			urls.append(Url(host + '/date/' + str(arc.year) + \
				                   '/' + str(arc.month), 'monthly', '0.8'))

		arts = Article.all()
		for art in arts:
			urls.append(Url(host + '/article/' + art.permalink, 'monthly', '0.6'))

		template_values = {
			'urls': urls
		}

		path = os.path.join(os.path.dirname(__file__), 'sitemap.xml')
		self.response.out.write(template.render(path, template_values))
示例#2
0
	def get(self):
		temp = self.request.path.split('/')
		key = temp[1]

		try:
			pageno = int(temp[-1])
		except ValueError:
			pageno = 1

		if key == 'category':
			if len(temp) == 4 or len(temp) > 3 and temp[3] != 'page':
				path = os.path.join(os.path.dirname(__file__), 'pages/error.html')
				self.response.out.write(template.render(path, None))
				return

			name = unicode(urllib.unquote(temp[2]), 'utf-8')
			if name == 'unsorted':
				query = Article.all().filter('has_category =', False).order('-time_stamp')
			else:
				query = Category.all().filter('name =', name)
				if query.get() != None:
					query = query.get().articles.order('-time_stamp')

		elif key == 'tag':
			if len(temp) == 4 or len(temp) > 3 and temp[3] != 'page':
				path = os.path.join(os.path.dirname(__file__), 'pages/error.html')
				self.response.out.write(template.render(path, None))
				return

			name = unicode(urllib.unquote(temp[2]), 'utf-8')
			query = Tag.all().filter('name =', name)
			if query.get() != None:
				query = query.get().ref_tag.order('-time_stamp')

		else:
			year = int(temp[2])
			month = int(temp[3])
			pageno = 1
			if len(temp) > 4:
				try:
					pageno = int(temp[-1])
				except ValueError:
					pageno = 1

			name = str(year) + '/' + str(month)
			query = db.GqlQuery('select * from Archive where year = :1 and month = :2',
								 year, month)
			if query.get() != None:
				query = query.get().articles.order('-time_stamp')
			

		group = query.fetch(1000)

		article_list = []
		pagei = 0
		start = 0
		length = len(group)

		while group != [] and article_list == []:
			# Pick out each page
			while start + self.article_per_page - 1 < length:
				pagei = pagei + 1
				if pagei == pageno:
					article_list = group[start : start + self.article_per_page]
				start = start + self.article_per_page
			if len(group[start : start + self.article_per_page]) > 0:
				pagei = pagei + 1
				if pagei == pageno:
					article_list = group[start : start + self.article_per_page]

			last_time = group[-1].time_stamp
			query.filter('time_stamp <', last_time)
			group = query.fetch(1000)

		show_left_arrow = 1 if pageno > 1 else 0
		show_right_arrow = 1 if pageno < pagei else 0
		show_left_dot = 1 if pageno >= 5 else 0
		show_right_dot = 1 if pageno <= pagei - 4 else 0

		pageno_list = [i for i in range(max(1, pageno - 3), min(pagei + 1, pageno + 4))]

		if key == 'tag':
			article_set = []
			for item in article_list:
				article_set.append(item.article)

		template_values = {
			'articles': article_list if key != 'tag' else article_set,
			'pagename': 'articles',
			'page_current': pageno,
			'page_total': pagei,
			'pageno_list': pageno_list,
			'show_left_arrow': show_left_arrow,
			'show_right_arrow': show_right_arrow,
			'show_left_dot': show_left_dot,
			'show_right_dot': show_right_dot,
			'colno': len(pageno_list) + show_left_arrow + show_right_arrow + 
			                           show_left_dot + show_right_dot,
			'page_prev': max(1, pageno - 1),
			'page_next': min(pagei, pageno + 1),
			'categories': get_all_categories(),
			'tags': get_all_tags(),
			'name': name,
			'key': key,
			'unsorted_count': count_unsorted(),
		}

		path = os.path.join(os.path.dirname(__file__), 'pages/articles.html')
		self.response.headers['Content-Type'] = 'text/html;charset=utf-8'
		self.response.out.write(template.render(path, template_values))