def local_query_create(self, query): assert not query.get_where( ), "Filters should be empty for a create request" #assert not query.get_select(), "Fields should be empty for a create request" cls = self.map_object[query.get_from()] params = query.get_params() # We encrypt the password according to the encryption of adduser.py # As a result from the frontend the new users' password will be inserted # into the local DB as encrypted if 'password' in params: params['password'] = self.encrypt_password(params['password']) _params = cls.process_params(query.get_params(), None, self.user) new_obj = cls() #from sqlalchemy.orm.attributes import manager_of_class #mgr = manager_of_class(cls) #instance = mgr.new_instance() if params: for k, v in params.items(): setattr(new_obj, k, v) db.add(new_obj) try: db.commit() except: db.rollback() return [new_obj]
def local_query_update(self, query): # XXX The filters that are accepted in config depend on the gateway # Real fields : user_credential # Convenience fields : credential, then redispatched to real fields # same for get, update, etc. # XXX What about filters on such fields # filter works with account and platform table only if query.object == 'account' or query.object == 'platform': if not query.filters.has_eq( 'platform_id') and not query.filters.has_eq('platform'): raise Exception, "Cannot update JSON fields on multiple platforms" cls = self.map_object[query.object] # Note: we can request several values # FIELDS: exclude them _fields = xgetattr(cls, query.fields) # FILTERS: Note we cannot filter on json fields _filters = cls.process_filters(query.filters) _filters = get_sqla_filters(cls, _filters) # PARAMS # # The fields we can update in params are either: # - the original fields, including json encoded ones # - fields inside the json encoded ones # - convenience fields # We refer to the model for transforming the params structure into the # final one # Password update # # if there is password update in query.params # We encrypt the password according to the encryption of adduser.py # As a result from the frontend the edited password will be inserted # into the local DB as encrypted if 'password' in query.params: query.params['password'] = self.encrypt_password( query.params['password']) _params = cls.process_params(query.params, _filters, self.user) # only 2.7+ _params = { getattr(cls, k): v for k,v in query.params.items() } _params = dict([(getattr(cls, k), v) for k, v in _params.items()]) #db.query(cls).update(_params, synchronize_session=False) q = db.query(cls) for _filter in _filters: q = q.filter(_filter) if self.user and cls.restrict_to_self and self.user[ 'email'] != ADMIN_USER: q = q.filter(getattr(cls, 'user_id') == self.user['user_id']) q = q.update(_params, synchronize_session=False) try: db.commit() except: db.rollback() return []
def manage(self): """ Ensure that the config has all the necessary fields """ assert self.auth_type == 'managed' # Finds the gateway corresponding to the platform gtype = self.platform.gateway_type if not gtype: print "I: Undefined gateway" return {} gw = getattr( __import__('manifold.gateways', globals(), locals(), gtype), gtype) print "I: Calling manage on the platform" config = json.dumps( gw.manage(self.user, self.platform, json.loads(self.config))) if self.config != config: self.config = config db.commit()
def set_config(self, value): self.config = json.dumps(value) db.add(self) db.commit()
def config_set(self, value): self.config = json.dumps(value) db.add(self) db.commit()