Esempio n. 1
0
 def post(self):
     arguments = self.request.arguments()
     if 'client_mac' not in arguments or 'base_mac' not in arguments or 'ss' not in arguments:
         utils.send_400(self.response, "Only pass client_mac, base_mac and ss arguments (as integers)")
         return
     try:
         client_mac = int(self.request.get('client_mac'))
         base_mac = int(self.request.get('base_mac'))
         ss = int(self.request.get('ss'))
     except ValueError:
         utils.send_400(self.response, "Pass all arguments as integers")
         return
     #Calculate the location from the scan
     #TODO for now we generate a random one
     now = utils.now_datetime()
     map_id = maps.random_map_id()
     location = model.Location(map_id=map_id, x=utils.random_int(0, 300), y=utils.random_int(0, 600), timestamp=now)
     location.put()
     #Find out which base_station
     base_station = base_stations.get_base_station_with_mac(base_mac)
     #Create or update the client entry
     client = clients.create_or_update_client(client_mac, base_station, now)
     #Create the scan and insert it
     scan = model.Scan(map_key=map_id, client=client.key(), base_ap=base_station.key(), ss=ss, timestamp=now,
                       location=location.key())
     scan.put()
     utils.send_200(self.response, "Scan and Location successfully inserted")
Esempio n. 2
0
 def post(self):
     arguments = self.request.arguments()
     if 'client_mac' not in arguments or 'base_mac' not in arguments or 'ss' not in arguments:
         utils.send_400(
             self.response,
             "Only pass client_mac, base_mac and ss arguments (as integers)"
         )
         return
     try:
         client_mac = int(self.request.get('client_mac'))
         base_mac = int(self.request.get('base_mac'))
         ss = int(self.request.get('ss'))
     except ValueError:
         utils.send_400(self.response, "Pass all arguments as integers")
         return
     #Calculate the location from the scan
     #TODO for now we generate a random one
     now = utils.now_datetime()
     map_id = maps.random_map_id()
     location = model.Location(map_id=map_id,
                               x=utils.random_int(0, 300),
                               y=utils.random_int(0, 600),
                               timestamp=now)
     location.put()
     #Find out which base_station
     base_station = base_stations.get_base_station_with_mac(base_mac)
     #Create or update the client entry
     client = clients.create_or_update_client(client_mac, base_station, now)
     #Create the scan and insert it
     scan = model.Scan(map_key=map_id,
                       client=client.key(),
                       base_ap=base_station.key(),
                       ss=ss,
                       timestamp=now,
                       location=location.key())
     scan.put()
     utils.send_200(self.response,
                    "Scan and Location successfully inserted")
Esempio n. 3
0
 def post(self, request, *args, **kwargs):
     req_params = request.POST
     mobile_number = req_params['mobile_number']
     password = req_params['password']
     person = Person.objects.get_person_by_num(mobile_number)
     try:
         if not person:
             raise InvalidPerson(STR_INVALID_PERSON)
         if person.user_password != gen_hash_pwd(password):
             raise LoginFailed(STR_LOGIN_FAILED)
         # TODO: Add it to redis and mantain session
         return send_200(self.response)
     except FamilyError, e:
         self.response['res_str'] = str(e)
         return send_400(self.response)
Esempio n. 4
0
 def post(self, request, *args, **kwargs):
     req_params = request.POST
     person_id = req_params['person_id']
     relationship_name = req_params['relationship_name']
     relation_person_id = req_params['relation_person_id']
     try:
         person, relationship_obj, relation_person = self._validate(
             person_id, relationship_name, relation_person_id)
         relative_obj = Relatives.objects.create_relatives(
             relation_person, relationship_obj)
         person.add_relatives(relative_obj)
         return send_200(self.response)
     except FamilyError, e:
         self.response['res_str'] = str(e)
         return send_400(self.response)
Esempio n. 5
0
 def get(self, request, *args, **kwargs):
     req_params = request.POST
     person_id = req_params['person_id']
     relation_name = req_params['relation_name']
     try:
         person = Person.objects.get_person(person_id)
         if not person:
             raise InvalidPerson(STR_INVALID_PERSON)
         relative_persons = person.search_relatives(relation_name)
         person_list = []
         for relative_person in relative_persons:
             person_list.append(relative_person.serializer())
         self.response['res_data'] = person_list
         return send_200(self.response)
     except FamilyError, e:
         self.response['res_str'] = str(e)
         return send_400(self.response)
Esempio n. 6
0
 def post(self, request, *args, **kwargs):
     req_params = request.POST
     first_name = req_params['first_name']
     last_name = req_params['last_name']
     gender = req_params['gender']
     dob = req_params['dob']
     email_id = req_params['email_id']
     user_password = req_params['password']
     mobile_number = req_params['mobile_number']
     try:
         self._validate(email_id, gender, mobile_number)
         Person.objects.create_person(first_name, last_name, dob, gender,
                                      mobile_number, email_id,
                                      user_password)
         return send_201(self.response)
     except FamilyError, e:
         self.response['res_str'] = str(e)
         return send_400(self.response)
Esempio n. 7
0
 def get(self):
     arguments = self.request.arguments()
     try:
         limit = int(self.request.get('limit', DEFAULT_LIMIT))
     except ValueError:
         utils.send_400(self.response, "Limit must be an integer")
         return
     if 't1' in arguments and 't2' in arguments:
         try:
             t1 = int(self.request.get('t1'))
             t2 = int(self.request.get('t2'))
         except ValueError:
             utils.send_400(self.response, "t1 and t2 should be integers representing unix epoch in seconds")
             return
         [start_date, end_date] = utils.parse_timestamps(t1, t2)
         query = model.Scan.all() \
             .filter("timestamp >= ", start_date) \
             .filter("timestamp <=", end_date) \
             .order("-timestamp")
         matched_scans = query.fetch(limit)
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(utils.json_encode(utils.query_to_array(matched_scans), True))
         return
     if 'client_mac' in arguments:
         mac_string = self.request.get("client_mac")
         client = clients.get_client_with_mac(utils.mac_string_to_int(mac_string))
         matched_scans = []
         if client is not None:
             query = model.Scan.all() \
                 .filter("client = ", client.key()) \
                 .order("-timestamp")
             matched_scans = query.fetch(limit)
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(utils.json_encode(utils.query_to_array(matched_scans), True))
         return
     if 'base_station_mac' in arguments:
         base_station_mac = self.request.get("base_station_mac")
         base_station = base_stations.get_base_station_with_mac(utils.mac_string_to_int(base_station_mac))
         matched_scans = []
         if base_station is not None:
             query = model.Scan.all() \
                 .filter("base_station = ", base_station.key()) \
                 .order("-timestamp")
             matched_scans = query.fetch(limit)
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(utils.json_encode(utils.query_to_array(matched_scans), True))
         return
     if 'map_id' in arguments:
         map_id = self.request.get("map_id")  #TODO check if map exists
         if 'x' in arguments and 'y' in arguments and 'radius' in arguments:
             try:
                 x = int(self.request.get('x'))
                 y = int(self.request.get('y'))
                 radius = float(self.request.get('radius'))
             except ValueError:
                 utils.send_400(self.response, "x and y must be integers, radius must be float in meters")
                 return
             scale = maps.get_scale(map_id)
             delta = scale * radius
             #TODO FOR NOW THE X/Y DISTANCE FILTERING IS VERY INEFFICIENT!!!
             query = model.Scan.all() \
                 .filter("map_id = ", map_id) \
                 .filter("x <= ", x + delta) \
                 .filter('x >= ', x - delta)
             matched_scans = query.fetch(limit)
             matched_scans[:] = [scan for scan in matched_scans
                                 if utils.point_in_circle(scan.location.x, scan.location.y, x, y, delta)]
             self.response.headers['Content-Type'] = 'application/json'
             self.response.out.write(utils.json_encode(utils.query_to_array(matched_scans), True))
             return
Esempio n. 8
0
 def get(self):
     arguments = self.request.arguments()
     try:
         limit = int(self.request.get('limit', DEFAULT_LIMIT))
     except ValueError:
         utils.send_400(self.response, "Limit must be an integer")
         return
     if 't1' in arguments and 't2' in arguments:
         try:
             t1 = int(self.request.get('t1'))
             t2 = int(self.request.get('t2'))
         except ValueError:
             utils.send_400(
                 self.response,
                 "t1 and t2 should be integers representing unix epoch in seconds"
             )
             return
         [start_date, end_date] = utils.parse_timestamps(t1, t2)
         query = model.Scan.all() \
             .filter("timestamp >= ", start_date) \
             .filter("timestamp <=", end_date) \
             .order("-timestamp")
         matched_scans = query.fetch(limit)
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(
             utils.json_encode(utils.query_to_array(matched_scans), True))
         return
     if 'client_mac' in arguments:
         mac_string = self.request.get("client_mac")
         client = clients.get_client_with_mac(
             utils.mac_string_to_int(mac_string))
         matched_scans = []
         if client is not None:
             query = model.Scan.all() \
                 .filter("client = ", client.key()) \
                 .order("-timestamp")
             matched_scans = query.fetch(limit)
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(
             utils.json_encode(utils.query_to_array(matched_scans), True))
         return
     if 'base_station_mac' in arguments:
         base_station_mac = self.request.get("base_station_mac")
         base_station = base_stations.get_base_station_with_mac(
             utils.mac_string_to_int(base_station_mac))
         matched_scans = []
         if base_station is not None:
             query = model.Scan.all() \
                 .filter("base_station = ", base_station.key()) \
                 .order("-timestamp")
             matched_scans = query.fetch(limit)
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(
             utils.json_encode(utils.query_to_array(matched_scans), True))
         return
     if 'map_id' in arguments:
         map_id = self.request.get("map_id")  #TODO check if map exists
         if 'x' in arguments and 'y' in arguments and 'radius' in arguments:
             try:
                 x = int(self.request.get('x'))
                 y = int(self.request.get('y'))
                 radius = float(self.request.get('radius'))
             except ValueError:
                 utils.send_400(
                     self.response,
                     "x and y must be integers, radius must be float in meters"
                 )
                 return
             scale = maps.get_scale(map_id)
             delta = scale * radius
             #TODO FOR NOW THE X/Y DISTANCE FILTERING IS VERY INEFFICIENT!!!
             query = model.Scan.all() \
                 .filter("map_id = ", map_id) \
                 .filter("x <= ", x + delta) \
                 .filter('x >= ', x - delta)
             matched_scans = query.fetch(limit)
             matched_scans[:] = [
                 scan for scan in matched_scans if utils.point_in_circle(
                     scan.location.x, scan.location.y, x, y, delta)
             ]
             self.response.headers['Content-Type'] = 'application/json'
             self.response.out.write(
                 utils.json_encode(utils.query_to_array(matched_scans),
                                   True))
             return
Esempio n. 9
0
 def get(self):
     try:
         limit = int(self.request.get('limit', DEFAULT_LIMIT))
     except ValueError:
         send_400(self.response, "Limit must be an int")
         return
     arguments = self.request.arguments()
     if 't1' in arguments and 't2' in arguments:
         #GET /locations/:t1:t2
         t1 = self.request.get('t1', None)
         t2 = self.request.get('t2', None)
         [start_date, end_date] = parse_timestamps(t1, t2)
         if start_date is None and end_date is None:
             send_400(self.response, "Timestamp format issue, t1 and t2 must be UNIX times in seconds")
             return
         query = model.Location.all() \
             .filter("timestamp >= ", start_date) \
             .filter("timestamp <= ", end_date) \
             .order('-timestamp')
         matched_locations = query.fetch(limit)
         if 'mac' in arguments:
             #GET /locations/:mac:t1:t2
             mac_address = self.request.get('mac')
             #get the clients with the mac_address
             client = model.Client.all().filter("mac = ", mac_address).fetch(1)
             #get the client's scans
             matching_scans = client.scans.order(-model.Scan.timestamp).fetch(limit)
             map_filter = 'map_id' in arguments
             map_id = self.request.get('map_id')
             matched_locations = []
             for scan in matching_scans:
                 if map_filter and scan.location.map_id == map_id:
                     matched_locations.append(scan.location)
                 elif not map_filter:
                     matched_locations.append(scan.location)
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(utils.json_encode(utils.query_to_array(matched_locations), True))
         return
     elif 'map_id' in arguments:
         #GET /locations/:map_id
         map_id = self.request.get('map_id')  #TODO check if map exists
         query = model.Location.all().filter("map_id = ", map_id)
         needs_y_filtering = False
         if 'x' in arguments and 'y' in arguments:
             #GET /locations/:map_id:x:y:radius
             try:
                 x = int(self.request.get('x'))
                 y = int(self.request.get('y'))
                 radius = float(self.request.get('radius', 100))
             except ValueError:
                 send_400(self.response, "x and y must be integers, radius must be float in meters")
                 return
             scale = maps.get_scale(map_id)
             delta = scale * radius
             #TODO FOR NOW THE X/Y DISTANCE FILTERING IS VERY INEFFICIENT!!!
             query.filter("x <= ", x + delta).filter('x >= ', x - delta)
             needs_y_filtering = True
         locations = query.fetch(limit)
         if needs_y_filtering:
             locations[:] = [location for location in locations
                             if utils.point_in_circle(location.x, location.y, x, y, delta)]
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(utils.json_encode(utils.query_to_array(locations), True))
         return
     elif 'mac' in arguments:
         #GET /locations/:mac(:limit)
         try:
             mac_address = int(self.request.get('mac'))
         except ValueError:
             send_400(self.response, "mac_address must be formatted as an int")
             return
         #get the clients with the mac_address
         client = model.Client.all().filter("mac = ", mac_address).fetch(1)
         if len(client) == 0:
             matching_scans = []
         else:
             #get the client's scans
             matching_scans = client[0].scans.order("-timestamp").fetch(limit)
         matched_locations = []
         for scan in matching_scans:
             matched_locations.append(scan.location)
         self.response.headers['Content-Type'] = 'application/json'
         self.response.out.write(utils.json_encode(utils.query_to_array(matched_locations), True))
         return
     #nothing matched
     send_400(self.response, "Argument combination not supported or not enough arguments")