def put(self, name): if not name: self.set_status(500) self.write('Name cannot be empty') return name = re.sub('\s', '-', name) conn = self.application.settings['db_connection'] req = json.loads(self.request.body) b58id = req['frag'].lstrip('#!') stateid = util.b58decode(b58id) if not req['confirmed']: exists = conn.execute("select * from names where name = ?", (name, )).fetchone() if exists: self.set_status(409) self.write( 'This name is already stored. Are you sure you want to overwrite it?' ) return conn.execute( "insert or replace into names (name, stateid) values (?, ?)", (name, stateid)) url = self.application.settings['url_path_prefix'] self.write('%snamed/%s' % (url, name)) return
def get(self, b58id): conn = self.application.settings['db_connection'] try: stateid = util.b58decode(b58id) except: raise tornado.web.HTTPError(404) row = conn.execute("select state from states where id=?", (stateid,)).fetchone() if row: self.set_header("Content-Type", "application/json; charset=UTF-8") self.write(row[0]) else: raise tornado.web.HTTPError(404)
def put(self, name): if not name: self.set_status(500) self.write('Name cannot be empty'); return name = re.sub('\s', '-', name) conn = self.application.settings['db_connection'] req = json.loads(self.request.body) b58id = req['frag'].lstrip('#!') stateid = util.b58decode(b58id) if not req['confirmed']: exists = conn.execute("select * from names where name = ?", (name,)).fetchone() if exists: self.set_status(409) self.write('This name is already stored. Are you sure you want to overwrite it?') return conn.execute("insert or replace into names (name, stateid) values (?, ?)", (name, stateid)) url = self.application.settings['url_path_prefix'] self.write('%snamed/%s' % (url, name)) return