fix quota limit organization validatione
This commit is contained in:
66
apps/pos_device/web/api/v1/validators/header_validation.py
Normal file
66
apps/pos_device/web/api/v1/validators/header_validation.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import datetime
|
||||||
|
|
||||||
|
from django.http import JsonResponse
|
||||||
|
|
||||||
|
from apps.pos_device.models import Device, ProviderCompany, Sessions, DeviceVersion
|
||||||
|
|
||||||
|
|
||||||
|
def get_client_ip(request):
|
||||||
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
||||||
|
if x_forwarded_for:
|
||||||
|
# اگر از پروکسی استفاده میشود، IP اول را برمیگردانیم
|
||||||
|
ip = x_forwarded_for.split(',')[0]
|
||||||
|
else:
|
||||||
|
# در غیر این صورت از REMOTE_ADDR استفاده میکنیم
|
||||||
|
ip = request.META.get('REMOTE_ADDR')
|
||||||
|
return ip
|
||||||
|
|
||||||
|
|
||||||
|
class PosDeviceValidator:
|
||||||
|
def __init__(self, request):
|
||||||
|
self.request = request
|
||||||
|
self.headers = request.headers
|
||||||
|
self.device_id = request.headers.get('device-id')
|
||||||
|
self.device_mac = request.headers.get('device-mac')
|
||||||
|
self.device_name = request.headers.get('device-name')
|
||||||
|
self.device_sdk = request.headers.get('device-sdk')
|
||||||
|
self.device_serial = request.headers.get('device-serial')
|
||||||
|
self.device_provider = request.headers.get('device-provider')
|
||||||
|
self.device_version = request.headers.get('device-version')
|
||||||
|
self.device_version_name = request.headers.get('device-vname')
|
||||||
|
self.device_lng = request.headers.get('device-lng')
|
||||||
|
self.device_lot = request.headers.get('device-lot')
|
||||||
|
|
||||||
|
def validation_version(self):
|
||||||
|
if self.device_provider == "" or self.device_provider == None:
|
||||||
|
return JsonResponse({'result': 'پارامتر های ارسالی صحیح نمیباشد!'}, status=402)
|
||||||
|
|
||||||
|
company = ProviderCompany.objects.filter(en_name=self.device_provider).first()
|
||||||
|
if not company:
|
||||||
|
return JsonResponse({'result': 'شرکت پرداخت الکترونیک پشتیبانی نمیشود!'}, status=402)
|
||||||
|
|
||||||
|
if not company.active:
|
||||||
|
return JsonResponse({'result': 'شرکت پرداخت الکترونیک توسط مدیریت مسدود شده است!'}, status=402)
|
||||||
|
version = DeviceVersion.objects.filter(company=company).order_by('code')
|
||||||
|
if not version:
|
||||||
|
return JsonResponse({'result': ' هیچ نسخه معتبری برای این شرکت پرداخت الکترونیک منتشر نشده است!'},
|
||||||
|
status=402)
|
||||||
|
|
||||||
|
current_version = version.filter(code=self.device_version).first()
|
||||||
|
if not current_version or current_version.remove:
|
||||||
|
return JsonResponse({'result': f'نسخه {self.device_version_name} منقضی شده است لطفا بروز رسانی کنید '},
|
||||||
|
status=402)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def validation_device(self):
|
||||||
|
pos_session = Sessions.objects.filter(pos__pos_id=self.device_id, mac=self.device_mac).first()
|
||||||
|
if not pos_session:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
pos_session.session_last_seen_date = datetime.datetime.now()
|
||||||
|
pos_session.lng = self.device_lng
|
||||||
|
pos_session.lot = self.device_lot
|
||||||
|
pos_session.version = self.device_version
|
||||||
|
pos_session.ip = get_client_ip(self.request)
|
||||||
|
pos_session.save()
|
||||||
|
return pos_session.pos.pos_id
|
||||||
@@ -124,19 +124,22 @@ class DeviceAssignmentViewSet(viewsets.ModelViewSet):
|
|||||||
@action(
|
@action(
|
||||||
methods=['get'],
|
methods=['get'],
|
||||||
detail=False,
|
detail=False,
|
||||||
url_name='my_assignment',
|
url_name='my_assignments',
|
||||||
url_path='my_assignments',
|
url_path='my_assignments',
|
||||||
name='my_assignment'
|
name='my_assignments'
|
||||||
)
|
)
|
||||||
def my_assignment(self, request):
|
def my_assignment(self, request):
|
||||||
""" list of company device assignment to clients """
|
""" list of company device assignment to clients """
|
||||||
|
|
||||||
company = pos_models.ProviderCompany.objects.get(
|
try:
|
||||||
user_relation__user=request.user
|
company = pos_models.ProviderCompany.objects.get(
|
||||||
)
|
user_relation__user=request.user
|
||||||
|
)
|
||||||
|
|
||||||
# get device assignment
|
# get device assignment
|
||||||
assignments = self.queryset.filter(company=company)
|
assignments = self.queryset.filter(company=company)
|
||||||
|
|
||||||
serializer = self.serializer_class(assignments, many=True)
|
serializer = self.serializer_class(assignments, many=True)
|
||||||
return Response
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
except Exception as e:
|
||||||
|
raise APIException('Non Object Error', code=403)
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
|||||||
raise QuotaClosedException()
|
raise QuotaClosedException()
|
||||||
|
|
||||||
# check if assigned organization is in quota limitation
|
# check if assigned organization is in quota limitation
|
||||||
if assigned_organization not in quota.limit_by_organizations.all():
|
if quota.has_organization_limit is True and assigned_organization not in quota.limit_by_organizations.all():
|
||||||
raise QuotaLimitByOrganizationException()
|
raise QuotaLimitByOrganizationException()
|
||||||
|
|
||||||
# total quota distributions weight
|
# total quota distributions weight
|
||||||
|
|||||||
Reference in New Issue
Block a user