forked from dengmin/logpress-tornado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
81 lines (69 loc) · 2.21 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
#coding=utf8
try:
import psyco
psyco.full()
except:pass
import tornado
import tornado.web
from tornado.httpserver import HTTPServer
from tornado.options import define,options
from tornado.web import url
import sys
from lib import filters,session
from core import jinja_environment,smtp_server
from core import settings
from core import redis_server
define("cmd", default='runserver', metavar="runserver|createuser")
define("port",default=9000,type=int)
define("autoreload",default=False,type=bool)
class Application(tornado.web.Application):
def __init__(self):
from urls import routes as handlers
#init jiaja2 environment
self.jinja_env = jinja_environment
#register filters for jinja2
self.jinja_env.filters.update(filters.register_filters())
self.jinja_env.tests.update({})
self.jinja_env.globals['settings'] = settings
tornado.web.Application.__init__(self,handlers,**settings)
self.session_store = session.RedisSessionStore(redis_server)
self.email_backend = smtp_server
def runserver():
http_server = HTTPServer(Application(),xheaders=True)
http_server.listen(options.port)
loop = tornado.ioloop.IOLoop.instance()
print 'Server running on http://0.0.0.0:%d'%(options.port)
loop.start()
def createuser():
username = raw_input('input username: ')
if username:
from models import User
q = User.select().where(User.username==username.strip())
if q.count()>0:
print 'username [ %s ] exists! please choice another one and try it again!'%(username)
sys.exit(0)
email = raw_input('input your Email: ')
password = raw_input('input password: ')
User.create(username=username,email=email.strip(),password=User.create_password(password))
print '%s created!'%(username)
else:
print 'username is null,exit!'
sys.exit(0)
def syncdb():
from lib.helpers import find_subclasses
from models import db
models = find_subclasses(db.Model)
for model in models:
if model.table_exists():
model.drop_table()
model.create_table()
print 'created table:',model._meta.db_table
if __name__ == '__main__':
tornado.options.parse_command_line()
if options.cmd == 'runserver':
runserver()
elif options.cmd == 'createuser':
createuser()
elif options.cmd == 'syncdb':
syncdb()