def get(self, request): kml = Kml(name='AUVSI SUAS LIVE Flight Data') kml_mission = kml.newfolder(name='Missions') (mission, err) = active_mission() if err: return err MissionConfig.kml_all(kml_mission, [mission]) kml_flyzone = kml.newfolder(name='Fly Zones') FlyZone.kml_all(kml_flyzone) parameters = '?sessionid={}'.format(request.COOKIES['sessionid']) uri = request.build_absolute_uri( '/auvsi_admin/update.kml') + parameters netlink = kml.newnetworklink(name="Live Data") netlink.link.href = uri netlink.link.refreshmode = RefreshMode.oninterval netlink.link.refreshinterval = 0.5 response = HttpResponse(kml.kml()) response['Content-Type'] = 'application/vnd.google-earth.kml+xml' response['Content-Disposition' ] = 'attachment; filename=%s.kml' % 'live' response['Content-Length'] = str(len(response.content)) return response
def get(self, request): kml = Kml(name='AUVSI SUAS LIVE Flight Data') kml_mission = kml.newfolder(name='Missions') (mission, err) = active_mission() if err: return err MissionConfig.kml_all(kml_mission, [mission]) kml_flyzone = kml.newfolder(name='Fly Zones') FlyZone.kml_all(kml_flyzone) parameters = '?sessionid={}'.format(request.COOKIES['sessionid']) uri = request.build_absolute_uri( '/auvsi_admin/update.kml') + parameters netlink = kml.newnetworklink(name="Live Data") netlink.link.href = uri netlink.link.refreshmode = RefreshMode.oninterval netlink.link.refreshinterval = 0.5 response = HttpResponse(kml.kml()) response['Content-Type'] = 'application/vnd.google-earth.kml+xml' response['Content-Disposition'] = 'attachment; filename=live.kml' response['Content-Length'] = str(len(response.content)) return response
def test_no_active_missions(self): """Tests when there are no active missions.""" _, err = active_mission() self.assertTrue(isinstance(err, HttpResponseServerError)) _, err = mission_for_request({}) self.assertTrue(isinstance(err, HttpResponseServerError))
def get(self, request): # Log user access to server information. logger.info('User downloaded server info: %s.' % request.user.username) ServerInfoAccessLog(user=request.user).save() # Form response. try: # Get the server info stored in the active mission. server_info_key = '/ServerInfo/latest' info = cache.get(server_info_key) if info is None: (mission, err) = active_mission() if err: return err info = mission.server_info if not info: return HttpResponseServerError( 'No server info for mission.') cache.set(server_info_key, info) except ServerInfo.DoesNotExist: # Failed to obtain server info. return HttpResponseServerError('No server info available.') # Form JSON response. data = info.json() data['server_time'] = datetime.datetime.now().isoformat() return JsonResponse(data)
def mission_for_request(request_params): """Gets the mission for the request. Args: request_params: The request parameter dict. If this has a 'mission' parameter, it will get the corresponding mission. Returns: Returns (MissionConfig, HttpResponse). The MissionConfig is the one corresponding to the request parameter, or the single active MissionConfig if one exists. The HttpResponse is the appropriate error if a MissionConfig could not be obtained. """ # If specific mission requested, get it. if 'mission' in request_params: try: mission_id_str = request_params['mission'] mission_id = int(mission_id_str) mission = MissionConfig.objects.get(pk=mission_id) return (mission, None) except ValueError: logger.warning('Invalid mission ID given. ID: %d.', mission_id_str) return (None, HttpResponseBadRequest('Mission ID is not an integer.')) except MissionConfig.DoesNotExist: logger.warning('Given mission ID not found. ID: %d.', mission_id) return (None, HttpResponseBadRequest('Mission not found.')) # Mission not specified, get the single active mission. return active_mission()
def test_active_mission(self): """Tests getting the single active mission.""" config = self.create_config() config.is_active = True config.save() recv_config, _ = active_mission() self.assertEqual(config, recv_config) recv_config, _ = mission_for_request({}) self.assertEqual(config, recv_config)
def test_multiple_active_missions(self): """Tests when too many active missions.""" config = self.create_config() config.is_active = True config.save() config = self.create_config() config.is_active = True config.save() _, err = active_mission() self.assertTrue(isinstance(err, HttpResponseServerError)) _, err = mission_for_request({}) self.assertTrue(isinstance(err, HttpResponseServerError))
def get(self, request): # Get active mission for forming responses. (mission, err) = active_mission() if err: return err # Form JSON response portion for stationary obstacles stationary_obstacles = mission.stationary_obstacles.select_related( ).all().order_by('pk') obst_set_proto = interop_api_pb2.ObstacleSet() for obst in stationary_obstacles: obst_proto = obst_set_proto.stationary_obstacles.add() obst_proto.latitude = obst.gps_position.latitude obst_proto.longitude = obst.gps_position.longitude obst_proto.radius = obst.cylinder_radius obst_proto.height = obst.cylinder_height # Return JSON data return HttpResponse(json_format.MessageToJson(obst_set_proto), content_type="application/json")
def get(self, request): time = timezone.now() # ?time=TIMESTAMP to get obstacle location at specific time if 'time' in request.GET: if not request.user.is_superuser: return HttpResponseBadRequest( 'Only superusers may set the time parameter') try: time = iso8601.parse_date(request.GET['time']) except iso8601.ParseError: return HttpResponseBadRequest("Bad timestamp '%s'" % \ request.GET['time']) # Log user access to obstacle info logger.info('User downloaded obstacle info: %s.' % request.user.username) # Get active mission for forming responses. (mission, err) = active_mission() if err: return err # Form JSON response portion for stationary obstacles stationary_obstacles = mission.stationary_obstacles.all() stationary_obstacles_json = [] for cur_obst in stationary_obstacles: # Add current obstacle cur_obst_json = cur_obst.json() stationary_obstacles_json.append(cur_obst_json) # Form final JSON response data = { 'stationary_obstacles': stationary_obstacles_json, } # Return JSON data return HttpResponse(json.dumps(data), content_type="application/json")
def get(self, request): log_access = True time = timezone.now() # ?log=(true|false) to enable/disable logging of superusers if 'log' in request.GET: if not request.user.is_superuser: return HttpResponseBadRequest( 'Only superusers may set the log parameter') try: log_access = boolean_param(request.GET['log']) except ValueError as e: return HttpResponseBadRequest(e) # ?time=TIMESTAMP to get obstacle location at specific time if 'time' in request.GET: if not request.user.is_superuser: return HttpResponseBadRequest( 'Only superusers may set the time parameter') try: time = iso8601.parse_date(request.GET['time']) except iso8601.ParseError: return HttpResponseBadRequest("Bad timestamp '%s'" % \ request.GET['time']) # Log user access to obstacle info logger.info('User downloaded obstacle info: %s.' % request.user.username) if log_access: ObstacleAccessLog(user=request.user).save() # Get active mission for forming responses. (mission, err) = active_mission() if err: return err # Form JSON response portion for stationary obstacles stationary_obstacles_cached = True stationary_obstacles_key = '/StationaryObstacle/all' stationary_obstacles = cache.get(stationary_obstacles_key) if stationary_obstacles is None: stationary_obstacles = mission.stationary_obstacles.all() stationary_obstacles_cached = False stationary_obstacles_json = [] for cur_obst in stationary_obstacles: # Add current obstacle cur_obst_json = cur_obst.json() stationary_obstacles_json.append(cur_obst_json) # Form JSON response portion for moving obstacles moving_obstacles_cached = True moving_obstacles_key = '/MovingObstacle/all' moving_obstacles = cache.get(moving_obstacles_key) if moving_obstacles is None: moving_obstacles = mission.moving_obstacles.all() moving_obstacles_cached = False moving_obstacles_json = [] for cur_obst in moving_obstacles: # Add current obstacle cur_obst_json = cur_obst.json(time=time) moving_obstacles_json.append(cur_obst_json) # Form final JSON response data = { 'stationary_obstacles': stationary_obstacles_json, 'moving_obstacles': moving_obstacles_json } # Cache obstacles for next request if not stationary_obstacles_cached: cache.set(stationary_obstacles_key, stationary_obstacles) if not moving_obstacles_cached: cache.set(moving_obstacles_key, moving_obstacles) # Return JSON data return HttpResponse(json.dumps(data), content_type="application/json")