def upload(): to_load = {} with open('/tmp/svrdata.pkl', 'rb') as f: to_load = cPickle.load(f) for d in to_load.get('pos_to_put', []): try: agent = Agent.select(Agent.q.AgentName == d['name'])[0] except IndexError: try: agent = Agent(**d['data']) # karena 'id'nya tidak standar, # SQLObject bilang Not Found except SQLObjectNotFound: agent = Agent.select(Agent.q.AgentName == d['name'])[0] # Periksa apakah table pemuat Logs untuk # pos ini telah tersedia try: rs = conn.queryAll("SELECT SamplingDate FROM %s \ LIMIT 0, 1" % (agent.table_name)) except: rs = conn.queryAll("CREATE TABLE %s \ LIKE tpl_agent" % agent.table_name) print agent.AgentName for l in d['logs']: print '\t', l['SamplingDate'], l['SamplingTime'] sql = "SELECT COUNT(*) FROM %s \ WHERE SamplingDate='%s' AND \ SamplingTime='%s'" % (agent.table_name, l['SamplingDate'], l['SamplingTime']) rs = conn.queryAll(sql) print '\t\trs[0][0]:', rs[0][0] if rs[0][0] == 0: sql = "INSERT INTO %s (RID, ReceivedDate, \ ReceivedTime, DataType, StatusPort, \ SamplingDate, SamplingTime, Temperature, \ Humidity, Rain, Rain1, Rain2, Rain3, \ Rain4, WLevel, Wlevel1, WLevel2, \ WLevel3, WLevel4, up_since, sq) VALUES (%s, '%s', \ '%s', %s, '%s', '%s', '%s', %s, %s, \ %s, %s, %s, %s, %s, %s, %s, %s, %s, \ %s, '%s', %s)" % ( agent.table_name, l['RID'], l['ReceivedDate'], l['ReceivedTime'], l['DataType'], l['StatusPort'], l['SamplingDate'], l['SamplingTime'], l['Temperature'], l['Humidity'], l['Rain'], l['Rain1'], l['Rain2'], l['Rain3'], l['Rain4'], l['WLevel'], l['WLevel1'], l['WLevel2'], l['WLevel3'], l['WLevel4'], l['up_since'], l['sq']) rs = conn.query(sql) print '\tsaved:', l['SamplingDate'], l['SamplingTime'] del l
def POST(self, table_name): inp = web.input() state = inp.get('state') if state == 'hapus': asset_id = inp.get('asset_id') delete = Delete('asset', where='id=' + asset_id) query = conn.sqlrepr(delete) conn.query(query) return "ok" elif state == 'tambah': nama = inp.get('nama_asset_add') kategori = inp.get('kategori-asset') merk = inp.get('merk') model = inp.get('model') tgl_perolehan = inp.get('tgl_perolehan') if inp.get('nilai'): n1 = inp.get('nilai').replace('Rp. ', '') n2 = n1.replace('.', '') nilai = n2 bmn = inp.get('bmn') Asset(table_name=table_name, cuser=session.get('username'), kategori=kategori + '_' + nama, nama=nama, merk=merk, model=model, perolehan=tgl_perolehan, nilai_perolehan=int(nilai), bmn=bmn) return web.redirect('asset') else: try: asset = Asset.get(int(inp.get('pk'))) asset.set(**{inp.get('name'): inp.get('value')}) except SQLObjectNotFound: return web.notfound() web.header('Content-Type', 'application/json') return json.dumps({"Ok": "true"})
def POST(self, table_name): inp = web.input() if inp.get('state') == 'hapus': kegiatan_id = inp.get('kegiatan_id') foto_id = inp.get('foto_id') filename = inp.get('filename') deletekeg = Delete('kegiatan', where='id=' + kegiatan_id) query1 = conn.sqlrepr(deletekeg) conn.query(query1) deleteft = Delete('foto', where='id=' + foto_id) query2 = conn.sqlrepr(deleteft) conn.query(query2) if os.path.exists(filename): os.remove(filename) return "ok" else: keg = Kegiatan(table_name=table_name, petugas=inp.get('petugas'), uraian=inp.get('uraian'), sampling=to_date(inp.get('waktu')), cuser=session.username) filename = FOTO_PATH + table_name + '_kegiatan_' + str( keg.id) + '_' + inp.get('filename').lower() if not os.path.isdir(FOTO_PATH): os.mkdir(FOTO_PATH) with open(filename, 'wb') as f: f.write(base64.b64decode(inp.get('data').split(',')[1])) foto = Foto(filepath=filename, keterangan=inp.get('uraian'), obj_type='kegiatan', obj_id=keg.id, cuser=session.get('username')) keg.foto = foto web.header('Content-Type', 'application/json') return json.dumps({"Ok": "true"})
def POST(self): inp = web.input() if inp.get('state') == '0': kategori = inp.get('kategori') if inp.get('lanjut') == '0': lanjut = False elif inp.get('lanjut') == '1': lanjut = True kerusakan_id = inp.get('kerusakan_id') uraian = inp.get('uraian') Tanggapan1(kerusakan=int(kerusakan_id), uraian=uraian, kategori=kategori, lanjut=lanjut, cuser=session.get('username')) return "ok" elif inp.get('state') == '1': kategori = "" lanjut = False kerusakan_id = inp.get('kerusakan_id') uraian = inp.get('uraian') Tanggapan1(kerusakan=int(kerusakan_id), uraian=uraian, kategori=kategori, lanjut=lanjut, cuser=session.get('username')) return "ok" elif inp.get('state') == 'update': table_name = inp.get('table_name') tanggapan1_id = inp.get('tanggapan1_id') uraian = inp.get('uraian') update = Update('tanggapan1', values={'uraian': uraian}, where='id=' + tanggapan1_id) query = conn.sqlrepr(update) conn.query(query) # return "ok" return web.redirect(table_name + '/kerusakan?' + str(datetime.datetime.now()))
def POST(self): auth = web.ctx.env.get('HTTP_AUTHORIZATION') authreq = False allowed = [(u.username, u.password) for u in Authuser.select()] if auth is None: authreq = True else: auth = re.sub('^Basic', '', auth) username, password = base64.decodestring(auth).split(':') password = md5(password).hexdigest() auth = None if (username, password) in allowed: x = web.input() of = open('/tmp/svrdata.pkl', 'wb') of.write(x['svr_data']) of.close() to_load = {} with open('/tmp/svrdata.pkl', 'rb') as f: to_load = cPickle.load(f) for d in to_load.get('pos_to_put', []): try: agent = Agent.select(Agent.q.AgentName == d['name'])[0] except IndexError: try: agent = Agent(**d['data']) # karena 'id'nya tidak standar, # SQLObject bilang Not Found except SQLObjectNotFound: agent = Agent.select( Agent.q.AgentName == d['name'])[0] # Periksa apakah table pemuat Logs untuk # pos ini telah tersedia try: rs = conn.queryAll("SELECT SamplingDate FROM %s \ LIMIT 0, 1" % (agent.table_name)) except: rs = conn.queryAll("CREATE TABLE %s \ LIKE tpl_agent" % agent.table_name) for l in d['logs']: sql = "SELECT COUNT(*) FROM %s \ WHERE SamplingDate='%s' AND \ SamplingTime='%s'" % (agent.table_name, l['SamplingDate'], l['SamplingTime']) rs = conn.queryAll(sql) if rs[0][0] == 0: sql = "INSERT INTO %s (RID, ReceivedDate, \ ReceivedTime, DataType, StatusPort, \ SamplingDate, SamplingTime, Temperature, \ Humidity, Rain, Rain1, Rain2, Rain3, \ Rain4, WLevel, Wlevel1, WLevel2, \ WLevel3, WLevel4, up_since, sq) VALUES (%s, '%s', \ '%s', %s, '%s', '%s', '%s', %s, %s, \ %s, %s, %s, %s, %s, %s, %s, %s, %s, \ %s, '%s', %s)" % ( agent.table_name, l['RID'], l['ReceivedDate'], l['ReceivedTime'], l['DataType'], l['StatusPort'], l['SamplingDate'], l['SamplingTime'], l['Temperature'], l['Humidity'], l['Rain'], l['Rain1'], l['Rain2'], l['Rain3'], l['Rain4'], l['WLevel'], l['WLevel1'], l['WLevel2'], l['WLevel3'], l['WLevel4'], l['up_since'], l['sq']) rs = conn.query(sql) l = None try: new_pos_data = d['data'] for k in new_pos_data.keys(): setattr(agent, k, new_pos_data[k]) except: pass # POS to Del for d in to_load.get('pos_to_del', []): try: agent = Agent.select( Agent.q.AgentName == d['AgentName'])[0] agent.destroySelf() except: pass return "Ok" else: authreq = True if authreq: web.header('WWW-Authenticate', 'Basic realm="incoming"') web.ctx.status = '401 unauthorized' return """<html>
def POST(self, table_name): inp = web.input() state = inp.get('state') if state == 'tambah_foto': kerusakan_id = inp.get('kerusakan_id') filename = FOTO_PATH + table_name + '_kerusakan_' + str( kerusakan_id) + '_' + inp.get('filename').lower() if not os.path.isdir(FOTO_PATH): os.mkdir(FOTO_PATH) with open(filename, 'wb') as f: f.write(base64.b64decode(inp.get('data').split(',')[1])) foto = Foto(filepath=filename, keterangan=inp.get('deskripsi_foto'), obj_type='kerusakan', obj_id=int(kerusakan_id), cuser=session.get('username')) return "ok" elif state == 'update': kerusakan_id = inp.get('kerusakan_id') uraian = inp.get('uraian') update = Update('kerusakan', values={'uraian': uraian}, where='id=' + kerusakan_id) query = conn.sqlrepr(update) conn.query(query) return "ok" elif inp.get('asset_id'): asset_id = inp.get('asset_id') deskripsi_foto = inp.get('deskripsi_foto') uraian_kerusakan = inp.get('uraian_kerusakan') kategori = inp.get('kategori') kerusakan_db = Kerusakan(asset=int(asset_id), table_name=table_name, cuser=session.get('username'), uraian=uraian_kerusakan, kategori=kategori) filename = FOTO_PATH + table_name + '_kerusakan_' + str( kerusakan_db.id) + '_' + inp.get('filename').lower() if not os.path.isdir(FOTO_PATH): os.mkdir(FOTO_PATH) with open(filename, 'wb') as f: f.write(base64.b64decode(inp.get('data').split(',')[1])) foto = Foto(filepath=filename, keterangan=deskripsi_foto, obj_type='kerusakan', obj_id=kerusakan_db.id, cuser=session.get('username')) return "ok" else: try: tanggapan2 = Tanggapan2.get(int(inp.get('pk'))) tanggapan2.set(**{inp.get('name'): inp.get('value')}) except SQLObjectNotFound: return web.notfound() web.header('Content-Type', 'application/json') return json.dumps({"Ok": "true"})
def import_input(self): print('import_input running') to_load = {} with open('/tmp/svrdata.pkl', 'rb') as f: to_load = pickle.load(f) for d in to_load.get('pos_to_put', []): try: agent = Agent.select(Agent.q.AgentName == d['name'])[0] except IndexError: try: agent = Agent(**d['data']) # karena 'id'nya tidak standar, # SQLObject bilang Not Found except SQLObjectNotFound: agent = Agent.select( Agent.q.AgentName == d['name'])[0] # Periksa apakah table pemuat Logs untuk # pos ini telah tersedia print('incoming:', agent.table_name) try: rs = conn.queryAll("SELECT SamplingDate FROM %s \ LIMIT 0, 1" % (agent.table_name)) except: try: rs = conn.queryAll("CREATE TABLE %s \ LIKE tpl_agent" % agent.table_name) except: pass for l in d['logs']: sql = "SELECT COUNT(*) FROM %s \ WHERE SamplingDate='%s' AND \ SamplingTime='%s'" % (agent.table_name, l['SamplingDate'], l['SamplingTime']) rs = conn.queryAll(sql) if rs[0][0] == 0: sql = "INSERT INTO %s (RID, ReceivedDate, \ ReceivedTime, DataType, StatusPort, \ SamplingDate, SamplingTime, Temperature, \ Humidity, Rain, Rain1, Rain2, Rain3, \ Rain4, WLevel, Wlevel1, WLevel2, \ WLevel3, WLevel4, up_since, sq) VALUES (%s, '%s', \ '%s', %s, '%s', '%s', '%s', %s, %s, \ %s, %s, %s, %s, %s, %s, %s, %s, %s, \ %s, '%s', %s)" % (agent.table_name, l['RID'], l['ReceivedDate'], l['ReceivedTime'], l['DataType'], l['StatusPort'], l['SamplingDate'], l['SamplingTime'], l['Temperature'], l['Humidity'], l['Rain'], l['Rain1'], l['Rain2'], l['Rain3'], l['Rain4'], l['WLevel'], l['WLevel1'], l['WLevel2'], l['WLevel3'], l['WLevel4'], l['up_since'], l['sq']) rs = conn.query(sql) l = None try: new_pos_data = d['data'] for k in new_pos_data.keys(): setattr(agent, k, new_pos_data[k]) except: pass # POS to Del for d in to_load.get('pos_to_del', []): try: agent = Agent.select( Agent.q.AgentName == d['AgentName'])[0] agent.destroySelf() except: pass return "Ok"