def sync_table(self, table=None): """make / update tables from models""" from lib.chai import model, objstore self.sql("set foreign_key_checks=0") tables = self.table_list() m = model.get({'type':table}) if not m._name in tables: self.sql(m._create_table) print "Created %s" % m._name else: self.repair_table(m._name, m._create_table) print "Repaired %s" % m._name # update parent-child map if hasattr(m, '_parent'): self.begin() try: objstore.insert(type="_parent_child", parent=m._parent, child=m._name) self.commit() except Exception, e: self.rollback() if e.args[0]!=1062: # updating raise e
def sync_table(self, table=None): """make / update tables from models""" from lib.chai import model, objstore self.sql("set foreign_key_checks=0") tables = self.table_list() m = model.get({'type': table}) if not m._name in tables: self.sql(m._create_table) print "Created %s" % m._name else: self.repair_table(m._name, m._create_table) print "Repaired %s" % m._name # update parent-child map if hasattr(m, '_parent'): self.begin() try: objstore.insert(type="_parent_child", parent=m._parent, child=m._name) self.commit() except Exception, e: self.rollback() if e.args[0] != 1062: # updating raise e
def test_update_with_children(self): objstore.insert(type="user", name="testuser", userrole=['Admin', 'Manager']) objstore.update(type="user", name="testuser", userrole=['Admin', 'MX']) obj = objstore.get(type="user", name="testuser") self.assertTrue('Admin' in obj['userrole']) self.assertFalse('Manager' in obj['userrole']) self.assertTrue('MX' in obj['userrole'])
def test_get(self): obj = dict(type="user", name="testuser", email="testmail", fullname="Test User") objstore.insert(**obj) obj1 = objstore.get(type="user", name="testuser") # clear nulls del obj1['_updated'] for key in obj1.keys(): if not obj1[key]: del obj1[key] self.assertEquals(obj, obj1)
def test_vector(self): obj = dict(type="user", name="testuser", email="testmail", fullname="Test User") objstore.insert(type="user", name="testuser", userrole=['Admin', 'Manager']) obj1 = objstore.get(type="user", name="testuser") self.assertTrue('Admin' in obj1['userrole']) self.assertTrue('Manager' in obj1['userrole'])
def test_bad_login(self): from webob import Request, Response import lib.chai objstore.insert(type='user', name='test',password='******') lib.chai.req = Request.blank('server/?_method=lib.chai.session.login&user=test&password=111') lib.chai.res = Response() # login ret = session.login(**lib.chai.req.params) self.assertTrue(ret['error']=='Invalid Login') self.assertTrue('message' not in ret)
def main(): """call action based on options""" import os, sys, conf (options, args) = getparser().parse_args() import lib.chai lib.chai.site = options.site or conf.default_site from lib.chai import db if options.newapp is not None: newapp() elif options.setup is not None: setup_db() elif options.update is not None: if options.update == 'all': sync_tables() else: from lib.chai import db db.get().sync_table(options.update) elif options.publish is not None: publish() elif options.adduser: from lib.chai import db, objstore db.begin() objstore.insert(type="user", name=options.adduser[0], password=options.adduser[1]) db.commit() elif options.uwsgi is not None: from lib.chai.util import uwsgi_manager m = uwsgi_manager.manager('conf/uwsgi.xml') getattr(m, options.uwsgi)(1) elif options.replace is not None: from lib.chai.util import replacer # in code replacer.replace(conf.sites[lib.chai.site]['path'], options.replace[0], \ options.replace[1], options.replace[2]) # replace in framework replacer.replace('lib', options.replace[0], options.replace[1], options.replace[2]) elif options.concat is not None: concat() db.close()
def test_bad_login(self): from webob import Request, Response import lib.chai objstore.insert(type='user', name='test', password='******') lib.chai.req = Request.blank( 'server/?_method=lib.chai.session.login&user=test&password=111') lib.chai.res = Response() # login ret = session.login(**lib.chai.req.params) self.assertTrue(ret['error'] == 'Invalid Login') self.assertTrue('message' not in ret)
def main(): """call action based on options""" import os, sys, conf (options, args) = getparser().parse_args() import lib.chai lib.chai.site = options.site or conf.default_site from lib.chai import db if options.newapp is not None: newapp() elif options.setup is not None: setup_db() elif options.update is not None: if options.update=='all': sync_tables() else: from lib.chai import db db.get().sync_table(options.update) elif options.publish is not None: publish() elif options.adduser: from lib.chai import db, objstore db.begin() objstore.insert(type="user", name=options.adduser[0], password=options.adduser[1]) db.commit() elif options.uwsgi is not None: from lib.chai.util import uwsgi_manager m = uwsgi_manager.manager('conf/uwsgi.xml') getattr(m, options.uwsgi)(1) elif options.replace is not None: from lib.chai.util import replacer # in code replacer.replace(conf.sites[lib.chai.site]['path'], options.replace[0], \ options.replace[1], options.replace[2]) # replace in framework replacer.replace('lib', options.replace[0], options.replace[1], options.replace[2]) elif options.concat is not None: concat() db.close()
def test_login(self): # make a user from webob import Request, Response import lib.chai objstore.insert(type='user', name='test',password='******') lib.chai.req = Request.blank('server/?_method=lib.chai.session.login&user=test&password=pass') lib.chai.res = Response() # login ret = session.login(**lib.chai.req.params) self.sid = lib.chai.res.headers.get('Set-Cookie').split(';')[0].split('=')[1] self.assertTrue(ret['userobj']['name']=='test') self.assertTrue(ret['message']=='ok') self.assertTrue(db.sql("""select * from session where user='******'""")[0]['user']=='test')
def login(**args): """login""" from lib.chai import res import hashlib pwd = db.sql("select password from user where name=%s", (args['user'],)) if pwd: pwd = pwd[0]['password'] if pwd == hashlib.sha256(args.get("password")).hexdigest(): d = dict(type='session', name=new_sid(), user=args['user']) objstore.insert(**d) res.set_cookie('sid', d['name']) d.update({"message":"ok", "userobj":objstore.get(type='user', name=args['user'])}) return d else: return {"error":"Invalid Login"}
def create_index(): """create index page""" from lib.chai import objstore, db content = ''' <h1>[Default Index]</h1> <p>To edit this page:</p> <ol> <li>Login <li>Admin -> Edit </ol> ''' db.begin() # index page objstore.insert(type="page", html=content, name="index", label=conf.app_name) db.commit() print "index created"
def test_new(self): self.test_insert() objstore.insert(type='page', name='p11', parent='p1') rebuild('page') r1 = objstore.get(type='page', name='r1') p1 = objstore.get(type='page', name='p1') p2 = objstore.get(type='page', name='p2') p11 = objstore.get(type='page', name='p11') self.assertEquals(r1['lft'], 1) self.assertEquals(p1['lft'], 2) self.assertEquals(p11['lft'], 3) self.assertEquals(p11['rgt'], 4) self.assertEquals(p1['rgt'], 5) self.assertEquals(p2['lft'], 6) self.assertEquals(p2['rgt'], 7) self.assertEquals(r1['rgt'], 8)
def test_login(self): # make a user from webob import Request, Response import lib.chai objstore.insert(type='user', name='test', password='******') lib.chai.req = Request.blank( 'server/?_method=lib.chai.session.login&user=test&password=pass') lib.chai.res = Response() # login ret = session.login(**lib.chai.req.params) self.sid = lib.chai.res.headers.get('Set-Cookie').split(';')[0].split( '=')[1] self.assertTrue(ret['userobj']['name'] == 'test') self.assertTrue(ret['message'] == 'ok') self.assertTrue( db.sql("""select * from session where user='******'""")[0]['user'] == 'test')
def login(**args): """login""" from lib.chai import res import hashlib pwd = db.sql("select password from user where name=%s", (args['user'], )) if pwd: pwd = pwd[0]['password'] if pwd == hashlib.sha256(args.get("password")).hexdigest(): d = dict(type='session', name=new_sid(), user=args['user']) objstore.insert(**d) res.set_cookie('sid', d['name']) d.update({ "message": "ok", "userobj": objstore.get(type='user', name=args['user']) }) return d else: return {"error": "Invalid Login"}
def test_insert(self): objstore.insert(type='page', name='r1') objstore.insert(type='page', parent='r1', name='p1', idx=0) objstore.insert(type='page', parent='r1', name='p2', idx=1) rebuild('page') r1 = objstore.get(type='page', name='r1') p1 = objstore.get(type='page', name='p1') p2 = objstore.get(type='page', name='p2') self.assertEquals(r1['lft'], 1) self.assertEquals(p1['lft'], 2) self.assertEquals(p1['rgt'], 3) self.assertEquals(p2['lft'], 4) self.assertEquals(p2['rgt'], 5) self.assertEquals(r1['rgt'], 6)
def make(): """write pages/toc.html""" index = db.sql("select name, label from page where name='index'")[0] objstore.delete_obj('page', 'toc') objstore.insert(type='page', name='toc', label='Contents', html=template(nodehtml(index)))
def test_insert(self): objstore.insert(type="user", name="testuser", email="testmail") obj = db.sql("""select * from user where name='testuser'""")[0] self.assertTrue(obj['email'] == 'testmail')