<filename>app/utils.py
import os
import re
import hmac
import jinja2
import hashlib
import random

from string import letters
from google.appengine.ext import db

template_dir = os.path.join(os.path.dirname(__file__), '../templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
                               autoescape = True)
SECRET = "imsosecret"
### Some utilities functions
def make_salt(length = 5):
    return ''.join(random.choice(letters) for x in xrange(length))

def make_pw_hash(name, pw, salt = None):
    if not salt:
        salt = make_salt()
    h = hashlib.sha256(name + pw + salt).hexdigest()
    return '%s,%s' % (salt, h)

def valid_pw(name, password, h):
    salt = h.split(',')[0]
    return h == make_pw_hash(name, password, salt)

def users_key(group = 'default'):
    return db.Key.from_path('users', group)

USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def valid_username(username):
    return username and USER_RE.match(username)

PASS_RE = re.compile(r"^.{3,20}$")
def valid_password(password):
    return password and PASS_RE.match(password)

EMAIL_RE  = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_email(email):
    return not email or EMAIL_RE.match(email)
    
def hash_str(s):
    return hmac.new(SECRET, s).hexdigest()

def make_secure_val(s):
    return "%s|%s" % (s, hmac.new(SECRET, s).hexdigest())

def check_secure_val(h):
    val = h.split('|')[0]
    if h == make_secure_val(val):
        return val
        
def render_str(template, **params):
    t = jinja_env.get_template(template)
    return t.render(params)
    
def blog_key(name = 'default'):
    return db.Key.from_path('blogs', name)
    
def render_post(response, post):
    response.out.write('<b>' + post.subject + '</b><br>')
    response.out.write(post.content)
    
def get_post_by_id(post_id):
    key = db.Key.from_path('Post', int(post_id), parent=blog_key())
    post = db.get(key)
    return post

def get_comment_by_post_id(post_id):
    comments = db.GqlQuery("select * from Comment where post_id = " + post_id)
    return comments

def get_like_by_userid(post_id, userid):
    like = db.GqlQuery("select * from Like where post_id = " + post_id + " and author = " 
                              + userid
                              ).get()
    return like

def get_count_of_like(post_id):
    c_like = db.GqlQuery("select * from Like where post_id = " + post_id)
    return c_like