def insert(self,table,fields):
dfields=dict((f.name,self.represent(v,f.type)) for f,v in fields)
# table._db['_lastsql'] = self._insert(table,fields)
# Field name 'gae_key_name' can be used insert using key_name for both DB and NDB.
keyname = None
if 'gae_key_name' in dfields:
keyname = dfields['gae_key_name']
if self.use_ndb:
dfields['id'] = dfields.pop('gae_key_name')
else:
dfields['key_name'] = dfields.pop('gae_key_name')
tmp = table._tableobj(**dfields)
tmp.put()
key = tmp.key if self.use_ndb else tmp.key()
rid = Reference(key.id())
(rid._table, rid._record, rid._gaekey) = (table, None, key)
return rid
def _listify(self,fields,update=False):
new_fields = {} # format: new_fields[name] = (field,value)
# store all fields passed as input in new_fields
# 'gae_key_name' is GAE specific and should not be defined in the table.
for name in fields:
if not name in self.fields:
if name not in ['id', 'gae_key_name']:
raise SyntaxError(
'Field %s does not belong to the table' % name)
if name == 'gae_key_name':
# Create stub Field for 'gae_key_name' so it can be included
# without being defined in the model.
field = Field('gae_key_name', 'string')
new_fields['gae_key_name'] = field, fields['gae_key_name']
else:
field = self[name]
value = fields[name]
if field.filter_in:
value = field.filter_in(value)
new_fields[name] = (field,value)
I would like to be able to do this using web2py. I could also choose to use the GAE API directly to do this but then a could not make use thing like calculated Fields etc in my web2py tables.
dfields=dict((f.name,self.represent(v,f.type)) for f,v in fields)
# table._db['_lastsql'] = self._insert(table,fields)
#quint
keyname = None
if 'gae_key_name' in dfields:
keyname = dfields['gae_key_name']
if self.use_ndb:
dfields['id'] = dfields.pop('gae_key_name')
else:
dfields['key_name'] = dfields.pop('gae_key_name')
tmp = table._tableobj(**dfields)
tmp.put()
if keyname:
return keyname
key = tmp.key if self.use_ndb else tmp.key()
rid = Reference(key.id() if self.use_ndb else key.id_or_name())
(rid._table, rid._record, rid._gaekey) = (table, None, key)
return rid
although I belive storing records without id assignment would be restrictive for apps
(and I doubt it will be actually compatible at all
Compatible with what?
But you would know that you don't need those functionalities if you would decide to use a key_name
How about adding support in dal.py for the following:
# processes the field input and add defaults, computes, etc. (does not make actual insertion).
>>> values = db._insert(spam="alot", ...)
{"spam": "alot", ...}
On Thursday, January 9, 2014 7:21:45 PM UTC+1, Quint wrote:
Excellent!
I already included it to try it out but how do i use it?
(I posted something stupid and removed the stupid post but left some traces. ;-))
Sorry for the confussion..
But it works fine. I will definitally use this.(I posted something stupid and removed the stupid post but left some traces. ;-))
def _pre_process(self, table, **fields):
"""
Takes a w2p table and a dictionary with values and processes
the field input and add defaults, computes, etc using the web2py table.
"""
fields = table._defaults(fields)
return dict((f.name,table._db._adapter.represent(v,f.type)) for f,v in table._listify(fields))
Sorry; however, you could temporarily implement a similar method by subclassing GoogleDatastoreAdapter so it pre-processes data without applying changes.
I gues my question is, can users safely rely on _<something> methods?
What is w2p's "policy" regarding underscore-prefixed functions.Are they considered private?
Functions that take arguments or start with a double underscore are not publicly exposed and can only be called by other functions.
So can I use the above function as it is?