148 lines
5.6 KiB
Python
148 lines
5.6 KiB
Python
from apps.pos_device.pos.api.v1.serializers.device import DeviceSerializer
|
|
from apps.authentication.services.service import get_users_of_organization
|
|
from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
|
|
from apps.pos_device import models as pos_models
|
|
from rest_framework.permissions import AllowAny
|
|
from rest_framework.decorators import action
|
|
from rest_framework.response import Response
|
|
from django.http.response import JsonResponse
|
|
from common.generics import get_client_ip
|
|
from rest_framework import viewsets
|
|
from django.db import transaction
|
|
from rest_framework import status
|
|
|
|
|
|
def test_web_server(request):
|
|
""" testing from pos device to check server status """
|
|
return JsonResponse({"message": "OK"}, status=status.HTTP_200_OK)
|
|
|
|
|
|
class POSDeviceViewSet(viewsets.ModelViewSet, POSDeviceMixin):
|
|
device_queryset = pos_models.Device.objects.all()
|
|
session_queryset = pos_models.Sessions.objects.all()
|
|
serializer_class = DeviceSerializer
|
|
HEADERS = [
|
|
'device-mac', 'device-serial', 'device-name',
|
|
'device-sdk', 'device-version',
|
|
'device-lng', 'device-lot', 'device-provider', # noqa
|
|
'device-vname', # noqa
|
|
]
|
|
permission_classes = [AllowAny]
|
|
|
|
@action(
|
|
methods=['post'],
|
|
detail=False,
|
|
url_path='login',
|
|
url_name='login',
|
|
name='login'
|
|
)
|
|
@transaction.atomic
|
|
def login(self, request):
|
|
""" login of pos device """
|
|
"""
|
|
# get device owner (organization)
|
|
organization = self.get_device_organization()
|
|
"""
|
|
|
|
# convert headers to dictionary
|
|
headers_data = {key: request.headers.get(key) for key in self.HEADERS}
|
|
|
|
serial = headers_data['device-serial']
|
|
sdk = headers_data['device-sdk']
|
|
psp_name = headers_data['device-provider']
|
|
# provider organization
|
|
organization = pos_models.Organization.objects.get(en_name=psp_name)
|
|
|
|
# check if device exists
|
|
if 'device_identity' in request.data.keys() and request.data['device_identity'] != "":
|
|
device = self.device_queryset.filter(device_identity=request.data['device_identity']).first()
|
|
else:
|
|
device = self.device_queryset.filter(serial=serial).first()
|
|
|
|
# activate device
|
|
if device:
|
|
if (not device.is_activated or not device.pre_registered) 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()
|
|
device.is_activated = True
|
|
device.save()
|
|
|
|
session = pos_models.Sessions.objects.create(
|
|
device=device,
|
|
password=device.password,
|
|
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'],
|
|
)
|
|
|
|
org_data = {
|
|
'id': device_owner_org.id,
|
|
'name': device_owner_org.name,
|
|
'en_name': device_owner_org.en_name
|
|
}
|
|
|
|
return Response({
|
|
"message": "login success - session activated",
|
|
"device_identity": device.device_identity,
|
|
"serial": device.serial,
|
|
"password": device.password,
|
|
"provider": organization.name,
|
|
"provider_tell": '0214021',
|
|
"device_owner": org_data,
|
|
"device_owner_users": get_users_of_organization(device_owner_org)
|
|
}, status=status.HTTP_200_OK)
|
|
|
|
return Response({
|
|
"message": "device pre registered - unauthorized",
|
|
"device_identity": device.device_identity,
|
|
"password": device.password,
|
|
"serial": device.serial,
|
|
"provider": organization.name,
|
|
"provider_tell": '0214021',
|
|
}, status=status.HTTP_401_UNAUTHORIZED)
|
|
|
|
pre_device = pos_models.Device.objects.create(
|
|
serial=serial,
|
|
organization=organization,
|
|
pre_registered=True,
|
|
is_activated=False
|
|
)
|
|
|
|
return Response({
|
|
"message": "device pre-registered",
|
|
"device_identity": pre_device.device_identity,
|
|
"password": pre_device.password,
|
|
"provider": organization.name,
|
|
"provider_tell": '0214021',
|
|
}, status=status.HTTP_412_PRECONDITION_FAILED)
|
|
|
|
@action(
|
|
methods=['post'],
|
|
detail=False,
|
|
url_path='merge_devices',
|
|
url_name='merge_devices',
|
|
name='merge_devices'
|
|
)
|
|
@transaction.atomic
|
|
def merge_devices(self, request):
|
|
""" merge pre register device & device has registered by psp user """
|
|
|
|
pre_device = self.device_queryset.get(device_identity=request.data['pre_device_identity'])
|
|
real_device = self.device_queryset.get(device_identity=request.data['real_device_identity'])
|
|
|
|
real_device.device_identity = pre_device.device_identity
|
|
real_device.is_activated = True
|
|
real_device.save()
|
|
|
|
pre_device.delete()
|
|
|
|
return Response({
|
|
"message": "device merged successfully",
|
|
"device_identity": real_device.device_identity,
|
|
"serial": real_device.serial
|
|
})
|