Commits

Sergey Astanin committed e80d19f

using MainPage.get_site_data() to avoid code duplication in multiple page handlers

  • Participants
  • Parent commits 62af48c
  • Tags 0.1.0

Comments (0)

Files changed (1)

 				for p in pages.order('date').fetch(max_pages)]
 		return menu
 
+	@staticmethod
+	def get_site_data():
+		"""returns a dictionary with site data except for the page text"""
+		data={}
+		site=SiteData.get_the_first()
+		user=users.get_current_user()
+		data['title']=site.title
+		data['menu']=MainPage.get_menu()
+		data['username']=user
+		if user:
+			data['logout_url']=cgi.escape(users.create_logout_url("/"))
+		else:
+			data['login_url']=cgi.escape(users.create_login_url("/"))
+		if users.is_current_user_admin():
+			data['is_editor']=True
+		if PageData.all().count(site.max_pages) < site.max_pages:
+			data['max_pages_not_reached']=True
+		return data
+
 	@require_admin
 	def delete_page(self,path):
 		"""delete page if it exists and is deletable"""
 			self.delete_page(path)	
 			self.redirect('/')
 			return
-		menu=self.get_menu()
-		user=users.get_current_user()
-		data={'title':self.site.title,'menu':menu,'page':page,'username':user}
+		data=self.get_site_data()
+		data['page']=page
 		if path != 'index':
 			data['delete_url']=self.request.path+'?delete'
-		if user:
-			data['logout_url']=cgi.escape(users.create_logout_url("/"))
-		else:
-			data['login_url']=cgi.escape(users.create_login_url("/"))
-		if users.is_current_user_admin():
-			data['is_editor']=True
-		if PageData.all().count(self.site.max_pages) < self.site.max_pages:
-			data['max_pages_not_reached']=True
 		self.response.out.write(template.render(TMPL.index_html,data))
 		logging.debug('get: action='+action)
 
 		if PageData.all().count(s.max_pages) >= s.max_pages:
 			self.redirect('/')
 			return
-		data={}
-		data['title']=s.title
-		data['is_editor']=True
-		data['logout_url']=cgi.escape(users.create_logout_url("/"))
-		data['username']=users.get_current_user()
-		data['menu']=MainPage.get_menu()
+		data=MainPage.get_site_data()
+		data['max_pages_not_reached']=False # do not show /newpage on /newpage
 		path = os.path.join(os.getcwd(), TMPL.newpage_html)
 		self.response.out.write(template.render(path,data))
 
 	def get(self):
 		data={}
 		s=SiteData.get_the_first()
+		data=MainPage.get_site_data()
 		data['max_pages']=s.max_pages
-		data['title']=s.title
-		data['logout_url']=cgi.escape(users.create_logout_url("/"))
-		data['username']=users.get_current_user()
-		data['is_editor']=True
-		data['menu']=MainPage.get_menu()
-		if PageData.all().count(s.max_pages) < s.max_pages:
-			data['max_pages_not_reached']=True
 		path = os.path.join(os.getcwd(), TMPL.options_html)
 		self.response.out.write(template.render(path,data))
 
 			' max_pages='+str(max_pages)+' site_title='+unicode(site_title))
 		site=SiteData.get_the_first()
 		if max_pages > 0 and max_pages >= PageData.all().count():
+			site.max_pages=max_pages
+		else:
 			# TODO: give error message
-			site.max_pages=max_pages
+			pass
 		site.title=site_title
 		site.put()
 		self.redirect('/')