Skip to content

Commit

Permalink
Merge pull request #1547 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
[optimization] location api
  • Loading branch information
adnankattekaden authored Nov 8, 2023
2 parents 364450f + e363bb8 commit 95b824b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 44 deletions.
22 changes: 19 additions & 3 deletions api/dashboard/location/location_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ class LocationSerializer(serializers.ModelSerializer):
value = serializers.CharField(source="id")
created_by = serializers.CharField(source="created_by.fullname")
updated_by = serializers.CharField(source="updated_by.fullname")

class Meta:
model = Country
fields = [
"label",
"value" ,
"value",
"created_at",
"updated_at",
"created_by",
Expand All @@ -29,7 +30,7 @@ class CountryCreateEditSerializer(serializers.ModelSerializer):
# value = serializers.CharField(source="id")
class Meta:
model = Country
fields = ["label","updated_by", "created_by"]
fields = ["label", "updated_by", "created_by"]


class StateRetrievalSerializer(serializers.ModelSerializer):
Expand All @@ -38,6 +39,7 @@ class StateRetrievalSerializer(serializers.ModelSerializer):
value = serializers.CharField(source="id")
created_by = serializers.CharField(source="created_by.fullname")
updated_by = serializers.CharField(source="updated_by.fullname")

class Meta:
model = State
fields = [
Expand All @@ -61,16 +63,21 @@ class Meta:


class ZoneRetrievalSerializer(serializers.ModelSerializer):
country = serializers.CharField(
source="state.country.name", required=False, default=None
)
state = serializers.CharField(source="state.name", required=False, default=None)
label = serializers.CharField(source="name")
value = serializers.CharField(source="id")
created_by = serializers.CharField(source="created_by.fullname")
updated_by = serializers.CharField(source="updated_by.fullname")

class Meta:
model = Zone
fields = [
"label",
"value",
"country",
"state",
"created_at",
"updated_at",
Expand All @@ -89,16 +96,25 @@ class Meta:


class DistrictRetrievalSerializer(serializers.ModelSerializer):
country = serializers.CharField(
source="zone.state.country.name", required=False, default=None
)
state = serializers.CharField(
source="zone.state.name", required=False, default=None
)
zone = serializers.CharField(source="zone.name", required=False, default=None)
label = serializers.CharField(source="name")
value = serializers.CharField(source="id")
created_by = serializers.CharField(source="created_by.fullname")
updated_by = serializers.CharField(source="updated_by.fullname")

class Meta:
model = District
fields = [
"label",
"value",
"country",
"state",
"zone",
"created_at",
"updated_at",
Expand All @@ -113,4 +129,4 @@ class DistrictCreateEditSerializer(serializers.ModelSerializer):

class Meta:
model = District
fields = ["label","created_by", "updated_by"]
fields = ["label", "created_by", "updated_by"]
93 changes: 54 additions & 39 deletions api/dashboard/location/location_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class CountryDataAPI(APIView):
@role_required([RoleType.ADMIN.value])
def get(self, request, country_id=None):
if country_id:
countries = Country.objects.filter(id=country_id)
countries = Country.objects.filter(id=country_id).select_related(
"created_by", "updated_by"
)
else:
countries = Country.objects.all()
countries = Country.objects.all().select_related("created_by", "updated_by")

paginated_queryset = CommonUtils.get_paginated_queryset(
countries, request, ["name"], {"label": "name"}
Expand Down Expand Up @@ -79,12 +81,16 @@ class StateDataAPI(APIView):
@role_required([RoleType.ADMIN.value])
def get(self, request, state_id=None):
if state_id:
states = State.objects.filter(
Q(pk=state_id) | Q(country__pk=state_id)
).all()
states = (
State.objects.filter(Q(pk=state_id) | Q(country__pk=state_id))
.all()
.select_related("country", "created_by", "updated_by")
)

else:
states = State.objects.all()
states = State.objects.all().select_related(
"country", "created_by", "updated_by"
)

paginated_queryset = CommonUtils.get_paginated_queryset(
states, request, ["name"], {"name": "name"}
Expand Down Expand Up @@ -112,9 +118,7 @@ def post(self, request):
if serializer.is_valid():
serializer.save()

return CustomResponse(
response=serializer.data
).get_success_response()
return CustomResponse(response=serializer.data).get_success_response()

return CustomResponse(general_message=serializer.errors).get_failure_response()

Expand All @@ -130,9 +134,7 @@ def patch(self, request, state_id):
if serializer.is_valid():
serializer.save()

return CustomResponse(
response=serializer.data
).get_success_response()
return CustomResponse(response=serializer.data).get_success_response()

return CustomResponse(
general_message="State edited successfully"
Expand All @@ -154,12 +156,18 @@ class ZoneDataAPI(APIView):
@role_required([RoleType.ADMIN.value])
def get(self, request, zone_id=None):
if zone_id:
zones = Zone.objects.filter(
Q(pk=zone_id) | Q(state__pk=zone_id) | Q(state__country__pk=zone_id)
).all()
zones = (
Zone.objects.filter(
Q(pk=zone_id) | Q(state__pk=zone_id) | Q(state__country__pk=zone_id)
)
.all()
.select_related("state", "state__country", "created_by", "updated_by")
)

else:
zones = Zone.objects.all()
zones = Zone.objects.all().select_related(
"state", "state__country", "created_by", "updated_by"
)

paginated_queryset = CommonUtils.get_paginated_queryset(
zones, request, ["name"], {"name": "name"}
Expand All @@ -180,34 +188,29 @@ def post(self, request):
"updated_by"
] = JWTUtils.fetch_user_id(request)
serializer = location_serializer.ZoneCreateEditSerializer(
data=request.data,
data=request.data,
)

if serializer.is_valid():
serializer.save()

return CustomResponse(
response=serializer.data
).get_success_response()
return CustomResponse(response=serializer.data).get_success_response()

return CustomResponse(general_message=serializer.errors).get_failure_response()

@role_required([RoleType.ADMIN.value])
def patch(self, request, zone_id):

zone = Zone.objects.get(id=zone_id)
request_data = request.data
request_data["updated_by"] = JWTUtils.fetch_user_id(request)
serializer = location_serializer.ZoneCreateEditSerializer(
zone, data=request.data,partial=True
zone, data=request.data, partial=True
)

if serializer.is_valid():
serializer.save()

return CustomResponse(
response=serializer.data
).get_success_response()
return CustomResponse(response=serializer.data).get_success_response()

return CustomResponse(general_message=serializer.errors).get_failure_response()

Expand All @@ -227,15 +230,31 @@ class DistrictDataAPI(APIView):
@role_required([RoleType.ADMIN.value])
def get(self, request, district_id=None):
if district_id:
districts = District.objects.filter(
Q(pk=district_id)
| Q(zone__pk=district_id)
| Q(zone__state__pk=district_id)
| Q(zone__state__country__pk=district_id)
).all()
districts = (
District.objects.filter(
Q(pk=district_id)
| Q(zone__pk=district_id)
| Q(zone__state__pk=district_id)
| Q(zone__state__country__pk=district_id)
)
.all()
.select_related(
"zone",
"zone__state",
"zone__state__country",
"created_by",
"updated_by",
)
)

else:
districts = District.objects.all()
districts = District.objects.all().select_related(
"zone",
"zone__state",
"zone__state__country",
"created_by",
"updated_by",
)

paginated_queryset = CommonUtils.get_paginated_queryset(
districts, request, ["name"], {"name": "name"}
Expand All @@ -257,14 +276,12 @@ def post(self, request):
] = JWTUtils.fetch_user_id(request)

serializer = location_serializer.DistrictCreateEditSerializer(
data=request.data,
data=request.data,
)
if serializer.is_valid():
serializer.save()

return CustomResponse(
response=serializer.data
).get_success_response()
return CustomResponse(response=serializer.data).get_success_response()

return CustomResponse(
general_message="Zone created successfully"
Expand All @@ -282,9 +299,7 @@ def patch(self, request, district_id):
if serializer.is_valid():
serializer.save()

return CustomResponse(
response=serializer.data
).get_success_response()
return CustomResponse(response=serializer.data).get_success_response()

return CustomResponse(general_message=serializer.errors).get_failure_response()

Expand Down
48 changes: 47 additions & 1 deletion api/dashboard/organisation/organisation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
InstitutionCreateUpdateSerializer,
InstitutionSerializer,
InstitutionPrefillSerializer,
OrganizationMergerSerializer,
OrganizationMergerSerializer, OrganizationKarmaTypeGetPostPatchDeleteSerializer,
OrganizationKarmaLogGetPostPatchDeleteSerializer,
)


Expand Down Expand Up @@ -470,3 +471,48 @@ def patch(self, request, organisation_id):
return CustomResponse(
general_message="An organization with the given id doesn't exist"
).get_failure_response()


class OrganizationKarmaTypeGetPostPatchDeleteAPI(APIView):
def post(self, request):
user_id = JWTUtils.fetch_user_id(request)

serializer = OrganizationKarmaTypeGetPostPatchDeleteSerializer(
data=request.data,
context={
'user_id': user_id
}
)
if serializer.is_valid():
serializer.save()

return CustomResponse(
"Organization karma type created successfully"
).get_success_response()

return CustomResponse(
serializer.errors
).get_failure_response()


class OrganizationKarmaLogGetPostPatchDeleteAPI(APIView):
def post(self, request):
user_id = JWTUtils.fetch_user_id(request)

serializer = OrganizationKarmaLogGetPostPatchDeleteSerializer(
data=request.data,
context={
'user_id': user_id
}
)

if serializer.is_valid():
serializer.save()

return CustomResponse(
"Organization karma Log created successfully"
).get_success_response()

return CustomResponse(
serializer.errors
).get_failure_response()
38 changes: 38 additions & 0 deletions api/dashboard/organisation/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
State,
OrgAffiliation,
Department,
OrgKarmaType,
OrgKarmaLog,
)
from utils.permission import JWTUtils
from utils.types import OrganizationType
Expand Down Expand Up @@ -361,3 +363,39 @@ def update(self, instance, validated_data):
source_org.delete()

return instance


class OrganizationKarmaTypeGetPostPatchDeleteSerializer(serializers.ModelSerializer):

class Meta:
model = OrgKarmaType
fields = [
"title",
"karma",
"description",
]

def create(self, validated_data):
user_id = self.context.get("user_id")
validated_data["updated_by_id"] = user_id
validated_data["created_by_id"] = user_id

return OrgKarmaType.objects.create(**validated_data)


class OrganizationKarmaLogGetPostPatchDeleteSerializer(serializers.ModelSerializer):

class Meta:
model = OrgKarmaLog
fields = [
"org",
"karma",
"type",
]

def create(self, validated_data):
user_id = self.context.get("user_id")
validated_data["updated_by_id"] = user_id
validated_data["created_by_id"] = user_id

return OrgKarmaLog.objects.create(**validated_data)
4 changes: 3 additions & 1 deletion api/dashboard/organisation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
path('departments/edit/<str:department_id>/', organisation_views.DepartmentAPI.as_view()),
path('departments/delete/<str:department_id>/', organisation_views.DepartmentAPI.as_view()),
path('affiliation/list/', organisation_views.AffiliationListAPI.as_view()),
path('merge_organizations/<str:organisation_id>/', organisation_views.OrganizationMergerView.as_view())
path('merge_organizations/<str:organisation_id>/', organisation_views.OrganizationMergerView.as_view()),
path('karma-type/create/', organisation_views.OrganizationKarmaTypeGetPostPatchDeleteAPI.as_view()),
path('karma-log/create/', organisation_views.OrganizationKarmaLogGetPostPatchDeleteAPI.as_view()),
]

0 comments on commit 95b824b

Please sign in to comment.