# Path: metamodel.py
# def createLocalSession():
# 	request._session = scoped_session(sessionmaker())
# 	request._session.configure(bind=engine)
# 
# def closeLocalSession():
# 	request._session.remove()
# 
# Path: model.py
# class User(object):
# 	enabled = Boolean
# 	admin = Boolean
# 	username = Unicode(255)
# 	password = String(88)
# 	gold = Integer
# 	email = String
# 	email_verified = Boolean
# 	email_verification = String
# 	email_notifications = Boolean
# 	phone_number = String
# 	phone_verified = Boolean
# 	phone_verification_code = Integer
# 	phone_verification_tries = Integer
# 	phone_notifications = Boolean
# 	feedback_score = Integer
# 	feedback_positive = Integer
# 	feedback_negative = Integer
# 
# 	characters = Character.relation(backref='user')
# 	news = News.relation(backref='creator')
# 	jobs = Job.relation(backref='user')
# 	gold_history = GoldHistory.relation(backref='user')
# 	feedbacks = Feedback.relation(backref='profile')
# 
# 
# 	@staticmethod
# 	def hash(password):
# 		salt = ''.join('%02x' % ord(c) for c in os.urandom(24))
# 		for i in range(1000):
# 			password = hashlib.sha1(salt + password + salt).hexdigest()
# 		return salt+password
# 
# 	@staticmethod
# 	def checkHash(hash, password):
# 		salt, hash = hash[:48], hash[48:]
# 		for i in range(1000):
# 			password = hashlib.sha1(salt + password + salt).hexdigest()
# 		if password == hash:
# 			return True
# 		return False
# 
# 	@staticmethod
# 	def add(username, password, admin):
# 		if User.one(enabled=True, username=username):
# 			return None
# 		with transact:
# 			return User.create(
# 				enabled=True, 
# 				username=username, 
# 				password=User.hash(password), 
# 				admin=admin, 
# 				gold=0, 
# 				phone_number='', 
# 				phone_verified=False, 
# 				phone_notifications=True, 
# 				email='', 
# 				email_verified=False,
# 				email_notifications=True, 
# 				feedback_score = 0,
# 				feedback_positive = 0,
# 				feedback_negative = 0
# 			)
# 	
# 	@staticmethod
# 	def find(username, password):
# 		if username == None or password == None:
# 			return None
# 		user = User.one(enabled=True, username=username)
# 		if user and User.checkHash(user.password, password):
# 			return user
# 		if not user and len(User.all()) == 0:
# 			return User.add(username, password, True)
# 		return None
# 	
# 	def change(self, email=None):
# 		if email != None and email != self.email:
# 			with transact:
# 				self.update(
# 						email=email, 
# 						email_verified=False
# 					)
# 			self.generateEmailVerification()
# 
# 	def generateEmailVerification(self):
# 		from handler import email
# 		code = ''.join('%02X' % random.randrange(256) for i in range(20))
# 		with transact:
# 			self.update(email_verification=code)
# 		email(self.email, 'verify', code=code)
# 
# 	def addGold(self, amount, price):
# 		with transact:
# 			self.update(gold=self.gold+amount)
# 			GoldHistory.create(
# 					user=self, 
# 					date=datetime.now(), 
# 					amount=amount, 
# 					balance=self.gold, 
# 					dollars=price, 
# 					job=None, 
# 					desc=u'Bought %i gold for $%.2f' % (amount, price / 100.0)
# 				)
# 
# 	def sms(self, message):
# 		if not self.phone_verified:
# 			return False
# 
# 		sms(self.phone_number, message)
# 		return True


import os
import smtplib
from json import dumps
from flask import abort, render_template, request, session
from flask import redirect as _redirect
from werkzeug.exceptions import HTTPException
from metamodel import createLocalSession, closeLocalSession
from model import User
from urllib import quote, urlencode
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from jinja2 import Environment, FileSystemLoader
		json = _json
		if name.startswith('get_'):
			name = name[4:]
			method = 'GET'
		elif name.startswith('post_'):
			method = 'POST'
		elif name.startswith('rpc_'):
			method = 'POST'
			rpc = json = True
			tpl = None
		else:
			raise Exception('All handlers must be marked get_ or post_.')

		module = func.__module__.split('.')[-1]
		if not module in all:
			all[module] = DictObject()
			setattr(handler, module, all[module])
		args = func.__code__.co_varnames[:func.__code__.co_argcount]
		hasId = len(args) > 0 and args[0] == 'id' and not rpc

		ofunc = func
		def func(id=None):
			createLocalSession()
			if 'csrf' not in session:
				token = os.urandom(16)
				session['csrf'] = ''.join('%02x' % ord(c) for c in token)
			if method == 'POST' and \
				('csrf' not in request.form or request.form['csrf'] != session['csrf']):
				abort(403)
			if 'userId' in session and session['userId']:

# Path: metamodel.py
# def createLocalSession():
# 	request._session = scoped_session(sessionmaker())
# 	request._session.configure(bind=engine)
# 
# def closeLocalSession():
# 	request._session.remove()
# 
# Path: model.py
# class User(object):
# 	enabled = Boolean
# 	admin = Boolean
# 	username = Unicode(255)
# 	password = String(88)
# 	gold = Integer
# 	email = String
# 	email_verified = Boolean
# 	email_verification = String
# 	email_notifications = Boolean
# 	phone_number = String
# 	phone_verified = Boolean
# 	phone_verification_code = Integer
# 	phone_verification_tries = Integer
# 	phone_notifications = Boolean
# 	feedback_score = Integer
# 	feedback_positive = Integer
# 	feedback_negative = Integer
# 
# 	characters = Character.relation(backref='user')
# 	news = News.relation(backref='creator')
# 	jobs = Job.relation(backref='user')
# 	gold_history = GoldHistory.relation(backref='user')
# 	feedbacks = Feedback.relation(backref='profile')
# 
# 
# 	@staticmethod
# 	def hash(password):
# 		salt = ''.join('%02x' % ord(c) for c in os.urandom(24))
# 		for i in range(1000):
# 			password = hashlib.sha1(salt + password + salt).hexdigest()
# 		return salt+password
# 
# 	@staticmethod
# 	def checkHash(hash, password):
# 		salt, hash = hash[:48], hash[48:]
# 		for i in range(1000):
# 			password = hashlib.sha1(salt + password + salt).hexdigest()
# 		if password == hash:
# 			return True
# 		return False
# 
# 	@staticmethod
# 	def add(username, password, admin):
# 		if User.one(enabled=True, username=username):
# 			return None
# 		with transact:
# 			return User.create(
# 				enabled=True, 
# 				username=username, 
# 				password=User.hash(password), 
# 				admin=admin, 
# 				gold=0, 
# 				phone_number='', 
# 				phone_verified=False, 
# 				phone_notifications=True, 
# 				email='', 
# 				email_verified=False,
# 				email_notifications=True, 
# 				feedback_score = 0,
# 				feedback_positive = 0,
# 				feedback_negative = 0
# 			)
# 	
# 	@staticmethod
# 	def find(username, password):
# 		if username == None or password == None:
# 			return None
# 		user = User.one(enabled=True, username=username)
# 		if user and User.checkHash(user.password, password):
# 			return user
# 		if not user and len(User.all()) == 0:
# 			return User.add(username, password, True)
# 		return None
# 	
# 	def change(self, email=None):
# 		if email != None and email != self.email:
# 			with transact:
# 				self.update(
# 						email=email, 
# 						email_verified=False
# 					)
# 			self.generateEmailVerification()
# 
# 	def generateEmailVerification(self):
# 		from handler import email
# 		code = ''.join('%02X' % random.randrange(256) for i in range(20))
# 		with transact:
# 			self.update(email_verification=code)
# 		email(self.email, 'verify', code=code)
# 
# 	def addGold(self, amount, price):
# 		with transact:
# 			self.update(gold=self.gold+amount)
# 			GoldHistory.create(
# 					user=self, 
# 					date=datetime.now(), 
# 					amount=amount, 
# 					balance=self.gold, 
# 					dollars=price, 
# 					job=None, 
# 					desc=u'Bought %i gold for $%.2f' % (amount, price / 100.0)
# 				)
# 
# 	def sms(self, message):
# 		if not self.phone_verified:
# 			return False
# 
# 		sms(self.phone_number, message)
# 		return True


# Path: handler.py
import os
import smtplib
from json import dumps
from flask import abort, render_template, request, session
from flask import redirect as _redirect
from werkzeug.exceptions import HTTPException
from metamodel import createLocalSession, closeLocalSession
from model import User
from urllib import quote, urlencode
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from jinja2 import Environment, FileSystemLoader

		json = _json
		if name.startswith('get_'):
			name = name[4:]
			method = 'GET'
		elif name.startswith('post_'):
			method = 'POST'
		elif name.startswith('rpc_'):
			method = 'POST'
			rpc = json = True
			tpl = None
		else:
			raise Exception('All handlers must be marked get_ or post_.')

		module = func.__module__.split('.')[-1]
		if not module in all:
			all[module] = DictObject()
			setattr(handler, module, all[module])
		args = func.__code__.co_varnames[:func.__code__.co_argcount]
		hasId = len(args) > 0 and args[0] == 'id' and not rpc

		ofunc = func
		def func(id=None):
			createLocalSession()
			if 'csrf' not in session:
				token = os.urandom(16)
				session['csrf'] = ''.join('%02x' % ord(c) for c in token)
			if method == 'POST' and \
				('csrf' not in request.form or request.form['csrf'] != session['csrf']):
				abort(403)
			if 'userId' in session and session['userId']:

				session.user = User.one(id=int(session['userId']))