Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

coco_api version 1.0.0 #1004

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
37ed1e4
Added Django Rest Framework
Mar 19, 2020
3a33446
Added DRF in requirement.txt
Mar 19, 2020
e82194c
geo-&-people-serializers
vermastuti Apr 9, 2020
2c53b4d
coco-api-version1.0.0
vermastuti May 8, 2020
78ea845
count-param-edited
vermastuti May 9, 2020
04ecd3c
req-txt-updated
vermastuti May 11, 2020
bc688f6
base_settings_updated
vermastuti May 11, 2020
6ed47eb
programs-api-added-and-csrf-auth-bug-exists
vermastuti May 19, 2020
883dc77
resolved-csrf-issue
vermastuti May 19, 2020
798c1b0
standard-urls-and-names
vermastuti May 20, 2020
b1950c9
phone-no-farmers-filter
vermastuti May 20, 2020
bded534
dashboard-F_count
vermastuti May 21, 2020
848893a
match-people-phone-numbers-api
vermastuti May 27, 2020
8e83408
people-post-requests-merged
vermastuti May 28, 2020
db84ff0
limit-queryset-function
vermastuti May 28, 2020
52d242b
coco_api_utils
vermastuti May 28, 2020
fec3889
logging-changes
vermastuti Jun 1, 2020
313009a
logging-with-utils
vermastuti Jun 1, 2020
57d5382
pagination-applied
vermastuti Jun 1, 2020
b61949a
view-permission-IsDGRestricted-added
vermastuti Jun 2, 2020
b6846f3
permissions
vermastuti Jun 4, 2020
f454919
api-app-changes
vermastuti Jun 10, 2020
c6e6687
admin-changes
vermastuti Jun 11, 2020
f0cc1e2
comments-linegaps
vermastuti Jun 11, 2020
2c9f352
code-review-changes
vermastuti Jun 11, 2020
4f4f03b
function-comments
vermastuti Jun 13, 2020
ddf463b
add-line-EOF
vermastuti Jun 13, 2020
85057fa
namespaces-added-to-urls
vermastuti Jun 14, 2020
df3f536
change queries to lower case
Jul 1, 2020
96eddb3
mysql-errors-fixed
Jul 17, 2020
f91bc72
people-FarmersCsvAPIView-limitquery
vermastuti Jul 22, 2020
ea40765
change variable name
sagardg Sep 16, 2020
77c63d0
resolve loading issue & change google charts version
sagardg Sep 16, 2020
a432733
change key name from YEAR, MONTH to year, month
sagardg Sep 16, 2020
cb7fedc
add file
sagardg Sep 16, 2020
e629bf2
Merge remote-tracking branch 'origin/analytics_fix' into coco_api_stuti
vermastuti Sep 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions people/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

urlpatterns=[
url(r'^api/default', views.DefaultView.as_view()),
url(r'^api/farmers', views.FarmersJsonAPIView.as_view()),
# url(r'^api/farmers', views.FarmersJsonAPIView.as_view()),
url(r'^api/farmers', views.FarmersJsonAPIView.as_view({'post':'getAllFarmers'})),
url(r'^api/match/phone', views.FarmersJsonAPIView.as_view({'post':'getPhoneMatchedResults'})),
url(r'^api/csv', views.FarmersCsvAPIView.as_view()), # r'^$' is used for regex of exact match as mentioned
url(r'^api/match/phone', views.FarmersMatchAPIView.as_view()),
# url(r'^api/match/phone', views.FarmersMatchAPIView.as_view()),
]
54 changes: 49 additions & 5 deletions people/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from rest_framework.settings import api_settings
from rest_framework_csv import renderers as r


class DefaultView(generics.ListCreateAPIView):
'''
coco_api class-based view to provide default message in JSON format.
Expand All @@ -47,7 +48,7 @@ def get(self, request):
return Response({"detail":"Method \"GET\" not allowed."})


class FarmersJsonAPIView(generics.ListCreateAPIView):
class FarmersJsonAPIView(viewsets.GenericViewSet): #(generics.ListCreateAPIView):
vermastuti marked this conversation as resolved.
Show resolved Hide resolved
'''
coco_api class-based view to query Person model and provide JSON response.
django-rest-framework based token passed in Header as {'Authorization': 'Token 12345exampleToken'}
Expand All @@ -66,7 +67,7 @@ def get(self, request):
return Response({"detail":"Method \"GET\" not allowed"})

# POST request
def post(self, request, *args, **kwargs):
def getAllFarmers(self, request, *args, **kwargs):
country_id = self.request.POST.get('country_id', 0) # POST param 'country_id', default value is 0
fields_values = request.POST.get('fields', '') # POST param 'fields', default value is empty string
phoneNumberExists = request.POST.get('phoneNumberExists','') # POST param 'filter_phone_no', default value is empty string
Expand Down Expand Up @@ -109,6 +110,51 @@ def post(self, request, *args, **kwargs):
# JSON Response is provided by default
return Response(serializer.data)

# POST request
def getPhoneMatchedResults(self, request, *args, **kwargs):
queryset = Person.objects.all().order_by('id')

fields_values = request.POST.get('fields', '') # POST param 'fields', default value is empty string
phone_numbers = request.POST.get('phoneNumbers', '') # POST param 'fields', default value is empty string

phoneNumberExists = request.POST.get('phoneNumberExists','') # POST param 'filter_phone_no', default value is empty string

# phone number exists or not
if phoneNumberExists in ["true","t","yes","y"]:
queryset = queryset.filter(phone_no__isnull=False).exclude(phone_no__in=[''])


# phone number matches
if phone_numbers:
ph_no_values = [ph.strip() for ph in phone_numbers.split(",")]
queryset = queryset.filter(phone_no__in=ph_no_values)

count = self.request.POST.get("count", "False") # POST param 'count', default value is string "False"
# returns count only if param value matched
if count.lower() in ["true","t","yes","y"]:
return Response({"count": queryset.count()})


start_limit = request.POST.get('start_limit') # POST param 'start_limit'
end_limit = request.POST.get('end_limit') # POST param 'end_limit'
# limits the total response count
if start_limit and end_limit: # case1: both are present
queryset = queryset[int(start_limit)-1:int(end_limit)]
elif start_limit: # case2: only start_limit is present
queryset = queryset[int(start_limit)-1:]
elif end_limit: # case3: only end_limit is present
queryset = queryset[:int(end_limit)]

if fields_values: # fields provided in POST request and if not empty serves those fields only
fields_values = [val.strip() for val in fields_values.split(",")]
# updated queryset is passed and fields provided in POST request is passed to the dynamic serializer
serializer = FarmerSerializer(queryset, fields=fields_values, many=True)
else:
# if fields param is empty then all the fields as mentioned in serializer are served to the response
serializer = FarmerSerializer(queryset, many=True)
# CSV Response is provided
return Response(serializer.data)


class FarmersCsvAPIView(APIView):
'''
Expand Down Expand Up @@ -237,6 +283,4 @@ def post(self, request, *args, **kwargs):
# if fields param is empty then all the fields as mentioned in serializer are served to the response
serializer = FarmerSerializer(queryset, many=True)
# CSV Response is provided
return Response(serializer.data)


return Response(serializer.data)