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 drupy_cron(): """clean out old stuff in the watchdog table""" time1 = time() - variable_get('watchdog_clear', 604800) time2 = time() - 3600 web.transact() web.query('DELETE FROM watchdog WHERE timestamp < $time1', vars=locals()) web.query('DELETE FROM flood WHERE timestamp < $time2', vars=locals()) web.commit()
def delete(self, id, providerName): username = self.usernameProvider.get() web.transact() web.delete('places', where="providerid=%s and provider=%s" % \ (web.db.sqlquote(id), web.db.sqlquote(providerName))) web.query(self.SQL_ACTION % (web.db.sqlquote(username), web.db.sqlquote('elimino propiedad %s-%s' % (providerName, id)))); web.commit()
def POST(self, providerName, id, state): lid = scrappers[providerName].local(id) web.transact() web.query(self.SQL_UPDATE % (web.db.sqlquote(state), lid)) web.query(self.SQL_ACTION % (web.db.sqlquote(usernameProvider.get()), web.db.sqlquote('cambio al estado %s la prop %s-%s' % (state,providerName,id)))); web.commit() web.seeother('../../')
def testCommit(self): web.transact() web.insert('person', False, name='user1') web.commit() web.transact() web.insert('person', False, name='user2') web.insert('person', False, name='user3') web.commit() self.assertRows(3)
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 POST(self, providerName, id): lid = scrappers[providerName].local(id) i = web.input() username = usernameProvider.get() web.transact() n = web.insert('places_forum', idPlace=lid, owner=username, description=i.description) web.query(self.SQL_ACTION % (web.db.sqlquote(username), web.db.sqlquote('agrego un comentario a %s-%s' % (providerName,id)))); web.commit() web.seeother('../')
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 login(email='',user='',remember_me=''): # TODO: implement login by username in addition to email r = '0' if remember_me: r = '1' login_time = int(time.time()) user = web.select('users',where='email = $email',vars=locals())[0] #print user web.transact() web.query("UPDATE users SET login = $login_time, remember_me = $r \ WHERE uid = $user.uid", vars=locals()) #print user.uid inc.session.regenerate(uid=user.uid) web.commit()
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 POST(self, key): i = web.input('key', _method='POST') page = web.ctx.site.get(key) assert ' ' not in i.key and i.key.startswith('/') web.transact() id = web.query('SELECT id FROM thing WHERE site_id=1 and key=$key', vars=locals())[0].id web.query("update thing set key=$i.key where id=$id", vars=locals()) web.query("update datum set value=$i.key where thing_id=$id and key='key' and datatype=1", vars=locals()) web.commit() from infogami.infobase.server import get_site site = get_site(config.site) site.thing_cache.clear() site.things_cache.clear() site.versions_cache.clear() web.seeother(i.key)
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 POST(self): page = self.page form = form_variable() form_new = form_new_variable() web.transact() # Insert new if form_new.validates(): form_data = form_new.d name = form_data.new_variable_name value = form_data.new_variable_value if not glbl.variable.has_key(name): page.message += '<p>Added variable ' + name + \ ' = ' + value + '</p>' glbl.variable[name] = value else: page.message += '<p>Did not add variable ' + \ name + \ ' because it already exists. </p>' # Do updates if form.validates(): form_data = form.d for key in form_data: # Only update the variable if it changed if glbl.variable[key] != form_data[key]: page.message += '<p>Updated ' + key + ' from "' + \ glbl.variable[key] + '" to "' + \ form_data[key] + '".</p>' glbl.variable[key] = form_data[key] # Do deletes i = web.input(new=[],delete=[]) if hasattr(i,'delete'): for key in i.delete: page.message += '<p>Deleted ' + key + '.</p>' del glbl.variable[key] web.commit() # Gah, there shouldn't be html content = '<p><a href="/admin/variable">Refresh settings form</a></p>' web.render('generic.html')
def store(self, id, model, providerName,username=None): username = self.usernameProvider.get() thegeom = model['the_geom'] del model['the_geom'] model['providerName'] = providerName model['originalId'] = str(id) web.transact() ret = web.query(self.SQL_PLACE % (thegeom.encode('latin1'), providerName, id)) appid = web.select("currval('places_id_seq')")[0].currval for k,v in model.iteritems(): sql = self.SQL_DESCR % (appid, web.db.sqlquote(k.encode('latin1')), web.db.sqlquote(v.encode('latin1'))) sql = sql.replace('$','S') web.query(sql) web.query(self.SQL_ACTION % (web.db.sqlquote(username), web.db.sqlquote('importo la propiedad %s-%s' % (providerName,id)))); web.commit()
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 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 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 rollback(value): web.transact() web.insert("test", seqname=False, value=value) web.rollback()
def load(filename): """Load a dump from a the given file to database.""" from infogami.plugins.wikitemplates import code code.validation_enabled = False pages = {} for _, id, data in tdb.logger.parse(filename): type = data.pop('__type__') name = data.pop('__name__') pages[int(id)] = web.storage(id=int(id), name=name, type=type, d=data) web.load() site = db.get_site(config.site) mapping = {} def flat(items): """Makes a nested list flat. >>> x = flat([1, [2, 3, [4, 5], 6], 7]) >>> list(x) [1, 2, 3, 4, 5, 6, 7] """ for item in items: if isinstance(item, list): for x in flat(item): yield x else: yield item def get_dependencies(page): d = [pages[v.id] for v in flat(page.d.values()) if isinstance(v, tdb.Thing)] if page.type.id != page.id: t = pages[page.type.id] d = [t] + d return d def remap(v): if isinstance(v, tdb.Thing): return tdb.withID(mapping[v.id], lazy=True) elif isinstance(v, list): return [remap(x) for x in v] else: return v def new_version(page): print "new_version", page.name d = dict([(k, remap(v)) for k, v in page.d.items()]) try: p = tdb.withName(page.name, site) p.setdata(d) # careful about type/type if page.type.id != page.id: p.type = remap(page.type) except tdb.NotFound: p = tdb.new(page.name, site, remap(page.type), d) p.save() return p.id def load_page(page): if page.id in mapping: return for p in get_dependencies(page): load_page(p) mapping[page.id] = new_version(page) web.transact() for p in pages.values(): load_page(p) web.commit()
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 commit(value): web.transact() web.insert("test", seqname=False, value=value) web.commit()