Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding the Load more link, /updates/cursor url:Json. Next step, parse…

… the json on the javascript and update the cursor, then refactor
  • Loading branch information...
commit ff5bf56efa0a619f1564c9dd2e370bdf0adaa89b 1 parent 7bf159b
@DFectuoso DFectuoso authored
Showing with 30 additions and 1 deletion.
  1. +20 −1 main.py
  2. +10 −0 templates/main.html
View
21 main.py
@@ -1,4 +1,5 @@
import logging
+import urllib
from google.appengine.ext import webapp, db
from google.appengine.api import urlfetch, memcache, users
@@ -6,6 +7,10 @@
from google.appengine.api.labs import taskqueue
from django.utils import simplejson
+#CONSTANTS#
+UPDATES_LIMIT = 10
+
+
# Parsing the username ourselfs because the nickname on GAE does funky stuff with non @gmail account
def username(user):
return user.nickname().split('@')[0] if user else None
@@ -60,8 +65,20 @@ class Comment(db.Model):
def user_fullname(self):
return fullname(username(self.user))
+#Json representations
+def updates_dict(update):
+ return {
+ 'user':update.user_fullname(),
+ 'body':update.body,
+ 'created':str(update.created)}
# Handlers:
+class UpdatesHandler(webapp.RequestHandler):
+ def get(self,cursor):
+ updates_query = Update.all().order('-created')
+ foo = updates_query.with_cursor(urllib.unquote(cursor)).fetch(UPDATES_LIMIT)
+ self.response.out.write(simplejson.dumps({'messages':map((lambda bar: updates_dict(bar)), foo)}, {'cursor':updates_query.cursor()}))
+
class CommentHandler(webapp.RequestHandler):
def post(self, update_id):
update = Update.get_by_id(int(update_id))
@@ -79,7 +96,8 @@ def get(self):
logout_url = users.create_logout_url('/')
else:
login_url = users.create_login_url('/')
- updates = Update.all().order('-created')
+ updates_query = Update.all().order('-created')
+ updates = updates_query.fetch(UPDATES_LIMIT)
self.response.out.write(template.render('templates/main.html', locals()))
def post(self):
@@ -90,6 +108,7 @@ def post(self):
def main():
application = webapp.WSGIApplication([
('/', MainHandler),
+ ('/updates/(.+)', UpdatesHandler),
('/comment/(.+)', CommentHandler),
('/worker/user', UserWorker),
], debug=True)
View
10 templates/main.html
@@ -20,6 +20,10 @@
</p>
{% endfor %}
{% endfor %}
+<div>
+ <a href="javascript:loadMoreUpdates('{{updates_query.cursor}}')">Load more...</a>
+</div>
+
<script type="text/javascript">
function toggleComment(id) {
@@ -31,5 +35,11 @@
$('#comment-link-'+id).css('display', 'none');
}
}
+
+ function loadMoreUpdates(cursor){
+ $.get('/updates/' + cursor, function(data) {
+ console.log(data)
+ });
+ }
</script>
{% endblock %}
Please sign in to comment.
Something went wrong with that request. Please try again.