# Path: model.py
# class Config(object):
# 	name = String(20)
# 	value = Unicode(255)
# 
# 	@staticmethod
# 	def get(name):
# 		if name == 'secret_key':
# 			return '1a2dd09e9b7aec16069690d56bf0ce19567a92b442a0e279'
# 		try:
# 			return Config.one(name=name).value
# 		except:
# 			return None
# 
# 	@staticmethod
# 	def getString(name):
# 		data = Config.get(name)
# 		if data == None:
# 			return None
# 		else:
# 			return str(data)
# 
# 	@staticmethod
# 	def set(name, value):
# 		with transact:
# 			try:
# 				row = Config.one(name=name)
# 				row.update(value=unicode(value))
# 			except:
# 				Config.create(
# 					name=name,
# 					value=unicode(value)
# 				)
# 
# 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 coffeescript, os
import handler
import handlers
			import traceback
		import traceback
from flask import Flask, request
from handlers import *
from model import Config, User

app = Flask(__name__)
app.debug = True
key = Config.getString('secret_key')
if key == None:
	key = os.urandom(24)

# Path: model.py
# class Config(object):
# 	name = String(20)
# 	value = Unicode(255)
# 
# 	@staticmethod
# 	def get(name):
# 		if name == 'secret_key':
# 			return '1a2dd09e9b7aec16069690d56bf0ce19567a92b442a0e279'
# 		try:
# 			return Config.one(name=name).value
# 		except:
# 			return None
# 
# 	@staticmethod
# 	def getString(name):
# 		data = Config.get(name)
# 		if data == None:
# 			return None
# 		else:
# 			return str(data)
# 
# 	@staticmethod
# 	def set(name, value):
# 		with transact:
# 			try:
# 				row = Config.one(name=name)
# 				row.update(value=unicode(value))
# 			except:
# 				Config.create(
# 					name=name,
# 					value=unicode(value)
# 				)
# 
# 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: serve.py
import coffeescript, os
import handler
import handlers
			import traceback
		import traceback
from flask import Flask, request
from handlers import *
from model import Config, User


app = Flask(__name__)
app.debug = True
key = Config.getString('secret_key')
if key == None:
	key = os.urandom(24)

	Config.set('secret_key', ''.join('%02x' % ord(c) for c in key))