def test_unicode(self): """Tests the unicode method executes.""" log = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) log.save() self.assertIsNotNone(log.__unicode__())
def user_json(user): """Generate JSON-style dict for user.""" return { 'name': user.username, 'id': user.pk, 'on_clock': MissionClockEvent.user_on_clock(user), 'on_timeout': MissionClockEvent.user_on_timeout(user), 'in_air': TakeoffOrLandingEvent.user_in_air(user), 'active': UasTelemetry.user_active(user), }
def put(self, request, pk): """PUT allows updating status.""" try: user = User.objects.get(pk=int(pk)) except User.DoesNotExist: return HttpResponseBadRequest('Unknown team %s' % pk) try: data = json.loads(request.body) except ValueError: return HttpResponseBadRequest('Invalid JSON: %s' % request.body) # Potential events to update. takeoff_event = None clock_event = None # Update whether UAS is in air. if 'in_air' in data: in_air = data['in_air'] if not isinstance(in_air, bool): return HttpResponseBadRequest('in_air must be boolean') currently_in_air = TakeoffOrLandingEvent.user_in_air(user) # New event only necessary if changing status if currently_in_air != in_air: takeoff_event = TakeoffOrLandingEvent(user=user, uas_in_air=in_air) # Update whether UAS in on clock or timeout. if 'on_clock' in data or 'on_timeout' in data: currently_on_clock = MissionClockEvent.user_on_clock(user) currently_on_timeout = MissionClockEvent.user_on_timeout(user) on_clock = data.get('on_clock', currently_on_clock) on_timeout = data.get('on_timeout', currently_on_timeout) if (not isinstance(on_clock, bool) or not isinstance(on_timeout, bool)): return HttpResponseBadRequest( 'on_clock and on_timeout must be boolean.') if on_clock and on_timeout: return HttpResponseBadRequest( 'Cannot be on mission clock and on timeout.') # New event only necessary if changing status if (on_clock != currently_on_clock or on_timeout != currently_on_timeout): clock_event = MissionClockEvent(user=user, team_on_clock=on_clock, team_on_timeout=on_timeout) # Request was valid. Save updates. if takeoff_event: takeoff_event.save() if clock_event: clock_event.save() return HttpResponse( json.dumps(user_json(user)), content_type="application/json")
def test_user_on_timeout(self): """Tests the user_on_timeout method.""" log = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=False) log.save() log = MissionClockEvent(user=self.user2, team_on_clock=False, team_on_timeout=True) log.save() self.assertFalse(MissionClockEvent.user_on_timeout(self.user1)) self.assertTrue(MissionClockEvent.user_on_timeout(self.user2))
def test_get_noneditable_without_thumbnail_targets(self): """Test GET when there are non-editable targets without thumbnail.""" MissionClockEvent(user=self.team, team_on_clock=True, team_on_timeout=False).save() MissionClockEvent(user=self.team, team_on_clock=False, team_on_timeout=False).save() target = Target(user=self.team, target_type=TargetType.qrc) target.save() response = self.client.get(targets_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(0, len(data))
def get(self, request): """Gets all of the targets ready for review.""" targets = [] for user in User.objects.all(): # Targets still editable aren't ready for review. if (MissionClockEvent.user_on_clock(user) or MissionClockEvent.user_on_timeout(user)): continue # Get targets which have thumbnail. targets.extend([t for t in Target.objects.filter(user=user).all() if t.thumbnail]) # Sort targets by last edit time, convert to json. targets = [t.json(is_superuser=request.user.is_superuser) for t in sorted(targets, key=lambda t: t.last_modified_time)] return JsonResponse(targets, safe=False)
def get(self, request): """Gets all of the targets ready for review.""" targets = [] for user in User.objects.all(): # Targets still editable aren't ready for review. if (MissionClockEvent.user_on_clock(user) or MissionClockEvent.user_on_timeout(user)): continue # Get targets which have thumbnail. targets.extend([ t for t in Target.objects.filter(user=user).all() if t.thumbnail ]) # Sort targets by last edit time, convert to json. targets = [ t.json(is_superuser=request.user.is_superuser) for t in sorted(targets, key=lambda t: t.last_modified_time) ] return JsonResponse(targets, safe=False)
def test_get_editable_targets(self): """Test GET when there are targets but are still in editable window.""" MissionClockEvent(user=self.team, team_on_clock=True, team_on_timeout=False).save() Target(user=self.team, target_type=TargetType.qrc).save() response = self.client.get(targets_review_url) self.assertEqual(200, response.status_code) self.assertEqual([], json.loads(response.content))
def test_get_noneditable_targets(self): """Test GET when there are non-editable targets.""" MissionClockEvent(user=self.team, team_on_clock=True, team_on_timeout=False).save() MissionClockEvent(user=self.team, team_on_clock=False, team_on_timeout=False).save() target = Target(user=self.team, target_type=TargetType.qrc) target.save() with open(test_image('A.jpg')) as f: target.thumbnail.save('%d.%s' % (target.pk, 'jpg'), ImageFile(f)) target.save() response = self.client.get(targets_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(1, len(data)) self.assertIn('type', data[0]) self.assertEqual('qrc', data[0]['type'])
def put(self, request, pk): """PUT allows updating status.""" try: user = User.objects.get(pk=int(pk)) except User.DoesNotExist: return HttpResponseBadRequest('Unknown team %s' % pk) try: data = json.loads(request.body) except ValueError: return HttpResponseBadRequest('Invalid JSON: %s' % request.body) # Potential events to update. takeoff_event = None clock_event = None # Update whether UAS is in air. if 'in_air' in data: in_air = data['in_air'] if not isinstance(in_air, bool): return HttpResponseBadRequest('in_air must be boolean') currently_in_air = TakeoffOrLandingEvent.user_in_air(user) # New event only necessary if changing status if currently_in_air != in_air: takeoff_event = TakeoffOrLandingEvent(user=user, uas_in_air=in_air) # Update whether UAS in on clock or timeout. if 'on_clock' in data or 'on_timeout' in data: currently_on_clock = MissionClockEvent.user_on_clock(user) currently_on_timeout = MissionClockEvent.user_on_timeout(user) on_clock = data.get('on_clock', currently_on_clock) on_timeout = data.get('on_timeout', currently_on_timeout) if (not isinstance(on_clock, bool) or not isinstance(on_timeout, bool)): return HttpResponseBadRequest( 'on_clock and on_timeout must be boolean.') if on_clock and on_timeout: return HttpResponseBadRequest( 'Cannot be on mission clock and on timeout.') # New event only necessary if changing status if (on_clock != currently_on_clock or on_timeout != currently_on_timeout): clock_event = MissionClockEvent(user=user, team_on_clock=on_clock, team_on_timeout=on_timeout) # Request was valid. Save updates. if takeoff_event: takeoff_event.save() if clock_event: clock_event.save() return HttpResponse(json.dumps(user_json(user)), content_type="application/json")
def create_data(self): """Create a basic sample dataset.""" self.user1 = User.objects.create_user('user1', '*****@*****.**', 'testpass') self.user1.save() self.user2 = User.objects.create_user('user2', '*****@*****.**', 'testpass') self.user2.save() # user1 is on mission event = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) event.save() # user1 is flying event = TakeoffOrLandingEvent(user=self.user1, uas_in_air=True) event.save() # user2 has landed event = TakeoffOrLandingEvent(user=self.user2, uas_in_air=True) event.save() event = TakeoffOrLandingEvent(user=self.user2, uas_in_air=False) event.save() # user2 is active self.timestamp = timezone.now() gps = GpsPosition(latitude=38.6462, longitude=-76.2452) gps.save() pos = AerialPosition(gps_position=gps, altitude_msl=0) pos.save() telem = UasTelemetry(user=self.user2, uas_position=pos, uas_heading=90) telem.save() telem.timestamp = self.timestamp telem.save()
def test_missions(self): """Tets the missions().""" on_clock = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) on_clock.save() on_clock.timestamp = self.year2000 on_clock.save() on_timeout = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=True) on_timeout.save() on_timeout.timestamp = self.year2001 on_timeout.save() off_timeout = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) off_timeout.save() off_timeout.timestamp = self.year2002 off_timeout.save() off_clock = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=False) off_clock.save() off_clock.timestamp = self.year2003 off_clock.save() random_event = MissionClockEvent(user=self.user2, team_on_clock=True, team_on_timeout=False) random_event.save() missions = MissionClockEvent.missions(self.user1) self.assertEqual([ TimePeriod(self.year2000, self.year2001), TimePeriod(self.year2002, self.year2003) ], missions)
def test_missions(self): """Tets the missions().""" on_clock = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) on_clock.save() on_clock.timestamp = self.year2000 on_clock.save() on_timeout = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=True) on_timeout.save() on_timeout.timestamp = self.year2001 on_timeout.save() off_timeout = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) off_timeout.save() off_timeout.timestamp = self.year2002 off_timeout.save() off_clock = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=False) off_clock.save() off_clock.timestamp = self.year2003 off_clock.save() random_event = MissionClockEvent(user=self.user2, team_on_clock=True, team_on_timeout=False) random_event.save() missions = MissionClockEvent.missions(self.user1) self.assertEqual( [TimePeriod(self.year2000, self.year2001), TimePeriod(self.year2002, self.year2003)], missions)