Skip to content

Commit

Permalink
Pager is added in list page.
Browse files Browse the repository at this point in the history
  • Loading branch information
alg0002 committed May 21, 2011
1 parent f774153 commit 198e58d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
62 changes: 55 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# ----------------------------------
# Simple Site Manager For Google App Engine (ssm4gae)

import datetime,mimetypes,os,urllib
import datetime,os,urllib
from google.appengine.api import users,memcache
from google.appengine.dist import use_library
use_library('django','0.96')
Expand All @@ -31,11 +31,14 @@
ALIAS_PATH ='/admin_editalias'
UPLOAD_PATH ='/admin_uploadfile'
SETTING_PATH='/admin_setting'
# Directory Index
DIRECTORY_INDEX='index.html'
# Display count per page
COUNT_PER_PAGE=20

class Content(db.Model):
# key_name = 'e'+path
name=db.StringProperty()
path=db.StringProperty(required=True)
public=db.BooleanProperty(required=True)
entitytype=db.StringProperty(required=True, choices=set(["text","file","alias"]), indexed=False)
contenttype=db.StringProperty(indexed=False)
Expand All @@ -46,9 +49,18 @@ class Content(db.Model):
aliastarget=db.StringProperty()
description=db.TextProperty()
lastupdate=db.DateTimeProperty(auto_now=True)
path=db.StringProperty(required=True)
parentpath=db.StringProperty()
def setparentinfo(self):
p=self.path
if p.rfind('/')==-1:
self.parentpath=''
else:
self.parentpath=p[0:p.rfind('/')]

class Setting(db.Model):
utcoffset=db.IntegerProperty(default=9)
# key_name = 'default'
utcoffset=db.IntegerProperty(default=9)

class MainHandler(webapp.RequestHandler):
def get(self):
Expand Down Expand Up @@ -101,7 +113,32 @@ def get(self):
path=os.path.join(os.path.dirname(__file__), 'template_admin.html')
q=Content.all()
q.order('__key__')
self.response.out.write(template.render(path, {'contents':q.fetch(1000,0),'CURRENT_PATH':LIST_PATH,'LIST_PATH':LIST_PATH,'EDIT_PATH':EDIT_PATH,'UPLOAD_PATH':UPLOAD_PATH,'ALIAS_PATH':ALIAS_PATH,'SETTING_PATH':SETTING_PATH}))
mp=int(float(q.count())/float(COUNT_PER_PAGE)+.99)
if mp<1:
mp=1
p=self.request.get(u'page')
if not p or not p.isdigit():
p=1
else:
p=int(p)
if p<1:
p=1
elif p>mp:
p=mp
allpages=range(1,mp+1)
if len(allpages)==1:
allpages=None
self.response.out.write(template.render(path, {
'contents':q.fetch(COUNT_PER_PAGE,(p-1)*COUNT_PER_PAGE),
'CURRENT_PATH':LIST_PATH,
'LIST_PATH':LIST_PATH,
'EDIT_PATH':EDIT_PATH,
'UPLOAD_PATH':UPLOAD_PATH,
'ALIAS_PATH':ALIAS_PATH,
'SETTING_PATH':SETTING_PATH,
'currentpage':p,
'allpages':allpages
}))
return
self.redirect(users.create_login_url(self.request.uri))

Expand Down Expand Up @@ -150,6 +187,8 @@ def post(self):
if p is None:
self.redirect(LIST_PATH)
return
elif p[len(p)-1]=='/':
p=p+DIRECTORY_INDEX
c=get_content(p)
if c:
c.delete()
Expand All @@ -160,6 +199,7 @@ def post(self):
c.name=self.request.get('name')
c.encoding=self.request.get('encoding')
c.textcontent=self.request.get('content')
import mimetypes
mtype,stype=mimetypes.guess_type(c.path)
if mtype:
c.contenttype=str(mtype)
Expand All @@ -170,6 +210,7 @@ def post(self):
else:
c.templatefile=None
c.description=self.request.get('description')
c.setparentinfo()
c.put()
self.redirect(LIST_PATH)
return
Expand Down Expand Up @@ -198,6 +239,8 @@ def post(self):
if fpath is None:
self.redirect(LIST_PATH)
return
elif fpath[len(fpath)-1]=='/':
fpath=fpath+DIRECTORY_INDEX
oldpath=self.request.get('oldpath')
if oldpath:
oldpath=convpath(oldpath)
Expand All @@ -216,12 +259,14 @@ def post(self):
entitytype='file',
blobcontent=ffile,
lastupdate=datetime.datetime.now())
import mimetypes
mtype,stype=mimetypes.guess_type(c.path)
if mtype:
c.contenttype=str(mtype)
else:
c.contenttype=None
c.description=self.request.get('description')
c.setparentinfo()
c.put()
self.redirect(LIST_PATH)
return
Expand Down Expand Up @@ -255,6 +300,8 @@ def post(self):
if p is None:
self.redirect(ALIAS_PATH)
return
elif p[len(p)-1]=='/':
p=p+DIRECTORY_INDEX
c=get_content(p)
if c:
c.delete()
Expand All @@ -265,6 +312,7 @@ def post(self):
c.name=self.request.get('name')
c.aliastarget=self.request.get('aliastarget')
c.description=self.request.get('description')
c.setparentinfo()
c.put()
self.redirect(LIST_PATH)
return
Expand Down Expand Up @@ -313,11 +361,11 @@ def get_content(path):
return c
else:
if path[len(path)-1]=='/':
c=Content.get_by_key_name('e'+path+'index.html')
c=Content.get_by_key_name('e'+path+DIRECTORY_INDEX)
if c:
return c
if path[len(path)-11:]=='/index.html':
c=Content.get_by_key_name('e'+path[0:len(path)-10])
if path[len(path)-1-len(DIRECTORY_INDEX):]=='/'+DIRECTORY_INDEX:
c=Content.get_by_key_name('e'+path[0:len(path)-len(DIRECTORY_INDEX)])
if c:
return c

Expand Down
28 changes: 22 additions & 6 deletions template_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@
padding: 2px 8px;
border: 0px solid #99A8B0;
}
span {
margin: 2px;
padding: 2px;
border: 1px solid #99A8B0;
}
span.current {
background-color: #CCE0FF;
}
table.list {
border: 1px solid #99A8B0;
}
Expand Down Expand Up @@ -155,14 +163,22 @@ <h2>List of contents</h2>
<tr><th>name</th><th>path</th><th>type</th><th>public</th><th>edit</th><th>delete</th></tr>
{% if contents %}
{% for c in contents %}
<tr><td class="listcell">{{ c.name }}</td><td class="listcell"><a href="{{ c.path }}" target="_blank">{{ c.path }}</a></td><td class="listcell">{{ c.entitytype }}</td><td class="listcell">{{ c.public }}</td><td class="listcell">
<a href="{% ifequal c.entitytype 'file' %}{{ UPLOAD_PATH }}?{% else %}{% ifequal c.entitytype 'alias' %}{{ ALIAS_PATH }}?{% else %}{{ EDIT_PATH }}?mode=modify&{% endifequal %}{% endifequal %}target={{ c.path }}">
edit</a></td><td class="listcell"><a href="{{ EDIT_PATH }}?mode=delete&target={{ c.path }}" onclick="return confirm('Are you sure you want to delete [{{ c.name }}] ?');">delete</a></td></tr>
<tr>
<td class="listcell">{{ c.name }}</td>
<td class="listcell"><a href="{{ c.path }}" target="_blank">{{ c.path }}</a></td>
<td class="listcell">{{ c.entitytype }}</td>
<td class="listcell">{{ c.public }}</td>
<td class="listcell"><a href="{% ifequal c.entitytype 'file' %}{{ UPLOAD_PATH }}?{% else %}{% ifequal c.entitytype 'alias' %}{{ ALIAS_PATH }}?{% else %}{{ EDIT_PATH }}?mode=modify&{% endifequal %}{% endifequal %}target={{ c.path }}">edit</a></td>
<td class="listcell"><a href="{{ EDIT_PATH }}?mode=delete&target={{ c.path }}" onclick="return confirm('Are you sure you want to delete [{{ c.name }}] ?');">delete</a></td>
</tr>
{% endfor %}
{% else %}
<tr><td colspan="4">&lt;no data&gt;</td></tr>
{% endif %}
</table><br><br>
{% for p in allpages %}
<span{% ifequal p currentpage %} class="current">{{ p }}{% else %}><a href="{{ LIST_PATH }}?page={{ p }}">{{ p }}</a>{% endifequal %}</span>
{% endfor %}
</div>
{% endifequal %}
{% ifequal CURRENT_PATH EDIT_PATH %}
Expand Down Expand Up @@ -231,9 +247,9 @@ <h2>Setting</h2>
</div>
<div id="menu">
<p id="menucontentslist"{% ifequal CURRENT_PATH LIST_PATH %} class="active">List of contents{% else %} class="inactive"><a href="{{ LIST_PATH }}">List of contents</a>{% endifequal %}</p>
<p id="menueditcontent"{% ifequal CURRENT_PATH EDIT_PATH %} class="active">Edit content{% else %} class="inactive"><a href="{{ EDIT_PATH }}">Edit content</a>{% endifequal %}</p>
<p id="menueditalias"{% ifequal CURRENT_PATH ALIAS_PATH %} class="active">Edit alias{% else %} class="inactive"><a href="{{ ALIAS_PATH }}">Edit alias</a>{% endifequal %}</p>
<p id="menuuploadfile"{% ifequal CURRENT_PATH UPLOAD_PATH %} class="active">Upload file{% else %} class="inactive"><a href="{{ UPLOAD_PATH }}">Upload file</a>{% endifequal %}</p>
<p id="menueditcontent"{% ifequal CURRENT_PATH EDIT_PATH %} class="active">{% if modify %}<a href="{{ EDIT_PATH }}">Edit content</a>{% else %}Edit content{% endif %}{% else %} class="inactive"><a href="{{ EDIT_PATH }}">Edit content</a>{% endifequal %}</p>
<p id="menueditalias"{% ifequal CURRENT_PATH ALIAS_PATH %} class="active">{% if modify %}<a href="{{ ALIAS_PATH }}">Edit alias</a>{% else %}Edit alias{% endif %}{% else %} class="inactive"><a href="{{ ALIAS_PATH }}">Edit alias</a>{% endifequal %}</p>
<p id="menuuploadfile"{% ifequal CURRENT_PATH UPLOAD_PATH %} class="active">{% if modify %}<a href="{{ UPLOAD_PATH }}">Upload file</a>{% else %}Upload file{% endif %}{% else %} class="inactive"><a href="{{ UPLOAD_PATH }}">Upload file</a>{% endifequal %}</p>
<br>
<p id="menusetting"{% ifequal CURRENT_PATH SETTING_PATH %} class="active">Setting{% else %} class="inactive"><a href="{{ SETTING_PATH }}">Setting</a>{% endifequal %}</p>
</div>
Expand Down

0 comments on commit 198e58d

Please sign in to comment.