feat: update device interaction module
All checks were successful
Build and Deploy LTY / build-and-deploy (push) Successful in 54m40s
All checks were successful
Build and Deploy LTY / build-and-deploy (push) Successful in 54m40s
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a8add9dc6e
commit
7d05339e05
@ -171,7 +171,8 @@
|
||||
"Bash(curl -s http://localhost:8000/api/card/category/dance/)",
|
||||
"Bash(C:/python/python.exe -X utf8 -c \"import sys,json; sys.stdout.reconfigure\\(encoding=''''utf-8''''\\); d=json.load\\(sys.stdin\\); [print\\(f''''ID={i[\"\"id\"\"]} status={i.get\\(\"\"status\"\",\"\"?\"\"\\)} name={i[\"\"name\"\"]}''''\\) for i in d.get\\(''''data'''',d\\).get\\(''''results'''',d.get\\(''''data'''',[]\\)\\)]\")",
|
||||
"Bash(ls c:/Users/admin/Desktop/Lila-Server/qy_lty/settings*)",
|
||||
"Bash(python manage.py makemigrations card)"
|
||||
"Bash(python manage.py makemigrations card)",
|
||||
"Bash(python manage.py runserver)"
|
||||
],
|
||||
"additionalDirectories": [
|
||||
"C:\\Users\\admin\\.claude"
|
||||
|
||||
@ -112,17 +112,7 @@ class DeviceBindSerializer(serializers.Serializer):
|
||||
|
||||
def validate_mac_address(self, value):
|
||||
try:
|
||||
device = Device.objects.get(mac_address=value)
|
||||
# 检查设备是否已被激活
|
||||
if not device.is_active:
|
||||
device.is_active = True
|
||||
device.activated_at = timezone.now()
|
||||
device.save()
|
||||
Device.objects.get(mac_address=value)
|
||||
except Device.DoesNotExist:
|
||||
raise serializers.ValidationError("设备不存在")
|
||||
|
||||
# 检查设备是否已被其他用户绑定
|
||||
if UserDevice.objects.filter(device=device).exists():
|
||||
raise serializers.ValidationError("设备已被其他用户绑定")
|
||||
|
||||
return value
|
||||
return value
|
||||
@ -669,7 +669,25 @@ class UserDeviceViewSet(viewsets.ModelViewSet):
|
||||
try:
|
||||
# 获取设备
|
||||
device = Device.objects.get(mac_address=mac_address)
|
||||
|
||||
|
||||
# 检查是否已被当前用户绑定
|
||||
existing = UserDevice.objects.filter(device=device, user=request.user).first()
|
||||
if existing:
|
||||
return success_response(
|
||||
data=UserDeviceSerializer(existing).data,
|
||||
message='设备已绑定'
|
||||
)
|
||||
|
||||
# 检查是否已被其他用户绑定
|
||||
if UserDevice.objects.filter(device=device).exists():
|
||||
return error_response(message='设备已被其他用户绑定', code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# 激活设备
|
||||
if not device.is_active:
|
||||
device.is_active = True
|
||||
device.activated_at = timezone.now()
|
||||
device.save()
|
||||
|
||||
# 创建用户设备关联
|
||||
user_device = UserDevice(
|
||||
user=request.user,
|
||||
@ -678,19 +696,13 @@ class UserDeviceViewSet(viewsets.ModelViewSet):
|
||||
is_primary=is_primary
|
||||
)
|
||||
user_device.save()
|
||||
|
||||
# 更新设备状态
|
||||
if not device.is_active:
|
||||
device.is_active = True
|
||||
device.activated_at = timezone.now()
|
||||
device.save()
|
||||
|
||||
|
||||
# 返回绑定结果
|
||||
return success_response(
|
||||
data=UserDeviceSerializer(user_device).data,
|
||||
message='设备绑定成功'
|
||||
)
|
||||
|
||||
|
||||
except Device.DoesNotExist:
|
||||
return not_found_response(message='设备不存在')
|
||||
except Exception as e:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user