Commits

Sergey Astanin committed e31d5f6

removed Options class, using per-user UserEditsData instead (possible conflicts between multiple editors in Options); minor formatting tweaks; create new page with the orderid=max(orderid)+1; removed stale en/ version

Comments (0)

Files changed (9)

en/404.html

-<html>
-<head><title>404 {{ message }}</title></head>
-<body bgcolor="white">
-	<center><h1>404 {{ message }}</h1></center>
-	<hr>
-</body>
-</html>
-

en/__init__.py

-# vim: set fileencoding=utf-8 :
-# vim: set ts=4 sw=4:
-
-index_html='en/page.html'
-newpage_html='en/newpage.html'
-
-default_site_title=unicode("Hello Web! | simple AppEngine site","utf8")
-default_page_title=unicode("Hello world!","utf8")
-default_page_text=unicode("""**Hello Web!** is a simple minimalistic site management system (CMS) designed to be used on Google AppEngine. You can easily create flat sites with **Hello Web!**. And IMHO flat sites == good sites. They are easy to use.
-
-Reasons to use **Hello Web!**:
-
-1. Create your own site in 5 minutes
-1. Edit pages in Markdown markup (plain text)
-1. Enjoy the auto-generated site menu
-1. Edit pages in-place, wiki style, without cumbersome “Admin” pages
-1. Use the power of Django templates to customize site appearance
-1. Host your site at Google free of charge
-
-And another equally important reason
-
-7. Change the code as you like, it is *only 5 kB*
-
-**Hello Web!** is distributed under the terms of [GNU Affero GPL](http://www.fsf.org/licensing/licenses/agpl-3.0.html)
-
-![Google App Engine](http://code.google.com/appengine/images/appengine-noborder-120x30.gif)
-""","utf8")
-

en/foot.html

-<div id="ft">
-<hr>
-{% if logout_url %}
-   {% spaceless %}
-   [★]&nbsp;{{ username }}{% if is_editor %}, you can edit pages {% else %}{% endif %}
-   {% endspaceless %} : 
-   <a href="{{ logout_url }}">log&nbsp;out</a>
-{% else %}
-   [☆]&nbsp;<a href="{{ login_url }}">log&nbsp;in</a>
-{% endif %}
-<p id="badges">
-<img src="http://code.google.com/appengine/images/appengine-noborder-120x30.gif" alt="Powered by Google App Engine">
-<img src="http://www.gnu.org/graphics/agplv3-88x31.png" alt="Distributed under tGNU AGPL">
-<img src="images/helloweb.png" alt="Powered by HelloWeb!">
-</p>
-</div><!-- #ft -->
-</div><!-- doc -->
-</body>
-</html>

en/head.html

-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
-"http://www.w3.org/TR/html4/strict.dtd">
-<html>
-<head>
-<title>{{ title }}</title>
-<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.2/build/grids/grids-min.css"> 
-<style type="text/css">
-#badges { text-align: center; }
-</style>
-</head>
-<body>
-<div id="doc" class="yui-t1">
-<div id="hd"><h1>{{ title }}</h1></div><!-- #hd -->

en/newpage.html

-{% include "head.html" %}
-<div id="bd">
-	<div id="yui-main">
-		<div class="yui-b">
-		{% if is_editor %}
-		<h2>Add new page</h2>
-		<form action="/save" method="post">
-		<div>Title: <input name="title" type="text" label="Title:"
-			width="120" value="{{page.title}}"/></div>
-		<div>URL (cannot change later): 
-			<input name="url" type="text" label="URL:"
-			width="120" value="{{page.title}}"/></div>
-		<div>Page text (you can use
-				<a href="http://ru.wikipedia.org/wiki/Markdown">Markdown</a> here):<br /><textarea name="content" label="Text:" rows="12"
-			cols="70">{{ page.content }}</textarea></div>
-		<div><input type="submit" value="Add page"></div>
-		</form>
-		{% endif %}
-		</div>
-	</div>
-	<div class="yui-b">
-	{% if menu %}
-	{% for i in menu %}
-	<p><a href="{{i.url}}" title="{{i.title}}">{{i.title}}</a></p>
-	{% endfor %}
-	{% endif %}
-	</div>
-</div><!-- #bd -->
-{% include "foot.html" %}

en/page.html

-{% include "head.html" %}
-<div id="bd">
-	<div id="yui-main">
-		<div class="yui-b">
-		<h2>{{ page.title }}</h2>
-		<div class="content">
-		{{ page.html_content }}
-		</div>
-		{% if is_editor %}
-		<hr>
-		<form action="/save" method="post">
-			<input name="url" type="hidden" value="{{ page.url }}">
-			<div><input name="title" type="text" label="Title:"
-				width="120" value="{{page.title}}"/></div>
-			<div><em>you can use <a
-				href="http://ru.wikipedia.org/wiki/Markdown">Markdown</a> below:</em><br>
-			<textarea name="content" label="Text:" rows="12"
-				cols="70">{{ page.content }}</textarea></div>
-			<div><input type="submit" value="Update">
-			{% if delete_url %}
-			<a href="{{delete_url}}" style="margin-right:0px;margin-left:20em;"
-				>[–]&nbsp;delete&nbsp;page</a>
-			{% endif %}
-			</div>
-		</form>
-		<br>
-		{% endif %}
-		</div>
-	</div>
-	<div class="yui-b" id="menu">
-	{% if menu %}
-	{% for i in menu %}
-	<p><a href="{{i.url}}" title="{{i.title}}">{{i.title}}</a></p>
-	{% endfor %}
-	{% endif %}
-	{% if is_editor %}{% if max_pages_reached %}{% else %}
-	<p><a href="newpage">[+]&nbsp;add&nbsp;page</a></p>
-	{% endif %}{% endif %}
-	</div>
-</div><!-- #bd -->
-{% include "foot.html" %}
 default_max_pages=12
 template_dir='ru'
 TMPL=__import__(template_dir)
-serv_pages = ['newpage']
+service_pages=['newpage','save','options','save_options',]
 
-
-class Option(db.Model):
-  name = db.StringProperty()
-  value = db.StringProperty()  
-
-class Options:
-	def __init__(self):
-		self.opt = Option().all()
-
-	def get(self, attr):
-		self.opt = Option().all()
-		self.opt = Option().all()
-		if  self.opt:
-			self.opt.filter('name = ', attr)
-			return self.opt.get().value
-
-	def set(self, attr, val):
-		self.opt = Option().all()
-		if self.opt:
-			self.opt.filter('name = ', attr)
-		if self.opt.get():
-			self.opt.get().delete()
-		Option(name=attr, value=val).put()
-
-
+class UserEditsData(db.Model):
+	author=db.UserProperty()
+	title=db.StringProperty()
+	url=db.StringProperty()
+	content=db.TextProperty()
+	error_message=db.StringProperty()
 
 class SiteData(db.Model):
 	title=db.StringProperty()
 	content=db.TextProperty()
 	date=db.DateTimeProperty(auto_now_add=True)
 	site=db.ReferenceProperty(SiteData)
-	orderid = db.IntegerProperty(default=0)
-	
+	orderid=db.IntegerProperty(default=0)
+
 	def get_url(self):
 		if self.url != None:
 			return self.url
 		else:
 			return self.title
-	
-	#def convert(self):
-	#	for p in self.all():		
-	#		if not hasattr(p,'orderid') or p.orderid == None:
-	#			p.orderid=0
-	#			p.put()
+
+	@staticmethod
+	def get_max_order_id():
+		p=PageData.all().order('-orderid').get()
+		if p and p.orderid:
+			return p.orderid
+		else:
+			return 0
 
 
 class RequestStats(db.Model):
-	last = db.StringProperty()
+	last=db.StringProperty()
 
 def MakeStats():
 	m=RequestStats.all().get()
 			return method(self,*args)
 	return do_if_admin
 
-def external_handler(request, data):
-	opt = Options()	
-	if 'error' in request.arguments():		
-		data['error_message'] = opt.get('error_message')
-	if 'restore' in request.arguments():
-		data['save_page_url'] = opt.get('save_page_url')
-		data['save_page_title'] = opt.get('save_page_title')
-		data['save_page_content'] = opt.get('save_page_content')
-	return data
-	
 
 class MainPage(webapp.RequestHandler):
 	@staticmethod
 	def get_menu():
 		"""returns a list of menu items, each as a dictionary"""
 		pages=PageData.all()
-		#pages = db.GqlQuery('SELECT * FROM PageData ORDER BY orderid')
 		max_pages=SiteData.get_the_first().max_pages
 		pages.order('orderid')
 		menu=[ {'url':p.get_url(),'title':p.title, 'order':p.orderid} \
 		page=self.get_page(path)
 		if page == None:
 			self.error(404)
-			path = os.path.join(template_dir, '404.html')
+			path=os.path.join(template_dir, '404.html')
 			self.response.out.write(template.render(path,
 				{'message':'Not found'}))
 			return
 		if 'delete' in self.request.arguments():
-			self.delete_page(path)	
+			self.delete_page(path)
 			self.redirect('/')
 			return
 		if 'up' in  self.request.arguments():
-			p = PageData.all().filter('url = ', path).get()
+			p=PageData.all().filter('url =', path).get()
 			p.orderid += 1
 			p.put()
 			self.redirect('/')
-			return	
+			return
 		if 'down' in  self.request.arguments():
-			p = PageData.all().filter('url = ', path).get()
+			p=PageData.all().filter('url =', path).get()
 			p.orderid -= 1
 			p.put()
 			self.redirect('/')
 			return
-		
-			
+
 		data=self.get_site_data()
 		data['page']=page
-		
+
 		if path != 'index':
 			data['delete_url']=self.request.path+'?delete'
 		self.response.out.write(template.render(TMPL.index_html,data))
 		logging.debug('get: action='+action)
 
 	@require_admin
-	def post(self,action=''):		
+	def post(self,action=''):
 		"""create a new page or update an existing page"""
+
 		def save_data_and_redirect(url, title, content, error_message):
-				opt = Options()
-				opt.set('save_page_url', url)
-				opt.set('save_page_title', title)
-				opt.set('save_page_content', content)
-				opt.set('error_message', error_message)
-				self.redirect('/newpage?restore&error')
-			
+			email=users.get_current_user().email()
+			d=UserEditsData.get_or_insert(email)
+			d.url=url
+			d.title=title
+			d.content=content
+			d.error_message=error_message
+			db.put(d)
+			self.redirect('/newpage')
+
 		path=self.request.get('url')
-		
-		# TODO: fix overwriting existing page from /newpage
 		p=PageData.all().filter('url =',path).get()
 		if p == None: # creating new page
 			if path.strip() != '':
-				if path.strip() not in serv_pages:
+				if path.strip() not in service_pages:
 					self.site=self.create_site_if_not_exists()
 					logging.debug('site='+str(self.site))
 					p=PageData(url=path,site=self.site.key())
-				else:					
-					save_data_and_redirect(
-							path,
+				else:
+					save_data_and_redirect(path,
 							cgi.escape(self.request.get('title')),
 							cgi.escape(self.request.get('content')),
-							u"Служебное имя страницы")
-					logging.debug('post: servis path')
+							u"Это служебный адрес страницы")
+					logging.debug('post: service path')
 					return
-			else:
-				save_data_and_redirect(
-						path,
+			else: # empty path
+				save_data_and_redirect(path,
 						cgi.escape(self.request.get('title')),
 						cgi.escape(self.request.get('content')),
 						u"Не указан путь к странице")
 				logging.debug('post: empty page path')
 				return
-				return
 		p.author=users.get_current_user()
 		p.title=cgi.escape(self.request.get('title'))
-		if p.title == "":
-			p.title = path
+		if p.title == '':
+			p.title=path
 		p.content=cgi.escape(self.request.get('content'))
+		p.orderid=1+PageData.get_max_order_id()
 		p.put()
 		redirect_url='/'+path.encode('utf8')
 		self.redirect(redirect_url)
 
 class NewPage(webapp.RequestHandler):
 	"""display a form to add a new page"""
+
+	@staticmethod
+	def recover_saved(request, data):
+		saved=UserEditsData.get_by_key_name(users.get_current_user().email())
+		logging.debug('recover_saved: '+users.get_current_user().email())
+		if saved != None:
+			data['page']={}
+			data['page']['url']=saved.url
+			data['page']['title']=saved.title
+			data['page']['content']=saved.content
+			data['error_message']=saved.error_message
+			saved.delete()
+		return data
+
 	@require_admin
 	def get(self):
 		s=SiteData.get_the_first()
 			self.redirect('/')
 			return
 		data=MainPage.get_site_data()
-		data = external_handler(self.request, data)
+		data=self.recover_saved(self.request,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))
 	{% if menu %}
 	{% for i in menu %}
 	<p>
-            <a href="{{i.url}}" title="{{i.title}}">{{i.title}}</a>
-           {% if is_editor %}
-                #{{i.order}}<a href="{{i.url}}?up" title="Вверх">+</a>
-                <a href="{{i.url}}?down" title="Вниз">-</a>
-            {% endif %}
-            
-
-
+	{% if is_editor %}
+		#{{i.order}}&nbsp;<a href="{{i.url}}?up"
+		   title="Вверх">↑</a><a href="{{i.url}}?down"
+		   title="Вниз">↓</a>&nbsp;
+	{% endif %}
+	<a href="{{i.url}}" title="{{i.title}}">{{i.title}}</a>
         </p>
 	{% endfor %}
 	{% endif %}
 		<h3>{{error_message}}</h3>
 		<form action="/save" method="post">
 		<div>Заголовок: <input name="title" type="text" label="Заголовок:"
-			size="50" value="{{save_page_title}}"/></div>
+			size="50" value="{{page.title}}"/></div>
 		<div>Адрес (потом изменить нельзя): 
 			<input name="url" type="text" label="Адрес:"
-			size="31" value="{{save_page_url}}"/></div>
+			size="31" value="{{page.url}}"/></div>
 		<div>Текст страницы (в тексте можно использовать разметку
-				<a href="http://ru.wikipedia.org/wiki/Markdown">Markdown</a>):<br /><textarea name="content" label="Текст:" rows="12"
-			cols="70">{{ save_page_content }}</textarea></div>
+						<a href="http://ru.wikipedia.org/wiki/Markdown">Markdown</a>):<br />
+						<textarea name="content" label="Текст:" rows="12"
+			cols="70">{{ page.content }}</textarea></div>
 		<div><input type="submit" value="Добавить страницу"></div>
 		</form>
 		{% endif %}