Skip to content

Instantly share code, notes, and snippets.

@nhanb
Created May 5, 2014 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhanb/53952eaf4a0a2e641456 to your computer and use it in GitHub Desktop.
Save nhanb/53952eaf4a0a2e641456 to your computer and use it in GitHub Desktop.
GAE
import os
import urllib
from google.appengine.datastore.datastore_query import Cursor
import jinja2
import webapp2
import datetime
from google.appengine.ext import ndb
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
class Category(ndb.Model):
"""Models a category record"""
name = ndb.StringProperty()
description = ndb.StringProperty()
class Article(ndb.Model):
"""Models an article record"""
title = ndb.StringProperty()
intro = ndb.StringProperty()
content = ndb.TextProperty()
imgUrl = ndb.StringProperty()
publishDate = ndb.DateProperty()
category = ndb.KeyProperty(kind=Category)
class MainPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('/html/homepage.html')
article_query = Article.query().order(-Article.publishDate)
article = article_query.fetch()
category = Category.query().fetch()
template_values = {'article': article, 'category': category}
self.response.write(template.render(template_values))
class DetailPage(webapp2.RequestHandler):
def get(self, id ):
template = JINJA_ENVIRONMENT.get_template('/html/detail.html')
article_query = Article.query().order(-Article.publishDate)
query = article_query.fetch()
article = Article.get_by_id(int(id))
category = Category.query().fetch()
template_values = {'query': query, 'article': article, 'category': category}
self.response.write(template.render(template_values))
class ListArticle(webapp2.RequestHandler):
def get(self, id ):
template = JINJA_ENVIRONMENT.get_template('/html/list.html')
#article_query = Article.query().order(-Article.publishDate)
#article = article_query.fetch()
category = Category.get_by_id(int(id))
query = Category.query().fetch()
# start
curs = Cursor(urlsafe=self.request.get('cursor'))
articles, next_curs, more = Article.query().fetch_page(1, start_cursor=curs)
next_cur_url = "asdf"
if more and next_curs:
next_cur_url = next_curs.urlsafe()
template_values = {'query': query, 'article': articles, 'category':
category, 'next_cur_url': next_cur_url}
#end
self.response.write(template.render(template_values))
############
class AdminPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('/templates/admin.html')
article_query = Article.query().order(-Article.publishDate)
article = article_query.fetch()
category = Category.query().fetch()
template_values = {'article': article, 'category': category}
self.response.write(template.render(template_values))
class ArticlePage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('/templates/article.html')
article = Article.query().fetch()
category = Category.query().fetch()
template_values = {'article': article, 'category': category}
self.response.write(template.render(template_values))
def post(self):
title = self.request.get('title')
intro = self.request.get('intro')
content = self.request.get('content')
imgUrl = self.request.get('imgUrl')
day = self.request.get('day')
month = self.request.get('month')
year = self.request.get('year')
categoryID = self.request.get('category')
category = Category.get_by_id(int(categoryID))
article = Article()
article.title = title
article.intro = intro
article.content = content
article.imgUrl = imgUrl
article.publishDate = datetime.date(year=int(year), month=int(month), day=int(day))
article.category = category.key
article.put()
self.redirect('/admin')
class ArticleEdit(webapp2.RequestHandler):
def get(self, id ):
template = JINJA_ENVIRONMENT.get_template('/templates/articleEdit.html')
article = Article.get_by_id(int(id))
category = Category.query().fetch()
template_values = {'article': article, 'category': category}
self.response.write(template.render(template_values))
def post(self, id):
title = self.request.get('title').replace("'", ''');
intro = self.request.get('intro')
content = self.request.get('content')
imgUrl = self.request.get('imgUrl')
day = self.request.get('day')
month = self.request.get('month')
year = self.request.get('year')
categoryID = self.request.get('category')
category = Category.get_by_id(int(categoryID))
article = Article.get_by_id(int(id))
article.title = title
article.intro = intro
article.content = content
article.imgUrl = imgUrl
article.publishDate = datetime.date(year=int(year), month=int(month), day=int(day))
article.category = category.key
article.put()
self.redirect('/admin')
#class for edit/update, delete
class ArticleDelete(webapp2.RequestHandler):
def get(self, id ):
template = JINJA_ENVIRONMENT.get_template('/templates/articleDelete.html')
article = Article.get_by_id(int(id))
template_values = {'article': article}
self.response.write(template.render(template_values))
def post(self, id):
article = Article.get_by_id(int(id))
article.key.delete()
self.redirect('/admin')
##for Category
class CategoryPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('/templates/category.html')
category = Category.query().fetch()
template_values = {'category': category}
self.response.write(template.render(template_values))
def post(self):
name = self.request.get('name')
description = self.request.get('description')
category = Category()
category.name = name
category.description = description
category.put()
self.redirect('/admin')
class CategoryEdit(webapp2.RequestHandler):
def get(self, id ):
template = JINJA_ENVIRONMENT.get_template('/templates/categoryEdit.html')
category = Category.get_by_id(int(id))
template_values = {'category': category}
self.response.write(template.render(template_values))
def post(self, id):
name = self.request.get('name')
description = self.request.get('description')
#get school by its id, every instance in datastore will have a unique number called id
category = Category.get_by_id(int(id))
category.name = name
category.description = description
category.put()
self.redirect('/admin')
class CategoryDelete(webapp2.RequestHandler):
def get(self, id ):
template = JINJA_ENVIRONMENT.get_template('/templates/categoryDelete.html')
#get category by its id
category = Category.get_by_id(int(id))
template_values = {'category': category}
self.response.write(template.render(template_values))
def post(self, id):
category = Category.get_by_id(int(id))
category.key.delete()
self.redirect('/admin')
application = webapp2.WSGIApplication([('/admin', AdminPage),
('/', MainPage),
('/Article', ArticlePage),
(r'/DetailPage/(\d+)', DetailPage),
(r'/ListArticle/(\d+).*', ListArticle),
(r'/ArticleEdit/(\d+)', ArticleEdit),
(r'/ArticleDelete/(\d+)', ArticleDelete),
('/Category', CategoryPage),
(r'/CategoryEdit/(\d+)', CategoryEdit),
(r'/CategoryDelete/(\d+)', CategoryDelete),
],debug=True)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Whatsoever Times</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../stylesheets/homepage.css">
<link rel="shortcut icon" href="../images/favicon.ico">
</head>
<a href="#top"></a>
<a href="#top" id="btt"><img src="../images/top.png" width="50px" height="auto"/></a>
<body>
<div class="container">
<header>
<h1>Whatsoever Times</h1>
<h2>Where you can find all the fun stuffs</h2>
<div class="containdiv">
<div class="outer-center">
<div class="inner-center" id="menu">
<ul>
<li><a href="../"><span class="category">Home</span></a></li>
{% for query in query %}
<li><a href='/ListArticle/{{ query.key.id() }}'><span class="category">{{ query.name }}</span></a></li>
{% endfor %}
</ul>
</div>
</div>
<div class="clear"></div>
</div>
<!--center the menu bar-->
<div class="banner">
<ul>
<li id="s1"><p>Welcome to<br>Whatsoever<br>Times</p></li>
<li id="s2"><p>The place where you get updated<br>information all around<br>the world.</p></li>
<li id="s3"><p>Have a cup of tea, relax,<br>and see what is going on.</p></li>
<li id="s4"><p>Catch<br>the<br>latest<br>news<br>with<br>us.</p></li>
</ul>
</div>
</header>
<div id="outer">
<div id="leftBar">
<nav>
<h1 id="news">Recent News</h1>
{% for article in article %}
{% if loop.index <= 5 %}
<h2><a href='/DetailPage/{{ article.key.id() }}'>{{ article.title }}</a></h2>
{% endif %}
{% endfor %}
<hr>
<hr>
<h1 class="left">Category List</h1>
{% for query in query %}
<h2 class="catelist"><a href='/ListArticle/{{ query.key.id() }}'>{{ query.name }}</a></h2>
{% endfor %}
<hr>
<hr>
<h1 class="right">External Link</h1>
<h2><a class="link" href="http://mail.google.com"><img src="../images/gmail.png" width="auto" height="40px"/></a></h2>
<h2><a class="link" href="http://google.com"><img src="../images/google.png" width="auto" height="40px"/></a></h2>
<h2><a class="link" href="http://youtube.com"><img src="../images/youtube.png" width="auto" height="40px"/></a></h2>
<hr>
</nav>
</div>
<section>
<div class="sect">
<h4><a href="/">Home</a>&nbsp;&nbsp;&nbsp;<img src="../images/next.png" width="30" height="auto" align="midde"/>&nbsp;&nbsp;&nbsp{{ category.name }}</a></h4>
{% for article in article %}
{% if category.name == article.category.get().name %}
<div class="Cimg img"><a href='/DetailPage/{{ article.key.id() }}'><img src="{{ article.imgUrl }}" width="320px" height="230px"></a></div>
<div class="Cgroup">
<h1 class="Ctitle"><a href='/DetailPage/{{ article.key.id() }}'>{{ article.title }}</a></h1>
<h3>{{ article.publishDate }}</h3>
<h2 class="Cintro">{{ article.intro }}</h2>
</div>
<a href="/ListArticle/{{ category.key.id() }}?cursor={{ next_cur_url }}">NEXT</a>
<hr>
{% endif %}
{% endfor %}
</div>
</section>
<div id="rightBar">
<aside>
<h1 id="about">About<br>Whatsoever</h1>
<p>The Whatsoever Times is a random non-existing daily newspaper, founded just to serve as an assignment for Web Programming Course in RMIT International University. Its website is the marking part.</p>
<hr>
<h1 class="right">Quote of the day</h1>
<blockquote class="blockquote-reverse">
<p>"Education: the path from cocky ignorance to miserable uncertainty."</p>
<footer>Mark Twain</footer>
</blockquote>
<hr>
<h1 class="left">Trivia</h1>
<blockquote class="blockquote-reverse"><p>What is "Lorem ipsum"?</p></blockquote>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<hr>
</aside>
</div>
</div>
</div>
<footer>
<h3>Contact us</h3>
<div id="imglogo">
<a class="logo" href="http://instagram.com/aruzego"><img src="../images/insta.png" width="80px" height="auto"/></a>
<a class="logo" href="https://www.facebook.com/aru.narcissist"><img src="../images/fb.png" width="80px" height="auto"/></a>
</div>
<hr>
<h2>Whatsoever Times is an unreal news, serving as Assignment 2<br>of Web Programming Course - RMIT International University</h2>
</footer>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="../js/unslider.min.js"></script>
<script>
$(function() {
$('.banner').unslider();
});
$('.banner').unslider({
speed: 500,
delay: 3000,
dots: true,
fluid: false
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment