Ejemplo n.º 1
0
 def GET(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()
         if (username, password) in allowed:
             if web.input()['what'] == 'pos':
                 poses = []
                 for a in Agent.select():
                     try:
                         samp_date = a.get_last_log()[5]
                         samp_time = a.get_last_log()[6]
                         if type(samp_time) == datetime.timedelta:
                             samp_time = datetime.datetime.strptime(
                                 str(samp_time), '%H:%M:%S')
                         last_log = str(samp_date) + ' ' + str(
                             samp_time.time())
                     except:
                         last_log = None
                     poses.append({'AgentName': a.AgentName,
                                   'md5': md5(
                                       str(a.sqlmeta.asDict())).hexdigest(),
                                   'last_log': last_log})
                 return json.dumps(poses)
         else:
             authreq = True
     if authreq:
         web.header('WWW-Authenticate', 'Basic realm="Remote Data"')
         web.ctx.status = '401 unauthorized'
         return """<html>
Ejemplo n.º 2
0
def auth(username, password):
    ret = False
    try:
        user = Authuser.select(Authuser.q.username == username)[0]
    except IndexError:
        return ret
    if md5(password).hexdigest() == user.password:
        session.username = username
        session.is_admin = user.is_admin
        session.table_name = user.table_name
        ret = True
    return ret
Ejemplo n.º 3
0
 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()
             t = Thread(target=self.import_input)
             t.start()
         else:
             authreq = True
     if authreq:
         web.header('WWW-Authenticate', 'Basic realm="incoming"')
         web.ctx.status = '401 unauthorized'
         return """<html>
Ejemplo n.º 4
0
    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>
Ejemplo n.º 5
0
 def GET(self):
     users = Authuser.select()
     return render.adm.users.index({'users': users})