fix device binding status
All checks were successful
Build and Deploy Backend / build-and-deploy (push) Successful in 23m29s
All checks were successful
Build and Deploy Backend / build-and-deploy (push) Successful in 23m29s
This commit is contained in:
parent
784115ce58
commit
cccb1b6213
@ -33,12 +33,12 @@ class DeviceViewSet(viewsets.ViewSet):
|
|||||||
authentication_classes = [AppJWTAuthentication]
|
authentication_classes = [AppJWTAuthentication]
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
@action(detail=False, methods=['get'], url_path='query-by-mac',
|
@action(detail=False, methods=['get'], url_path='query-by-mac')
|
||||||
authentication_classes=[], permission_classes=[AllowAny])
|
|
||||||
def query_by_mac(self, request):
|
def query_by_mac(self, request):
|
||||||
"""
|
"""
|
||||||
通过MAC地址查询SN码(无需登录)
|
通过MAC地址查询设备信息及绑定状态
|
||||||
GET /api/v1/devices/query-by-mac?mac=AA:BB:CC:DD:EE:FF
|
GET /api/v1/devices/query-by-mac?mac=AA:BB:CC:DD:EE:FF
|
||||||
|
返回 bound_by_me / bound_by_other / unbound
|
||||||
"""
|
"""
|
||||||
mac = request.query_params.get('mac', '')
|
mac = request.query_params.get('mac', '')
|
||||||
if not mac:
|
if not mac:
|
||||||
@ -49,13 +49,6 @@ class DeviceViewSet(viewsets.ViewSet):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
device = Device.objects.select_related('device_type').get(mac_address=mac)
|
device = Device.objects.select_related('device_type').get(mac_address=mac)
|
||||||
return success(data={
|
|
||||||
'sn': device.sn,
|
|
||||||
'mac_address': device.mac_address,
|
|
||||||
'device_type': DeviceTypeSerializer(device.device_type).data if device.device_type else None,
|
|
||||||
'status': device.status,
|
|
||||||
'is_bound': device.status == 'bound'
|
|
||||||
})
|
|
||||||
except Device.DoesNotExist:
|
except Device.DoesNotExist:
|
||||||
return error(
|
return error(
|
||||||
code=404,
|
code=404,
|
||||||
@ -63,6 +56,25 @@ class DeviceViewSet(viewsets.ViewSet):
|
|||||||
status_code=status.HTTP_404_NOT_FOUND
|
status_code=status.HTTP_404_NOT_FOUND
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 判断绑定归属
|
||||||
|
bind_status = 'unbound'
|
||||||
|
if device.status == 'bound':
|
||||||
|
owner = UserDevice.objects.filter(
|
||||||
|
device=device, is_active=True
|
||||||
|
).first()
|
||||||
|
if owner and owner.user == request.user:
|
||||||
|
bind_status = 'bound_by_me'
|
||||||
|
elif owner:
|
||||||
|
bind_status = 'bound_by_other'
|
||||||
|
|
||||||
|
return success(data={
|
||||||
|
'sn': device.sn,
|
||||||
|
'mac_address': device.mac_address,
|
||||||
|
'device_type': DeviceTypeSerializer(device.device_type).data if device.device_type else None,
|
||||||
|
'status': device.status,
|
||||||
|
'bind_status': bind_status,
|
||||||
|
})
|
||||||
|
|
||||||
@action(detail=False, methods=['get'])
|
@action(detail=False, methods=['get'])
|
||||||
def latest(self, request):
|
def latest(self, request):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user