Skip to content

Instantly share code, notes, and snippets.

@Alligator
Created October 20, 2012 20:39
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 Alligator/3924729 to your computer and use it in GitHub Desktop.
Save Alligator/3924729 to your computer and use it in GitHub Desktop.
import webapp2
import jinja2
import os
import random
import pprint
import math
from google.appengine.ext import db
jinja = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
def get_id():
uid = ''
s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for i in range(5):
uid += random.choice(s)
return uid
def lsplit(inp, n):
# split into paragraphs
lines = inp.split('\r\n\r\n')
n = int(math.ceil(len(lines)/float(n)))
return [db.Text('\r\n\r\n'.join(lines[i:i+n])) for i in range(0, len(lines), n)]
class Lyrics(db.Model):
name = db.StringProperty()
uid = db.StringProperty()
cols = db.IntegerProperty()
cols_content = db.ListProperty(db.Text)
private = db.BooleanProperty()
date = db.DateTimeProperty(auto_now_add=True)
class MainHandler(webapp2.RequestHandler):
def get(self, got):
if not got:
recent = Lyrics.gql('WHERE private = false ORDER BY date DESC').fetch(limit=5)
template = jinja.get_template('index.html')
self.response.out.write(template.render({'recent': recent}))
else:
l = Lyrics.gql('WHERE uid = :1', got).get()
if not l:
self.response.out.write('not found')
return
template = jinja.get_template('lyric.html')
prop = {
'lyrics': l
}
self.response.out.write(template.render(prop))
def post(self, got):
l = Lyrics()
l.name = self.request.get('name')
l.cols = int(self.request.get('cols'))
if self.request.get('private') == 'yes':
l.private = True
else:
l.private = False
if l.cols > 1:
l.cols_content = lsplit(self.request.get('content'), l.cols)
else:
l.cols_content = [db.Text(self.request.get('content'))]
uid = get_id()
while Lyrics.gql('WHERE uid = :1', uid).get():
uid = get_id()
l.uid = uid
l.put()
self.redirect('/' + uid)
app = webapp2.WSGIApplication([(r'/(.*)', MainHandler)],
debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment