def load(**args): """load an existing sesion from cookies or start a new guest session""" from lib.chai import req, res if 'sid' in req.cookies and req.cookies['sid'] != 'guest': sess = objstore.get(type='session', name=req.cookies['sid']) if sess: sess['userobj'] = objstore.get(type='user', name=sess['user']) return sess # no session, start a new one as guest (no need to save) sess = dict(type='session', name='guest', user='******') return sess
def load(**args): """load an existing sesion from cookies or start a new guest session""" from lib.chai import req, res if 'sid' in req.cookies and req.cookies['sid']!='guest': sess = objstore.get(type='session', name=req.cookies['sid']) if sess: sess['userobj'] = objstore.get(type='user', name=sess['user']) return sess # no session, start a new one as guest (no need to save) sess = dict(type='session', name='guest', user='******') return sess
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_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 forgot_password(**args): """email the user with a unique link to generate the password""" user = db.sql("""select name from user where name=%s or email=%s""", (args['email'], args['email'])) if not user: return 'No such user' userobj = model.get(objstore.get(type="user", name=user[0]['name'])) userobj.request_reset_password() return 'ok'
def content(**args): """get page html with header and footer""" obj = objstore.get(type='page', name=args.get('name', 'index')) if not 'html' in obj: obj['html'] = """<h1>Not Found</h1><p>This link is not active. <a href="#index">Go back to Home Page</a></p>""" return {'html': header(obj) + obj["html"] + footer(obj), 'js': obj.get('js', ''), 'css':obj.get('css', '')}
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_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_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 content(**args): """get page html with header and footer""" obj = objstore.get(type='page', name=args.get('name', 'index')) if not 'html' in obj: obj['html'] = """<h1>Not Found</h1><p>This link is not active. <a href="#index">Go back to Home Page</a></p>""" return { 'html': header(obj) + obj["html"] + footer(obj), 'js': obj.get('js', ''), 'css': obj.get('css', '') }
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 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_update(self): self.test_insert() objstore.update(type="user", name="testuser", email="testmail2") objstore.get(type="user", name="testuser")['email'] == 'testmail2'
def test_update(self): self.test_insert() objstore.update(type="user", name="testuser", email="testmail2") objstore.get(type="user", name="testuser")['email']=='testmail2'