def testRollback(self): web.transact() web.insert('person', False, name='user1') web.insert('person', False, name='user2') web.insert('person', False, name='user3') web.rollback() self.assertRows(0)
def approve(self, url, user_id, path, revision): site_id = core.db.get_site_id(url) page_id = core.db.get_page_id(url, path) # @@ is there any better way? web.transact() try: web.delete( "review", where= "site_id=$site_id AND page_id=$page_id AND user_id=$user_id", vars=locals(), ) web.insert( "review", site_id=site_id, page_id=page_id, user_id=user_id, revision=revision, ) except: web.rollback() raise else: web.commit()
def select(query, chunk_size=50000): """Selects large number of rows efficiently using cursors.""" web.transact() web.query('DECLARE select_cursor CURSOR FOR ' + query) while True: result = web.query('FETCH FORWARD $chunk_size FROM select_cursor', vars=locals()) if not result: break for r in result: yield r web.rollback()
def remove(self, id_): '''removes session''' web.transact() try: web.delete(web.config.handler_parameters.db_table, where='id = $id', vars={'id': id_}) web.commit() except Exception, inst: web.rollback() raise inst
def upload(self, query): """Inserts""" assert isinstance(query, list) web.transact() try: self.process_creates(query) self.process_inserts(query) except: web.rollback() raise else: web.commit()
def clean(self, timeout): '''removes all expired sessions''' web.transact() try: web.delete(web.config.handler_parameters.db_table, where='($now - touched) >= $timeout', vars={'timeout': timeout, 'now': int(time.time())}) web.commit() except Exception, inst: web.rollback() raise inst
def approve(self, url, user_id, path, revision): site_id = core.db.get_site_id(url) page_id = core.db.get_page_id(url, path) #@@ is there any better way? web.transact() try: web.delete('review', where="site_id=$site_id AND page_id=$page_id AND user_id=$user_id", vars=locals()) web.insert('review', site_id=site_id, page_id=page_id, user_id=user_id, revision=revision) except: web.rollback() raise else: web.commit()
def testNestedTransactions(self): web.transact() web.insert('person', False, name='user1') self.assertRows(1) web.transact() web.insert('person', False, name='user2') self.assertRows(2) web.rollback() self.assertRows(1) web.transact() web.insert('person', False, name='user3') web.commit() self.assertRows(2) web.commit()
def get_key2id(): """Return key to id mapping for all things in the database.""" key2id = {} offset = 0 limit = 100000 web.transact() # declare a cursor to read all the keys web.query("DECLARE key2id CURSOR FOR SELECT id, key FROM thing") while True: result = web.query('FETCH FORWARD $limit FROM key2id', vars=locals()) if not result: break for row in result: key2id[row.key] = row.id web.query("CLOSE key2id") web.rollback() return key2id
def get_key2id(): """Return key to id mapping for all things in the database.""" key2id = {} offset = 0 limit = 100000 web.transact() # declare a cursor to read all the keys web.query("DECLARE key2id CURSOR FOR SELECT id, key FROM thing") while True: result = web.query('FETCH FORWARD $limit FROM key2id', vars=locals()) if not result: break for row in result: key2id[row.key] = row.id web.query("CLOSE key2id"); web.rollback(); return key2id
def store(self, id_, client_ip, data, old_id=False): '''takes client_ip - client ip id_ - string data - Storage old_id - if the id regenerates after every request''' do_insert = True if not old_id: old_id = id_ if len(list(web.select(web.config.handler_parameters.db_table, vars={'id': old_id}, what='id', where='id = $id'))) == 1: do_insert = False web.transact() now = int(time.time()) try: if do_insert: web.db.insert(web.config.handler_parameters.db_table, seqname=False, id=id_, ip=client_ip, touched=now, created=now, data=pickle.dumps(data, 0)) else: web.update(web.config.handler_parameters.db_table, where='id = $old_id', vars={'old_id': old_id}, id=id_, ip=client_ip, touched=now, data=pickle.dumps(data, 0)) web.commit() except Exception, inst: web.rollback() raise inst
def set_auth_token(self, user_key): t = datetime.datetime(*time.gmtime()[:6]).isoformat() text = "%s,%s" % (user_key, t) text += "," + self._generate_salted_hash(self.secret_key, text) web.ctx.infobase_auth_token = text def _generate_salted_hash(self, key, text, salt=None): salt = salt or hmac.HMAC(key, str(random.random())).hexdigest()[:5] hash = hmac.HMAC(key, web.utf8(salt) + web.utf8(text)).hexdigest() return '%s$%s' % (salt, hash) def _check_salted_hash(self, key, text, salted_hash): salt, hash = salted_hash.split('$', 1) return self._generate_salted_hash(key, text, salt) == salted_hash def checkpassword(self, username, raw_password): details = self.site.store.get_user_details(username) if details is None or details.get('active', True) == False: return False else: return self._check_salted_hash(self.secret_key, raw_password, details.password) if __name__ == "__main__": web.transact() from infobase import Infobase site = Infobase().get_site('infogami.org') a = AccountManager(site) web.rollback()
def rollback(value): web.transact() web.insert("test", seqname=False, value=value) web.rollback()