Exemplo n.º 1
0
    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
Exemplo n.º 2
0
	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
Exemplo n.º 3
0
def update_password(**args):
    """update user password"""
    userobj = model.get(dict(type="user", name="any"))
    db.sql("""update user set password=%s, reset_password_id=NULL
		where reset_password_id=%s""", \
     (userobj.encrypt_password(args['password']), args['requestid']))

    return 'ok'
Exemplo n.º 4
0
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'
Exemplo n.º 5
0
def update_password(**args):
	"""update user password"""
	userobj = model.get(dict(type="user", name="any"))
	db.sql("""update user set password=%s, reset_password_id=NULL
		where reset_password_id=%s""", \
		(userobj.encrypt_password(args['password']), args['requestid']))

	return 'ok'
	
Exemplo n.º 6
0
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'
Exemplo n.º 7
0
def get(**args):
	"""get an object"""	
	obj = _get_obj(type=args['type'], name=args['name'])
	if not obj:
		return {}
		
	# check permissions
	modelobj = model.get(obj) or None
	if modelobj:
		modelobj.check_allow('get')
		modelobj.execute('before_get')

	return obj
Exemplo n.º 8
0
def delete(**args):
	"""delete object and its children, if permitted"""
	from lib.chai import model
	
	obj = _get_obj(args['type'], args['name'])
	if not obj:
		return {"message":"ok", "info":"object did not exist"}
	modelobj = model.get(obj)
	modelobj.check_allow('delete')
	hasattr(modelobj, 'before_delete') and modelobj.before_delete()
	
	delete_obj(args['type'], args['name'])
	return {"message":"ok"}
Exemplo n.º 9
0
def get(**args):
    """get an object"""
    obj = _get_obj(type=args['type'], name=args['name'])
    if not obj:
        return {}

    # check permissions
    modelobj = model.get(obj) or None
    if modelobj:
        modelobj.check_allow('get')
        modelobj.execute('before_get')

    return obj
Exemplo n.º 10
0
def delete(**args):
    """delete object and its children, if permitted"""
    from lib.chai import model

    obj = _get_obj(args['type'], args['name'])
    if not obj:
        return {"message": "ok", "info": "object did not exist"}
    modelobj = model.get(obj)
    modelobj.check_allow('delete')
    hasattr(modelobj, 'before_delete') and modelobj.before_delete()

    delete_obj(args['type'], args['name'])
    return {"message": "ok"}
Exemplo n.º 11
0
def post(args, action):
	"""Post a vector object, the property name is the type. See test case for example"""

	obj = get_obj_from_args(args)
	if action=='update':
		delete_children(obj['type'], obj['name'])

	modelobj = (not obj.get('parent_type')) and model.get(obj) or None
	execute_before_post(modelobj, action)

	obj_single, is_vector = get_single(obj)
	
	# save the parent
	post_single(obj_single, action)

	if is_vector:
		post_children(obj)
	
	execute_after_post(modelobj, action)
		
	return {"message":"ok", "obj": obj}
Exemplo n.º 12
0
def post(args, action):
    """Post a vector object, the property name is the type. See test case for example"""

    obj = get_obj_from_args(args)
    if action == 'update':
        delete_children(obj['type'], obj['name'])

    modelobj = (not obj.get('parent_type')) and model.get(obj) or None
    execute_before_post(modelobj, action)

    obj_single, is_vector = get_single(obj)

    # save the parent
    post_single(obj_single, action)

    if is_vector:
        post_children(obj)

    execute_after_post(modelobj, action)

    return {"message": "ok", "obj": obj}