本文整理汇总了Python中auvsi_suas.models.target.Target.json方法的典型用法代码示例。如果您正苦于以下问题:Python Target.json方法的具体用法?Python Target.json怎么用?Python Target.json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类auvsi_suas.models.target.Target
的用法示例。
在下文中一共展示了Target.json方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_targets
# 需要导入模块: from auvsi_suas.models.target import Target [as 别名]
# 或者: from auvsi_suas.models.target.Target import json [as 别名]
def test_get_targets(self):
"""We get back the targets we own."""
t1 = Target(user=self.user, target_type=TargetType.standard)
t1.save()
t2 = Target(user=self.user, target_type=TargetType.standard)
t2.save()
response = self.client.get(targets_url)
self.assertEqual(200, response.status_code)
d = json.loads(response.content)
self.assertItemsEqual([t1.json(), t2.json()], d)
示例2: test_json
# 需要导入模块: from auvsi_suas.models.target import Target [as 别名]
# 或者: from auvsi_suas.models.target.Target import json [as 别名]
def test_json(self):
"""Test target JSON."""
l = GpsPosition(latitude=38, longitude=-76)
l.save()
t = Target(user=self.user,
target_type=TargetType.standard,
location=l,
orientation=Orientation.s,
shape=Shape.square,
background_color=Color.white,
alphanumeric='ABC',
alphanumeric_color=Color.black,
description='Test target',
autonomous=True)
t.save()
d = t.json()
self.assertIn('id', d)
self.assertEqual(self.user.pk, d['user'])
self.assertEqual('standard', d['type'])
self.assertEqual(38, d['latitude'])
self.assertEqual(-76, d['longitude'])
self.assertEqual('s', d['orientation'])
self.assertEqual('square', d['shape'])
self.assertEqual('white', d['background_color'])
self.assertEqual('ABC', d['alphanumeric'])
self.assertEqual('black', d['alphanumeric_color'])
self.assertEqual('Test target', d['description'])
self.assertEqual(True, d['autonomous'])
self.assertNotIn('thumbnail_approved', d)
d = t.json(is_superuser=True)
self.assertIn('description_approved', d)
self.assertIn('thumbnail_approved', d)
t.description_approved = True
t.thumbnail_approved = True
t.actionable_override = True
t.save()
d = t.json(is_superuser=True)
self.assertEqual(True, d['description_approved'])
self.assertEqual(None, d['thumbnail'])
self.assertEqual(True, d['thumbnail_approved'])
self.assertEqual(True, d['actionable_override'])
示例3: test_get_own
# 需要导入模块: from auvsi_suas.models.target import Target [as 别名]
# 或者: from auvsi_suas.models.target.Target import json [as 别名]
def test_get_own(self):
"""Test GETting a target owned by the correct user."""
t = Target(user=self.user, target_type=TargetType.standard)
t.save()
response = self.client.get(targets_id_url(args=[t.pk]))
self.assertEqual(200, response.status_code)
self.assertEqual(t.json(), json.loads(response.content))
示例4: test_not_others
# 需要导入模块: from auvsi_suas.models.target import Target [as 别名]
# 或者: from auvsi_suas.models.target.Target import json [as 别名]
def test_not_others(self):
"""We don't get targets owned by other users."""
user2 = User.objects.create_user('testuser2', '[email protected]',
'testpass')
mine = Target(user=self.user, target_type=TargetType.standard)
mine.save()
theirs = Target(user=user2, target_type=TargetType.standard)
theirs.save()
response = self.client.get(targets_url)
self.assertEqual(200, response.status_code)
d = json.loads(response.content)
self.assertItemsEqual([mine.json()], d)
示例5: test_put_append
# 需要导入模块: from auvsi_suas.models.target import Target [as 别名]
# 或者: from auvsi_suas.models.target.Target import json [as 别名]
def test_put_append(self):
"""PUT sets a new field that wasn't set before."""
t = Target(user=self.user, target_type=TargetType.standard)
t.save()
data = {'description': 'Hello'}
response = self.client.put(
targets_id_url(args=[t.pk]),
data=json.dumps(data))
self.assertEqual(200, response.status_code)
t.refresh_from_db()
self.assertEqual('Hello', t.description)
# Response also matches
self.assertEqual(t.json(), json.loads(response.content))
示例6: post
# 需要导入模块: from auvsi_suas.models.target import Target [as 别名]
# 或者: from auvsi_suas.models.target.Target import json [as 别名]
def post(self, request):
try:
data = json.loads(request.body)
except ValueError:
return HttpResponseBadRequest('Request body is not valid JSON.')
# Target type is required.
if 'type' not in data:
return HttpResponseBadRequest('Target type required.')
latitude = data.get('latitude')
longitude = data.get('longitude')
# Require zero or both of latitude and longitude.
if (latitude is not None and longitude is None) or \
(latitude is None and longitude is not None):
return HttpResponseBadRequest(
'Either none or both of latitude and longitude required.')
try:
data = normalize_data(data)
except ValueError as e:
return HttpResponseBadRequest(str(e))
l = None
if latitude is not None and longitude is not None:
l = GpsPosition(latitude=data['latitude'],
longitude=data['longitude'])
l.save()
# Use the dictionary get() method to default non-existent values to None.
t = Target(user=request.user,
target_type=data['type'],
location=l,
orientation=data.get('orientation'),
shape=data.get('shape'),
background_color=data.get('background_color'),
alphanumeric=data.get('alphanumeric', ''),
alphanumeric_color=data.get('alphanumeric_color'),
description=data.get('description', ''),
autonomous=data.get('autonomous', False))
t.save()
return JsonResponse(
t.json(is_superuser=request.user.is_superuser),
status=201)
示例7: test_minimal_json
# 需要导入模块: from auvsi_suas.models.target import Target [as 别名]
# 或者: from auvsi_suas.models.target.Target import json [as 别名]
def test_minimal_json(self):
"""Test target JSON with minimal data."""
t = Target(user=self.user, target_type=TargetType.standard)
t.save()
d = t.json()
self.assertIn('id', d)
self.assertEqual(self.user.pk, d['user'])
self.assertEqual('standard', d['type'])
self.assertEqual(None, d['latitude'])
self.assertEqual(None, d['longitude'])
self.assertEqual(None, d['orientation'])
self.assertEqual(None, d['shape'])
self.assertEqual(None, d['background_color'])
self.assertEqual(None, d['alphanumeric'])
self.assertEqual(None, d['alphanumeric_color'])
self.assertEqual(None, d['description'])
self.assertEqual(False, d['autonomous'])
示例8: post
# 需要导入模块: from auvsi_suas.models.target import Target [as 别名]
# 或者: from auvsi_suas.models.target.Target import json [as 别名]
def post(self, request):
try:
data = json.loads(request.body)
except ValueError:
return HttpResponseBadRequest('Request body is not valid JSON.')
# Must be a json dictionary.
if not isinstance(data, dict):
return HttpResponseBadRequest('Request body not a JSON dict.')
# Target type is required.
if 'type' not in data:
return HttpResponseBadRequest('Target type required.')
# Team id can only be specified if superuser.
user = request.user
if 'team_id' in data:
if request.user.is_superuser:
user = User.objects.get(username=data['team_id'])
else:
return HttpResponseForbidden(
'Non-admin users cannot send team_id')
latitude = data.get('latitude')
longitude = data.get('longitude')
# Require zero or both of latitude and longitude.
if (latitude is not None and longitude is None) or \
(latitude is None and longitude is not None):
return HttpResponseBadRequest(
'Either none or both of latitude and longitude required.')
# Cannot submit JSON with actionable_override if not superuser.
if 'actionable_override' in data and not request.user.is_superuser:
return HttpResponseForbidden(
'Non-admin users cannot submit actionable override.')
try:
data = normalize_data(data)
except ValueError as e:
return HttpResponseBadRequest(str(e))
l = None
if latitude is not None and longitude is not None:
l = GpsPosition(latitude=data['latitude'],
longitude=data['longitude'])
l.save()
# Use the dictionary get() method to default non-existent values to None.
t = Target(user=user,
target_type=data['type'],
location=l,
orientation=data.get('orientation'),
shape=data.get('shape'),
background_color=data.get('background_color'),
alphanumeric=data.get('alphanumeric', ''),
alphanumeric_color=data.get('alphanumeric_color'),
description=data.get('description', ''),
autonomous=data.get('autonomous', False),
actionable_override=data.get('actionable_override', False))
t.save()
return JsonResponse(
t.json(is_superuser=request.user.is_superuser),
status=201)