remove defualt from stake holder model

This commit is contained in:
2025-09-14 12:03:49 +03:30
parent 88c8a55bcb
commit 9b523ff1bd
2 changed files with 73 additions and 93 deletions

View File

@@ -45,116 +45,97 @@ class POSDeviceViewSet(viewsets.ModelViewSet, POSDeviceMixin):
# get device owner (organization)
organization = self.get_device_organization()
"""
try:
# convert headers to dictionary
headers_data = {key: request.headers.get(key) for key in self.HEADERS}
# convert headers to dictionary
headers_data = {key: request.headers.get(key) for key in self.HEADERS}
serial = headers_data['device-serial']
mac = headers_data['device-mac']
psp_name = headers_data['device-provider']
serial = headers_data['device-serial']
mac = headers_data['device-mac']
psp_name = headers_data['device-provider']
# provider organization
organization = pos_models.Organization.objects.get(en_name=psp_name)
# provider organization
organization = pos_models.Organization.objects.get(en_name=psp_name)
device = self.device_queryset.filter(serial=serial).first()
device = self.device_queryset.filter(serial=serial).first()
if not device and 'device_identity' in request.data.keys() and request.data['device_identity'] != "":
device = self.device_queryset.filter(device_identity=request.data['device_identity']).first()
if not device and 'device_identity' in request.data.keys() and request.data['device_identity'] != "":
device = self.device_queryset.filter(device_identity=request.data['device_identity']).first()
if device:
if not device.is_activated and device.pre_registered and device.assigned_state:
# activate device
device.is_activated = True
device.save()
if device:
if not device.is_activated and device.pre_registered and device.assigned_state:
# activate device
device.is_activated = True
device.save()
if device.is_activated and device.assigned_state:
# when device is logged in, its has an organization owner client, if not it will show error
device_owner_org = self.get_device_organization()
if device.is_activated and device.assigned_state:
# when device is logged in, its has an organization owner client, if not it will show error
device_owner_org = self.get_device_organization()
session = self.session_queryset.filter(serial=serial).first()
if not session:
pos_models.Sessions.objects.create(
device=device,
name=headers_data['device-name'],
version=headers_data['device-version'],
mac=headers_data['device-mac'],
ip=get_client_ip(request),
sdk=headers_data['device-sdk'],
serial=headers_data['device-serial'],
latitude=headers_data['device-lot'],
longitude=headers_data['device-lng'],
)
else:
session.name = headers_data['device-name']
session.version = headers_data['device-version']
session.mac = headers_data['device-mac']
session.ip = get_client_ip(request)
session.sdk = headers_data['device-sdk']
session.latitude = headers_data['device-lot']
session.longitude = headers_data['device-lng']
session.save()
session = self.session_queryset.filter(serial=serial).first()
if not session:
pos_models.Sessions.objects.create(
device=device,
name=headers_data['device-name'],
version=headers_data['device-version'],
mac=headers_data['device-mac'],
ip=get_client_ip(request),
sdk=headers_data['device-sdk'],
serial=headers_data['device-serial'],
latitude=headers_data['device-lot'],
longitude=headers_data['device-lng'],
)
else:
session.name = headers_data['device-name']
session.version = headers_data['device-version']
session.mac = headers_data['device-mac']
session.ip = get_client_ip(request)
session.sdk = headers_data['device-sdk']
session.latitude = headers_data['device-lot']
session.longitude = headers_data['device-lng']
session.save()
org_data = {
'id': device_owner_org.id,
'name': device_owner_org.name,
'en_name': device_owner_org.en_name
}
return Response({
"message": "خوش آمدید", # noqa
"device_identity": device.device_identity,
"serial": device.serial,
"password": device.password,
"provider": organization.name,
"provider_tell": organization.phone,
"device_owner": org_data,
"device_owner_users": get_users_of_organization(device_owner_org)
}, status=status.HTTP_200_OK)
org_data = {
'id': device_owner_org.id,
'name': device_owner_org.name,
'en_name': device_owner_org.en_name
}
return Response({
"message": "دستگاه در سیستم ثبت در انتظار فعال سازی توسط پذیرنده می باشد", # noqa
"message": "خوش آمدید", # noqa
"device_identity": device.device_identity,
"password": "****",
"serial": device.serial,
"password": device.password,
"provider": organization.name,
"provider_tell": organization.phone,
}, status=status.HTTP_401_UNAUTHORIZED)
"device_owner": org_data,
"device_owner_users": get_users_of_organization(device_owner_org)
}, status=status.HTTP_200_OK)
else:
pre_device = pos_models.Device.objects.create(
serial=serial,
mac=mac,
organization=organization,
pre_registered=True,
is_activated=False
)
return Response({
"message": "دستگاه در سیستم ثبت در انتظار فعال سازی توسط پذیرنده می باشد", # noqa
"device_identity": device.device_identity,
"password": "****",
"serial": device.serial,
"provider": organization.name,
"provider_tell": organization.phone,
}, status=status.HTTP_401_UNAUTHORIZED)
return Response({
"message": "دستگاه در سیستم ثبت در انتظار فعال سازی توسط پذیرنده می باشد", # noqa
"device_identity": pre_device.device_identity,
"password": "****",
"provider": organization.name,
"provider_tell": organization.phone,
}, status=status.HTTP_412_PRECONDITION_FAILED)
except Exception as e:
error_message = str(e)
error_title = e.__class__.__name__
error_traceback = traceback.format_exc()
print(error_message)
print(error_title)
print(error_traceback)
print(headers_data)
return Response(
{
"result": "خطای سرور داخلی رخ داده است. لطفا بعدا تلاش کنید.", # noqa
"error_message": error_message,
"error_title": error_title,
"error_traceback": error_traceback,
},
status=status.HTTP_500_INTERNAL_SERVER_ERROR
else:
pre_device = pos_models.Device.objects.create(
serial=serial,
mac=mac,
organization=organization,
pre_registered=True,
is_activated=False
)
return Response({
"message": "دستگاه در سیستم ثبت در انتظار فعال سازی توسط پذیرنده می باشد", # noqa
"device_identity": pre_device.device_identity,
"password": "****",
"provider": organization.name,
"provider_tell": organization.phone,
}, status=status.HTTP_412_PRECONDITION_FAILED)
@action(
methods=['post'],
detail=False,

View File

@@ -238,7 +238,6 @@ class DeviceAssignmentViewSet(viewsets.ModelViewSet, SoftDeleteMixin):
assignment=assignment,
device=assignment.device,
organization=assignment.client.organization,
default=True
)
return Response(serializer.data, status=status.HTTP_201_CREATED)