Commits

Anonymous committed bd8b62c

-pagination for post media pages

Comments (0)

Files changed (20)

 	title = db.StringProperty()
 	created = db.DateTimeProperty(auto_now_add=True)
 	media = blobstore.BlobReferenceProperty()
+	filename = db.StringProperty()
+	filesize = db.IntegerProperty()
+	type = db.StringProperty()
+
+
+	def get_absolute_url(self):
+		return '/media/item/%s' % self.media.key()
+
+	def get_delete_url(self):
+		return '/media/delete/%s' % self.media.key()
 from django.http import HttpResponseRedirect, Http404, HttpResponse
 from django.shortcuts import render_to_response
 from google.appengine.ext import blobstore
+from google.appengine.api import memcache
 from media import models
 from fileform import FileForm
 from utilities import blob_helper
 import urllib
+from django.conf import settings
 import logging
 
+ADMIN_PAGESIZE = settings.PAGESIZE
+
+def listMedia(request):
+    if request.GET.get('page'):
+        page = int(request.GET.get('page'))
+    else:
+        page = 1
+
+    offset = ADMIN_PAGESIZE *(page - 1)
+
+    if memcache.get('mediapage-'+str(page)):
+        list = memcache.get('mediapage-'+str(page))
+    else:
+        list = models.Media.all().order('-created').fetch(ADMIN_PAGESIZE, offset)
+        memcache.set('mediapage-'+str(page), list)
+
+    # check if there is next page
+    offset = ADMIN_PAGESIZE *(page)
+    next_page = models.Media.all().order('-created').fetch(ADMIN_PAGESIZE, offset)
+    if next_page:
+        p_next = page + 1
+    else:
+        p_next = None
+
+    paging = {'prev': page - 1, 'next':p_next}
+
+    return render_to_response('admin/medialist.html', {'list':list,
+                                                 'paging':paging})
+
+
+def delMedia(request, key):
+    blob_key = str(urllib.unquote(key))
+    logging.info(key)
+    logging.info(blobstore.BlobKey(key))
+    logging.info(blobstore.BlobKey(blob_key))
+
+
+    blobstore.delete(blob_key)
+    memcache.delete('mediapage-1')
+    return HttpResponseRedirect('/media/')
+
 def upload(request):
     form = None
     if request.method =='POST':
         form = FileForm(request.POST)
         if form.is_valid() and len(blob_info) == 1:
             media_item = models.Media(title=form.cleaned_data['title'],
-                                   media=blob_info[0])
+                                   media=blob_info[0].key(),
+                                   filename = blob_info[0].filename,
+                                   filesize = blob_info[0].size,
+                                   type = blob_info[0].content_type)
             media_item.put()
             return HttpResponseRedirect('/posts/')
 
-# Create your views here.
-from pages import models
+# Django module
 from django.shortcuts import render_to_response
 from django.http import HttpResponseRedirect, Http404
 from django.template import RequestContext
+from django.conf import settings
 
+# App module
+from pages import models
 from pageform import PageForm
 from contactform import ContactForm
 
+# Appengine module
 from google.appengine.api import memcache
 from google.appengine.api import mail
 
+PAGESIZE = settings.PAGESIZE
+
 def render(request, name):
     page = models.Page.get_by_key_name(name)
     if page:
 
 
 def listPages(request):
-    pages = models.Page.all()
-    return render_to_response('admin/pagelist.html', {
-                                                     'pages':pages})
+    if request.GET.get('page'):
+        page = int(request.GET.get('page'))
+    else:
+        page = 1
+
+    offset = PAGESIZE *(page - 1)
+
+    if memcache.get('pagespage-'+str(page)):
+        pages = memcache.get('pagespage-'+str(page))
+    else:
+        pages = models.Page.all().fetch(PAGESIZE, offset)
+        memcache.set('pagespage-'+str(page), pages)
+
+    # check if there is next page
+    offset = PAGESIZE *(page)
+    next_page = models.Page.all().fetch(PAGESIZE, offset)
+    if next_page:
+        p_next = page + 1
+    else:
+        p_next = None
+
+    paging = {'prev': page - 1, 'next':p_next}
+
+    return render_to_response('admin/pagelist.html', {'pages':pages,
+                                                      'paging':paging})
+
 
 def newPage(request):
     pageForm = None

pages/views.pyc

Binary file modified.
 from gaesessions import get_current_session
 
 
+PAGESIZE = settings.PAGESIZE
+
 def listPost(request):
-	posts = models.Post.all().order('-pub_date')
-	return render_to_response('admin/postlist.html', {
-													  'posts':posts})
+    if request.GET.get('page'):
+        page = int(request.GET.get('page'))
+    else:
+        page = 1
+
+    offset = PAGESIZE *(page - 1)
+
+    if memcache.get('postpage-'+str(page)):
+        posts = memcache.get('postpage-'+str(page))
+    else:
+        posts = models.Post.all().order('-pub_date').fetch(PAGESIZE, offset)
+        memcache.set('postpage-'+str(page), posts)
+
+    # check if there is next page
+    offset = PAGESIZE *(page)
+    next_page = models.Post.all().order('-pub_date').fetch(PAGESIZE, offset)
+    if next_page:
+        p_next = page + 1
+    else:
+        p_next = None
+
+    paging = {'prev': page - 1, 'next':p_next}
+    return render_to_response('admin/postlist.html', {'posts':posts,
+                            						'paging':paging})
 
 
 def get_tag_cat_list():
 
-	cat_tag = memcache.get('cat_tag')
-	if cat_tag is not None:
-		return cat_tag
+  cat_tag = memcache.get('cat_tag')
+  if cat_tag is not None:
+    return cat_tag
 
-	posts = models.Post.all()
+  posts = models.Post.all()
 
-	# get all category
-	cat_list = {}
-	for p in posts:
-		if p.category in cat_list:
-			cat_list[p.category] += 1
-		else:
-			cat_list[p.category] = 1
+  # get all category
+  cat_list = {}
+  for p in posts:
+    if p.category in cat_list:
+      cat_list[p.category] += 1
+    else:
+      cat_list[p.category] = 1
 
-	sorted_cat_list = []
-	for cat in sorted(cat_list):
-		sorted_cat_list.append({'category': cat,
-							    'count':cat_list[cat],
-							    'url': '/posts/category/%s' % cat.replace(' ','-'),
-							    })
+  sorted_cat_list = []
+  for cat in sorted(cat_list):
+    sorted_cat_list.append({'category': cat,
+                  'count':cat_list[cat],
+                  'url': '/posts/category/%s' % cat.replace(' ','-'),
+                  })
 
-	# get all tags
-	tag_list = {}
-	for p in posts:
-		for tag in p.tags:
-			if tag in tag_list:
-				tag_list[tag] +=1
-			else:
-				tag_list[tag] = 1
+  # get all tags
+  tag_list = {}
+  for p in posts:
+    for tag in p.tags:
+      if tag in tag_list:
+        tag_list[tag] +=1
+      else:
+        tag_list[tag] = 1
 
-	sorted_tag_list = []
-	for tag in sorted(tag_list.iterkeys()):
-		sorted_tag_list.append({'tag': tag,
+  sorted_tag_list = []
+  for tag in sorted(tag_list.iterkeys()):
+    sorted_tag_list.append({'tag': tag,
                                 'count': tag_list[tag],
                                 'url': '/posts/tag/%s' % (tag),
                                })
 
-	cat_tag  = {'tag_list': sorted_tag_list,
-		    	'cat_list': sorted_cat_list}
+  cat_tag  = {'tag_list': sorted_tag_list,
+          'cat_list': sorted_cat_list}
 
-	memcache.set('cat_tag', cat_tag)
-	return cat_tag
+  memcache.set('cat_tag', cat_tag)
+  return cat_tag
 
 
 
 
 def stream(request):
-	PAGESIZE = int(settings.PAGESIZE)
-	offset = 0
+  PAGESIZE = int(settings.PAGESIZE)
+  offset = 0
 
-	# get post list
-	if request.GET.get('page'):
-		#get the corrent page
-		page = int(request.GET.get('page'))
-	else:
-		page = 1
+  # get post list
+  if request.GET.get('page'):
+    page = int(request.GET.get('page'))
+  else:
+    page = 1
 
+  offset = settings.PAGESIZE *(page - 1)
 
-	offset = settings.PAGESIZE *(page - 1)
+  if memcache.get('blogpage-'+str(page)):
+    posts = memcache.get('blogpage-'+str(page))
+  else:
+    posts = models.Post.all().order('-pub_date').fetch(settings.PAGESIZE, offset)
+    memcache.set('blogpage-'+str(page), posts)
 
-	if memcache.get('blogpage-'+str(page)):
-		posts = memcache.get('blogpage-'+str(page))
-	else:
-		posts = models.Post.all().order('-pub_date').fetch(settings.PAGESIZE, offset)
-		memcache.set('blogpage-'+str(page), posts)
+  # check if there is next page
+  offset = settings.PAGESIZE *(page)
+  next_page = models.Post.all().order('-pub_date').fetch(settings.PAGESIZE, offset)
+  if next_page:
+    p_next = page + 1
+  else:
+    p_next = None
 
-	# check if there is next page
-	offset = settings.PAGESIZE *(page)
-	next_page = models.Post.all().order('-pub_date').fetch(settings.PAGESIZE, offset)
-	if next_page:
-		p_next = page + 1
-	else:
-		p_next = None
+  paging = {'prev': page - 1, 'next':p_next}
+  # get tag and categories
+  cat_tag = get_tag_cat_list()
 
-	paging = {'prev': page - 1, 'next':p_next}
-	# get tag and categories
-	cat_tag = get_tag_cat_list()
-
-	return render_to_response('front/stream.html', {'posts': posts,
-												    'paging':paging,
-													  'categories': cat_tag['cat_list'],
-													  'tags': cat_tag['tag_list']},
-		                           						context_instance=RequestContext(request))
+  return render_to_response('front/stream.html', {'posts': posts,
+                            'paging':paging,
+                            'categories': cat_tag['cat_list'],
+                            'tags': cat_tag['tag_list']},
+                                           context_instance=RequestContext(request))
 
 
 
 
 def listPostByCategory(request, cat):
-	posts = models.Post.all().filter('category =', cat.replace('-',' '))
+  PAGESIZE = int(settings.PAGESIZE)
+  offset = 0
 
-	# get tag and categories
-	cat_tag = get_tag_cat_list()
+  # get post list
+  if request.GET.get('page'):
+    page = int(request.GET.get('page'))
+  else:
+    page = 1
 
-	return render_to_response('front/stream.html', {'posts': posts,
-													  'categories': cat_tag['cat_list'],
-													  'tags': cat_tag['tag_list']},
-		                           						context_instance=RequestContext(request))
+  offset = settings.PAGESIZE *(page - 1)
+
+  if memcache.get('catpage-'+str(page)):
+    posts = memcache.get('catpage-'+str(page))
+  else:
+    posts = models.Post.all().order('-pub_date').filter('category =', cat.replace('-',' ')).fetch(settings.PAGESIZE, offset)
+    memcache.set('catpage-'+str(page), posts)
+
+  # check if there is next page
+  offset = settings.PAGESIZE *(page)
+  next_page = models.Post.all().order('-pub_date').filter('category =', cat.replace('-',' ')).fetch(settings.PAGESIZE, offset)
+  if next_page:
+    p_next = page + 1
+  else:
+    p_next = None
+
+  paging = {'prev': page - 1, 'next':p_next}
+
+  # get tag and categories
+  cat_tag = get_tag_cat_list()
+
+  return render_to_response('front/stream.html', {'posts': posts,
+                            'paging':paging,
+                            'categories': cat_tag['cat_list'],
+                            'tags': cat_tag['tag_list']},
+                                           context_instance=RequestContext(request))
 
 
 
 def listPostByTag(request, tag):
-	posts = models.Post.all().filter('tags =', tag)
+  PAGESIZE = int(settings.PAGESIZE)
+  offset = 0
 
-	# get tag and categories
-	cat_tag = get_tag_cat_list()
-	return render_to_response('front/stream.html', {'posts': posts,
-											  'categories': cat_tag['cat_list'],
-											  'tags': cat_tag['tag_list']},
-                           						context_instance=RequestContext(request))
+  # get post list
+  if request.GET.get('page'):
+    page = int(request.GET.get('page'))
+  else:
+    page = 1
+
+  offset = settings.PAGESIZE *(page - 1)
+
+  if memcache.get('catpage-'+str(page)):
+    posts = memcache.get('catpage-'+str(page))
+  else:
+    posts = models.Post.all().order('-pub_date').filter('tags =', tag).fetch(settings.PAGESIZE, offset)
+    memcache.set('catpage-'+str(page), posts)
+
+  # check if there is next page
+  offset = settings.PAGESIZE *(page)
+  next_page = models.Post.all().order('-pub_date').filter('tags =', tag).fetch(settings.PAGESIZE, offset)
+  if next_page:
+    p_next = page + 1
+  else:
+    p_next = None
+
+  paging = {'prev': page - 1, 'next':p_next}
+
+
+  # get tag and categories
+  cat_tag = get_tag_cat_list()
+  return render_to_response('front/stream.html', {'posts': posts,
+                            'paging':paging,
+                            'categories': cat_tag['cat_list'],
+                            'tags': cat_tag['tag_list']},
+                                         context_instance=RequestContext(request))
 
 
 
 def showPost(request, year, month, day, key_name):
-	post = models.Post.get_by_key_name(key_name)
-	if post:
-		# get tag and categories
-		cat_tag = get_tag_cat_list()
+  if memcache.get('page-'+key_name):
+    post = memcache.get('page-'+key_name)
+  else:
+    post = models.Post.get_by_key_name(key_name)
+    memcache.set('page-'+key_name, post)
 
-		return render_to_response('front/post.html', {'post': post,
-													  'categories': cat_tag['cat_list'],
-													  'tags': cat_tag['tag_list']},
-		                           						context_instance=RequestContext(request))
-	else:
-		raise Http404
+  if post:
+    # get tag and categories
+    cat_tag = get_tag_cat_list()
+
+    return render_to_response('front/post.html', {'post': post,
+                            'categories': cat_tag['cat_list'],
+                            'tags': cat_tag['tag_list']},
+                                           context_instance=RequestContext(request))
+  else:
+    raise Http404
 
 
 def newPost(request):
-	postForm = None
-	if request.method == 'POST':
-		newPost = postform.PostForm(request.POST)
-		if newPost.is_valid():
-			newPost.save()
-			return HttpResponseRedirect('/posts/')
-		else:
-			postForm = postform.PostForm(request.POST)
+  postForm = None
+  if request.method == 'POST':
+    newPost = postform.PostForm(request.POST)
+    if newPost.is_valid():
+      newPost.save()
+      return HttpResponseRedirect('/posts/')
+    else:
+      postForm = postform.PostForm(request.POST)
 
 
-	if postForm is None:
-		postForm = postform.PostForm()
-	return render_to_response('admin/newpost.html', {
-													'postForm':postForm})
+  if postForm is None:
+    postForm = postform.PostForm()
+  return render_to_response('admin/newpost.html', {
+                          'postForm':postForm})
 
 
 def editPost(request, year, month, day, key_name):
-	if request.method == 'POST':
-		post = models.Post.get_by_key_name(key_name)
-		if post:
-			form = postform.PostForm(request.POST)
-			if form.is_valid():
-				form.save(post)
-		return HttpResponseRedirect('/posts/')
+  if request.method == 'POST':
+    post = models.Post.get_by_key_name(key_name)
+    if post:
+      form = postform.PostForm(request.POST)
+      if form.is_valid():
+        form.save(post)
+    return HttpResponseRedirect('/posts/')
 
-	if request.method == 'GET':
-		post = models.Post.get_by_key_name(key_name)
-		editPostForm = postform.PostForm(initial={
-												  'title': post.title,
-												  'body': post.body,
-												  'category': post.category,
-												  'tags': ' '.join(post.tags)})
-		return render_to_response('admin/newpost.html', {
-														 'postForm':editPostForm,
-														 'action':post.get_edit_url(),})
+  if request.method == 'GET':
+    post = models.Post.get_by_key_name(key_name)
+    editPostForm = postform.PostForm(initial={
+                          'title': post.title,
+                          'body': post.body,
+                          'category': post.category,
+                          'tags': ' '.join(post.tags)})
+
+    return render_to_response('admin/newpost.html', {
+                             'postForm':editPostForm,
+                             'action':post.get_edit_url(),})
 
 
 def delPost(request, year, month, day, key_name):
-	post = models.Post.get_by_key_name(key_name)
-	if post:
-		post.delete()
-	return HttpResponseRedirect('/posts/')
+  post = models.Post.get_by_key_name(key_name)
+  if post:
+    post.delete()
+  return HttpResponseRedirect('/posts/')

posts/views.pyc

Binary file modified.

resources/css/admin.css

 	-webkit-border-radius:6px;
 	border-radius:6px;
 	width:635px;
-	height:500px;
+	height:550px;
 	float:left;
 }
 div.right-form{
 	text-align:left;
 }
 
-table .menu{width:150px;}
+table .menu{width:100px;}
 table .date{width:150px;}
 
 tr.odd td {

resources/img/small/edit.png

Added
New image

resources/img/small/trash.png

Added
New image
 
 
 # APP SETTINGS
-PAGESIZE = 5
+PAGESIZE = 10

settings.pyc

Binary file modified.

templates/admin/header.html

 			<li class="top"><a href="/pages/">Pages</a></li>
 			<li class="item"><a href="/page/new/">New Page</a></li>
 		</ul>
+		<ul>
+			<li class="top"><a href="/media/">Media</a></li>
+			<li class="item"><a href="/media/upload/">Upload</a></li>
+		</ul>
 	</div>
 </div>
 </div>

templates/admin/medialist.html

+{% extends 'admin/base.html' %}
+
+{% block content %}
+	<h3>Media</h3>
+	<table>
+		<thead>
+			<tr>
+				<th class="first menu">Action</th>
+				<th>Title</th>
+				<th>Filename</th>
+				<th>Type</th>
+				<th>Size</th>
+				<th class="last date">Created</th>
+			</tr>
+		</thead>
+		<tbody>
+		{% if list.count > 0 %}
+			{% for l in list %}
+				<tr>
+					<td><a href="{{ l.get_delete_url }}"><img src="/resources/img/small/trash.png" border="0" title="Delete"></a></td>
+					<td>{{ l.title }}</td>
+					<td>{{ l.filename }}</td>
+					<td>{{ l.type }}</td>
+					<td>{{ l.filesize }}</td>
+					<td>{{ l.created.year }}-{{l.created.month }}-{{ l.created.day }}</td>
+				</tr>
+			{% endfor %}
+		{% else %}
+			<tr>
+				<td colspan="6">No Media File</td>
+			</tr>
+		{% endif %}
+		</tbody>
+	</table>
+	{% if paging.prev %}<a href="/media/?page={{ paging.prev }}">Prev Posts</a> &nbsp;&nbsp;{% endif %}
+	{% if paging.next %}<a href="/media/?page={{ paging.next }}">More Post</a>{% endif %}
+{% endblock %}

templates/admin/newpost.html

 <div class="left-form">
 			<div class="ctrlHolder">
 				<label for="id_title" style="display:block">Title {{ postForm.title.errors }}</label>
-				<input id="id_title" class="textInput" type="text" size="50" name="title" max_length="100">
-				
+				<input id="id_title" class="textInput" type="text" size="50" name="title" max_length="100" value="{% if postForm.title.data %}{{ postForm.title.data }}{% endif %}">
+
 				<span class="inlineLabels"><p class="formHint">Max : 100 characters</p></span>
 			</div>
 			<div class="ctrlHolder">
 				<label for="id_body" style="display:block">Body {{ postForm.body.errors }}</label>
-				<textarea id="id_body" name="body" cols="40" rows="18"></textarea>
+				<textarea id="id_body" name="body" cols="40" rows="18">{% if postForm.body.data %}{{ postForm.body.data }}{% endif %}</textarea>
 				<span class="inlineLabels"><p class="formHint">Use markdown to style the text.
 					<a href="http://daringfireball.net/projects/markdown/basics">Please read documentation</a></p></span>
 			</div>
 <div class="right-form">
 	<div class="ctrlHolder">
 		<label for="id_category" style="display:block">Category {{ postForm.category.errors }}</label>
-		<input id="id_category" class="textInput" type="text" size="50" name="category" max_length="30">
+		<input id="id_category" class="textInput" type="text" size="50" name="category" max_length="30"  value="{% if postForm.category.data %}{{ postForm.category.data }}{% endif %}">
 		<span class="inlineLabels"><p class="formHint">Word or Phrase</p></span>
 	</div>
 	<div class="ctrlHolder">
 		<label for="id_tags" style="display:block">Tags {{ postForm.tags.errors }}</label>
-		<input id="id_tags" class="textInput" type="text" size="50" name="tags">
+		<input id="id_tags" class="textInput" type="text" size="50" name="tags"  value="{% if postForm.tags.data %}{{ postForm.tags.data }}{% endif %}">
 		<span class="inlineLabels"><p class="formHint">Separate by space</p></span>
 	</div>
 </div>

templates/admin/pagelist.html

 		{% if pages.count > 0 %}
 			{% for p in pages %}
 				<tr>
-					<td><a href="{{ p.get_edit_url }}">Edit</a> | <a href="{{ p.get_delete_url }}">Remove</a></td>
+					<td><a href="{{ p.get_delete_url }}"><img src="/resources/img/small/trash.png" border="0" title="Delete"></a> |
+						<a href="{{ p.get_edit_url }}"><img src="/resources/img/small/edit.png" border="0" title="Edit"></a></td>
 					<td>{{ p.name }}</td>
 					<td>{% if p.publish %} Yes {% else %} No {% endif %}</td>
 					<td>{{ p.template }}</td>
 		{% endif %}
 		</tbody>
 	</table>
+	{% if paging.prev %}<a href="/pages/?page={{ paging.prev }}">Prev Posts</a> &nbsp;&nbsp;{% endif %}
+	{% if paging.next %}<a href="/pages/?page={{ paging.next }}">More Post</a>{% endif %}
 {% endblock %}

templates/admin/postlist.html

 			<tr>
 				<th class="first menu">Action</th>
 				<th class="title">Title</th>
+				<th>Category</th>
+				<th>Tags</th>
 				<th class="last date">Created</th>
 			</tr>
 		</thead>
 		{% if posts.count > 0 %}
 			{% for p in posts %}
 				<tr>
-					<td><a href="{{ p.get_edit_url }}">Edit</a> | <a href="{{ p.get_delete_url }}">Remove</a></td>
+					<td><a href="{{ p.get_delete_url }}"><img src="/resources/img/small/trash.png" border="0" title="Delete"></a> |
+						<a href="{{ p.get_edit_url }}"><img src="/resources/img/small/edit.png" border="0" title="Edit"></a></td>
 					<td class="left">{{ p.title }}</td>
+					<td class="left">{{ p.category }}</td>
+					<td class="left">{{ p.tags|join:"," }}</td>
 					<td>{{ p.pub_date.year }}-{{ p.pub_date.month }}-{{ p.pub_date.day }}</td>
 				</tr>
 			{% endfor %}
 		{% endif %}
 		</tbody>
 	</table>
+	{% if paging.prev %}<a href="/posts/?page={{ paging.prev }}">Prev Posts</a> &nbsp;&nbsp;{% endif %}
+	{% if paging.next %}<a href="/posts/?page={{ paging.next }}">More Post</a>{% endif %}
 {% endblock %}

templates/pages/about.html

 
 {% block main_content %}
 {% if page %}
+	<h3>{{ page.name }}</h3>
 	{% autoescape off %}
 	{{ page.body_html }}
 	{% endautoescape %}

templates/pages/work.html

 
 {% block main_content %}
 {% if page %}
+	<h3>{{ page.name }}</h3>
 	{% autoescape off %}
-	{{ page.content_html }}
+	{{ page.body_html }}
 	{% endautoescape %}
 {% else %}
 	<h3>Sorry, this page is not ready yet. Please come back soon.</h3>
     (r'^page/edit/([-\w]+)', 'pages.views.editPage'),
     (r'^page/delete/([-\w]+)', 'pages.views.delPage'),
 
+    (r'^media/$', 'media.views.listMedia'),
+    (r'^media/delete/([-\w]+)', 'media.views.delMedia'),
 	(r'^media/upload/', 'media.views.upload'),
     (r'^media/download/([-\w]+)', 'media.views.download'),
 
+
     (r'^contact/$', 'pages.views.contact'),
     (r'^([-\w]+)', 'pages.views.render'),
 )
Binary file modified.